Monday 23 July 2018

Timers on the ARM Microcontroller (LPC2148)

Aim:

Timers are widely used in industrial and domestic application for automating tasks. Microcontroller scan be used to design versatile and accurate timers with ease. Here I present a simple timer that can be used to turn on/off a load after user specified time.

Description:


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.



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 
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)]
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.



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.

Block Diagram

2.interfacing array of leds

Schematic:


Code

// **********************************************
// Project: Interfacing 8-bit timer0 to LPC2148
// Author: Hack Projects India
// Module description: Operate array of LED's
// **********************************************              
#include <LPC214x.H>                       /* LPC21xx definitions */
void timer_sec(unsigned int x)
{
 T0MR0 = x;
 T0MCR = 1;         // Clear on match
 T0PR = 0x00E4E1C0; // Prescaler for 1 sec
 T0TCR = 0x1;    // Enable
 while(T0TC < T0MR0);  // wait till match
 T0TCR = 0x0;    // Disable
 T0TC = 0x0;     // Precautionally clear the counter
}

int main (void) 
{
  unsigned int n; 
  PINSEL2=0x00000000;
  IODIR0 = 0x00FF;                     /* P1.16..23 defined as Outputs  */
  while (1) 
  {                              /* Loop forever */
    
      
      IOSET0 = 0x00FF;                          /* Turn on LED */
      timer_sec(1);                             /* Delay */
      IOCLR0 = 0x00FF;                 /* Turn off LEDs */
      timer_sec(1); 
   
  }
}


Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment