Question
Write a C-program that reads in a 20Hz sine wave with an amplitude of 1V (rms) through ADC0 channel and writes out its derivative at
Write a C-program that reads in a 20Hz sine wave with an amplitude of 1V (rms) through ADC0 channel and writes out its derivative at DAC0 channel. Use a sampling frequency of 750 Hz. Use the following code for creating this program:
/****************************************************************************** sine_io.c
sine_io.c reads in voltages from an a/d and outputs the same voltage to a d/a. This is done at approximately 1KHz. Program updated in Feb. 2018 ******************************************************************************/
#include
#include "Win626.h" #include "App626.h"
#define SAMPLE_RATE 1000.0 #define EOPL 0x80 #define PERFORMANCE_COUNTS 3323900 #define RANGE_5V 0x10 typedef DWORD HBD;
//---*****WARNING!*****---DO NOT CHANGE THE CODE BETWEEN WARNINGS-----------------
/* sleepfor takes a LARGE_INTEGER (64 bits) and returns after the input number of performance_clicks have passed from that time. This can serve as a timer for a real-time loop to ensure that a loop executes a certain number of performance_clicks. For these machines running Windows 7 there are 3323900 performance_clicks per second, thus providing a high resolution timer capable of ensuring reasonable real-time performance is used properly. */ void sleepfor(LARGE_INTEGER from, long performance_clicks) { LARGE_INTEGER pc0; long pc_diff;
QueryPerformanceCounter(&pc0); pc_diff = (long) pc0.QuadPart - (long) from.QuadPart;
if(pc_diff > performance_clicks) printf(".");
while(pc_diff < performance_clicks) { QueryPerformanceCounter(&pc0); pc_diff = (long) pc0.QuadPart - (long) from.QuadPart; }
return; } //--***** END WARNING! *****-------------------------------------------------------
int main() { HBD hbd=0; BYTE poll_list[16] = {0x10 | EOPL, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, \ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f | EOPL}; char key = 0; short int data[16], reading=0; int i,ncount, dacdata = 0; long sample_period; LARGE_INTEGER hr0; double voltage, elapsed_time,freq; clock_t start,stop; // ADD ALL NEW VARIABLES HERE (BEFORE THIS LINE)
//---*****WARNING!*****---DO NOT CHANGE THE CODE BETWEEN WARNINGS-----------------
// sample period in NT performance counts sample_period = PERFORMANCE_COUNTS/SAMPLE_RATE; poll_list[0] = 0 | RANGE_5V | EOPL; // set this program to run at a high priority under NT to ensure real-time behavior SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS); // initialize the Sensoray 626 board S626_DLLOpen(); S626_OpenBoard( hbd, 0, 0, 3); // initialize the ADC S626_ResetADC(hbd, poll_list);
//--***** END WARNING! *****-------------------------------------------------------
// start a system clock and counter to determine data acquisition rate start = clock(); ncount=0;
// get system performance counts NOW QueryPerformanceCounter(&hr0);
// go until the user hits a key while(!kbhit()) { // read in a voltage S626_ReadADC(hbd, &data[0]); // convert adc data to (double) voltage voltage = ((double) data[0])*(5.0/32767.0);
// convert voltage to DAC output dacdata = (int)( voltage*8191.0/10.0 );
// write out the voltage just read in.Call twice due to Sensoray error. S626_WriteDAC(hbd,0,dacdata); S626_WriteDAC(hbd,0,dacdata);
// this loop should execute once per msec sleepfor(hr0,sample_period); QueryPerformanceCounter(&hr0); // counts from right NOW ncount++; } stop = clock();
//---*****WARNING!*****---DO NOT CHANGE THE CODE BETWEEN WARNINGS-----------------
// calculate how fast the loop ran elapsed_time = (double) (stop - start) / CLOCKS_PER_SEC; freq = ((double)ncount)/elapsed_time; printf(" %d cycles in %f sec (%f Hz, %f sec)) ", ncount,elapsed_time, freq, 1.0/freq); // make sure all DACs are outputing 0 V. Call twice due to Sensoray error for(i=0; i<4; i++) { S626_WriteDAC(hbd,(WORD) i,0); S626_WriteDAC(hbd,(WORD) i,0); } // close everything down S626_CloseBoard( hbd ); S626_DLLClose();
//--***** END WARNING! *****------------------------------------------------------- return(0); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started