PicoScope 7 Software
Available on Windows, Mac and Linux
Code: Select all
/**************************************************************************
*
* Filename: USBDrDAQcon.c
*
* Copyright: Pico Technology Limited 2010
*
* Author: RPM
*
* Description:
* This is a console-mode program that demonstrates how to use the
* USB DrDAQ driver.
*
* There are the following examples:
* Collect a block of samples immediately
* Collect a block of samples when a trigger event occurs
* Use windowing to collect a sequence of overlapped blocks
* Write a continuous stream of data to a file
* Take individual readings
* Set the signal generator
* Set digital outputs
* Get states of digital inputs
* Set PWM
* Pulse counting
* Set the RGB LED
*
* To build this application
* set up a project for a 32-bit console mode application
* add this file, UsbDrDaqApi.h, picoStatus.h and
* USBDrDAQ.lib (Microsoft C only) to the project and then
* build it
*
***************************************************************************/
#include
#include
#include
#include "picoStatus.h"
#include "C:\Users\Eminar1101\Documents\Visual Studio 2008\Projects\USBDrDAQcon\usbDrDaqApi.h"
#define TRUE 1
#define FALSE 0
int scale_to_mv;
unsigned short max_adc_value;
short g_handle;
short isReady;
short d1State, d2State, d3State, d4State = 0;
PICO_STATUS status;
USB_DRDAQ_INPUTS channel;
/****************************************************************************
*
* adc_to_mv
*
* If the user selects scaling to millivolts,
* Convert a 12-bit ADC count into millivolts
*
****************************************************************************/
int adc_to_mv (int raw)
{
int scaled;
if (scale_to_mv)
{
scaled = raw * 2500 / max_adc_value;
}
else
{
scaled = raw;
}
return scaled;
}
/****************************************************************************
*
* mv_to_adc
*
* Convert a millivolt value into an ADC count
*
* (useful for setting trigger thresholds)
*
****************************************************************************/
int mv_to_adc (int mv)
{
return mv * max_adc_value / 2500;
}
/****************************************************************************
*
* channel_select
* Allow user to select channel
*
****************************************************************************/
void channel_select()
{
int value;
printf("\n");
printf("1: External Sensor 1\n");
printf("2: External Sensor 2\n");
printf("3: External Sensor 3\n");
printf("4: Scope Channel\n");
printf("5: PH\n");
printf("6: Resistance\n");
printf("7: Light\n");
printf("8: Thermistor\n");
printf("9: Microphone Waveform\n");
printf("10:Microphone Level\n");
printf("\n");
do
{
printf("Enter channel number: ");
scanf_s("%d", &value);
}while(value < USB_DRDAQ_CHANNEL_EXT1 || value > USB_DRDAQ_MAX_CHANNELS);
channel = (USB_DRDAQ_INPUTS)value;
}
/****************************************************************************
*
* Collect_block_immediate
* this function demonstrates how to collect a single block of data
* from the unit (start collecting immediately)
*
****************************************************************************/
void collect_block_immediate (void)
{
unsigned int i;
unsigned long nSamples = 6000;
short nChannels = 1;
unsigned long nSamplesPerChannel = 6000;
unsigned long nSamplesCollected;
short samples[6000];
unsigned long usForBlock = 6000000;
unsigned short overflow;
unsigned long triggerIndex = 0;
FILE *fp;
printf ("Collect block immediate (channel %d)...\n", channel);
printf ("Press a key to start\n");
//_getch();
//Set the trigger (disabled)
status = UsbDrDaqSetTrigger(g_handle, FALSE, 0, 0, 0, 0, 0, 0, 0);
//set sampling rate and channels
status = UsbDrDaqSetInterval(g_handle, &usForBlock, nSamples, &channel, nChannels);
//printf("Press any key to stop\n");
fopen_s(&fp, "test.out", "w");
printf("file opened\n");
//while(!_kbhit())
//{
//Run
status = UsbDrDaqRun(g_handle, nSamples, BM_SINGLE);
//Wait until unit is ready
//isReady = 0;
//while(isReady == 0)
//{
// status = UsbDrDaqReady(g_handle, &isReady);
//}
nSamplesCollected = nSamplesPerChannel;
status = UsbDrDaqGetValues(g_handle, samples, &nSamplesCollected, &overflow, &triggerIndex);
printf("get values\n");
//Print out the first 10 readings, converting the readings to mV if required
//printf ("First 10 readings of %i (press any key to stop)\n", nSamplesCollected);
//for (i = 0; i < 10; i++)
//{
// printf ("%d\n", adc_to_mv(samples[i]));
//}
for (i = 0; i < nSamplesCollected; i++)
{
fprintf (fp, "%d\n", adc_to_mv(samples[i]));
}
printf ("Done");
fclose(fp);
status = UsbDrDaqStop(g_handle);
//Sleep(100);
}
//fclose(fp);
//status = UsbDrDaqStop(g_handle);
//_getch();
/****************************************************************************
*
* Collect_block_triggered
* this function demonstrates how to collect a single block of data from the
* unit, when a trigger event occurs.
*
****************************************************************************/
void collect_block_triggered (void)
{
unsigned int i;
unsigned long nSamples = 1000;
short nChannels = 1;
unsigned long nSamplesPerChannel = 1000;
unsigned long nSamplesCollected;
short samples[1000];
unsigned long usForBlock = 1000000;
unsigned short overflow;
unsigned long triggerIndex = 0;
int threshold;
FILE *fp;
printf ("Collect block triggered (channel %d)...\n", channel);
printf ("Enter threshold:");
scanf_s("%d", &threshold);
printf ("\nPress a key to start...\n");
_getch();
//Set the trigger
status = UsbDrDaqSetTrigger(g_handle, TRUE, 0, 0, channel, (unsigned short)threshold, 16000, 0, -50);
//set sampling rate and channels
status = UsbDrDaqSetInterval(g_handle, &usForBlock, nSamples, &channel, nChannels);
printf ("Trigger delay is set to -50%% (trigger event in centre of block)\n");
printf ("\nWaiting for trigger...\n\n");
printf ("Press a key to abort\n");
fopen_s(&fp, "test.out", "w");
//Run
status = UsbDrDaqRun(g_handle, nSamples, BM_SINGLE);
//Wait until unit is ready
isReady = 0;
while(isReady == 0 && (!_kbhit ()))
{
status = UsbDrDaqReady(g_handle, &isReady);
}
nSamplesCollected = nSamplesPerChannel;
status = UsbDrDaqGetValues(g_handle, samples, &nSamplesCollected, &overflow, &triggerIndex);
//Print out the first 10 readings, converting the readings to mV if required
printf ("5 readings either side of trigger event (%i samples collected)\n", nSamplesCollected);
for (i = triggerIndex-5; i < triggerIndex+6; i++)
{
printf ("%d\n", adc_to_mv(samples[i]));
}
for (i = 0; i < nSamplesCollected; i++)
{
fprintf (fp, "%d\n", adc_to_mv(samples[i]));
}
fclose(fp);
status = UsbDrDaqStop(g_handle);
}
/****************************************************************************
*
* Collect_windowed_blocks
*
* This function demonstrates how to use windowed blocks.
*
****************************************************************************/
void collect_windowed_blocks (void)
{
unsigned int i;
unsigned long nSamples = 1000;
short nChannels = 1;
unsigned long nSamplesPerChannel = 1000;
unsigned long nSamplesCollected;
short samples[1000];
unsigned long usForBlock = 10000000; //10 seconds
unsigned short overflow;
unsigned long triggerIndex = 0;
short nLines = 0;
FILE *fp;
printf ("Collect windowed block (channel %d)...\n", channel);
printf ("First block appears after 10 seconds,\n");
printf ("then 10 second blocks are collected every second\n");
printf ("Press a key to start\n");
_getch();
//Set the trigger (disabled)
status = UsbDrDaqSetTrigger(g_handle, FALSE, 0, 0, 0, 0, 0, 0, 0);
//set sampling rate and channels
status = UsbDrDaqSetInterval(g_handle, &usForBlock, nSamples, &channel, nChannels);
//Start
status = UsbDrDaqRun(g_handle, nSamples, BM_WINDOW);
//Wait until unit is ready
printf ("Waiting for first block...\n");
isReady = 0;
while(isReady == 0)
{
status = UsbDrDaqReady(g_handle, &isReady);
}
printf("Press any key to stop\n");
fopen_s(&fp, "testtxt.txt", "w");
while(!_kbhit())
{
nSamplesCollected = nSamplesPerChannel;
status = UsbDrDaqGetValues(g_handle, samples, &nSamplesCollected, &overflow, &triggerIndex);
printf("%d values\n", nSamplesCollected);
if(nLines == 20)
{
printf("Press any key to stop\n");
nLines = 0;
}
else
nLines++;
for (i = 0; i < nSamplesCollected; i++)
{
fprintf (fp, "%d\n", adc_to_mv(samples[i]));
}
Sleep(30000); //Wait 1 second before collecting next 10 second block.
}
fclose(fp);
status = UsbDrDaqStop(g_handle);
_getch();
}
/****************************************************************************
*
* Collect_streaming
*
* This function demonstrates how to use streaming.
*
****************************************************************************/
void collect_streaming (void)
{
unsigned int i;
unsigned long nSamples = 1000;
short nChannels = 1;
unsigned long nSamplesPerChannel = 1000;
unsigned long nSamplesCollected;
short samples[1000];
unsigned long usForBlock = 1000000;
unsigned short overflow;
unsigned long triggerIndex = 0;
short nLines = 0;
FILE *fp;
printf ("Collect streaming (channel %d)...\n", channel);
printf ("Data is written to disk file (test.out)\n");
printf ("Press a key to start\n");
_getch();
//Set the trigger (disabled)
status = UsbDrDaqSetTrigger(g_handle, FALSE, 0, 0, 0, 0, 0, 0, 0);
//set sampling rate and channels
status = UsbDrDaqSetInterval(g_handle, &usForBlock, nSamples, &channel, nChannels);
//Start streaming
status = UsbDrDaqRun(g_handle, nSamples, BM_STREAM);
//Wait until unit is ready
isReady = 0;
while(isReady == 0)
{
status = UsbDrDaqReady(g_handle, &isReady);
}
printf("Press any key to stop\n");
fopen_s(&fp, "test.out", "w");
while(!_kbhit())
{
nSamplesCollected = nSamplesPerChannel;
status = UsbDrDaqGetValues(g_handle, samples, &nSamplesCollected, &overflow, &triggerIndex);
printf("%d values\n", nSamplesCollected);
if(nLines == 20)
{
printf("Press any key to stop\n");
nLines = 0;
}
else
nLines++;
for (i = 0; i < nSamplesCollected; i++)
{
fprintf (fp, "%d\n", adc_to_mv(samples[i]));
}
Sleep(100);
}
fclose(fp);
status = UsbDrDaqStop(g_handle);
_getch();
}
/****************************************************************************
*
* Collect_individual
*
****************************************************************************/
void collect_individual (void)
{
int sample_no;
int c;
short value;
unsigned short overflow;
printf ("Collect individual...\n");
printf ("Takes individual readings under program control\n");
printf ("Sample from all channels\n");
printf ("Press a key to start\n");
_getch();
sample_no = 20;
while (!_kbhit ())
{
Sleep(100);
if (++sample_no > 20) //Print headings again
{
sample_no = 0;
printf ("\nPress any key to stop\n \n");
for (c = 1; c <= USB_DRDAQ_MAX_CHANNELS; c++)
{
printf ("ch%d\t", c);
}
}
for (c = 1; c <= USB_DRDAQ_MAX_CHANNELS; c++)
{
value = 0;
UsbDrDaqGetSingle(g_handle, c, &value, &overflow);
printf ("%d\t", adc_to_mv (value));
}
}
_getch ();
}
/****************************************************************************
*
* Set/clear digital outputs
*
****************************************************************************/
void outputToggle(USB_DRDAQ_GPIO IOChannel)
{
switch(IOChannel)
{
case USB_DRDAQ_GPIO_1:
{
if(d1State)
d1State = 0;
else
d1State = 1;
UsbDrDaqSetDO(g_handle, IOChannel, d1State);
break;
}
case USB_DRDAQ_GPIO_2:
{
if(d2State)
d2State = 0;
else
d2State = 1;
UsbDrDaqSetDO(g_handle, IOChannel, d2State);
break;
}
case USB_DRDAQ_GPIO_3:
{
if(d3State)
d3State = 0;
else
d3State = 1;
UsbDrDaqSetDO(g_handle, IOChannel, d3State);
break;
}
case USB_DRDAQ_GPIO_4:
{
if(d4State)
d4State = 0;
else
d4State = 1;
UsbDrDaqSetDO(g_handle, IOChannel, d4State);
break;
}
}
}
/****************************************************************************
*
* Display digital output states
*
****************************************************************************/
void displayOutputStates()
{
printf("\nDigital Outputs\n");
printf("===============\n");
printf("GPIO 1\tGPIO 2\tGPIO 3\tGPIO 4\t\n");
printf("%i\t%i\t%i\t%i\t\n\n", d1State, d2State, d3State, d4State);
}
/****************************************************************************
*
* Set pulse width modulation
*
****************************************************************************/
void pwm()
{
int period = 0;
int cycle = 0;
int IOChannel;
printf("\n----------PWM----------\n");
do
{
printf("Select GPIO channel (1 or 2):");
scanf_s("%d", &IOChannel);
fflush(stdin);
}while(IOChannel < 1 || IOChannel > 2);
do
{
printf("Enter period (0 to 65535 microseconds):");
scanf_s("%d", &period);
fflush(stdin);
}while(period < 0 || period > 65535);
do
{
printf("Enter duty cycle (0 to 100%%):");
scanf_s("%d", &cycle);
fflush(stdin);
}while(cycle < 0 || cycle > 100);
UsbDrDaqSetPWM(g_handle, IOChannel, (unsigned short)period, (unsigned char)cycle);
//Clear state of channel we are using
if(IOChannel == 1)
d1State = 0;
else if(IOChannel == 2)
d2State = 0;
}
/****************************************************************************
*
* Read digital inputs
*
****************************************************************************/
void DigitalInput()
{
short value;
USB_DRDAQ_GPIO channel;
printf("Press any key to stop...\n");
while(!_kbhit())
{
for(channel = USB_DRDAQ_GPIO_1; channel <= USB_DRDAQ_GPIO_4; channel++)
{
UsbDrDaqGetInput(g_handle, channel, 0, &value); //Not using pull-up resistor
printf("%d\t", value);
}
printf("\n");
Sleep(500);
}
_getch();
//reset digital output status
d1State = d2State = d3State = d4State = 0;
}
/****************************************************************************
*
* Pulse counting
*
****************************************************************************/
void PulseCounting()
{
int value, direction;
USB_DRDAQ_GPIO IOChannel;
short count;
do
{
printf("Select GPIO (1 or 2):");
scanf_s("%d", &value);
}while(value != USB_DRDAQ_GPIO_1 && value != USB_DRDAQ_GPIO_2);
printf("\n");
IOChannel = (USB_DRDAQ_GPIO)value;
do
{
printf("Select direction (0: rising. 1: falling):");
scanf_s("%d", &direction);
}while(direction != 0 && direction != 1);
printf("\n");
printf("Press any key to start counting pulses\n");
_getch();
UsbDrDaqStartPulseCount(g_handle, IOChannel, (short)direction);
printf("Press any key to stop...\n");
while(!_kbhit())
{
Sleep(1000);
UsbDrDaqGetPulseCount(g_handle, IOChannel, &count);
printf("%d\n", count);
}
_getch();
//reset digital output status
if(IOChannel == USB_DRDAQ_GPIO_1)
d1State = 0;
else if(IOChannel == USB_DRDAQ_GPIO_2)
d2State = 0;
}
/****************************************************************************
*
* Set the signal generator
*
****************************************************************************/
void SigGen()
{
USB_DRDAQ_WAVE waveType;
int frequency = 0;
int pToP = 0;
int value, offset;
printf("0: Sine\n");
printf("1: Square\n");
printf("2: Triangle\n");
printf("3: Ramp Up\n");
printf("4: Rampe Down\n");
printf("5: DC\n");
printf("99: OFF\n");
do
{
printf("\nSelect wave type:");
scanf_s("%d", &value);
}while((value < 0 || value > 5) && value != 99);
if(value == 99)
{
UsbDrDaqStopSigGen(g_handle);
return;
}
waveType = (USB_DRDAQ_WAVE)value;
do
{
printf("\nEnter offset (microvolts):");
scanf_s("%d", &offset);
}while(offset < -1500000 || offset > 1500000);
if(waveType != USB_DRDAQ_DC) //Frequency and peak to peak are ignored for DC output
{
do
{
printf("\nEnter frequency (0 to 20,000 Hz):");
scanf_s("%d", &frequency);
}while(frequency < 0 || frequency > 20000);
do
{
printf("Enter peak-to-peak amplitude (microvolts)");
scanf_s("%d", &pToP);
}while(pToP < 0|| pToP > 3000000);
}
UsbDrDaqSetSigGenBuiltIn(g_handle, (long)offset, (unsigned long)pToP, (short)frequency, waveType);
}
/****************************************************************************
*
* Get availabe scaling for current channel and allow user to select
* the scaling to use
*
****************************************************************************/
void ChannelScaling()
{
short nScales, currentScale, i;
char names[1000];
char ch;
int selectedScaleNo;
UsbDrDaqGetScalings(g_handle, channel, &nScales, ¤tScale, names, 1000);
printf("%d scale(s) available for channel %d:\n\n", nScales, channel);
i = 0;
while(names[i] != NULL)
{
if(names[i] == 13) //Carriage return
printf("\n");
else
printf("%c", names[i]);
i++;
}
if(nScales > 1)
{
printf("\ncurrent scale: %d\n\n", currentScale);
printf("Press 'C' to change scale or any other key to continue\n");
ch = toupper(_getch());
if(ch == 'C')
{
do
{
printf("Select scale (0 to %d):", nScales - 1);
scanf_s("%d", &selectedScaleNo);
printf("\n");
}while(selectedScaleNo < 0 || selectedScaleNo > nScales - 1);
UsbDrDaqSetScalings(g_handle, channel, (short)selectedScaleNo);
}
}
}
/****************************************************************************
*
* Enable or disable the RGB LED
*
****************************************************************************/
void LED()
{
int enable, red, green, blue;
printf("\n");
printf("0: Disable RGB LED\n");
printf("1: Enable RGB LED\n");
do
{
printf("\n>");
scanf_s("%d", &enable);
}while(enable < 0 || enable > 1);
UsbDrDaqEnableRGBLED(g_handle, (short)enable);
if(enable)
{
do
{
printf("\nEnter Red value (0 to 255):");
scanf_s("%d", &red);
}while(red < 0); //Doesn't matter if > 255
do
{
printf("\nEnter Green value (0 to 255):");
scanf_s("%d", &green);
}while(green < 0);
do
{
printf("\nEnter Blue value (0 to 255):");
scanf_s("%d", &blue);
}while(blue < 0);
UsbDrDaqSetRGBLED(g_handle, (unsigned short)red, (unsigned short)green, (unsigned short)blue);
}
}
/****************************************************************************
*
*
****************************************************************************/
void main (void)
{
char ch;
char info[20];
short requiredSize = 0;
printf ("USB DrDAQ driver example program\n");
printf ("Version 1.0\n\n");
printf ("\n\nOpening the device...\n");
status = UsbDrDaqOpenUnit(&g_handle);
if (status != 0)
{
printf ("Unable to open device\nPress any key\n");
_getch();
return;
}
else
{
collect_block_immediate ();
}
/*
printf ("Device opened successfully\n\n");
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_VARIANT_INFO);
printf("Model:\t\t\t %s\n", info);
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_BATCH_AND_SERIAL);
printf("Serial Number:\t\t %s\n", info);
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_CAL_DATE);
printf("Calibration Date:\t %s\n\n", info);
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_USB_VERSION);
printf("%s\n", info);
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_HARDWARE_VERSION);
printf("Hardware version %s\n", info);
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_DRIVER_VERSION);
printf("USBDrDAQ.dll version %s\n", info);
status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_KERNEL_VERSION);
printf("%s\n", info);
//Set to scope channel initially
channel = USB_DRDAQ_CHANNEL_SCOPE;
ch = ' ';
while (ch != 'X')
{
printf ("\n");
printf ("Select an operation:\n");
printf ("B - Immediate block\t\t1, 2, 3, 4 - Toggle digital outputs\n");
printf ("T - Triggered block\t\tP - Set PWM\n");
printf ("W - Windowed block\t\tD - Get digital input states\n");
printf ("S - Streaming\t\t\tE - Pulse counting\n");
printf ("C - Select channel\t\tF - Set signal generator\n");
printf ("G - Channel scaling\t\tH - Set RGB LED\n");
printf ("A - Select mV or ADC counts\n");
printf ("I - Individual reading\t\tX - exit\n");
ch = toupper (_getch());
printf ("\n");
switch (ch)
{
case 'C':
channel_select();
break;
case 'B':
collect_block_immediate ();
break;
case 'T':
collect_block_triggered ();
break;
case 'W':
collect_windowed_blocks ();
break;
case 'S':
collect_streaming ();
break;
case 'I':
collect_individual ();
break;
case 'D':
DigitalInput();
break;
case 'E':
PulseCounting();
break;
case 'F':
SigGen();
break;
case 'P':
pwm();
break;
case 'G':
ChannelScaling();
break;
case 'H':
LED();
break;
case '1':
outputToggle(USB_DRDAQ_GPIO_1);
displayOutputStates();
break;
case '2':
outputToggle(USB_DRDAQ_GPIO_2);
displayOutputStates();
break;
case '3':
outputToggle(USB_DRDAQ_GPIO_3);
displayOutputStates();
break;
case '4':
outputToggle(USB_DRDAQ_GPIO_4);
displayOutputStates();
break;
case 'A':
scale_to_mv = !scale_to_mv;
if (scale_to_mv)
{
printf ("Values will be displayed in mV\n");
}
else
{
printf ("Values will be displayed in ADC counts\n");
}
break;
case 'X':
break;
default:
printf ("Invalid operation\n");
break;
}
}
*/
UsbDrDaqCloseUnit(g_handle);
}
Code: Select all
channel = USB_DRDAQ_CHANNEL_SCOPE;
Code: Select all
collect_block_immediate ();
Code: Select all
//Wait until unit is ready
isReady = 0;
while(isReady == 0)
{
status = UsbDrDaqReady(g_handle, &isReady);
}