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:

31 Aug 2011

thanks for this!

22 Jun 2012

Thank you! This was exactly what I needed!

22 Jun 2012

Seems to me that this can be achieved more easily by a standard ticker function from the mbed library. The ticker can be set to generate either the 100ms interrupt or even the 8 second interrupt. The benefits of a low level approach wont show unless you start doing things like change the interval after each interrupt or something like that.

04 Jul 2012

Great example, but timer_count must be volatile!

04 Jul 2012

Serial pc(USBTX, USBRX);

16 Mar 2013

hi can yougive me how i can converts my programs from basic language to C language thanks you

17 Mar 2013

temam rabah wrote:

hi can yougive me how i can converts my programs from basic language to C language thanks you

Hello, Temam Rabah. I don't know of any "translator" that automatically translates a BASIC program into the C language. I suggest you start with a flow chart that shows how the BASIC program operates and then use that chart as an outline for your C code. Sometimes it is better to start over so you can take advantage of the capabilities of C and the mbed libraries. Perhaps someone else can help with another answer or two. Jon

21 Sep 2015

Nice example. Thanks !!

13 Oct 2015

Wim Huiskamp wrote:

Seems to me that this can be achieved more easily by a standard ticker function from the mbed library. The ticker can be set to generate either the 100ms interrupt or even the 8 second interrupt. The benefits of a low level approach wont show unless you start doing things like change the interval after each interrupt or something like that.

Hi, I want to implement this code in other board (Nucleo) different than the LPC1768, which I think this code is for. If you use the standard functions from mbed libraries, I think the same code can be use for different platforms. Can you help with this? thanks

Please log in to post comments.