![](/media/cache/group/15095033_10153875960761761_5473312716092994677_n.jpg.50x50_q85.jpg)
For NTHU_Racing MCU tutorial Learn to check task over load
Dependencies: mbed
main.cpp@0:29cea2f51203, 2017-01-21 (annotated)
- Committer:
- open4416
- Date:
- Sat Jan 21 13:10:56 2017 +0000
- Revision:
- 0:29cea2f51203
For NTHU_Racing MCU tutorial; Learn to check task over load
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
open4416 | 0:29cea2f51203 | 1 | #include "mbed.h" |
open4416 | 0:29cea2f51203 | 2 | |
open4416 | 0:29cea2f51203 | 3 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 4 | DigitalOut led(D13); //link leg |
open4416 | 0:29cea2f51203 | 5 | DigitalOut TT_main_ext(D12); //linl leg |
open4416 | 0:29cea2f51203 | 6 | AnalogIn adc(A0); //analog leg |
open4416 | 0:29cea2f51203 | 7 | Serial pc(D1, D0); //Serial reg |
open4416 | 0:29cea2f51203 | 8 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 9 | |
open4416 | 0:29cea2f51203 | 10 | |
open4416 | 0:29cea2f51203 | 11 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 12 | Ticker TT; //call a timer |
open4416 | 0:29cea2f51203 | 13 | int count = 0; //one second counter for extrenal led blink |
open4416 | 0:29cea2f51203 | 14 | float adc_read = 0; |
open4416 | 0:29cea2f51203 | 15 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 16 | |
open4416 | 0:29cea2f51203 | 17 | |
open4416 | 0:29cea2f51203 | 18 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 19 | void init_IO(); //initialize IO state |
open4416 | 0:29cea2f51203 | 20 | void init_TIMER(); //set TT_main{} rate |
open4416 | 0:29cea2f51203 | 21 | void TT_main(); //timebase function rated by TT |
open4416 | 0:29cea2f51203 | 22 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 23 | |
open4416 | 0:29cea2f51203 | 24 | |
open4416 | 0:29cea2f51203 | 25 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 26 | int main() |
open4416 | 0:29cea2f51203 | 27 | { |
open4416 | 0:29cea2f51203 | 28 | init_IO(); //initialized value |
open4416 | 0:29cea2f51203 | 29 | init_TIMER(); //start TT_main |
open4416 | 0:29cea2f51203 | 30 | pc.baud(9600); //set baud rate |
open4416 | 0:29cea2f51203 | 31 | |
open4416 | 0:29cea2f51203 | 32 | while(1) { //main() loop |
open4416 | 0:29cea2f51203 | 33 | } |
open4416 | 0:29cea2f51203 | 34 | |
open4416 | 0:29cea2f51203 | 35 | } |
open4416 | 0:29cea2f51203 | 36 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 37 | |
open4416 | 0:29cea2f51203 | 38 | |
open4416 | 0:29cea2f51203 | 39 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 40 | void init_IO(void) //initialize |
open4416 | 0:29cea2f51203 | 41 | { |
open4416 | 0:29cea2f51203 | 42 | led = 0; |
open4416 | 0:29cea2f51203 | 43 | TT_main_ext = 0; |
open4416 | 0:29cea2f51203 | 44 | } |
open4416 | 0:29cea2f51203 | 45 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 46 | |
open4416 | 0:29cea2f51203 | 47 | |
open4416 | 0:29cea2f51203 | 48 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |
open4416 | 0:29cea2f51203 | 49 | void init_TIMER() //set TT_main{} rate |
open4416 | 0:29cea2f51203 | 50 | { |
open4416 | 0:29cea2f51203 | 51 | TT.attach_us(&TT_main, 1000); |
open4416 | 0:29cea2f51203 | 52 | } |
open4416 | 0:29cea2f51203 | 53 | |
open4416 | 0:29cea2f51203 | 54 | void TT_main() //interrupt function by TT |
open4416 | 0:29cea2f51203 | 55 | { |
open4416 | 0:29cea2f51203 | 56 | TT_main_ext = !TT_main_ext; //indicate TT_main() function working |
open4416 | 0:29cea2f51203 | 57 | count = count+1; //one second counter |
open4416 | 0:29cea2f51203 | 58 | if(count == 1000) { //check if main working |
open4416 | 0:29cea2f51203 | 59 | count=0; |
open4416 | 0:29cea2f51203 | 60 | led = !led; |
open4416 | 0:29cea2f51203 | 61 | } |
open4416 | 0:29cea2f51203 | 62 | adc_read = adc.read(); |
open4416 | 0:29cea2f51203 | 63 | pc.printf("%.2f\r", adc_read); |
open4416 | 0:29cea2f51203 | 64 | } |
open4416 | 0:29cea2f51203 | 65 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// |