Timer0 Example Code
I had trouble determining how to get the ARM mbed Timer0 to work. Bits and pieces of code only went so far. Here's the code I used based on experiments of mine and others. It changes the state of an LED every 8 or so seconds. The code is simplistic so beginners can understand it. Each interrupt from timer0, with a period of about 100 milliseconds, increments timer_count. When timer_count exceeds 80, as checked in the main program, the LED changes state. The interrupt-service routine checks to ensure the interrupt came from timer0. In this code, timer0 continues to run but you could disable the interrupt and re-enable it at a later time. I hope this example helps you use timer0.
#include "mbed.h" #include "LPC17xx.h" DigitalOut myled(LED1); unsigned short timer_count; extern "C" void TIMER0_IRQHandler (void) { if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed { LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag timer_count++; //increment timer_count } } void timer0_init(void) { LPC_SC->PCONP |=1<1; //timer0 power on LPC_TIM0->MR0 = 2398000; //100 msec LPC_TIM0->MCR = 3; //interrupt and reset control //3 = Interrupt & reset timer0 on match //1 = Interrupt only, no reset of timer0 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt LPC_TIM0->TCR = 1; //enable Timer0 pc.printf("Done timer_init\n\r"); } int main (void) { myled = 0; timer0_init(); timer_count = 0; while(1) { if (timer_count > 80) //Set timer_count for about 8 seconds { if (myled == 1) //If LED off, turn it on & { myled = 0; timer_count = 0; //reset count } else { myled = 1; //If LED on, turn it off & timer_count = 0; //reset count } } } }
-end-
9 comments on Timer0 Example Code:
Please log in to post comments.
thanks for this!