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.

learn-embedded-system

TIMER REGISTERS in LPC2147 ARM7

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

PC Prescale 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
PR Prescale Register: The 32-bit register which hold the maximum value of prescale counter after which it reset
TC Timer Counter: This is 32-bit Timer Counter which gets incremented whenever PC Prescale Counter value reaches to its maximum value as specified in PR
TCR Timer Control Register: Timer Control register used to control the timer control functions. We’ll enable, disable and reset Timer Counter (TC) through this register
CTCR Count 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)]


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.
#include <lpc214x.h>

void initClocks(void); 	            // Setup PLL and Clock Frequency
void initTimer0(void);              // Setup and Initialize Timer0 
void delay_ms(unsigned int counts); // Generate delay

int main(void)
{
    initClocks();        //Initialize CPU and Peripheral Clocks @ 60Mhz
    initTimer0();        //Initialize Timer0
    IO0DIR = (1<<10);	//Configure Pin P0.10 as Output
   
    while(1)
    {
        IO0SET = (1<<10); //Turn ON LED
        delay_ms(1000);
        IO0CLR = (1<<10); //Turn LED OFF
        delay_ms(1000);
    }
    //return 0;  
}

void initTimer0(void)
{
    T0CTCR = 0x0;   //Set Timer 0 into Timer Mode
    T0PR = 59999;   //Increment T0TC at every 60000 clock cycles
        //Count begins from zero hence subtracting 1
        //60000 clock cycles @60Mhz = 1 mS
    T0TCR = 0x02;   //Reset Timer
}

void delay_ms(unsigned int counts) //Using Timer0
{
    T0TCR = 0x02;        //Reset Timer
    T0TCR = 0x01;        //Enable timer
   
    while(T0TC < counts);//wait until TC reaches the desired delay
   
    T0TCR = 0x00;        //Disable timer
}

void initClocks(void)
{
  PLL0CON = 0x01;   //Enable PLL
  PLL0CFG = 0x24;   //Multiplier and divider setup
  PLL0FEED = 0xAA;  //Feed sequence
  PLL0FEED = 0x55;
  
  while(!(PLL0STAT & 0x00000400)); //is locked?
    
  PLL0CON = 0x03;   //Connect PLL after PLL is locked
  PLL0FEED = 0xAA;  //Feed sequence
  PLL0FEED = 0x55;
  VPBDIV = 0x01;    // PCLK is same as CCLK i.e.60 MHz
}

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.

void initTimer0(void)
{
    T0CTCR = 0x0;   //Set Timer 0 into Timer Mode
    T0PR = 59999;   //Increment T0TC at every 60000 clock cycles
        //Count begins from zero hence subtracting 1
        //60000 clock cycles @60Mhz = 1 mS
    T0TCR = 0x02;   //Reset Timer
}

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.

void delay_ms(unsigned int counts) //Using Timer0
{
    T0TCR = 0x02;         //Reset Timer
    T0TCR = 0x01;         //Enable timer
   
    while(T0TC < counts); //wait until TC reaches the desired delay
   
    T0TCR = 0x00;         //Disable timer
}

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.

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

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

Login

Register | Lost your password?