Timers in LPC2148 ARM7 Microcontroller

In this tutorial we’ll explore the use timers in LPC2148 ARM7 Microcontroller. In general, timer means exactly how it sounds.  Timer and counter is very important feature which allows us to provide time variable to our microcontroller based project. Most microcontrollers comes with built-in timer peripheral. The LPC2148 has two functionally identical general purpose timers: Timer0 and Timer1. These both timers are 32-bit along with 32-bit prescaler. Timer allows us to generate precise time delay. For Example: In our blink LED example project, we’ve generated random delay of approximate 1 Sec. but using Timers we can generate accurate time delay. We’ll get into that while discussing example project of Timer. Apart from this we can use timers as pulse width modulator and also as free running timer.

Timers in LPC2148 ARM7 Microcontroller enable us to do really cool stuffs. Also timer enhances the use of microcontroller in many different ways. We may need to dedicate one more post to understand Match and Capture registers and its uses in real world application. Let’s first understand free running counter and related fundamentals.

How Timers in LPC2148 ARM7 Microcontroller Works?

The heart of timers of the LPC2148 Microcontroller is a 32-bit free running counter, which is designed to count cycles of the Peripheral Clock (PCLK) or an external clock, this counter is programmable with 32-bit prescaler.

timers in LPC2148 ARM7 Microcontroller
32 bit Timer Counter with 32 bit Prescaler

The tick rate of the Timer Counter (TC) is controlled by the 32-bit number written in the Prescaler Register (PR) in the following way. There is a Prescale Counter (PC) which increments on each tick of the PCLK. When it reaches the value in the prescaler register, the timer count is incremented and the Prescaler Counter (PC) is reset, on the next PCLK. This cause the timer counters to increment on every PCLK when PR=0, every 2 PCLKs when PR=1, etc.

TIMER REGISTERS in LPC2147 ARM7

This is quick introduction. You’ll use datasheet for your reference [Page No: 247-248]

PCPrescale Counter: The 32-bit PC is a counter which is incremented to the value stored in PR (Prescale Register) when value in PR is reached, The TC (Timer Counter) is incremented and PC is cleared. The PC is observable and controllable through bus interface
PRPrescale Register: The 32-bit register which hold the maximum value of prescale counter after which it reset
TCTimer Counter: This is 32-bit Timer Counter which gets incremented whenever PC Prescale Counter value reaches to its maximum value as specified in PR
TCRTimer Control Register: Timer Control register used to control the timer control functions. We’ll enable, disable and reset Timer Counter (TC) through this register
CTCRCount Control Register: This register selects Timer Counter Mode. In our example we have used Timer Mode. This can be done by setting CTCR to 0x0. [In Timer Mode every rising PCLK edge can increment Timer’s Prescale Counter (PC) or clear PC and increment Timer Counter (TC)]

Check out this ARM7 LPC2148 Development Kit I found on Amazon

EXAMPLE PROJECT: In this example project we’ll generate precise 1 Sec. of delay using Timer0. The Timer uses PCLK (Peripheral Clock) as a clock source. From previous post we’ve seen how to set up PLL in LPC2148 ARM7. The Peripheral Clock (PCLK) has to be initialized before using Timer. Here in this example: we have used 12 MHz external clock to be tick at 60 MHz.

Circuit Timers in ARM7 LPC2148
Circuit Timers in ARM7 LPC2148

Before we jump start on writing program. We have to understand setting up of Timer Registers or you can say sequence of operations. We’ll be following along:

  • Setup Timer T0 into Timer Mode (T0CTCR = 0x0)
  • Setup Prescale value in T0PR (in our case 59999)
  • Reset Timer by setting (T0TCR = 0x02)
  • Setup T0TCR to 0x01 to Enable Timer when needed
  • Reset T0TCR to 0x00 to Disable Timer when needed

You may be wondering, why we have set Prescale Value to 59,999. Let’s calculate prescale count value:

Required Time 1 Second = 1/1000 milliseconds = 0.001 seconds (Resolution=1 ms)
PRESCALE COUNT= (60 MHz x Required Time Delay) – 1
= (60000000 x 0.001) – 1
= 59999
Hence the delay required for TC to increment by 1 will be 1 ms.
Download Project:  Timers in LPC2148 ARM7 Microcontroller

CODE EXPLANATION:

We believe that you’re following these tutorial series in a sequence. Here we’ll discuss only about some functions which deals with setup and configuration of Timer0. First few lines are self explanatory & well commented in code itself.

In this function we’ll setup and initialize Timer0. Also we have to select Timer Mode then setup prescale value in Prescale Register (T0PR) to 59999. The prescale value defines resolution of Timer0. In this case, at 6000 clock cycles at 60 MHz. We’ll get 1 ms resolution. Then simply reset Timer0.
Here in this function, we’ll use Timer0 to generate precise delay of 1 sec. We have to check value in Timer Counter Register (T0TCR) and wait until T0TC reaches to 1000 counts. Which will then generate 1000 ms i.e. 1 sec. time delay. We hope this post will help you to understand Timers in LPC2148 ARM7 Microcontroller. If you have any questions then write into comment section. In next tutorial we’ll explore Interrupt in LPC2148 ARM7 Microcontroller.[/box]

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

7 comments

  1. its so easy to understand……….thanks

  2. Thank you so much, this is really helpful.. Quick and pointing to the subject without any unwanted descriptions.

  3. rama krishna reddy.v

    thank you very much and better we have to know about timer counter registers format

  4. I am trying to generate delay as described above but T0TC value not going to zero with reset.

  5. I want external frequency counter code for lpc2148

  6. Great Work buddy, i may know that how to calculate 30 Seconds using your Calculations.. Please Guide me.. i am currently working on IoT Project based ESP8266 to update the API for every 30Secs…

Leave a Reply

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

Login

Register | Lost your password?