0% found this document useful (0 votes)
14 views123 pages

GPIO and LCD Programming with LPC2138

embedded system manual

Uploaded by

surajvarude14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views123 pages

GPIO and LCD Programming with LPC2138

embedded system manual

Uploaded by

surajvarude14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Exp. No. Title Page No.

1 GPIO Programming 2

2 Study of Seven Segment display Interface with LPC2138 10

3 Study of 16x2 LCD Interface with LPC2138 19

4 PLL Programming 29

5 Study of Interrupts 35

6 Study of Interrupts for multiple interrupts 42

7 Study of Timers and capture facility 51

8 Study of Match Facility 57

9 Study of PWM 69

10 Study Serial Communication of LPC2138 72

11 Study of ADC of LPC2138 83

12 Study of DAC of LPC2138 95

13 Extra Question 1 99

14 Extra Question 2 107

15 Extra Question 3 120


Embedded System Design Lab, 6EN352

Experiment Number: 1
Experiment Title: GPIO Programming
Objective 1: Flash LED connected at PORT0.4 using software delay.
Proteus Circuit Diagram:

Connections:
P0.4 → LED
Program:
#include <LPC213X.H>
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<2200;j++);
}
}
int main()
{

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

IODIR0|=1<<4;
while(1)
{
IOSET0|=1<<4;
delay(10);
IOCLR0|=1<<4;
delay(10);
}
}
Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 1
Experiment Title: GPIO Programming
Objective 2: Operate LED connected at PORT0.4 using a SWITCH connected at
PORT1.16
Proteus Circuit Diagram:

Connections:
P0.4 → LED
Program:
#include <LPC213X.H>
void delay(unsigned int t){
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<2200;j++);
}
}
int main()
{
IODIR0|=1<<4;
while(1)
{
if(IOPIN1&(1<<16))
{
IOSET0|=1<<4;
delay(10);
}
else

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
IOCLR0|=1<<4;
delay(10);
}
}
}
Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 1
Experiment Title: GPIO Programming
Objective 3: Operate AC bulb connected at PORT0.4 using a SWITCH connected at
PORT1.16 (Use RELAY).
Proteus Circuit Diagram:

Connections:
P0.4 → AC bulb
Program:
#include <LPC213X.H>
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<2200;j++);
}
}
int main()
{
IODIR0|=1<<4;
while(1)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
if(IOPIN1&(1<<16))
{
IOSET0|=1<<4;
delay(10);
}
else
{
IOCLR0|=1<<4;
delay(10);
}
}

}
Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 2
Experiment Title: Study of Seven Segment Display interface with LPC2138/48
Objective 1: Interface common cathode type seven segment display with LPC2138/48
and write an embedded C program to display numbers from 0 to 9 on seven segment
display.
Proteus Circuit Diagram:

Connections:
P0.0 to P0.6 → COMMON CATHODE SEVEN SEGMENT DISPLAY
Program:
#include <LPC213X.H>

void delay()
{

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

unsigned int i,j;


for(i=0;i<100;i++)
{
for(j=0;j<1275;j++);
}
}

int main()
{
unsigned char VAL[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char i;
IODIR0=(0x7F << 0);
while(1)
{
for(i=0;i<10;i++)
{
IOPIN0 &= 0;
IOPIN0 |= (VAL[i] << 0);
delay();
}
}
return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 2: Interface keyboard matrix (Keypad) and common cathode type seven
segment display with LPC2138/48 and write an embedded C program to display
pressed number on seven segment display.
Proteus Circuit Diagram:

Connections:
P0.0 to P0.6 → COMMON CATHODE SEVEN SEGMENT DISPLAY
P1.16 to P1.19 → KEYPAD MATRIX ROWS
P1.20 to P1.22 → KEYPAD MATRIX COLUMNS
Program:
#include <LPC213X.H>

void delay()
{
unsigned int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<1275;j++);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

}
}

