Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE mbed-rtos AD5206
wave.cpp@10:159f38636ed4, 2015-12-01 (annotated)
- Committer:
- hanjiex
- Date:
- Tue Dec 01 18:59:42 2015 +0000
- Revision:
- 10:159f38636ed4
extern issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hanjiex | 10:159f38636ed4 | 1 | /* |
hanjiex | 10:159f38636ed4 | 2 | * wave.cpp |
hanjiex | 10:159f38636ed4 | 3 | * |
hanjiex | 10:159f38636ed4 | 4 | */ |
hanjiex | 10:159f38636ed4 | 5 | |
hanjiex | 10:159f38636ed4 | 6 | #include "mbed.h" |
hanjiex | 10:159f38636ed4 | 7 | #include "wave.h" |
hanjiex | 10:159f38636ed4 | 8 | #include "para.h" |
hanjiex | 10:159f38636ed4 | 9 | #include "menu.h" |
hanjiex | 10:159f38636ed4 | 10 | Serial pc(USBTX, USBRX); |
hanjiex | 10:159f38636ed4 | 11 | AnalogOut aout(p18); |
hanjiex | 10:159f38636ed4 | 12 | |
hanjiex | 10:159f38636ed4 | 13 | // from global variable |
hanjiex | 10:159f38636ed4 | 14 | extern para mypara; |
hanjiex | 10:159f38636ed4 | 15 | int waveform_type = mypara.get_type(); |
hanjiex | 10:159f38636ed4 | 16 | int freq = mypara.get_freq(); |
hanjiex | 10:159f38636ed4 | 17 | float amp = mypara.get_amp(); |
hanjiex | 10:159f38636ed4 | 18 | float offset = mypara.get_offset(); |
hanjiex | 10:159f38636ed4 | 19 | |
hanjiex | 10:159f38636ed4 | 20 | |
hanjiex | 10:159f38636ed4 | 21 | int datapoint_counter=0; |
hanjiex | 10:159f38636ed4 | 22 | float duty_cycle=0.2; |
hanjiex | 10:159f38636ed4 | 23 | int length_waveform=100; |
hanjiex | 10:159f38636ed4 | 24 | float time_interv=1.0/freq/length_waveform; |
hanjiex | 10:159f38636ed4 | 25 | uint16_t waveform[100]; |
hanjiex | 10:159f38636ed4 | 26 | int x; |
hanjiex | 10:159f38636ed4 | 27 | const double pi = 3.14; |
hanjiex | 10:159f38636ed4 | 28 | void generate_waveform_datapoints(){ |
hanjiex | 10:159f38636ed4 | 29 | float t=0; |
hanjiex | 10:159f38636ed4 | 30 | |
hanjiex | 10:159f38636ed4 | 31 | //------if sine wave-------- |
hanjiex | 10:159f38636ed4 | 32 | if (waveform_type==0){ |
hanjiex | 10:159f38636ed4 | 33 | for (int counter=0; counter<length_waveform; counter++){ |
hanjiex | 10:159f38636ed4 | 34 | x=amp/3.3*cos(2*pi*freq*t)+offset/3.3; |
hanjiex | 10:159f38636ed4 | 35 | waveform[counter]=(uint16_t) (x*65535); |
hanjiex | 10:159f38636ed4 | 36 | t += time_interv; |
hanjiex | 10:159f38636ed4 | 37 | } |
hanjiex | 10:159f38636ed4 | 38 | } |
hanjiex | 10:159f38636ed4 | 39 | |
hanjiex | 10:159f38636ed4 | 40 | //------if square wave-------- |
hanjiex | 10:159f38636ed4 | 41 | if (waveform_type==1){ |
hanjiex | 10:159f38636ed4 | 42 | for (int counter=0; counter<length_waveform; counter++){ |
hanjiex | 10:159f38636ed4 | 43 | if (counter<=length_waveform*duty_cycle){ |
hanjiex | 10:159f38636ed4 | 44 | x=amp/3.3+offset/3.3; |
hanjiex | 10:159f38636ed4 | 45 | }else{ |
hanjiex | 10:159f38636ed4 | 46 | x=-1.0*amp/3.3+offset/3.3; |
hanjiex | 10:159f38636ed4 | 47 | } |
hanjiex | 10:159f38636ed4 | 48 | waveform[counter]=(uint16_t) (x*65535); |
hanjiex | 10:159f38636ed4 | 49 | } |
hanjiex | 10:159f38636ed4 | 50 | } |
hanjiex | 10:159f38636ed4 | 51 | |
hanjiex | 10:159f38636ed4 | 52 | //------if triangle wave--------- |
hanjiex | 10:159f38636ed4 | 53 | if (waveform_type==2){ |
hanjiex | 10:159f38636ed4 | 54 | for (int counter=0; counter<length_waveform/2; counter++){ |
hanjiex | 10:159f38636ed4 | 55 | x=amp/1.65*(2.0*counter/length_waveform-0.5)+offset/3.3; |
hanjiex | 10:159f38636ed4 | 56 | waveform[counter]=(uint16_t) (x*65535); |
hanjiex | 10:159f38636ed4 | 57 | } |
hanjiex | 10:159f38636ed4 | 58 | |
hanjiex | 10:159f38636ed4 | 59 | for (int counter=length_waveform/2; counter<length_waveform; counter++){ |
hanjiex | 10:159f38636ed4 | 60 | waveform[counter]=waveform[length_waveform-counter-1]; |
hanjiex | 10:159f38636ed4 | 61 | } |
hanjiex | 10:159f38636ed4 | 62 | |
hanjiex | 10:159f38636ed4 | 63 | } |
hanjiex | 10:159f38636ed4 | 64 | } |
hanjiex | 10:159f38636ed4 | 65 | |
hanjiex | 10:159f38636ed4 | 66 | |
hanjiex | 10:159f38636ed4 | 67 | |
hanjiex | 10:159f38636ed4 | 68 | void output_waveform_datapoints(){ |
hanjiex | 10:159f38636ed4 | 69 | pc.printf("%d",freq); |
hanjiex | 10:159f38636ed4 | 70 | for (int counter=0; counter<length_waveform; counter++){ |
hanjiex | 10:159f38636ed4 | 71 | aout.write_u16(waveform[counter]); |
hanjiex | 10:159f38636ed4 | 72 | wait(time_interv); |
hanjiex | 10:159f38636ed4 | 73 | } |
hanjiex | 10:159f38636ed4 | 74 | } |
hanjiex | 10:159f38636ed4 | 75 | |
hanjiex | 10:159f38636ed4 | 76 | |
hanjiex | 10:159f38636ed4 | 77 | void output_waveform_datapoints_timer_ISR(){ |
hanjiex | 10:159f38636ed4 | 78 | aout.write_u16(waveform[datapoint_counter]); |
hanjiex | 10:159f38636ed4 | 79 | datapoint_counter++; |
hanjiex | 10:159f38636ed4 | 80 | |
hanjiex | 10:159f38636ed4 | 81 | if (datapoint_counter==length_waveform-1){datapoint_counter=0;} |
hanjiex | 10:159f38636ed4 | 82 | } |