For NTHU_Racing MCU tutorial Learn to use timer inturrupt

Dependencies:   mbed

sample_timebase.cpp

Committer:
open4416
Date:
2016-07-15
Revision:
0:d68e088dfbcd
Child:
1:5968363c5d23

File content as of revision 0:d68e088dfbcd:

#include "mbed.h"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
DigitalOut  led =           D13;            //link leg
DigitalOut  TT_main_ext =   D12;            //linl leg
//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of GPIO registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//


//~~~~~~~~~~~~~~~~~~~~~~~~~~~Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Ticker  TT;                                 //call a timer
int     count = 0;                          //one second counter for extrenal led blink
//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Varible registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//


//~~~~~~~~~~~~~~~~~~~~~~~~~~~Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void init_IO();                         //initialize IO state
void init_TIMER();                      //set TT_main{} rate
void TT_main();                         //timebase function rated by TT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Function registor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//


//~~~~~~~~~~~~~~~~~~~~~~~~~~~main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int main()
{
    init_IO();                          //initialized value
    init_TIMER();                       //start TT_main

    while(1) {                          //main() loop
        if(count >= 1000) {             //check if main working
            count=0;
            led = !led;
        }

    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of main funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//






//~~~~~~~~~~~~~~~~~~~~~~~~~~~init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void init_IO(void)                      //initialize
{
    led = 1;
    TT_main_ext = 1;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of init_IO funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//



//~~~~~~~~~~~~~~~~~~~~~~~~~~~Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void init_TIMER()                       //set TT_main{} rate
{
    TT.attach_us(&TT_main, 1000);
}

void TT_main()                          //interrupt function by TT
{
    TT_main_ext = !TT_main_ext;         //indicate TT_main() function working
    count = count+1;                    //one second counter
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~end of Timebase funtion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//