Shaojie Wang
/
singenerator
sinwave
sin.cpp@0:1c98216d18eb, 2014-02-15 (annotated)
- Committer:
- bmdlh
- Date:
- Sat Feb 15 06:09:34 2014 +0000
- Revision:
- 0:1c98216d18eb
202hw1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bmdlh | 0:1c98216d18eb | 1 | #include "mbed.h" |
bmdlh | 0:1c98216d18eb | 2 | AnalogOut DA(PTE30); |
bmdlh | 0:1c98216d18eb | 3 | //AnalogIn AD(PTB1); |
bmdlh | 0:1c98216d18eb | 4 | |
bmdlh | 0:1c98216d18eb | 5 | Serial pc(USBTX, USBRX); // tx, rx |
bmdlh | 0:1c98216d18eb | 6 | |
bmdlh | 0:1c98216d18eb | 7 | float D2A; |
bmdlh | 0:1c98216d18eb | 8 | |
bmdlh | 0:1c98216d18eb | 9 | |
bmdlh | 0:1c98216d18eb | 10 | float sineTable[] = {0, 0.01745, 0.0349, 0.05234, 0.06976, 0.08716, 0.10453, 0.12187, 0.13917, 0.15643, 0.17365, 0.19081, 0.20791, 0.22495, 0.24192, 0.25882, 0.27564, 0.29237, 0.30902, 0.32557, 0.34202, 0.35837, 0.37461, 0.39073, 0.40674, 0.42262, 0.43837, 0.45399, 0.46947, 0.48481, 0.5, 0.51504, 0.52992, 0.54464, 0.55919, 0.57358, 0.58779, 0.60182, 0.61566, 0.62932, 0.64279, 0.65606, 0.66913, 0.682, 0.69466, 0.70711, 0.71934, 0.73135, 0.74314, 0.75471, 0.76604, 0.77715, 0.78801, 0.79864, 0.80902, 0.81915, 0.82904, 0.83867, 0.84805, 0.85717, 0.86603, 0.87462, 0.88295, 0.89101, 0.89879, 0.90631, 0.91355, 0.9205, 0.92718, 0.93358, 0.93969, 0.94552, 0.95106, 0.9563, 0.96126, 0.96593, 0.9703, 0.97437, 0.97815, 0.98163, 0.98481, 0.98769, 0.99027, 0.99255, 0.99452, 0.99619, 0.99756, 0.99863, 0.99939, 0.99985, 1}; // 0 to 90 degree |
bmdlh | 0:1c98216d18eb | 11 | int TableLength = sizeof(sineTable)/sizeof(sineTable[0]); |
bmdlh | 0:1c98216d18eb | 12 | |
bmdlh | 0:1c98216d18eb | 13 | int main(void) { |
bmdlh | 0:1c98216d18eb | 14 | //pc.printf("T-length=%d\n",TableLength); |
bmdlh | 0:1c98216d18eb | 15 | |
bmdlh | 0:1c98216d18eb | 16 | //Timer t; |
bmdlh | 0:1c98216d18eb | 17 | |
bmdlh | 0:1c98216d18eb | 18 | while(1){ |
bmdlh | 0:1c98216d18eb | 19 | //t.start(); |
bmdlh | 0:1c98216d18eb | 20 | for (int i=0; i<360; i++) { |
bmdlh | 0:1c98216d18eb | 21 | if(i<91){ |
bmdlh | 0:1c98216d18eb | 22 | D2A = sineTable[i]; |
bmdlh | 0:1c98216d18eb | 23 | } |
bmdlh | 0:1c98216d18eb | 24 | else if(i<181){ |
bmdlh | 0:1c98216d18eb | 25 | D2A = sineTable[181-i]; |
bmdlh | 0:1c98216d18eb | 26 | } |
bmdlh | 0:1c98216d18eb | 27 | else if(i<271){ |
bmdlh | 0:1c98216d18eb | 28 | D2A = - sineTable[i-181]; |
bmdlh | 0:1c98216d18eb | 29 | } |
bmdlh | 0:1c98216d18eb | 30 | else{ |
bmdlh | 0:1c98216d18eb | 31 | D2A = - sineTable[360-i]; |
bmdlh | 0:1c98216d18eb | 32 | } |
bmdlh | 0:1c98216d18eb | 33 | |
bmdlh | 0:1c98216d18eb | 34 | D2A = 0.5* (D2A + 1.0); |
bmdlh | 0:1c98216d18eb | 35 | |
bmdlh | 0:1c98216d18eb | 36 | DA.write(D2A); |
bmdlh | 0:1c98216d18eb | 37 | wait(0.01); |
bmdlh | 0:1c98216d18eb | 38 | pc.printf("%f ",D2A); |
bmdlh | 0:1c98216d18eb | 39 | |
bmdlh | 0:1c98216d18eb | 40 | |
bmdlh | 0:1c98216d18eb | 41 | } |
bmdlh | 0:1c98216d18eb | 42 | |
bmdlh | 0:1c98216d18eb | 43 | //t.stop(); |
bmdlh | 0:1c98216d18eb | 44 | //pc.printf("The time taken was %f seconds\n", t.read()); |
bmdlh | 0:1c98216d18eb | 45 | } |
bmdlh | 0:1c98216d18eb | 46 | } |