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
Diff: wave.cpp
- Revision:
- 10:159f38636ed4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave.cpp Tue Dec 01 18:59:42 2015 +0000 @@ -0,0 +1,82 @@ +/* +* wave.cpp +* +*/ + +#include "mbed.h" +#include "wave.h" +#include "para.h" +#include "menu.h" +Serial pc(USBTX, USBRX); +AnalogOut aout(p18); + +// from global variable +extern para mypara; +int waveform_type = mypara.get_type(); +int freq = mypara.get_freq(); +float amp = mypara.get_amp(); +float offset = mypara.get_offset(); + + +int datapoint_counter=0; +float duty_cycle=0.2; +int length_waveform=100; +float time_interv=1.0/freq/length_waveform; +uint16_t waveform[100]; +int x; +const double pi = 3.14; +void generate_waveform_datapoints(){ + float t=0; + + //------if sine wave-------- + if (waveform_type==0){ + for (int counter=0; counter<length_waveform; counter++){ + x=amp/3.3*cos(2*pi*freq*t)+offset/3.3; + waveform[counter]=(uint16_t) (x*65535); + t += time_interv; + } + } + + //------if square wave-------- + if (waveform_type==1){ + for (int counter=0; counter<length_waveform; counter++){ + if (counter<=length_waveform*duty_cycle){ + x=amp/3.3+offset/3.3; + }else{ + x=-1.0*amp/3.3+offset/3.3; + } + waveform[counter]=(uint16_t) (x*65535); + } + } + + //------if triangle wave--------- + if (waveform_type==2){ + for (int counter=0; counter<length_waveform/2; counter++){ + x=amp/1.65*(2.0*counter/length_waveform-0.5)+offset/3.3; + waveform[counter]=(uint16_t) (x*65535); + } + + for (int counter=length_waveform/2; counter<length_waveform; counter++){ + waveform[counter]=waveform[length_waveform-counter-1]; + } + + } +} + + + +void output_waveform_datapoints(){ + pc.printf("%d",freq); + for (int counter=0; counter<length_waveform; counter++){ + aout.write_u16(waveform[counter]); + wait(time_interv); + } +} + + +void output_waveform_datapoints_timer_ISR(){ + aout.write_u16(waveform[datapoint_counter]); + datapoint_counter++; + + if (datapoint_counter==length_waveform-1){datapoint_counter=0;} +} \ No newline at end of file