GPIO and LCD Programming with LPC2138
GPIO and LCD Programming with LPC2138
1 GPIO Programming 2
4 PLL Programming 29
5 Study of Interrupts 35
9 Study of PWM 69
13 Extra Question 1 99
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()
{
IODIR0|=1<<4;
while(1)
{
IOSET0|=1<<4;
delay(10);
IOCLR0|=1<<4;
delay(10);
}
}
Keil Simulation:
Proteus Simulation:
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
{
IOCLR0|=1<<4;
delay(10);
}
}
}
Keil Simulation:
Proteus Simulation:
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)
{
if(IOPIN1&(1<<16))
{
IOSET0|=1<<4;
delay(10);
}
else
{
IOCLR0|=1<<4;
delay(10);
}
}
}
Keil Simulation:
Proteus Simulation:
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()
{
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:
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++);
}
}
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();
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;
}
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();
}
}
Keil Simulation:
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++);
}
}
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:
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
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:
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:
Connections:
P0.4 → RS
P0.5 → R/W
P0.6 → E
P0.8 to P0.15 → D0 to D7.
Program:
#include <LPC213X.H>
unsigned int i, j;
for (j = 0; j < t; j++)
{
for (i = 0; i < 1275; i++);
}
}
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 M1[] = "EMBEDDED SYSTEMS";
unsigned char M2[] = "ELECTRONICS DEPARTMENT";
unsigned int toggle = 0;
while (1)
{
lcd_init();
if (toggle)
{
lcd_display_string(M1);
}
else
{
lcd_display_string(M2);
}
delay(1000);
lcd_command(0x01);
delay(1000);
toggle = !toggle;
}
}
Keil Simulation:
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++);
}}
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:
Proteus Simulation:
Objective 2:Configure and use PLL to obtain CCLK = 48 MHz from oscillator frequency
= 12 MHz (Using PLL as interrupt).
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++);
}
}
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);
}
Keil Simulation:
Proteus Simulation:
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++);
}
}
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:
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)
{
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.
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);
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:
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.
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++)
{
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){
}
}
Keil Simulation:
Proteus Simulation:
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.
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++)
{
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){
}
}
Keil Simulation:
Proteus Simulation:
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.
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++)
{
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);
PINSEL0 = (3<<2)|(3<<6);
int_init();
while(1){
}
}
Keil Simulation:
Proteus Simulation:
Experiment Number: 7
Experiment Title: Study of Timers and capture facility
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:
#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:
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:
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:
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:
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);
return 0;
}
Keil Simulation:
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
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:
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%
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);
Keil Simulation:
Proteus Simulation:
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
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();
}
Keil Simulation:
Proteus Simulation:
Connections:
P0.0 → RX of virtual Terminal
P0.1 → TX of virtual Terminal
Program:
#include <LPC213X.H>
#define TEMT (1<<6)
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)
{
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:
Connections:
P0.0 → RX of virtual Terminal
P0.1 → TX of virtual Terminal
Program:
#include <LPC213X.H>
#define TEMT (1<<6) //transmitter empty bit
#define LINE_FEED 0xA #define
CARRIAGE_RET 0xD
void delay()
{ int i;
for(i=0;i<5000;i++){;}
}
void init_UART0()
{
PINSEL0=0x05; //P0.0 tx output p0.1 rx output
Keil Simulation:
Proteus Simulation:
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>
void ADC_Init()
{
PINSEL1 = (1 << 24);
}
return data;
}
int main()
{
ADC_Init();
while (1)
{
val = ADC_Read();
delay(1000);
}
Keil Simulation:
Proteus Simulation:
Connections:
P0.4 to P0.15 🡪 LCD
P0.28 🡪 Potentiometer
P1.16 🡪 LED
Program:
#include <LPC213X.H>
void lcd_init()
{
lcd_cmd(0x01);
delay(10);
lcd_cmd(0x38);
delay(10);
lcd_cmd(0x80);
delay(10);
lcd_cmd(0x0C);
delay(10);
}
int i = 0, sign = 0, j;
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return;
if (num < 0)
{
sign = 1;
num = -num;
}
void ADC_Init()
{
PINSEL1 = (1 << 24);
}
int main()
float voltage ;
char voltage_str[6];
unsigned int val;
ADC_Init();
while (1)
{
val = ADC_Read();
voltage = (val * 3.3) / 1023.0;
lcd_cmd(0x80);
lcd_string("Voltage: ");
dvolt(val, voltage_str);
lcd_string(voltage_str);
delay(1000);
}
Keil Simulation:
Proteus Simulation:
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;
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:
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)
{
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:
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
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);
}
}
}
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%
Connections:
P1.16 → SWITCH 1
P1.17 → SWITCH 2
Program:
#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
}
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);
}
}
}
}
Keil Simulation:
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 lcd_init()
{
delay(15);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
delay(10);
lcd_cmd(0x06);
lcd_cmd(0x80);
}
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);
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]);
}
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);
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]);
}
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;
}
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;
}
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:
Proteus Simulation:
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 lcd_init()
{
delay(15);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
delay(10);
lcd_cmd(0x06);
lcd_cmd(0x80);
}
lcd_cmd(0x80);
for (i = 0; lbl[i] != '\0'; i++)
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 == 0)
lcd_data1('0');
else {
idx = 0;
val = T0CR0;
if (i == 0)
{
x = val;
i++;
}
else
{
y = val;
t = y - x;
if (t > 0) {
frequency = 15000000.0 / t;
lcd_display_frequency(frequency);
}
i = 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:
Proteus Simulation:
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 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);
}
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return;
}
if (num < 0) {
sign = 1;
num = -num;
}
// Extract digits
while (num > 0) {
temp = num % 10;
str[i++] = temp + '0';
num /= 10;
}
void ADC_Init(void) {
PINSEL1 = (1<<24); // P0.28, AD0.1
int main() {
unsigned char DATA[] = "ESD";
unsigned char ADC[] = "Temperature: ";
int temp;
char str[5];
int adc;
// 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_display_string(ADC);
while (1) {
adc = ADC_GetAdcReading(); // Get ADC reading
temp = (adc * 3.3 / 1023)*100;