Aaron Huang
/
WaveGen
Generate sine wave, using analog output
main.cpp@3:be30774cb93f, 2014-02-14 (annotated)
- Committer:
- aaronice
- Date:
- Fri Feb 14 08:00:07 2014 +0000
- Revision:
- 3:be30774cb93f
- Parent:
- 2:e723d10f745d
- Child:
- 4:ec289b4b7758
bug fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aaronice | 0:fc7729e19c10 | 1 | |
aaronice | 0:fc7729e19c10 | 2 | #include "mbed.h" |
aaronice | 0:fc7729e19c10 | 3 | AnalogOut DA(PTE30); |
aaronice | 1:6b531bf059e4 | 4 | //AnalogIn AD(PTB1); |
aaronice | 0:fc7729e19c10 | 5 | |
aaronice | 0:fc7729e19c10 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
aaronice | 0:fc7729e19c10 | 7 | |
aaronice | 0:fc7729e19c10 | 8 | float da; |
aaronice | 1:6b531bf059e4 | 9 | float Amp = 1.0; |
aaronice | 0:fc7729e19c10 | 10 | float w = 1.0; |
aaronice | 0:fc7729e19c10 | 11 | float pi = 3.14159; |
aaronice | 3:be30774cb93f | 12 | float deltaT = 0.01; |
aaronice | 0:fc7729e19c10 | 13 | void callback() { |
aaronice | 0:fc7729e19c10 | 14 | // Note: you need to actually read from the serial to clear the RX interrupt |
aaronice | 0:fc7729e19c10 | 15 | //int AA; |
aaronice | 0:fc7729e19c10 | 16 | //int ww; |
aaronice | 0:fc7729e19c10 | 17 | printf("%c\n", pc.getc()); |
aaronice | 0:fc7729e19c10 | 18 | wait(0.5); |
aaronice | 1:6b531bf059e4 | 19 | pc.printf("Input parameter A(<1.0) and press Enter:\n"); |
aaronice | 0:fc7729e19c10 | 20 | pc.scanf("%f", &Amp); |
aaronice | 0:fc7729e19c10 | 21 | pc.printf("%f\n", Amp); |
aaronice | 0:fc7729e19c10 | 22 | //Amp = (float)AA; |
aaronice | 1:6b531bf059e4 | 23 | pc.printf("Input parameter w and press Enter:\n"); |
aaronice | 0:fc7729e19c10 | 24 | pc.scanf("%f", &w); |
aaronice | 0:fc7729e19c10 | 25 | pc.printf("%f\n", w); |
aaronice | 2:e723d10f745d | 26 | pc.printf("Continuing...\n"); |
aaronice | 0:fc7729e19c10 | 27 | //w = (float)ww; |
aaronice | 0:fc7729e19c10 | 28 | } |
aaronice | 0:fc7729e19c10 | 29 | |
aaronice | 0:fc7729e19c10 | 30 | |
aaronice | 0:fc7729e19c10 | 31 | int main(void) { |
aaronice | 0:fc7729e19c10 | 32 | |
aaronice | 0:fc7729e19c10 | 33 | |
aaronice | 0:fc7729e19c10 | 34 | pc.attach(&callback); |
aaronice | 0:fc7729e19c10 | 35 | |
aaronice | 0:fc7729e19c10 | 36 | while(1){ |
aaronice | 1:6b531bf059e4 | 37 | for (float i=0; i<(360); i++) { |
aaronice | 1:6b531bf059e4 | 38 | da = Amp*(sin(w*i/(180)*pi)*0.5+0.5); |
aaronice | 0:fc7729e19c10 | 39 | DA.write(da); |
aaronice | 2:e723d10f745d | 40 | wait(deltaT) |
aaronice | 2:e723d10f745d | 41 | pc.printf("%f ",da); |
aaronice | 0:fc7729e19c10 | 42 | |
aaronice | 1:6b531bf059e4 | 43 | //Monitor Received Analog In |
aaronice | 1:6b531bf059e4 | 44 | //pc.printf("%f ", AD.read()); |
aaronice | 0:fc7729e19c10 | 45 | |
aaronice | 0:fc7729e19c10 | 46 | } |
aaronice | 0:fc7729e19c10 | 47 | //wait(3.0); |
aaronice | 0:fc7729e19c10 | 48 | } |
aaronice | 0:fc7729e19c10 | 49 | } |