## Simple LPC2148 GPIO Programming examples Using timers of LPC2148 to generate delay

### Simple LPC2148 GPIO Programming examples Using timers of LPC2148 to generate delay

##### QUESTION- Write an embedded C program to generate the delay of 100 msec using Timer of LPC 2148 with CCLK = 20 MHz and VPBDIV = 0 × 00

Solution-
Given – CPU Clock Frequency i.e. Cclk = 20 MHz
VPBDIV = 0 × 00
Delay Required  = 100 msec

Step 1 :-  if VPBDIV = 0 × 00 then  Pclk  = (1 /4 ) of Cclk ……….Relation
Therefore Pclk  = 5 Mhz

Step 2 :- Delay Generated by TC when incremented by 1 count value is =  (PR+1) / Pclk….seconds

Note keep in mind if possible always try to bring Delay generated by TC= 1 millisec
Assume PR = 4999
Put value in step 2
We get

Delay Generated by TC = (4999+1) / 5 x 10^6 = 1 milliSecond

Step 3 :- Now in order to Generate delay of 100 milliSeconds...(Reuired Delay)
TC must be incremented by 100, So we have to load Match Register with value 100 i.e. MRx = 100

Embedded C Program

void delay(void)
{
T0PR=4999;
T0MR0=100; // Count in match register
T0TC=0x00000000; // initial value always starts with Zero
T0TCR=0X01; //START TIMER
while(T0TC !=T0MR0); // Wait till T0TC value becomes equal to T0MR0
T0TCR=0X02; //STOP TIMER
}

##### QUESTION- Write an embedded C program to generate the delay of 1 sec using Timer of LPC 2148 with PR = 10,  CCLK = 20 MHz

Solution-
Given – CPU Clock Frequency i.e. Cclk = 20 MHz
PR = 10
Delay Required  = 1 sec

Step 1 :-  if VPBDIV = 0 × 00 then  Pclk  = (1 /4 ) of Cclk ……….Relation
Therefore Pclk  = 5 Mhz

Step 2 :- Delay Generated by TC when incremented by 1 count value is =  (PR+1) / Pclk….seconds

Note keep in mind if possible always try to bring Delay generated by TC= 1 millisec
But as in this case  PR = 10 Given
Put value in step 2
We get

Delay Generated by TC = (10+1) / (5 x 10^6 ) = 2.2 microSecond

Step 3 :- Now in order to Generate delay of 1 Second...(Reuired Delay)
TC must be incremented by 454545, So we have to load Match Register with value 454545 i.e. MRx = 454545

OR

Solution-
Given – CPU Clock Frequency i.e. Cclk = 20 MHz
PR = 10
Delay Required  = 1 sec

Step 1 :-  if  VPBDIV = 0 × 01 then  Pclk  = Cclk ……….Relation
Therefore Pclk  = 20 Mhz

Step 2 :- Delay Generated by TC when incremented by 1 count value is =  (PR+1) / Pclk….seconds

Note keep in mind if possible always try to bring Delay generated by TC= 1 millisec
But as in this case  PR = 10 Given
Put value in step 2
We get

Delay Generated by TC = (10+1) / (20 x 10^6) = 550 nanoSecond

Step 3 :- Now in order to Generate delay of 1 Second...(Reuired Delay)
TC must be incremented by 1818181, So we have to load Match Register with value 1818181 i.e. MRx = 1818181
OR
Solution-
Given – CPU Clock Frequency i.e. Cclk = 20 MHz
PR = 10
Delay Required  = 1 sec

Step 1 :-  if  VPBDIV = 0 × 02 then  Pclk  = ( 1 / 2 ) x Cclk ……….Relation
Therefore Pclk  = 10 Mhz

Step 2 :- Delay Generated by TC when incremented by 1 count value is =  (PR+1) / Pclk….seconds

Note keep in mind if possible always try to bring Delay generated by TC= 1 millisec
But as in this case  PR = 10 Given
Put value in step 2
We get

Delay Generated by TC = (PR+1) / Pclk = (10+1) / 10MHz =  1.1 microSecond

Step 3 :- Now in order to Generate delay of 1 Second...(Reuired Delay)
TC must be incremented by 909090, So we have to load Match Register with value 909090 i.e. MRx = 909090

Embedded C Program

void delay(void)
{
T0PR=10;
T0MR0=909090; // Count in match register
T0TC=0x00000000; // initial value always starts with Zero
T0TCR=0X01; //START TIMER
while(T0TC !=T0MR0); // Wait till T0TC value becomes equal to T0MR0
T0TCR=0X02; //STOP TIMER
}

##### Example-/* TOGGLE LEDS FROM P0.0 TO P0.15 WITH EXACT DELAY OF 3SEC */ Fosc=60 Mhz

/**************************************************************************/
/* Project Name:-Delay Generation using LPC2148 Timer */
/* Device:- LPC2148                          */
/* Compiler:- KeiluVision4                   */
/* Language:- Embedded C  */
/* Visit www.wikinote.org for more Details   */
/********************************************************************************************/
#include<lpc21xx.h>
void delay(void);
int main()
{ VPBDIV=0X02; //30MHZ// PINSEL0=0X00000000; IODIR0=0X0000FFFF;
IOCLR0=0X0000FFFF;
while(1)
{
IOSET0=0X0000FFFF;
delay();
IOCLR0=0X0000FFFF;
delay();
}
}
void delay(void)
{
T0PR=29999;
T0MR0=3000;
T0TC=0x00000000;
T0TCR=0X01; //START TIMER//
while(T0TC !=T0MR0); //1 SEC//
T0TCR=0X02; //STOP TIMER/
}

##### Example:-/* TOGGLE LEDS FROM P0.0 TO P0.7 WITH EXACT DELAY OF 2 SEC BY USING T0MR3.TOGGLE LEDS FROM P0.15 TO P0.23 WITH EXACT DELAY OF 5 SEC BY USING T1MR2. */(Fosc=60Mhz)

/**************************************************************************/
/* Project Name:-Delay Generation Using Timers of LPC2148  */
/* Device:- LPC2148                          */
/* Compiler:- KeiluVision4                   */
/* Language:- Embedded C  */
/* Visit www.wikinote.org for more Details   */
/********************************************************************************************/
#include<lpc21xx.h>
void delay1(void);
void delay2(void);
int main()
{
VPBDIV=0X02;//30MHZ//
PINSEL0=0X00000000;
IODIR0=0X00FF00FF;
IOCLR0=0X00FF00FF;
while(1)
{IOSET0=0X00FF00FF;
delay1();
IOCLR0=0X00FF00FF;
delay2();
}
}
void delay1(void)
{
T0PR=29999;
T0MR3=2000;
T0TC=0x00000000;
T0TCR=0X01; //START TIMER//
while(T0TC !=T0MR3); //2 SEC//
T0TCR=0X02; //STOP TIMER/
}
void delay2(void)
{
T1PR=29999;
T1MR2=5000;
T1TC=0x00000000;
T1TCR=0X01; //START TIMER//
while(T1TC !=T1MR2); //5 SEC//
T1TCR=0X02; //STOP TIMER//
}

### References

• Created, Edited, Notes by Prof. Sujit Wagh, Sinhgad's SKNCOE, Pune
• WikiNote Foundation