Creating Time delay using Timer in ARM7 (LPC2124) Microcontroller

Timers are one of the important concepts in a Microcontroller since it helps us to create precise time delays for a specific application. The usage of timers are inevitable in many of the Embedded Applications which we use in our real life. This tutorial focuses in creating time delays using timer in ARM7 (LPC2124) Microcontroller.

REGISTERS USED IN TIMERS:

I assume that you all are familiar with the Timer concepts in other Microcontrollers such as 8051 or PIC or AVR so i don’t need to explain the basic working principle behind the timers. In ARM Microcontroller there are two 32 bit Timers Timer0 and Timer1 which serves the purpose of generating time delays. Here are the registers used to program Timers in LPC2124 Microcontroller.

T0TCR:This register is used to control the functions of the Timer0. Enable and reset operations of the Timer0 register can be controlled by this register.

T0PR:This is a 32 bit Prescale register which holds the maximum value which the Prescale counter can take.

T0PC:This is a 32 bit Prescale counter which specifies the division value of the processor clock before feeding it to the timers. The value in the register increments with every input pulse or Processor clock fed into it. Prescale register T0PR gives the max value this T0PC register can take. When reaching the max count T0PC register gets reset on the next Processor clock.

HOW TIMER IN ARM7 CONTROLLER WORKS:

Like other Microcontrollers ARM controllers count the clock pulses in their timer operation but in a complicated way. Generally the clock fed to the controller is called as Processor Clock (PCLK) at rate which processor works. And for peripherals we can split the value in to divisions by using VPBDIV register to fit our requirements. By default Peripheral clock is equal to PCLK/4 and the same division is used in our code.

Initially the value in the T0PC increases with every incoming pulse and can attain a max limit up to the value stored in the T0PR register.

Once the T0PC reaches maximum value it gives rise to an increment of value in the T0TC register.

The value in the T0MR0 and T0TC will be checked by the processor to detect any match between.

Then finally interrupt or any operation will take place based on the T0MCR register.

CALCULATION OF TIME DELAY:

Peripheral Frequency = Oscillator Frequecny / VPB divider

Since we are using Default 1/4 divider here our peripheral frequency will be

Peripheral Frequency = 20 MHz / 4 = 5MHz

Then we have to calculate T0MR0 (count) value to generate required delay by matching

Count = (5MHz * Required time delay) – 1

= (5,000,000 * 100ms) – 1

= (5,000,000 * .1) -1

= 4,99,999

Now count 499999 gives the T0MR0 value to generate 100ms delay. So to make it as one sec we have to multiply it with 10. And that we can use it in the Prescale register T0PR, thus we can generate a delay of 1 sec using this.