int main()
{
IODIR0=(0x7F << 0);
IODIR1=(0x0F << 16);
IOPIN0 &= 0;
while(1)
{
IOCLR1=(1<<16);
IOSET1=(1<<17);
IOSET1=(1<<18);
IOSET1=(1<<19);

if(((IOPIN1)&(1<<20))!=(1<<20))
{
IOPIN0 &= 0;
IOSET0 |= 0x06;
}
else if(((IOPIN1)&(1<<21))!=(1<<21))
{
IOPIN0 &= 0;
IOSET0 |= 0x5B;
}
else if(((IOPIN1)&(1<<22))!=(1<<22))
{
IOPIN0 &= 0;
IOSET0 |= 0x4F;
}
delay();

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

IOSET1=(1<<16);
IOCLR1=(1<<17);
IOSET1=(1<<18);
IOSET1=(1<<19);

if(((IOPIN1)&(1<<20))!=(1<<20))
{
IOPIN0 &= 0;
IOSET0 |= 0x66;
}
else if(((IOPIN1)&(1<<21))!=(1<<21))
{
IOPIN0 &= 0;
IOSET0 |= 0x6D;
}
else if(((IOPIN1)&(1<<22))!=(1<<22))
{
IOPIN0 &= 0;
IOSET0 |= 0x7D;
}
delay();

IOSET1=(1<<16);
IOSET1=(1<<17);
IOCLR1=(1<<18);
IOSET1=(1<<19);

if(((IOPIN1)&(1<<20))!=(1<<20))
{
IOPIN0 &= 0;
IOSET0 |= 0x07;
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

else if(((IOPIN1)&(1<<21))!=(1<<21))
{
IOPIN0 &= 0;
IOSET0 |= 0x7F;
}
else if(((IOPIN1)&(1<<22))!=(1<<22))
{
IOPIN0 &= 0;
IOSET0 |= 0x6F;
}
delay();

IOSET1=(1<<16);
IOSET1=(1<<17);
IOSET1=(1<<18);
IOCLR1=(1<<19);

if(((IOPIN1)&(1<<20))!=(1<<20))
{
IOPIN0 &= 0;
}
else if(((IOPIN1)&(1<<21))!=(1<<21))
{
IOPIN0 &= 0;
IOSET0 |= 0x3F;
}
else if(((IOPIN1)&(1<<22))!=(1<<22))
{
IOPIN0 &= 0;
}
delay();
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 3: Interface common cathode type seven segment multiplexed display with
LPC2138/48 and write an embedded C program to display “HELP” on multiplexed
display.
Proteus Circuit Diagram:

Connections:
P0.0 to P0.6 → DATA LINES OF COMMON CATHODE MULTIPLEXED DISPLAY
P0.11 to P0.14 → CONTROL LINES OF COMMON CATHODE MULTIPLEXED
DISPLAY
Program:
#include <LPC213X.H>
void delay()
{
unsigned int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

}
}

int main()
{
unsigned char VAL1[]={0x76,0x79,0x38,0x73};
unsigned char KEY[]={0x0E,0x0D,0x0B,0x07};
unsigned char i;
IODIR0=(0x7F << 0) | (0xF << 11);
while(1)
{
for(i=0;i<4;i++)
{
IOPIN0 &= 0;
IOSET0 |= (KEY[i] << 11);
IOSET0 |= (VAL1[i] << 0);
delay();
}
}
return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 3
Experiment Title: Study of 16 * 2 LCD interface with LPC2138/48
Objective 1:
Interface 16 *2 LCD with LPC2138/48 and write an embedded C program to display
string “EMBEDDED SYSTEMS” on LCD. (Use 8-bit mode of LCD.)
Proteus Circuit Diagram:

Connections:
P0.4 → RS
P0.5 → R/W
P0.6 → E

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

P0.8 to P0.15 → D0 to D7.


Program:
#include <LPC213X.H>

void delay(unsigned int t)


{
unsigned int i,j;
for(j=0;j<t;j++)
{
for(i=0;i<1275;i++);
}
}

void lcd_command(unsigned char cmd)


{
IOPIN0 &= 0;
IOPIN0 |= (cmd << 8);
IOCLR0 |= (1<<4);
IOCLR0 |= (1<<5);
IOSET0 |= (1<<6);
delay(1);
IOCLR0 |= (1<<6);
}

void lcd_data(unsigned char data)


{
IOPIN0 &= 0;
IOPIN0 |= (data << 8);
IOSET0 |= (1<<4);
IOCLR0 |= (1<<5);
IOSET0 |= (1<<6);
delay(1);
IOCLR0 |= (1<<6);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void lcd_init()
{
lcd_command(0x01);
delay(10);
lcd_command(0x38);
delay(10);
lcd_command(0x80);
delay(10);
lcd_command(0x0C);
delay(10);
}

int main()
{
unsigned char i;
unsigned char M1[]="EMBEDDED SYSTEMS";
IODIR0=(7<<4) | (0xFF<<8);
while(1)
{
i=0;
lcd_init();
while(M1[i] != '\0')
{
lcd_data(M1[i]);
delay(500);
i++;
}
}
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation:

Objective 2:
Interface 16 *2 LCD with LPC2138/48 and write an embedded C programto
display/toggle string “EMBEDDED SYSTEMS” and “ELECTRONICSDEPT.” on
LCD. (Use 8-bit mode of LCD.
Proteus Circuit Diagram:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Connections:
P0.4 → RS
P0.5 → R/W
P0.6 → E
P0.8 to P0.15 → D0 to D7.
Program:
#include <LPC213X.H>

void delay(unsigned int t)


{

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

unsigned int i, j;
for (j = 0; j < t; j++)
{
for (i = 0; i < 1275; i++);
}
}

void lcd_command(unsigned char cmd)


{
IOPIN0 &= 0;
IOPIN0 |= (cmd << 8);
IOCLR0 |= (1 << 4);
IOCLR0 &= ~(1 << 5);
IOSET0 |= (1 << 6);
delay(1);
IOCLR0 |= (1 << 6);
}

void lcd_data(unsigned char data)


{
IOPIN0 &= 0;
IOPIN0 |= (data << 8);
IOSET0 |= (1 << 4);
IOCLR0 &= ~(1 << 5);
IOSET0 |= (1 << 6);
delay(1);
IOCLR0 |= (1 << 6);
}

void lcd_init()
{
lcd_command(0x01);
delay(10);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

lcd_command(0x38);
delay(10);
lcd_command(0x80);
delay(10);
lcd_command(0x0C);
delay(10);
}

void lcd_display_string(const unsigned char *str)


{
while (*str != '\0')
{
lcd_data(*str++);
delay(50);
}
}

int main()
{
unsigned char M1[] = "EMBEDDED SYSTEMS";
unsigned char M2[] = "ELECTRONICS DEPARTMENT";
unsigned int toggle = 0;

IODIR0 = (7 << 4) | (0x7F << 8);

while (1)
{
lcd_init();
if (toggle)
{
lcd_display_string(M1);
}
else

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
lcd_display_string(M2);
}

delay(1000);
lcd_command(0x01);
delay(1000);

toggle = !toggle;
}
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 4
Experiment Title: PLL Programming
Objective 1:Configure and use PLL to obtain CCLK = 60 MHz from oscillator frequency
= 12 MHz (without using PLL as interrupt).
Proteus Circuit Diagram:

Connections:
P0.4 → Digital Oscilloscope
Program:
#include <LPC213X.H>

void delay(){
unsigned int i,j;
for(i=0;i<100;i++){
for(j=0;j<1275;j++);
}}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

int main() {
PLLCFG = 0x24;
PLLCON = 0x01;
PLLFEED = 0xAA;
PLLFEED = 0x55;
// Wait till PLL gets locked
while(!(PLLSTAT & 0x0400));
PLLCON = 0x03;
PLLFEED = 0xAA;
PLLFEED = 0x55;
IODIR0 = (1<<4);

while(1){
IOSET0 = (1<<4);
delay();
IOCLR0 = (1<<4);
delay();
}

return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 2:Configure and use PLL to obtain CCLK = 48 MHz from oscillator frequency
= 12 MHz (Using PLL as interrupt).

Proteus Circuit Diagram:

Connections:
P0.4 → Digital Oscilloscope

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Program:

#include <LPC213X.H>
void delay(){
unsigned int i,j;
for(i=0;i<100;i++){
for(j=0;j<1275;j++);
}
}

void ISR_PLL() __irq {


PLLCON = 0x03;
PLLFEED = 0xAA;
PLLFEED = 0x55;
IOSET0 = (1<<4);
delay();
IOCLR0 = (1<<4);
delay();
VICVectAddr = 0x00
}

void init_PLL() {
PLLCFG = 0x23;
PLLCON = 0x01;
PLLFEED = 0xAA;
PLLFEED = 0x55;
VICVectAddr0 = (unsigned)ISR_PLL;
VICVectCntl0 = (1<<5) | (12<<0);
VICIntEnable = (1<<12);
while(!(PLLSTAT & (1<<10)));
}

int main() {
IODIR0 = (1<<4);
init_PLL();
while(1);
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 5
Experiment Title: Study Of Interrupts
Objective 1: Write an Embedded C program to generate square wave on port pin
P0.4 using external interrupt 0 as VIRQ .
Proteus Circuit Diagram:

Connections:
P0.4 → OSCILLOSCOPE
Program:
#include <LPC213X.H>

void delay()
{
unsigned int i,j;
for(j=0;j<100;j++)
{
for(i=0;i<1000;i++);
}
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void isr_ext0()__irq
{
IOPIN0=0x00000010;
delay();
IOCLR0=0x00000010;
delay();
EXTINT=0x00000001;
VICVectAddr=0x00;
}

void irq_init()
{
PINSEL0=0x0000000C;
VICIntSelect=0x00000000;
VICVectAddr0=(unsigned)isr_ext0;
VICVectCntl0=0x0000002E;
VICIntEnable=0x00004000;
}

int main()
{
IODIR0=0x00000010;
irq_init();
while(1);
}
Keil Simulation:

Objective 2: Write an Embedded C program to generate square wave on port pin


P0.4 using external interrupt 0 as FIQ .
Proteus Circuit Diagram:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Connections:
P0.4 → OSCILLOSCOPE
Program:
#include <LPC213X.H>
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1275;j++);

}
void FIQ_Handler()__irq
{
IOSET0=(1<<4);
delay(10);
IOCLR0=(1<<4);
delay(10);
EXTINT=(1<<0);
}

void fiq_init()
{
PINSEL0=(3<<2);
VICIntSelect=(1<<14);
VICIntEnable =(1<<14);

}
int main(void)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
IODIR0 |= (1<<4);
fiq_init();
while(1);
return 0;
}

Keil Simulation:

Objective 3: Write an Embedded C program to toggle port pin P0.4 using external
interrupt 3 . Configure interrupt as NVIRQ.

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Circuit Diagram:

Connections:
P0.4 → OSCILLOSCOPE
P0.9 → SWITCH
Program:
#include <LPC213X.H>

void isr_ext3()__irq
{
static int i;
if(i%2 == 0)
{
IOSET0 = (1<<4);
i++;
}
else
{
IOCLR0 = (1<<4);
i--;
}
EXTINT = (1<<3);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

VICVectAddr = 0x00;
}

void irq_init()
{
PINSEL0 = (3<<18);
VICDefVectAddr = (unsigned)isr_ext3;
EXTMODE = (1<<3);
VICIntEnable = (1<<17);
}

int main()
{
IODIR0 |= (1<<4);
irq_init();
while(1);
return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 6
Experiment Title: Study of Interrupt programming for multiple interrupt
1. Objective 1: Write an Embedded C program to generate square wave on port pin P0.4
and rectangular wave on port pin P0.5 using external interrupt 0 and external interrupt
1 respectively. Configure both interrupts as VIRQ. Assign highest priority to external
interrupt 0.

Proteus Circuit Diagram:

Connections:
P0.4 → LED , OSCILLOSCOPE
P0.5 → LED , OSCILLOSCOPE
P0.1 → EXTINT0
P0.1 → EXTINT1
#include <LPC213X.H>

void delay()
{
unsigned int i,j;
for(i=0;i<100;i++)
{

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

for(j=0;j<100;j++);
}
}

void ex0_isr()__irq
{
IOPIN0 |= (1<<4);
delay();
IOPIN0 &= ~(1<<4);
delay();
EXTINT = (1<<0);
VICVectAddr = 0x00;
}

void ex1_isr()__irq
{
IOPIN0 |= (1<<5);
delay();
delay();
IOPIN0 &= ~(1<<5);
delay();
EXTINT = (1<<1);
VICVectAddr = 0x00;
}

void int_init()
{
VICIntSelect = (0<<14)|(0<<15);
VICVectAddr0 = (unsigned)ex0_isr;
VICVectAddr1 = (unsigned)ex1_isr;
VICVectCntl0 = (1<<5)|(14<<0);
VICVectCntl1 = (1<<5)|(15<<0);
VICIntEnable = (3<<14);
}

int main()
{
IODIR0 = (3<<4);
PINSEL0 = (3<<2)|(3<<6);
int_init();
while(1){
}
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

2. Objective 2: Write an Embedded C program to generate square wave on port pin P0.4
and rectangular wave on port pin P0.5 using external interrupt 0 and external interrupt
1 respectively. Configure bothinterrupts as NVIRQ. Assign highest priority to external
interrupt 0.

Proteus Circuit Diagram:

Connections:
P0.4 → LED , OSCILLOSCOPE
P0.5 → LED , OSCILLOSCOPE
P0.1 → EXTINT0
P0.1 → EXTINT1

#include <LPC213X.H>

void delay()
{
unsigned int i,j;
for(i=0;i<100;i++)
{

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

for(j=0;j<100;j++);

}
}

void ext_isr()__irq
{
if(VICIRQStatus &= (1<<14))
{
IOPIN0 |= (1<<4);
delay();
IOPIN0 &= ~(1<<4);
delay();
EXTINT = (1<<0);
}
else if(VICIRQStatus &= (1<<15))
{
IOPIN0 |= (1<<5);
delay();
delay();
IOPIN0 &= ~(1<<5);
delay();
EXTINT = (1<<1);
}

VICVectAddr = 0x00;
}

void int_init()
{
VICIntSelect = (0<<14)|(0<<15);
VICDefVectAddr = (unsigned)ext_isr;
VICIntEnable = (3<<14);
}

int main()
{
IODIR0 = (3<<4);
PINSEL0 = (3<<2)|(3<<6);
int_init();
while(1){
}
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

3. Objective 3: Write an Embedded C program to generate square wave on port pin P0.4
and rectangular wave on port pin P0.5 using external interrupt 0 and external interrupt
1 respectively. Configure both interrupts as FIQ. Assign highest priority to external
interrupt 0.

Proteus Circuit Diagram:

Connections:
P0.4 → LED , OSCILLOSCOPE
P0.5 → LED , OSCILLOSCOPE
P0.1 → EXTINT0
P0.1 → EXTINT1

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

#include <LPC213X.H>

void delay()
{
unsigned int i,j;
for(i=0;i<100;i++)
{
for(j=0;j<100;j++);
}
}

void FIQ_Handler()__irq
{
if(VICFIQStatus &= (1<<14)){
IOPIN0 |= (1<<4);
delay();
IOPIN0 &= ~(1<<4);
delay();
EXTINT = (1<<0);

}
else if(VICFIQStatus &= (1<<15))
{
IOPIN0 |= (1<<5);
delay();
delay();
IOPIN0 &= ~(1<<5);
delay();
EXTINT = (1<<1);
}

VICVectAddr = 0x00;
}

void int_init()
{
VICIntSelect = (1<<14)|(1<<15);
VICIntEnable = (3<<14);
}

int main()
{
IODIR0 = (1<<4)|(1<<5);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

PINSEL0 = (3<<2)|(3<<6);
int_init();
while(1){
}
}

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 7
Experiment Title: Study of Timers and capture facility

Objective 1: Write an Embedded C program to configure Timer 0 as Timer With prescaler

Proteus Circuit Diagram:

Connections:

Program:
#include <LPC213X.H>

int main()
{
T0CTCR = 0X00; //Timer as Timer
T0TCR = (1<<1); //reset the timer
T0PR = 100;
T0TCR = (1<<0); //Start the Timer

return 0;
}
Keil Simulation:

Proteus Simulation:

Objective 2: Write an Embedded C program to configure Timer 0 as Timer Without prescaler


Connections:
P0.4 → LED
Program:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

#include <LPC213X.H>

int main()
{
T0CTCR = 0X00; //Timer as Timer
T0TCR = (1<<1); //reset the timer
T0TCR = (1<<0); //Start the Timer

return 0;
}
Keil Simulation:

Objective 3: Write an Embedded C program to configure Timer 0 as Counter With prescaler


Connections:
P0.4 → LED
Program:
#include <LPC213X.H>

int main()
{
PINSEL0 = (2<<4); //P0.2 Third Function
T0CTCR = (1<<0); //Timer as counter rising edge
T0PR = 10;
T0TCR = (1<<1); //reset the timer
T0TCR = (1<<0); //Start the Timer

return 0;
}

Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 4: Write an Embedded C program to configure Timer 0 as Counter Without


prescaler
Connections:
P0.4 → LED
Program:
#include <LPC213X.H>

int main()
{
PINSEL0 = (2<<4); //P0.2 Third Function
T0CTCR = (1<<0); //Timer as counter rising edge
T0TCR = (1<<1); //reset the timer
T0TCR = (1<<0); //Start the Timer

return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 5: Write an Embedded C program to use Capture facility of Timer 0 (Without


Interrupt)
Connections:
P0.4 → LED
Program:
#include <LPC213X.H>

int main()
{
PINSEL0 = (2<<4); //P0.2 Third Function
T0TCR = (1<<1); //reset the timer
T0CCR = (1<<0); //Capture as rising edge
T0TCR = (1<<0); //Start the Timer

return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 6: Write an Embedded C program to use Capture facility of Timer 0 (With Interrupt)
Connections:
P0.4 → LED
Program:
#include <LPC213X.H>

void cap_isr()__irq
{
unsigned int value;
value = T0CR0;

EXTINT = (1<<0);
VICVectAddr = 0x00;
}

void init_int()
{
VICVectAddr0 = (unsigned)cap_isr;
VICVectCntl0 = (1<<5)|(4<<0);
VICIntEnable = (1<<4);
}

int main()
{
PINSEL0 = (2<<4); //P0.2 Third Function
init_int();
T0TCR = (1<<1); //reset the timer
T0CCR = (1<<0)|(1<<2); //Capture as rising edge with Interrupt
T0TCR = (1<<0); //Start the Timer

while(1);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 9
Experiment Title: Study of PWM
Objective 1: Write an Embedded C program to generate single edge controlled PWM.
Generate it on PWM1 with frequency = 100 Hz and duty cycle = 40 %
Proteus Circuit Diagram:

Connections: P0.0

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Program:
#include <LPC213X.H>

int main ()
{
PINSEL0= 0X02;
PWMTCR= (1<<1);
PWMPR=999;
PWMMR0=30;
PWMMR1=12;
PWMLER= (3>>0);
PWMMCR= (1<<1);
PWMPCR= (1<<9);
PWMTCR= (1<<0) | (1<<3);
}

Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation: d

Experiment Number: 9
Experiment Title: Study of PWM
Objective 2: Write an Embedded C program to generate dual edge controlled PWM.
Generate it on PWM2 with frequency = 200 Hz and duty cycle = 76%

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Circuit Diagram:

Connections: P0.7
Program:
#include <LPC213X.H> int
main ()
{
PINSEL0= (2<<14);
PWMTCR= (1<<1);
PWMPR=0;
PWMMR0=15000;
PWMMR1=30;
PWMMR2=11430;
PWMLER= (7<<0);
PWMMCR= (1<<1);
PWMPCR= (1<<10) | (1<<2);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

PWMTCR= (1<<0) | (1<<3);


return 0;
}

Keil Simulation:

Proteus Simulation:

76% DUTY CYCLE (DUAL EDGE PWM)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 10
Experiment Title: To Study Serial Communication of LPC2138
Objective 1: Write an Embedded C program to transfer character serially.
Proteus Circuit Diagram:

Connections:
P0.0 → RX of virtual Terminal
P0.1 → TX of virtual Terminal

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Program:
#include <LPC213X.H>
#define TEMT (1<<6)
#define LINE_FEED 0x0A #define
CARRIAGE_RET 0xD
void delay()
{
int i;
for(i=0;i<5000;i++)
{;}
}
void init_UR0()
{
PINSEL0=0x05;
U0FCR=0x07;
U0LCR=0X83;
U0DLL=20;
U0DLM=0x00;
U0LCR=0X03;
}
int main()
{
while(1)
{

int j;
char c[]="ESD EXP 11";
init_UR0();
j=0;
while(c[j])
{
U0THR=c[j];
while(!(U0LSR & TEMT)){;}
j++;
}
U0THR=LINE_FEED;
while(!U0LSR & TEMT){;}
U0THR=CARRIAGE_RET;
delay();
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 2: Write an Embedded C program to receive character serially.


Proteus Circuit Diagram:

Connections:
P0.0 → RX of virtual Terminal
P0.1 → TX of virtual Terminal
Program:
#include <LPC213X.H>
#define TEMT (1<<6)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void delay()
{ unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}
void lcd_cmd(unsigned char data)
{
IOPIN0=data<<16;
IOCLR1|=0x00100000;
IOCLR1|=0x00200000;
IOSET1|=0x00400000;
IOCLR1|=0x00400000;
}
void lcd_data(unsigned char data)
{
IOPIN0=data<<16;
IOSET1|=0x00100000;
IOCLR1|=0x00200000;
IOSET1|=0x00400000;
IOCLR1|=0x00400000;
}
void lcd_init()
{
lcd_cmd(0x38);
delay();
lcd_cmd(0x01);
delay();
lcd_cmd(0x06);
delay();
lcd_cmd(0x0C);
delay();
}
void uart_init()
{
PINSEL0=0x05;
U0FCR=0x07;
U0LCR=0X83;
U0DLL=19;
U0DLM=0x00;
U0LCR=0X03;
}
int main(void)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
PINSEL0=0;
PINSEL1=0;
IODIR0=0xFFFF0000;
IODIR1=0x00F00000;
uart_init();
lcd_init();
lcd_cmd(0x85);
delay();
while(1)
{ unsigned char a;
while(!(U0LSR & 0x01));
a=U0RBR;
lcd_data(a);
delay();
delay();
}
}

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 3: Write an Embedded C program for ECHO


Proteus Circuit Diagram:

Connections:
P0.0 → RX of virtual Terminal
P0.1 → TX of virtual Terminal

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Program:
#include <LPC213X.H>
#define TEMT (1<<6) //transmitter empty bit
#define LINE_FEED 0xA #define
CARRIAGE_RET 0xD
void delay()

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{ int i;
for(i=0;i<5000;i++){;}
}
void init_UART0()
{
PINSEL0=0x05; //P0.0 tx output p0.1 rx output

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

U0FCR=0x07; //enable and reset FIFOs


U0LCR=0x83; //set DLAB bit and length to 8 bit
U0DLL=19; //baud rate
U0DLM=0x00;
U0LCR=0x03; //clear DLAB
} int
main()
{ while(1)
{ unsigned char byte;
init_UART0();

while(!(U0LSR & 0x01));


byte=U0RBR;
U0THR=byte;

while(!(U0LSR & TEMT))


{;}
delay();
}
}

Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 11
Experiment Title: Study of ADC of LPC2138
Objective 1: Write an Embedded C program to control LED as per applied voltage at
ADC input pin. (Apply variable voltage as input to ADC in the range 0 to 3 V. For
voltage greater than 1.5 V LED should be ON and below 1.5 V LED should be OFF)
Proteus Circuit Diagram:

Connections:
P0.28 🡪 Potentiometer
P1.16 🡪 LED
Program:
#include <LPC213X.H>

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void delay(unsigned int t)


{
unsigned int i, j;
for (j = 0; j < t; j++)
{
for (i = 0; i < 1275; i++);
}
}

void ADC_Init()
{
PINSEL1 = (1 << 24);
}

unsigned int ADC_Read()


{
unsigned int data;

AD0CR = (1 << 21) | (3 << 9) | (1 << 1);


AD0CR |= (1 << 24);

while ((AD0GDR & (1 << 31)) == 0);


data = AD0GDR;
data = (data >> 6) & 0x03FF;

return data;
}

int main()
{

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

unsigned int val;


IODIR1 |= (1<<16);

ADC_Init();

while (1)
{
val = ADC_Read();

if(val < 512)


IOSET1 = (1<<16);
else
IOCLR1 = (1<<16);

delay(1000);
}

Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 2: Modify above program to display voltage on LCD.


Proteus Circuit Diagram:

Connections:
P0.4 to P0.15 🡪 LCD
P0.28 🡪 Potentiometer
P1.16 🡪 LED
Program:

#include <LPC213X.H>

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void delay(unsigned int t)


{
unsigned int i, j;
for (j = 0; j < t; j++)
{
for (i = 0; i < 1275; i++);
}

void lcd_cmd(unsigned char cmd)


{
IOCLR0 = (0xFF << 8);
IOPIN0 = (cmd << 8);
IOCLR0 |= (1 << 4);
IOCLR0 |= (1 << 5);
IOSET0 |= (1 << 6);
delay(1);
IOCLR0 |= (1 << 6);
}

void lcd_data(unsigned char data)


{
IOCLR0 = (0xFF << 8);
IOPIN0 = (data << 8);
IOSET0 |= (1 << 4);
IOCLR0 |= (1 << 5);
IOSET0 |= (1 << 6);
delay(1);
IOCLR0 |= (1 << 6);
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void lcd_init()
{
lcd_cmd(0x01);
delay(10);
lcd_cmd(0x38);
delay(10);
lcd_cmd(0x80);
delay(10);
lcd_cmd(0x0C);
delay(10);
}

void lcd_string(char *str)


{
int i = 0;
while (str[i] != '\0') {
lcd_data(str[i]);
i++;
delay(10);
}
}

void itstr(int num, char *str)


{

int i = 0, sign = 0, j;

if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return;

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

if (num < 0)
{
sign = 1;
num = -num;
}

void dvolt(int adc_value, char *voltage_str)


{
float voltage = (adc_value * 3.3) / 1023.0;
int integer_part = (int)voltage;
int decimal_part = (voltage - integer_part) * 100;

voltage_str[0] = integer_part + '0';


voltage_str[1] = '.';
voltage_str[2] = (decimal_part / 10) + '0';
voltage_str[3] = (decimal_part % 10) + '0';
voltage_str[4] = 'V';
voltage_str[5] = '\0';
}

void ADC_Init()
{
PINSEL1 = (1 << 24);
}

unsigned int ADC_Read()


{
unsigned int data;
AD0CR = (1 << 21) | (3 << 9) | (1 << 1);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

AD0CR |= (1 << 24);


while ((AD0GDR & (1 << 31)) == 0);
data = AD0GDR;
data = (data >> 6) & 0x03FF;
return data;
}

int main()

float voltage ;
char voltage_str[6];
unsigned int val;

IODIR0 = (0xFF << 8) | (0x7 << 4);


IODIR1 |= (1<<16);
lcd_init();

ADC_Init();

while (1)
{
val = ADC_Read();
voltage = (val * 3.3) / 1023.0;

if(val < 512)


IOSET1 = (1<<16);
else
IOCLR1 = (1<<16);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

lcd_cmd(0x80);
lcd_string("Voltage: ");
dvolt(val, voltage_str);
lcd_string(voltage_str);

delay(1000);
}

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 12
Experiment Title: Study of DAC of LPC2138
Objective 1: Write an Embedded C program to generate sine wave using DAC
.Frequency of waveform should be 50Hz
Proteus Circuit Diagram:

Connections:
P0.25 → Oscilloscope
Program:
#include <LPC213X.H>
void delay()
{
T0TCR=(1<<1);
T0MCR=(3<<0);
T0MR0=1500;
T0TCR=(1<<0);
while(!(T0IR&(1<<0)));
T0IR=(1<<0);
T0TCR=(0<<0);
}
int main()
{
unsigned int i;

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

int data[]={516, 605, 692, 773, 847, 910, 962, 1000, 1023,
1023, 1023, 1000, 962, 910, 847, 773, 692, 605, 516, 426,
339, 258, 185, 121, 70 ,32, 8 ,0, 8, 32, 70,
121, 185, 258, 339, 426, 516};

PINSEL1=(2<<18);
while(1)
{
for(i=0;i<36;i++)
{
DACR=(data[i]<<6);
delay();
}
}
}

Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 2: Write an Embedded C program to generate triangular wave using DAC.


Frequency of waveform should be 25Hz
Proteus Circuit Diagram:

Connections:
P0.25 → Oscilloscope
Program:
#include <LPC213X.H>
void delay()
{
T0TCR=(1<<1);
T0MCR=(3<<0);//interrupt and reset
T0MR0=53;
T0TCR=(1<<0);
while(!(T0IR&(1<<0)));
T0IR=(1<<0);
T0TCR=(0<<0);
}

int main()
{
unsigned int i=0;
PINSEL1=(2<<18);//P0.25=AOUT
while(1)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
for(i=0;i<1024;i++)
{
DACR=(i<<6);
delay();//19.6us
}
for(i=1022;i>0;i--)
{
DACR=(i<<6);
delay();
}
}
}
Keil Simulation:

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 13
Experiment Title: Extra Question 1
Objective 1: Generate dual edge control PWM using PWM2 Having frequency 100Hz.
Toggle duty cycle in between 30% to 60% using switch connected at P1.16
Proteus Circuit Diagram:

U1

Connections:
P1.16 → SWITCH

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Program:
#include <LPC213X.H>

void init_pwm()
{
PINSEL0=(2<<14);
PWMTCR=(1<<2);
PWMPR=999;
PWMMR0=30;
PWMMR1=10;
PWMMR2=12;
PWMLER=(1<<2)|(3<<0);
PWMMCR=(1<<1);
PWMPCR=(1<<2)|(1<<10);
PWMTCR=(1<<0)|(1<<3);
}

int main()
{
init_pwm();
while(1)
{
if(IOPIN1 & (1<<16))
{
PWMMR2=19;
PWMLER=(1<<2);
}
else
{
PWMMR2=12;
PWMLER=(1<<2);
}
}
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Keil Simulation:

Experiment Number:
Experiment Title: Extra Question 2
Objective 2: Generate Single edge control PWM using PWM1 having frequency 200Hz.
Vary duty cycle from 5% to 95% using 2 switches connected at P1.16 and P1.17. Switch
1- Increase duty cycle by 5%, Switch 2- decrease duty cycle by 5%

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Circuit Diagram:


U1

Connections:
P1.16 → SWITCH 1
P1.17 → SWITCH 2
Program:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

#include <LPC213X.H>

void init_PWM() {
VPBDIV = 0X01; // Set PCLK to CCLK
PINSEL0 = 0x00008002;
PWMTCR = (1<<1); // Reset PWM Timer Counter and PWM Prescale Counter
PWMMCR = (1<<1); // Reset PWM Timer Counter on match with PWMMR0
PWMMR0 = 60000; // Set PWM period for 200Hz (PCLK = 12MHz, 12MHz/200Hz =
60000)
PWMPCR = (1<<9); // Enable PWM2 output
PWMTCR = (1<<0) | (1<<3); // Enable PWM Timer Counter and PWM mode
}

void set_PWM_duty_cycle(unsigned int duty_cycle) {


PWMMR1 = (PWMMR0 * duty_cycle) / 100; // Set duty cycle for
PWM1 PWMLER = (1<<1); // Latch enable for PWM1 }

int main() {
unsigned int duty_cycle = 50; // Initial duty cycle set to 50%
init_PWM();
set_PWM_duty_cycle(duty_cycle);

while(1) {
if(IOPIN1 & (1<<16)) { // Check if switch 1 is pressed
if(duty_cycle < 95) {
duty_cycle += 5; // Increase duty cycle by 5%
set_PWM_duty_cycle(duty_cycle);
}
} else if(IOPIN1 & (1<<17)) { // Check if switch 2 is pressed
if(duty_cycle > 5) {
duty_cycle -= 5; // Decrease duty cycle by 5%
set_PWM_duty_cycle(duty_cycle);
}
}
}
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 14
Experiment Title: Extra Question 2
Objective 1: Generate and apply random pulses using push button connected at port
pin P1.16 and display the duration between two successive pulses on 16*2 LCD. Use
capture channel cap1.0.
Proteus Circuit Diagram:

Connections:
CAP1.0 (P0.10) → Switch PDN (To apply Falling Edge)
P1.16 to P1.25 → 16x2 LCD (RS RW E D0-D7)
Program:
#include <LPC213X.H>

void delay(unsigned int t)


{
int a, b;
for (a = 0; a < t; a++)
for (b = 0; b < 1275; b++);
}
Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025
Embedded System Design Lab, 6EN352

void lcd_cmd(unsigned char cmd)


{
IOPIN1 &= 0;
IOPIN1 = (cmd << 19);
IOCLR1 = (1 << 16);
IOCLR1 = (1 << 17);
IOSET1 = (1 << 18);
delay(10);
IOCLR1 = (1 << 18);
}

void lcd_init()
{
delay(15);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
delay(10);
lcd_cmd(0x06);
lcd_cmd(0x80);
}

void lcd_data1(unsigned char data)


{
IOPIN1 &= 0;
IOPIN1 = (data << 19);
IOSET1 = (1 << 16);
IOCLR1 = (1 << 17);
IOSET1 = (1 << 18);
delay(10);
IOCLR1 = (1 << 18);
}

void lcd_data(float x)
{
int ip;
int fp;
char istr[10];
char frac_str[10];
int idx, i;
const char *lbl = "Time: ";
const char *unit = " ms";

ip = (int)x;
fp = (int)((x - ip) * 10);

for (i = 0; lbl[i] != '\0'; i++)


lcd_data1(lbl[i]);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

if (ip == 0)
lcd_data1('0');
else {
idx = 0;
while (ip > 0) {
istr[idx++] = (ip % 10) + '0';
ip /= 10;
}
for (i = idx - 1; i >= 0; i--)
lcd_data1(istr[i]);
}

lcd_data1('.');
if (fp < 10)
lcd_data1('0');

if (fp == 0)
lcd_data1('0');
else {
idx = 0;
while (fp > 0) {
frac_str[idx++] = (fp % 10) + '0';
fp /= 10;
}
for (i = idx - 1; i >= 0; i--)
lcd_data1(frac_str[i]);
}

for (i = 0; unit[i] != '\0'; i++)


lcd_data1(unit[i]);
}

void lcd_data2(float x)
{
int ip;
int fp;
char istr[10];
char frac_str[10];
int idx, i;
const char *lbl = "Time: ";
const char *unit = " s";

ip = (int)x;
fp = (int)((x - ip) * 100);

for (i = 0; lbl[i] != '\0'; i++)


lcd_data1(lbl[i]);

if (ip == 0)
lcd_data1('0');

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

else {
idx = 0;
while (ip > 0) {
istr[idx++] = (ip % 10) + '0';
ip /= 10;
}
for (i = idx - 1; i >= 0; i--)
lcd_data1(istr[i]);
}

lcd_data1('.');
if (fp < 10)
lcd_data1('0');

if (fp == 0)
lcd_data1('0');
else {
idx = 0;
while (fp > 0) {
frac_str[idx++] = (fp % 10) + '0';
fp /= 10;
}
for (i = idx - 1; i >= 0; i--)
lcd_data1(frac_str[i]);
}

for (i = 0; unit[i] != '\0'; i++)


lcd_data1(unit[i]);
}

void cap_isr()__irq
{
static int i = 0, x = 0, y = 0;
static int cnt = 0;
int val;
int t;
float time_ms, time_s;

val = T1CR0;
if (i == 0)
{
x = val;
i++;
}
else
{
y = val;
cnt++;
i = 0;
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

if (cnt == 1)
{
t = y - x;
time_ms = (float)t / 15000.0;
time_s = (float)t / 15000000.0;
lcd_cmd(0x80);
lcd_data(time_ms);
lcd_cmd(0xC0);
lcd_data2(time_s);
x = y = cnt = 0;
}

T1IR = (1 << 4);


VICVectAddr = 0;
}

void cap_init()
{
PINSEL0 = 0x00200000;
T1TCR = (1 << 1);
T1CCR = (1 << 0) | (1 << 2);
T1TCR = (1 << 0);
}

void init_int()
{
VICVectAddr0 = (unsigned)cap_isr;
VICVectCntl0 = (1 << 5) | (5 << 0);
VICIntEnable = (1 << 5);
}

int main()
{
IODIR1 = (0xFFF << 16);
cap_init();
init_int();
lcd_init();
while (1);
return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Objective 2: Apply square wave with random frequency at cap0.0 and display
frequency of waveform on 16*2 LCD. Use capture facility interrupt.
Proteus Circuit Diagram:

Connections:
CAP0.0 (P0.2) → Digital Clock 1.2Hz
P1.16 to P1.25 → 16x2 LCD (RS RW E D0-D7)
Program:
#include <LPC213X.H>

void delay(unsigned int t)


{
int a, b;
for (a = 0; a < t; a++)
for (b = 0; b < 1275; b++);

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void lcd_cmd(unsigned char cmd)


{
IOPIN1 &= 0;
IOPIN1 = (cmd << 19);
IOCLR1 = (1 << 16);
IOCLR1 = (1 << 17);
IOSET1 = (1 << 18);
delay(10);
IOCLR1 = (1 << 18);
}

void lcd_init()
{
delay(15);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
delay(10);
lcd_cmd(0x06);
lcd_cmd(0x80);
}

void lcd_data1(unsigned char data)


{
IOPIN1 &= 0;
IOPIN1 = (data << 19);
IOSET1 = (1 << 16);
IOCLR1 = (1 << 17);
IOSET1 = (1 << 18);
delay(10);
IOCLR1 = (1 << 18);
}

void lcd_display_frequency(float frequency)


{
int ip;
int fp;
char istr[10];
char frac_str[10];
int idx, i;
const char *lbl = "Freq: ";
const char *unit = " Hz";
frequency -= 0.24;
ip = (int)frequency;
fp = (int)((frequency - ip) * 100);

lcd_cmd(0x80);
for (i = 0; lbl[i] != '\0'; i++)

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

lcd_data1(lbl[i]);

if (ip == 0)
lcd_data1('0');
else {
idx = 0;
while (ip > 0) {
istr[idx++] = (ip % 10) + '0';
ip /= 10;
}
for (i = idx - 1; i >= 0; i--)
lcd_data1(istr[i]);
}

lcd_data1('.');

if (fp < 10)


lcd_data1('0');

if (fp == 0)
lcd_data1('0');
else {
idx = 0;

while (fp > 0) {


frac_str[idx++] = (fp % 10) + '0';
fp /= 10;
}
for (i = idx - 1; i >= 0; i--)
lcd_data1(frac_str[i]);
}

for (i = 0; unit[i] != '\0'; i++)


lcd_data1(unit[i]);
}

void cap_isr() __irq


{
static int i = 0, x = 0, y = 0;
int val, t;
float frequency;

val = T0CR0;

if (i == 0)
{
x = val;
i++;
}
else

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

{
y = val;
t = y - x;

if (t > 0) {
frequency = 15000000.0 / t;
lcd_display_frequency(frequency);
}

i = 0;
}

T0IR = (1 << 4);


VICVectAddr = 0;
}

void cap_init()
{
PINSEL0 |= 0x00000020;
T0TCR = (1 << 1);
T0CCR = (1 << 0) | (1 << 2);
T0TCR = (1 << 0);
}

void init_int()
{
VICVectAddr0 = (unsigned)cap_isr;
VICVectCntl0 = (1 << 5) | (4<<0);
VICIntEnable = (1 << 4);
}

int main()
{
IODIR1 = (0xFFF << 16);
lcd_init();
cap_init();
init_int();
while (1);
return 0;
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Proteus Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

Experiment Number: 15
Experiment Title: LM35 interfacing with LPC2138 and 16*2 LCD
Objective 1: Display temperature sensed by LM35 on 16*2 LCD. Use step size = 10
mv and 10-bit resolution.
Proteus Circuit Diagram:

Connections:
P0.4 → RS
P0.5 → RW
P0.6 → E
P0.8-P0.15 → D0-D7
P0.28 → LM35
Program:
#include <lpc213x.h>

void delay(unsigned int t) {


unsigned int i, j;
for (j = 0; j < t; j++) {
for (i = 0; i < 1275; i++);
}
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

void lcd_cmd(unsigned char cmd) {


IOCLR0 = (0xFF<<8); // Clear data lines (D0-D7)
IOPIN0 = (cmd << 8); // Place command on data lines
IOCLR0 |= (1 << 4); // RS = 0 for command
IOCLR0 |= (1 << 5); // RW = 0 for write
IOSET0 |= (1 << 6); // E = 1 to latch command
delay(1);
IOCLR0 |= (1 << 6); // E = 0
}

void lcd_data(unsigned char data) {


IOCLR0 = (0xFF<<8); // Clear data lines (D0-D7)
IOPIN0 = (data << 8); // Place data on data lines
IOSET0 |= (1 << 4); // RS = 1 for data
IOCLR0 |= (1 << 5); // RW = 0 for write
IOSET0 |= (1 << 6); // E = 1 to latch data
delay(1);
IOCLR0 |= (1 << 6); // E = 0
}

void lcd_init() {
lcd_cmd(0x01); // Clear display
delay(10);
lcd_cmd(0x38); // 8-bit, 2-line, 5x7 dots
delay(10);
lcd_cmd(0x80); // Force cursor to the beginning of the 1st line
delay(10);
lcd_cmd(0x0C); // Display on, cursor off
delay(10);
}

// Function to display a string on the LCD


void lcd_display_string(char *str) {
int i = 0;
while (str[i] != '\0') {
lcd_data(str[i]);
i++;
delay(10);
}
}

void int_to_str(int num, char *str) {


int i = 0, temp, sign = 0, j;

if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return;
}

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

if (num < 0) {
sign = 1;
num = -num;
}

// Extract digits
while (num > 0) {
temp = num % 10;
str[i++] = temp + '0';
num /= 10;
}

// Add '-' if negative


if (sign) {
str[i++] = '-';
}

str[i] = '\0'; // Null-terminate string

// Reverse the string


for (j = 0; j < i / 2; j++) {
char temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
}

void ADC_Init(void) {
PINSEL1 = (1<<24); // P0.28, AD0.1

unsigned int ADC_GetAdcReading() {


unsigned int adcdata;

AD0CR=(1<<21)|(3<<9)|(1<<1); //select channel


AD0CR|=(1<<24); //start conversion
while((AD0GDR& (1<<31))==0);
adcdata=AD0GDR;
adcdata=(adcdata >> 6) & 0x03FF; // bit 6:15 is 10 bit AD value
return adcdata;
}

int main() {
unsigned char DATA[] = "ESD";
unsigned char ADC[] = "Temperature: ";
int temp;
char str[5];
int adc;

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025


Embedded System Design Lab, 6EN352

// Set P0.8 to P0.15 as output for data lines and P0.4 to P0.6 for control lines
IODIR0 = (0xFF << 8) | (0x7 << 4);

lcd_init(); // Initialize the LCD

// Display the initial string on the LCD


lcd_display_string(DATA);
delay(10);
lcd_cmd(0x01);

lcd_display_string(ADC);

ADC_Init(); // Initialize the ADC

while (1) {
adc = ADC_GetAdcReading(); // Get ADC reading
temp = (adc * 3.3 / 1023)*100;

lcd_cmd(0xC0); // Move to the next line of the LCD


int_to_str(temp, str); // Convert the temperature to a string
lcd_display_string(str); // Display the temp
lcd_data(0xDF);
lcd_data('C');
delay(10);
}
}
Keil Simulation:

Third Year B. Tech. Electronics, Sem. V, A.Y. – 2024-2025

You might also like