Last active 1 month ago

Revision 1637f0a7d84e94f89f8ce62fd58a060d1fd4c8a6

IIR_first.c Raw
1///////////////////////////////////////////////////////////////////////////////////////////////
2// SINEIO
3///////////////////////////////////////////////////////////////////////////////////////////////
4
5#include <time.h>
6#include "../myWin826.h"
7#include "../826api.h"
8#include "../RealTime.h"
9
10#define SAMPLE_RATE 750 // SET ME
11
12// IO card configuration constants
13#define IO_BOARD_NUM 0 // no change
14#define ADC_SLOT 0 // no change
15
16#define DAC_ZERO_OUTPUT 0x8000
17#define DAC_CONFIG_GAIN S826_DAC_SPAN_10_10 // -10 to 10 spans 20 volts so...
18#define DAC_VRANGE 20.0 // This MUST correspond to the DAC_SPAN just above
19#define DAC_CNT_RANGE 0xFFFF // 16-bit DAC
20#define DAC_OFFSET_COUNTS 0x8000 // 32768 offset for a signed desired output mapped to unsigned DAC write.
21
22#define DAC_CHANNEL 1 // SET ME! CHECK YOUR SETUP: DAC channel output
23
24#define ADC_CHANNEL 0 // SET ME! CHECK YOUR SETUP: analog input channel to track
25
26
27#define ADC_CNT_RANGE 0xFFFF // 2^16= 0xFFFF (16 bit converter)
28#define ADC_GAIN S826_ADC_GAIN_1 // -10 to 10 option
29#define ADC_VRANGE 20 // this must correspond to gain setting
30
31
32#define ADC_ENABLE 1
33
34int main()
35{
36 int errcode = S826_ERR_OK;
37 int boardflags = S826_SystemOpen(); // open 826 driver and find all 826 boards
38 int slotdata; // adc data from the slot of interest
39 int ncount = 0;
40 uint dacout;
41
42 double voltagein = 0.0;
43 double voltageout = 0;
44
45 double v_prev = 0.0;
46 double A_coeff = 0.84606;
47 double B_coeff = 0.15394;
48
49 // instantiate our "real time" object that will pace our loop
50 RealTime realTime(SAMPLE_RATE);
51
52 // Configure data achqisition interfaces and start them running.
53 X826(S826_AdcSlotConfigWrite(IO_BOARD_NUM, ADC_SLOT, ADC_CHANNEL, 0, ADC_GAIN)); // program adc timeslot attributes: slot, chan, 0us settling time. For -5V-5V use: "S826_ADC_GAIN_2"
54 X826(S826_AdcSlotlistWrite(IO_BOARD_NUM, 1 << ADC_SLOT, S826_BITWRITE)); // enable adc timeslot; disable all other slots
55 X826(S826_AdcEnableWrite(IO_BOARD_NUM, ADC_ENABLE)); // enable adc conversions
56
57 X826(S826_DacRangeWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_CONFIG_GAIN, 0)); // program dac output range: -10V to +10V
58
59 // commence pseudo-real-time loop.
60 realTime.Start();
61
62 while (!_kbhit())
63 {
64 // read ADC sample
65 X826(AdcReadSlot(IO_BOARD_NUM, ADC_SLOT, &slotdata));
66 signed short int adcin = slotdata;
67
68 // get the ADC sample as a voltage
69 voltagein = (double)(adcin * ADC_VRANGE) / ADC_CNT_RANGE;
70 voltageout = (A_coeff * v_prev) + (B_coeff * voltagein);
71
72 // set v_prev for use in next iteration through loop
73 v_prev = voltageout;
74
75 // scale the desired voltage to the DAC integer output.
76 dacout = (uint)(voltageout * (DAC_CNT_RANGE / DAC_VRANGE) + DAC_OFFSET_COUNTS);
77
78 // output to the DAC
79 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, dacout, 0));
80
81 realTime.Sleep();
82 ncount++;
83 }
84
85 realTime.Stop(ncount); // stop real-time loop
86
87
88 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_ZERO_OUTPUT, 0)); // put to zero at the end
89 S826_SystemClose();
90
91
92 return 0;
93}
94
95
96
97
IIR_second.c Raw
1///////////////////////////////////////////////////////////////////////////////////////////////
2// SINEIO
3///////////////////////////////////////////////////////////////////////////////////////////////
4
5#include <time.h>
6#include "../myWin826.h"
7#include "../826api.h"
8#include "../RealTime.h"
9
10#define SAMPLE_RATE 750 // SET ME
11
12// IO card configuration constants
13#define IO_BOARD_NUM 0 // no change
14#define ADC_SLOT 0 // no change
15
16#define DAC_ZERO_OUTPUT 0x8000
17#define DAC_CONFIG_GAIN S826_DAC_SPAN_10_10 // -10 to 10 spans 20 volts so...
18#define DAC_VRANGE 20.0 // This MUST correspond to the DAC_SPAN just above
19#define DAC_CNT_RANGE 0xFFFF // 16-bit DAC
20#define DAC_OFFSET_COUNTS 0x8000 // 32768 offset for a signed desired output mapped to unsigned DAC write.
21
22#define DAC_CHANNEL 1 // SET ME! CHECK YOUR SETUP: DAC channel output
23
24#define ADC_CHANNEL 0 // SET ME! CHECK YOUR SETUP: analog input channel to track
25
26
27#define ADC_CNT_RANGE 0xFFFF // 2^16= 0xFFFF (16 bit converter)
28#define ADC_GAIN S826_ADC_GAIN_1 // -10 to 10 option
29#define ADC_VRANGE 20 // this must correspond to gain setting
30
31
32#define ADC_ENABLE 1
33
34int main()
35{
36 int errcode = S826_ERR_OK;
37 int boardflags = S826_SystemOpen(); // open 826 driver and find all 826 boards
38 int slotdata; // adc data from the slot of interest
39 int ncount = 0;
40 uint dacout;
41
42 double voltagein = 0.0;
43 double voltageout = 0;
44
45 double yv = 0.0;
46 double zv = 0.0;
47 double y_prev = 0.0;
48 double z_prev = 0.0;
49 double A_coeff = 0.771587;
50 double B_coeff = 0.228413;
51
52 // instantiate our "real time" object that will pace our loop
53 RealTime realTime(SAMPLE_RATE);
54
55 // Configure data achqisition interfaces and start them running.
56 X826(S826_AdcSlotConfigWrite(IO_BOARD_NUM, ADC_SLOT, ADC_CHANNEL, 0, ADC_GAIN)); // program adc timeslot attributes: slot, chan, 0us settling time. For -5V-5V use: "S826_ADC_GAIN_2"
57 X826(S826_AdcSlotlistWrite(IO_BOARD_NUM, 1 << ADC_SLOT, S826_BITWRITE)); // enable adc timeslot; disable all other slots
58 X826(S826_AdcEnableWrite(IO_BOARD_NUM, ADC_ENABLE)); // enable adc conversions
59
60 X826(S826_DacRangeWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_CONFIG_GAIN, 0)); // program dac output range: -10V to +10V
61
62 // commence pseudo-real-time loop.
63 realTime.Start();
64
65 while (!_kbhit())
66 {
67 // read ADC sample
68 X826(AdcReadSlot(IO_BOARD_NUM, ADC_SLOT, &slotdata));
69 signed short int adcin = slotdata;
70
71 // get the ADC sample as a voltage
72 voltagein = (double)(adcin * ADC_VRANGE) / ADC_CNT_RANGE;
73 yv = (A_coeff * y_prev) + (B_coeff * voltagein);
74 zv = (A_coeff * z_prev) + (B_coeff * yv);
75
76 // set v_prev for use in next iteration through loop
77 y_prev = yv;
78 z_prev = zv;
79
80 voltageout = zv;
81
82 // scale the desired voltage to the DAC integer output.
83 dacout = (uint)(voltageout * (DAC_CNT_RANGE / DAC_VRANGE) + DAC_OFFSET_COUNTS);
84
85 // output to the DAC
86 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, dacout, 0));
87
88 realTime.Sleep();
89 ncount++;
90 }
91
92 realTime.Stop(ncount); // stop real-time loop
93
94
95 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_ZERO_OUTPUT, 0)); // put to zero at the end
96 S826_SystemClose();
97
98
99 return 0;
100}
101
derivative.c Raw
1///////////////////////////////////////////////////////////////////////////////////////////////
2// SINEIO
3///////////////////////////////////////////////////////////////////////////////////////////////
4
5#include <time.h>
6#include "../myWin826.h"
7#include "../826api.h"
8#include "../RealTime.h"
9
10#define SAMPLE_RATE 750 // SET ME
11
12// IO card configuration constants
13#define IO_BOARD_NUM 0 // no change
14#define ADC_SLOT 0 // no change
15
16#define DAC_ZERO_OUTPUT 0x8000
17#define DAC_CONFIG_GAIN S826_DAC_SPAN_10_10 // -10 to 10 spans 20 volts so...
18#define DAC_VRANGE 20.0 // This MUST correspond to the DAC_SPAN just above
19#define DAC_CNT_RANGE 0xFFFF // 16-bit DAC
20#define DAC_OFFSET_COUNTS 0x8000 // 32768 offset for a signed desired output mapped to unsigned DAC write.
21
22#define DAC_CHANNEL 1 // SET ME! CHECK YOUR SETUP: DAC channel output
23
24#define ADC_CHANNEL 0 // SET ME! CHECK YOUR SETUP: analog input channel to track
25
26
27#define ADC_CNT_RANGE 0xFFFF // 2^16= 0xFFFF (16 bit converter)
28#define ADC_GAIN S826_ADC_GAIN_1 // -10 to 10 option
29#define ADC_VRANGE 20 // this must correspond to gain setting
30
31
32#define ADC_ENABLE 1
33
34int main()
35{
36 int errcode = S826_ERR_OK;
37 int boardflags = S826_SystemOpen(); // open 826 driver and find all 826 boards
38 int slotdata; // adc data from the slot of interest
39 int ncount = 0;
40 uint dacout;
41
42 double voltagein = 0.0;
43 double voltageout = 0;
44
45 // instantiate our "real time" object that will pace our loop
46 RealTime realTime(SAMPLE_RATE);
47
48 // Configure data achqisition interfaces and start them running.
49 X826(S826_AdcSlotConfigWrite(IO_BOARD_NUM, ADC_SLOT, ADC_CHANNEL, 0, ADC_GAIN)); // program adc timeslot attributes: slot, chan, 0us settling time. For -5V-5V use: "S826_ADC_GAIN_2"
50 X826(S826_AdcSlotlistWrite(IO_BOARD_NUM, 1 << ADC_SLOT, S826_BITWRITE)); // enable adc timeslot; disable all other slots
51 X826(S826_AdcEnableWrite(IO_BOARD_NUM, ADC_ENABLE)); // enable adc conversions
52
53 X826(S826_DacRangeWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_CONFIG_GAIN, 0)); // program dac output range: -10V to +10V
54
55 // commence pseudo-real-time loop.
56 realTime.Start();
57
58 while (!_kbhit())
59 {
60 // read ADC sample
61 X826(AdcReadSlot(IO_BOARD_NUM, ADC_SLOT, &slotdata));
62 signed short int adcin = slotdata;
63
64 // get the ADC sample as a voltage
65 voltagein = (double)(adcin * ADC_VRANGE) / ADC_CNT_RANGE;
66
67 // derivative of the 20 Hz input wave
68 voltageout = cos(2.0*3.1415926*20 + ncount/SAMPLE_RATE);
69
70 // scale the desired voltage to the DAC integer output.
71 dacout = (uint)(voltageout * (DAC_CNT_RANGE / DAC_VRANGE) + DAC_OFFSET_COUNTS);
72
73 // output to the DAC
74 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, dacout, 0));
75
76 realTime.Sleep();
77 ncount++;
78 }
79
80 realTime.Stop(ncount); // stop real-time loop
81
82
83 X826(S826_DacDataWrite(IO_BOARD_NUM, DAC_CHANNEL, DAC_ZERO_OUTPUT, 0)); // put to zero at the end
84 S826_SystemClose();
85
86
87 return 0;
88}
89
90