Providing a delay in LPC2148

To provide a delay in LPC2148, T0CTCR (Timer 0 Count Control Register), T0PR (Timer 0 Prescalar Register), T0TCR (Timer 0 Control Register), T0MR0 (Timer 0 Match Register 0) and T0MCR (Timer 0 Match Control Register) is used.
T0CTCR specifies the mode, i.e. in this case we take timer mode.
T0PR is the prescalar, if I want 1 second delay, and the clock speed is 12MHz, then I will load the prescalar register with 12,000,000-1 = 11,999,999 or B71AFF in hex.


(The point of processor adding 1 to the prescalar register is to avoid the Divide By Zero error).


T0TCR is a control register, it will start, stop or continue the timer/counter based on the value set. So initially it has to be reset and enable for which 2 and 1, effectively 3 can be loaded. After the value overflows, it has to be disabled by giving 0.
T0MR0 is used to generate an interrupt every time MR0 matches the TC.
T0MCR decides what to do, i.e. whether to reset or trigger an interrupt.


#include<lpc214x.h>
void delay(int n)
{
//timer function for 1 sec delay
T0CTCR=0X00; //TIMER MODE
T0PR=0X00B71AFF; // Load value in PR register
T0TCR=0X02; //Reset Counter
T0TCR=0X01; //Enable Counter
T0MR0=n;// Match Register
while(T0TC!=T0MR0);
T0TCR=0X00; //Disable Counter
T0MCR=0X0002; //Reset on MR0 and Interrupt is disabled
}

int main(void)
{
PINSEL0=0X00000000;
IO0DIR=0X00000001;
while(1)
{
IO0CLR|=0X00000001;
delay(1);
IO0SET|=0X00000001;
delay(1);
}
}

No comments:

Post a Comment