Saturday, August 27, 2022

ARM LPC2148 embedded C programming : Timer implementation

ARM LPC2148 Timer programming



TIMERS (Timer0 and Timer1) & SFR Registers:

There are 2 nos. of 32 bit timers in this Microcontroller. Timer is basically a counter. For timer, internal clock is given as input. Internally, PCLK, Peripheral clock is given as the input pulses.

Timer0 registers

TC0 – Timer Counter Register (32 bit register). For every clock pulse, this register is incremented. Before the clock pulse is given to TCO, the clock pulses are given to the Prescale Counter namely

PC0 – 32 bit register. Along with PC0, there is another register namely Prescale Register PR0 – 32 bit register.

The PC0 gives one output pulse for each prescale register value. For example, if the PR0 has the value of decimal 10, then the PC0 gives one output pulse fo r every 10 input clock pulses. The Prescale  Counter output is given as the clock pulse to Timer Counter register TC0. In this way, 32 bit Prescale divider is included in the timer operation.

TCR0 – Timer Control Register. This register is used to reset the counter and disable/enable the Timer

Counter Register TC0.

T0MR0 – Match0 Register. It is a 32 bit register. The 32 bit content entered is compared with the content of  Timer/Counter Register TC0 continuously. When the match occurs, the following functions can be actuated.

1. If the interrupt enable bit is made 1, interrupt flag bit for MR0 is made 1 in T0IR register.

2. The Timer/Counter register can be made to reset.

3. The Timer/Counter register can be disabled.

4. In the corresponding pin MAT0.0 for MR0 , output can be generated.

The first 3 functions can be actuated by writing control bits in MCR register – Match Control Register.

Similar to T0MR0, there are T0MR1, T0MR2, T0MR3 match registers. They can be also used for comparing with the contents of Timer/Counter (TC) register. On match, the above functions for the corresponding match register can be actuated.

T0IR – It is an 8 bit interrupt register. For every match, corresponding bit in this interrupt register is set. On writing again the same bit in t he Interrupt Service Routine, the corresponding bit will be cleared. Similar to match registers, there are 4 capture registers namely T0CR0, T0CR1, T0CR2 and

T0CR3. The content of Timer/Counter (TC) register value is loaded into T0CR0, when an event on physical pin CAP0.0, one particular physical input.

T0CCR is a 32 bit register which controls which edges of the capture inputs are used to load the Capture registers and whether or not an interrupt is generated when a capture takes place. In order to use the timer as counter, T0CTCR register is used. It is used to change to counter mode and to determine in which pin, physical count input pulses is given.


PROGRAMLOGIC:

* Configure 8 pins of port1 from P1.16 to P1.23as output pins by writing value ‘1’ to these pins

* Connect 8 LEDS in output pins.

*Timer 0 of ARM7 is configure to produce delay of 0.5 Second

* Program loop is created to toggle LEDs connected in PORT1 for every 0.5 sec.


Program code

#include <LPC214x.h>

#define DELAY_MS 1000 //0.5 Second(s) Delay

#define PRESCALE 60000 //60000 PCLK clock cycles to increment TC by 1

void Timer0_Init(void);

void Timer0_Run(void);

int main(void)

{

VPBDIV = 0x01; //PCLK=60Mhz

IO1DIR = 0x00FF0000; //P1.16 to P1.23 are output

Timer0_Init(); //Initialize Timer0

while(1)

{

Timer0_Run();

}

}

void Timer0_Init(void)

{

/*Assuming that PLL0 has been setup with CCLK = 60Mhz and PCLK also = 60Mhz.*/

T0CTCR = 0x0;

T0PR = PRESCALE-1; //(Value in Decimal!) - Increment T0TC at every 60000 clock cycles

//Count begins from zero hence subtracting 1

//60000 clock cycles @60Mhz = 1 mS

T0MR0 = DELAY_MS-1; //(Value in Decimal!) Zero Indexed Count - hence subtracting 1

T0MCR = 3; //Set bit0 & bit1 to High which is to : Interrupt & Reset TC on MR0

T0TCR = 0x02; //Reset Timer

T0TCR = 0x01; //Enable timer

}

void Timer0_Run(void)

{

unsigned char regVal;

if(T0IR) //Polling the Interrupt Flag

{

regVal = T0IR;  //R ead current IR value

IO1PIN ^= 0x00FF0000; // Toggle LED pins in Port 1

T0IR = regVal; // Write back to IR to clear Interrupt Flag

}

}




Circuit Diagram







No comments:

Post a Comment

GPS sensor interface with ESP8266 using Blynk IoT cloud

   Circuit diagram: Source Code: #include <TinyGPS++.h> #include <SoftwareSerial.h> #define BLYNK_PRINT Serial #include <ESP8...