Zhangcheng Huang
/
sine_HZC
This program is used for generate sine wave
sine.cpp@1:fb7df7817e53, 2014-02-15 (annotated)
- Committer:
- stanislashzc
- Date:
- Sat Feb 15 06:13:37 2014 +0000
- Revision:
- 1:fb7df7817e53
UCLA ee 202a homework1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stanislashzc | 1:fb7df7817e53 | 1 | #include "mbed.h" |
stanislashzc | 1:fb7df7817e53 | 2 | AnalogOut DA(PTE30); |
stanislashzc | 1:fb7df7817e53 | 3 | |
stanislashzc | 1:fb7df7817e53 | 4 | Serial pc(USBTX, USBRX); // tx, rx |
stanislashzc | 1:fb7df7817e53 | 5 | |
stanislashzc | 1:fb7df7817e53 | 6 | float sine; |
stanislashzc | 1:fb7df7817e53 | 7 | float A = 1.0; |
stanislashzc | 1:fb7df7817e53 | 8 | float w = 1.0; |
stanislashzc | 1:fb7df7817e53 | 9 | float pi = 3.14159; |
stanislashzc | 1:fb7df7817e53 | 10 | |
stanislashzc | 1:fb7df7817e53 | 11 | |
stanislashzc | 1:fb7df7817e53 | 12 | void callback() { |
stanislashzc | 1:fb7df7817e53 | 13 | printf("%c\n", pc.getc()); |
stanislashzc | 1:fb7df7817e53 | 14 | wait(1); |
stanislashzc | 1:fb7df7817e53 | 15 | pc.printf("Amplitude A(<=1.0):\n"); |
stanislashzc | 1:fb7df7817e53 | 16 | pc.scanf("%f", &A); |
stanislashzc | 1:fb7df7817e53 | 17 | pc.printf("%f\n", A); |
stanislashzc | 1:fb7df7817e53 | 18 | pc.printf("Angular Rate w:\n"); |
stanislashzc | 1:fb7df7817e53 | 19 | pc.scanf("%f", &w); |
stanislashzc | 1:fb7df7817e53 | 20 | pc.printf("%f\n", w); |
stanislashzc | 1:fb7df7817e53 | 21 | |
stanislashzc | 1:fb7df7817e53 | 22 | } |
stanislashzc | 1:fb7df7817e53 | 23 | |
stanislashzc | 1:fb7df7817e53 | 24 | |
stanislashzc | 1:fb7df7817e53 | 25 | int main(void) { |
stanislashzc | 1:fb7df7817e53 | 26 | |
stanislashzc | 1:fb7df7817e53 | 27 | |
stanislashzc | 1:fb7df7817e53 | 28 | pc.attach(&callback); |
stanislashzc | 1:fb7df7817e53 | 29 | |
stanislashzc | 1:fb7df7817e53 | 30 | while(1){ |
stanislashzc | 1:fb7df7817e53 | 31 | for (float i=0; i<(3600); i++) { |
stanislashzc | 1:fb7df7817e53 | 32 | |
stanislashzc | 1:fb7df7817e53 | 33 | sine = 0.5*A*(sin(w*i/(1800)*pi)+1); |
stanislashzc | 1:fb7df7817e53 | 34 | DA.write(sine); |
stanislashzc | 1:fb7df7817e53 | 35 | pc.printf("%f ",sine); |
stanislashzc | 1:fb7df7817e53 | 36 | wait(0.001); |
stanislashzc | 1:fb7df7817e53 | 37 | |
stanislashzc | 1:fb7df7817e53 | 38 | } |
stanislashzc | 1:fb7df7817e53 | 39 | } |
stanislashzc | 1:fb7df7817e53 | 40 | } |