For NTHU_Racing MCU tutorial Learn to use timer inturrupt

Dependencies:   mbed

Committer:
open4416
Date:
Fri Jul 15 09:14:36 2016 +0000
Revision:
0:d68e088dfbcd
Child:
1:5968363c5d23
2016/7/15;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
open4416 0:d68e088dfbcd 1 #include "mbed.h"
open4416 0:d68e088dfbcd 2
open4416 0:d68e088dfbcd 3 //~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 4 DigitalOut led = D13; //link leg
open4416 0:d68e088dfbcd 5 DigitalOut TT_main_ext = D12; //linl leg
open4416 0:d68e088dfbcd 6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 7
open4416 0:d68e088dfbcd 8
open4416 0:d68e088dfbcd 9 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 10 Ticker TT; //call a timer
open4416 0:d68e088dfbcd 11 int count = 0; //one second counter for extrenal led blink
open4416 0:d68e088dfbcd 12 //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 13
open4416 0:d68e088dfbcd 14
open4416 0:d68e088dfbcd 15 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 16 void init_IO(); //initialize IO state
open4416 0:d68e088dfbcd 17 void init_TIMER(); //set TT_main{} rate
open4416 0:d68e088dfbcd 18 void TT_main(); //timebase function rated by TT
open4416 0:d68e088dfbcd 19 //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 20
open4416 0:d68e088dfbcd 21
open4416 0:d68e088dfbcd 22 //~~~~~~~~~~~~~~~~~~~~~~~~~~~main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 23 int main()
open4416 0:d68e088dfbcd 24 {
open4416 0:d68e088dfbcd 25 init_IO(); //initialized value
open4416 0:d68e088dfbcd 26 init_TIMER(); //start TT_main
open4416 0:d68e088dfbcd 27
open4416 0:d68e088dfbcd 28 while(1) { //main() loop
open4416 0:d68e088dfbcd 29 if(count >= 1000) { //check if main working
open4416 0:d68e088dfbcd 30 count=0;
open4416 0:d68e088dfbcd 31 led = !led;
open4416 0:d68e088dfbcd 32 }
open4416 0:d68e088dfbcd 33
open4416 0:d68e088dfbcd 34 }
open4416 0:d68e088dfbcd 35 }
open4416 0:d68e088dfbcd 36 //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 37
open4416 0:d68e088dfbcd 38
open4416 0:d68e088dfbcd 39
open4416 0:d68e088dfbcd 40
open4416 0:d68e088dfbcd 41
open4416 0:d68e088dfbcd 42
open4416 0:d68e088dfbcd 43 //~~~~~~~~~~~~~~~~~~~~~~~~~~~init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 44 void init_IO(void) //initialize
open4416 0:d68e088dfbcd 45 {
open4416 0:d68e088dfbcd 46 led = 1;
open4416 0:d68e088dfbcd 47 TT_main_ext = 1;
open4416 0:d68e088dfbcd 48 }
open4416 0:d68e088dfbcd 49 //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 50
open4416 0:d68e088dfbcd 51
open4416 0:d68e088dfbcd 52
open4416 0:d68e088dfbcd 53 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
open4416 0:d68e088dfbcd 54 void init_TIMER() //set TT_main{} rate
open4416 0:d68e088dfbcd 55 {
open4416 0:d68e088dfbcd 56 TT.attach_us(&TT_main, 1000);
open4416 0:d68e088dfbcd 57 }
open4416 0:d68e088dfbcd 58
open4416 0:d68e088dfbcd 59 void TT_main() //interrupt function by TT
open4416 0:d68e088dfbcd 60 {
open4416 0:d68e088dfbcd 61 TT_main_ext = !TT_main_ext; //indicate TT_main() function working
open4416 0:d68e088dfbcd 62 count = count+1; //one second counter
open4416 0:d68e088dfbcd 63 }
open4416 0:d68e088dfbcd 64 //~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//