Timer Interrupt in LPC1768 Microcontroller

In previous tutorial, we have learned how to program most basic peripheral i.e. GPIO Port Pin of Microcontroller. Here in this tutorial, we’ll write simple application which will introduce you timer interrupt in LPC1768 Microcontroller.

Timer is internal peripheral in LPC1768. They use CPU clock to keep track of time and count. Timer enhances use of microcontroller in number of ways. Timers send periodic events and make precise measurements. It makes time available for your microcontroller project. This means you can start using temporal information in your program, without having to use unwieldy spin loops. In this tutorial our goal is to set up a timer and then with the help of interrupts we’ll blink the LED.

Timers in LPC1768 Basics

In LPC1768 Microcontroller there are four timers Timer 0, Timer1, Timer2 and Timer3. These are 32-bit timer/counter with programmable 32-bit prescaler. All are identical but can have options to set them independently. And can be used without interfering with each other.

timer_counter_block_diagram
Timer Counter Block Diagram

All timer’s are built around an internal Timer Counter (TC), which is a 32-bit register and incremented periodically. The rate of change is derived from the current speed of the CPU clock you’ve connected up and what the Prescale Counter is set to. There is nothing more to it than that. The Prescale Counter is clock divider. As mentioned earlier, Timer Counter is a 32-bit register and it counts in range from 0x00000000 to 0xFFFFFFFF.

Before we get into programming fundamentals, it’s important to understand powering procedure for LPC1768 microcontroller. Powering device is necessary before choosing the peripheral clock and setting the prescale register. We need to turn on timer. Upon RESET most of the LPC’s peripherals are OFF and aren’t being supplied power by microcontroller.

This can save a lot of power, but few core peripherals are turned on when the LPC starts, among these GPIO, TIMER0 and TIMER1. This means that TIMER2 and TIMER3 start off and if you need them in your application then you’ll have to turn them on. Additionally, if you don’t need TIMER0 or TIMER1, you can even turn them OFF to save some power.

Usually, power control is considered a system feature and is controlled by register LPC_SC–>PCONP. We’ll see in details while discussing program in later part of this tutorial. For more detail description on timer, Please visit User Manual UM10360 [Chapter: 21, Page No: 499].

Now let’s hook up LED to Pin P1.29. Please have a reference of Circuit Set:

led-blink-lpc1768-mcu
Circuit Schematic: LED with LPC1768

Let’s have a look at code snippet which uses TIMER0 to blink LED about every 1 Second,

Code Explanation:

Let me slice down complete program into pieces for explanation,

Power the device; here we set the bit in LPC_SC–>PCONP for TIMER0, to turn power on. This is essentially the same for most of other peripheral. After enabling power option we required to set the clock for TIMER0 to turn off CPU clock. Read more in User Manual [page No: 57, Table:40 and Table: 42].

In LPC1768 we have 4 match registers for each Timer. These match registers can be used for several reasons like, to reset the timer, generate interrupt, stop the timer, to generate timing signal on external pin. Here in this case, Match0 is set to 2^23 (Note: because startup code gets the chip running at 100 MHz by default, 1 tick or period of timer = 1/100MHz = 10ns, hence (2^23+1) ticks = 0.08388609 seconds. You’ll see LED blinking after every 0.083 sec. everything else in this section is well commented in code & self explanatory.

Inside the interrupt handler, we check for source of interrupt. In this case, TIMER0 can produce interrupt from Match0 and then toggle the LED. We’ll discuss more about Interrupt feature of Cortex-M3 microcontroller in future. I hope this tutorial will help you to understand timer interrupt in LPC1768 Microcontroller. You can download project files click button below:

Buy Cortex-M3 LPC1768 Microcontroller Kit

Buy Cortex-M3 LPC1768 Microcontroller Kit

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

6 comments

  1. hello
    i run your code but interrupt doesnt occur
    i also use cmsis example and interupt deosnt occur
    please help me

    • Hello Mohamad

      I am not sure what you are trying to do with interrupt. if you simply want to blink LED when interrupt triggers then I am sure that this code will work.

      Make sure to test your code on real hardware. I request you to provide more details on problem.

      Thanks and have a good day !!!!

  2. This Code works Perfectly fine..!!

    Good Job !!

    Just if you can tell me how to get the delay down to 1 micro sec. In the above code its coming in the TIMER0_IRQHandler fucntion every sec .

    What value should be put in LPC_TIM0->MR0 if the my CCLK is 72 Mhz.

    Thanks in Advance..!! Hoping to hear from you soon.

    Regards

    Sanket Chalke

  3. hello can I know how to make the counter coding? can you help me its urgent. thank you

    • hello aminrul ,

      First you have to intialize timer and set value in MR0 and also enable the TCR register then TC(TIMER COUNTER) increment starts upto it is equals to MR0 value..If match occur then MCR(Match Control Register) gives an interrupt.

      Here some example code :

      LPC_TIM0->MCR =0X03;
      LPC_TIM0->MR0= (120000000-1);
      NVIC_EnableIRQ(TIMER0_IRQn);
      NVIC_SetPriority(TIMER0_IRQn,0);
      LPC_TIM0->TCR =0x01;
      LPC_TIM0->IR =0x01;

Leave a Reply

Your email address will not be published. Required fields are marked *

Login

Register | Lost your password?