For NTHU_Racing MCU tutorial Learn to use Serial Oscilloscope & analog pin
Dependencies: mbed
Fork of Practice_3_example by
main.cpp@2:04dde3a9d171, 2017-01-20 (annotated)
- Committer:
- open4416
- Date:
- Fri Jan 20 13:44:57 2017 +0000
- Revision:
- 2:04dde3a9d171
- Parent:
- sample_timebase.cpp@1:5968363c5d23
For NTHU_Racing MCU tutorial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
open4416 | 0:d68e088dfbcd | 1 | #include "mbed.h" |
open4416 | 2:04dde3a9d171 | 2 | #define pi 3.141592f |
open4416 | 2:04dde3a9d171 | 3 | #define d2r 0.01745329f |
open4416 | 0:d68e088dfbcd | 4 | |
open4416 | 0:d68e088dfbcd | 5 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 2:04dde3a9d171 | 6 | AnalogIn adc = A0; //analog reading |
open4416 | 2:04dde3a9d171 | 7 | Serial pc(D1, D0); //Serial reg |
open4416 | 2:04dde3a9d171 | 8 | PwmOut led = D13; //link leg |
open4416 | 2:04dde3a9d171 | 9 | AnalogOut dac = A2; //Note!!! Only for F446re, not aviable for MCU without DAC |
open4416 | 0:d68e088dfbcd | 10 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 11 | |
open4416 | 0:d68e088dfbcd | 12 | |
open4416 | 0:d68e088dfbcd | 13 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 1:5968363c5d23 | 14 | Ticker TT1; //call a timer |
open4416 | 2:04dde3a9d171 | 15 | int TimeCount = 0; //work as a clock |
open4416 | 2:04dde3a9d171 | 16 | float SignalGenValue = 0.0f; //value for DAC or PWM reference |
open4416 | 2:04dde3a9d171 | 17 | float SignalProbeValue = 0.0f; //value for ADC readed |
open4416 | 0:d68e088dfbcd | 18 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 19 | |
open4416 | 0:d68e088dfbcd | 20 | |
open4416 | 0:d68e088dfbcd | 21 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 22 | void init_IO(); //initialize IO state |
open4416 | 0:d68e088dfbcd | 23 | void init_TIMER(); //set TT_main{} rate |
open4416 | 1:5968363c5d23 | 24 | void TT1_main(); //timebase function rated by TT1 |
open4416 | 0:d68e088dfbcd | 25 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 26 | |
open4416 | 0:d68e088dfbcd | 27 | |
open4416 | 0:d68e088dfbcd | 28 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 29 | int main() |
open4416 | 0:d68e088dfbcd | 30 | { |
open4416 | 0:d68e088dfbcd | 31 | init_IO(); //initialized value |
open4416 | 1:5968363c5d23 | 32 | init_TIMER(); //initialized Timer Inturrupt rate |
open4416 | 0:d68e088dfbcd | 33 | |
open4416 | 0:d68e088dfbcd | 34 | while(1) { //main() loop |
open4416 | 0:d68e088dfbcd | 35 | } |
open4416 | 0:d68e088dfbcd | 36 | } |
open4416 | 0:d68e088dfbcd | 37 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 38 | |
open4416 | 0:d68e088dfbcd | 39 | |
open4416 | 0:d68e088dfbcd | 40 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 41 | void init_IO(void) //initialize |
open4416 | 0:d68e088dfbcd | 42 | { |
open4416 | 2:04dde3a9d171 | 43 | led = 0.0f; |
open4416 | 2:04dde3a9d171 | 44 | dac = 0.0f; |
open4416 | 2:04dde3a9d171 | 45 | pc.baud(115200); //set baud rate |
open4416 | 0:d68e088dfbcd | 46 | } |
open4416 | 0:d68e088dfbcd | 47 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:d68e088dfbcd | 48 | |
open4416 | 0:d68e088dfbcd | 49 | |
open4416 | 0:d68e088dfbcd | 50 | |
open4416 | 0:d68e088dfbcd | 51 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 1:5968363c5d23 | 52 | void init_TIMER() //set TTx_main{} rate |
open4416 | 0:d68e088dfbcd | 53 | { |
open4416 | 2:04dde3a9d171 | 54 | TT1.attach_us(&TT1_main, 20000); //0.02 sec |
open4416 | 0:d68e088dfbcd | 55 | } |
open4416 | 0:d68e088dfbcd | 56 | |
open4416 | 1:5968363c5d23 | 57 | void TT1_main() //interrupt function by TT1 |
open4416 | 0:d68e088dfbcd | 58 | { |
open4416 | 2:04dde3a9d171 | 59 | //generator time reference |
open4416 | 2:04dde3a9d171 | 60 | TimeCount = TimeCount + 1; |
open4416 | 2:04dde3a9d171 | 61 | if(TimeCount > 50) { |
open4416 | 2:04dde3a9d171 | 62 | TimeCount=0; |
open4416 | 2:04dde3a9d171 | 63 | } |
open4416 | 2:04dde3a9d171 | 64 | |
open4416 | 2:04dde3a9d171 | 65 | //start generating signal output |
open4416 | 2:04dde3a9d171 | 66 | SignalGenValue = pow(sin(pi*TimeCount/50.0f),2); |
open4416 | 2:04dde3a9d171 | 67 | dac = SignalGenValue; //Note!!! Only for F446re, not aviable for MCU without DAC |
open4416 | 2:04dde3a9d171 | 68 | led = SignalGenValue; //only for you to see directly from eyes |
open4416 | 2:04dde3a9d171 | 69 | |
open4416 | 2:04dde3a9d171 | 70 | //start reading analog signal |
open4416 | 2:04dde3a9d171 | 71 | SignalProbeValue = adc.read(); //acquire analog data |
open4416 | 2:04dde3a9d171 | 72 | |
open4416 | 2:04dde3a9d171 | 73 | //print out data |
open4416 | 2:04dde3a9d171 | 74 | pc.printf("%.2f\r", SignalProbeValue); //transfer data to PC |
open4416 | 0:d68e088dfbcd | 75 | } |
open4416 | 0:d68e088dfbcd | 76 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |