/**** This program allows you to interactively capture a series of data from the analog inputs of the NIDAQ board. It queries you for the analog channel, the total number of samples to take, and the number of samples per second. The program then writes out the data to the screen. ****/ /**** These are include files. They contain data types and function prototypes that the compiler needs for your program. ****/ #include // standard C I/O library #include // Acquisition board library include file /**** These are constants that should be easy to set for the entire program. ****/ #define MAXDATA 1000 // Maximum number of data points #define BOARD 1 // The slot in which the board resides (should be 1) #define GAIN 1 // An integer that determines, in part, the gain on analog // inputs /**** The main program starts here ****/ main() { // Variable declarations i16 channel, // which I/O channel to use timebase, // timing base errNum, // error detection values[MAXDATA], // binary values read from the I/O board units = 0, // pts/s (0) or s/pt (1) devNumCode; // device number code long count; // how many samples to take u16 sampleInterval; // sampling interval unsigned long retrieved; // Boolean value indicating when the sampling is finished short status; // status variable f64 rate, // samples per second voltages[MAXDATA]; // Final floating point voltages FILE *fp; // file pointer long i; // generic loop variable char filename[128]; // filename for output data // Get the sampling setup information from the user printf("Capture program starting.\n\n"); printf("Which analog input pin do you want to use? "); channel = -1; while (channel < 0 || channel > 7) { scanf("%hd", &channel); if (channel < 0 || channel > 7) { printf("The analog input pin must be between 0 and 7\n"); printf("Please enter a new value: "); } } printf("\nUsing analog input channel: %hd\n\n", channel); printf("How many samples do you want to take? "); count = -1; while (count < 0 || count > MAXDATA) { scanf("%d", &count); if (channel < 0 || channel > MAXDATA) { printf("The number of samples must be between 0 and %d\n", MAXDATA); printf("Please enter a new value: "); } } printf("\nCapturing %d samples\n\n", count); printf("How many samples per second do you want to take? "); rate = -1.0; while (rate < 0.0) { scanf("%lf", &rate); if (rate < 0.0) { printf("The rate in samples/sec must be positive\n"); printf("Please enter a new value: "); } } printf("\nUsing a sampling rate of %lf samples per second\n\n", rate); // This routine calculates the internal parameters timebase and sampleinterval status = DAQ_Rate(rate, units, &timebase, &sampleInterval); printf("(timebase, sampleInterval) = (%hd, %uhd)\n", timebase, sampleInterval); // Reset board, clear analog io and check for errors errNum = Init_DA_Brds(BOARD, &devNumCode); if (errNum != 0) { printf("Init_DA_Brds: errNum %d devNumCode %d\n", errNum, devNumCode); exit(0); } errNum = AI_Clear(BOARD); if (errNum != 0) { printf("AI_Clear: errNum %d devNumCode %d\n", errNum, devNumCode); exit(0); } // Start the data capture printf("Starting data capture\n"); errNum = DAQ_Start(BOARD, channel, GAIN, values, count, timebase, sampleInterval); // Wait for acquisition to complete, or an error status = 0; while ((status == 0) && (errNum == 0)) { errNum = DAQ_Check(BOARD, &status, &retrieved); // retrieved holds the number of samples collected so far // The value if retrieved > 0, then (retrieved - 1) holds the latest sample } printf("Data collection is done, errNum=%d.\n", errNum); // Scale the data - ie convert it to voltages. errNum = DAQ_VScale(BOARD, channel, GAIN, 1.0, 0.0, count, values, voltages); // Write out your results to the screen here printf("# Analog Capture: %ld samples taken at %lf samples per second\n", count, rate); for (i = 0; i < count; i++) printf("%6d %12lf\n", i, voltages[i]); return(0); }