For NTHU_Racing MCU tutorial Learn to use Serial Oscilloscope & analog pin
Dependencies: mbed
Fork of Practice_3_example by
Revision 2:04dde3a9d171, committed 2017-01-20
- Comitter:
- open4416
- Date:
- Fri Jan 20 13:44:57 2017 +0000
- Parent:
- 1:5968363c5d23
- Commit message:
- For NTHU_Racing MCU tutorial
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
sample_timebase.cpp | Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jan 20 13:44:57 2017 +0000 @@ -0,0 +1,76 @@ +#include "mbed.h" +#define pi 3.141592f +#define d2r 0.01745329f + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// +AnalogIn adc = A0; //analog reading +Serial pc(D1, D0); //Serial reg +PwmOut led = D13; //link leg +AnalogOut dac = A2; //Note!!! Only for F446re, not aviable for MCU without DAC +//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// +Ticker TT1; //call a timer +int TimeCount = 0; //work as a clock +float SignalGenValue = 0.0f; //value for DAC or PWM reference +float SignalProbeValue = 0.0f; //value for ADC readed +//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// +void init_IO(); //initialize IO state +void init_TIMER(); //set TT_main{} rate +void TT1_main(); //timebase function rated by TT1 +//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// +int main() +{ + init_IO(); //initialized value + init_TIMER(); //initialized Timer Inturrupt rate + + while(1) { //main() loop + } +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// +void init_IO(void) //initialize +{ + led = 0.0f; + dac = 0.0f; + pc.baud(115200); //set baud rate +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// + + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// +void init_TIMER() //set TTx_main{} rate +{ + TT1.attach_us(&TT1_main, 20000); //0.02 sec +} + +void TT1_main() //interrupt function by TT1 +{ + //generator time reference + TimeCount = TimeCount + 1; + if(TimeCount > 50) { + TimeCount=0; + } + + //start generating signal output + SignalGenValue = pow(sin(pi*TimeCount/50.0f),2); + dac = SignalGenValue; //Note!!! Only for F446re, not aviable for MCU without DAC + led = SignalGenValue; //only for you to see directly from eyes + + //start reading analog signal + SignalProbeValue = adc.read(); //acquire analog data + + //print out data + pc.printf("%.2f\r", SignalProbeValue); //transfer data to PC +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// \ No newline at end of file
--- a/sample_timebase.cpp Thu Jan 19 07:42:30 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -#include "mbed.h" - -//~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// -DigitalOut led = D13; //link leg -//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// - - -//~~~~~~~~~~~~~~~~~~~~~~~~~~~Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// -Ticker TT1; //call a timer -Ticker TT2; //call a timer -//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// - - -//~~~~~~~~~~~~~~~~~~~~~~~~~~~Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// -void init_IO(); //initialize IO state -void init_TIMER(); //set TT_main{} rate -void TT1_main(); //timebase function rated by TT1 -void TT2_main(); //timebase function rated by TT2 -//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// - - -//~~~~~~~~~~~~~~~~~~~~~~~~~~~main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// -int main() -{ - init_IO(); //initialized value - init_TIMER(); //initialized Timer Inturrupt rate - - while(1) { //main() loop - } -} -//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// - - -//~~~~~~~~~~~~~~~~~~~~~~~~~~~init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// -void init_IO(void) //initialize -{ - led = 1; -} -//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// - - - -//~~~~~~~~~~~~~~~~~~~~~~~~~~~Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// -void init_TIMER() //set TTx_main{} rate -{ - TT1.attach_us(&TT1_main, 1000); - TT2.attach_us(&TT2_main, 1010); -} - -void TT1_main() //interrupt function by TT1 -{ - led = !led; -} - - void TT2_main() //interrupt function by TT2 -{ - led = !led; -} -//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// \ No newline at end of file