Curtis Mattull
/
function_generator
using LUT generate sine, square, triangle wave with variable freq
main.cpp@0:4a2e0ba6c332, 2016-11-08 (annotated)
- Committer:
- cpm219
- Date:
- Tue Nov 08 01:14:27 2016 +0000
- Revision:
- 0:4a2e0ba6c332
latest version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cpm219 | 0:4a2e0ba6c332 | 1 | /* |
cpm219 | 0:4a2e0ba6c332 | 2 | function generator |
cpm219 | 0:4a2e0ba6c332 | 3 | Curtis Mattull |
cpm219 | 0:4a2e0ba6c332 | 4 | 10/14/16 |
cpm219 | 0:4a2e0ba6c332 | 5 | */ |
cpm219 | 0:4a2e0ba6c332 | 6 | #include "mbed.h" |
cpm219 | 0:4a2e0ba6c332 | 7 | #include "arm_math.h" |
cpm219 | 0:4a2e0ba6c332 | 8 | #include "arm_const_structs.h" |
cpm219 | 0:4a2e0ba6c332 | 9 | #include "my_LUT.h" |
cpm219 | 0:4a2e0ba6c332 | 10 | |
cpm219 | 0:4a2e0ba6c332 | 11 | void inc_func(void); |
cpm219 | 0:4a2e0ba6c332 | 12 | void inc_freq(void); |
cpm219 | 0:4a2e0ba6c332 | 13 | |
cpm219 | 0:4a2e0ba6c332 | 14 | AnalogOut vout(DAC0_OUT); |
cpm219 | 0:4a2e0ba6c332 | 15 | Serial pc(USBTX,USBRX); |
cpm219 | 0:4a2e0ba6c332 | 16 | |
cpm219 | 0:4a2e0ba6c332 | 17 | InterruptIn sw2(SW2); |
cpm219 | 0:4a2e0ba6c332 | 18 | InterruptIn sw3(SW3); |
cpm219 | 0:4a2e0ba6c332 | 19 | |
cpm219 | 0:4a2e0ba6c332 | 20 | |
cpm219 | 0:4a2e0ba6c332 | 21 | const int num_pts = 128; |
cpm219 | 0:4a2e0ba6c332 | 22 | double desired_freq = 60; //60 Hz |
cpm219 | 0:4a2e0ba6c332 | 23 | int func = 0; |
cpm219 | 0:4a2e0ba6c332 | 24 | double dt = (1/desired_freq)/num_pts; // 1/60 = 0.0167, dt = 0.0167/num_pts |
cpm219 | 0:4a2e0ba6c332 | 25 | const double dac_write_delay = 0.000009; |
cpm219 | 0:4a2e0ba6c332 | 26 | double sample_delay = dt-dac_write_delay; |
cpm219 | 0:4a2e0ba6c332 | 27 | |
cpm219 | 0:4a2e0ba6c332 | 28 | int main(void) |
cpm219 | 0:4a2e0ba6c332 | 29 | { |
cpm219 | 0:4a2e0ba6c332 | 30 | pc.baud(115200); |
cpm219 | 0:4a2e0ba6c332 | 31 | pc.printf("start\r\n"); |
cpm219 | 0:4a2e0ba6c332 | 32 | |
cpm219 | 0:4a2e0ba6c332 | 33 | int i =0; |
cpm219 | 0:4a2e0ba6c332 | 34 | double amplitude = 0.5; |
cpm219 | 0:4a2e0ba6c332 | 35 | // static float write_dac_time = 0; |
cpm219 | 0:4a2e0ba6c332 | 36 | // static Timer timer; |
cpm219 | 0:4a2e0ba6c332 | 37 | // static float begin = 0, |
cpm219 | 0:4a2e0ba6c332 | 38 | // end = 0; |
cpm219 | 0:4a2e0ba6c332 | 39 | sw2.rise(&inc_func); |
cpm219 | 0:4a2e0ba6c332 | 40 | sw3.rise(&inc_freq); |
cpm219 | 0:4a2e0ba6c332 | 41 | |
cpm219 | 0:4a2e0ba6c332 | 42 | // timer.start(); |
cpm219 | 0:4a2e0ba6c332 | 43 | // begin = timer.read_us(); |
cpm219 | 0:4a2e0ba6c332 | 44 | // vout.write(amplitude+.5*LUT[func][i*(int)(512/num_pts)]); |
cpm219 | 0:4a2e0ba6c332 | 45 | // timer.stop(); |
cpm219 | 0:4a2e0ba6c332 | 46 | // end = timer.read_us(); //trigger_end |
cpm219 | 0:4a2e0ba6c332 | 47 | // write_dac_time = end-begin; |
cpm219 | 0:4a2e0ba6c332 | 48 | // |
cpm219 | 0:4a2e0ba6c332 | 49 | // pc.printf("dac write time[us]: %f", write_dac_time); |
cpm219 | 0:4a2e0ba6c332 | 50 | // wait(1); |
cpm219 | 0:4a2e0ba6c332 | 51 | |
cpm219 | 0:4a2e0ba6c332 | 52 | while(1) { |
cpm219 | 0:4a2e0ba6c332 | 53 | /*populate some dummy sin data*/ |
cpm219 | 0:4a2e0ba6c332 | 54 | for(i = 0; i< num_pts; i++) |
cpm219 | 0:4a2e0ba6c332 | 55 | {//dac write time was determined to be 8us |
cpm219 | 0:4a2e0ba6c332 | 56 | vout.write(amplitude+.5*LUT[func][i*(int)(512/num_pts)]); |
cpm219 | 0:4a2e0ba6c332 | 57 | wait(sample_delay); |
cpm219 | 0:4a2e0ba6c332 | 58 | } |
cpm219 | 0:4a2e0ba6c332 | 59 | } |
cpm219 | 0:4a2e0ba6c332 | 60 | } |
cpm219 | 0:4a2e0ba6c332 | 61 | |
cpm219 | 0:4a2e0ba6c332 | 62 | void inc_func(void) |
cpm219 | 0:4a2e0ba6c332 | 63 | { |
cpm219 | 0:4a2e0ba6c332 | 64 | func++; |
cpm219 | 0:4a2e0ba6c332 | 65 | if (func==3) |
cpm219 | 0:4a2e0ba6c332 | 66 | { |
cpm219 | 0:4a2e0ba6c332 | 67 | func = 0; |
cpm219 | 0:4a2e0ba6c332 | 68 | } |
cpm219 | 0:4a2e0ba6c332 | 69 | } |
cpm219 | 0:4a2e0ba6c332 | 70 | |
cpm219 | 0:4a2e0ba6c332 | 71 | void inc_freq(void) |
cpm219 | 0:4a2e0ba6c332 | 72 | { |
cpm219 | 0:4a2e0ba6c332 | 73 | desired_freq++; |
cpm219 | 0:4a2e0ba6c332 | 74 | if (desired_freq>200) |
cpm219 | 0:4a2e0ba6c332 | 75 | { |
cpm219 | 0:4a2e0ba6c332 | 76 | desired_freq =1; |
cpm219 | 0:4a2e0ba6c332 | 77 | } |
cpm219 | 0:4a2e0ba6c332 | 78 | dt = (1/desired_freq)/num_pts; // 1/60 = 0.0167, dt = 0.0167/num_pts |
cpm219 | 0:4a2e0ba6c332 | 79 | sample_delay = dt-dac_write_delay; |
cpm219 | 0:4a2e0ba6c332 | 80 | |
cpm219 | 0:4a2e0ba6c332 | 81 | } |