Documente Academic
Documente Profesional
Documente Cultură
4. Write a c program for counting the no of times that a switch is pressed & released.
5. Illustrate the use of port header file (port M) using an interface consisting of a keypad and liquid
crystal display.
#include <lpc214x.h>
#define rs 0x00010000
#define en 0x00020000
IOCLR1|= rs;
IOSET1|= en;
delay_ms(10);
IOCLR1|= en;
delay_ms(10);
}
IOSET1|= rs;
IOSET1|= en;
delay_ms(10);
IOCLR1|=en;
delay_ms(10);
}
void disp_str(unsigned char *x)
{
while(*x)
lcd_disp(*x++);
}
void lcdinit()
{
IODIR1=0x03FF0000;
cmd(0x38);
cmd(0x01);
cmd(0x06);
cmd(0x0c);
cmd(0x80);
}
int main()
{
IODIR0=0xffffffff;
lcdinit();
while(1)
{
cmd(0x80);
disp_str("hello world");
}
}
2. Write a simple program to show a delay.
#include <lpc214x.h>
#define led1 0x00000001 // declare led1 is p0.0
#define led2 0x00000002 // declare led2 is p0.1
unsigned char x;
void delay_ms(unsigned int t) // delay functiobn
{
unsigned int i;
for(;t>0;t--)
for(i=0;i<5000;i++); //
}
int main()
{
IODIR0=0x00000003; // declare p0.1 to p0.2 as output pis for leds
}
}
3. Write a loop application to copy values from P1 to P2
#include <lpc214x.h>
unsigned char x;
unsigned int y;
int main()
IOPIN0 = IOPIN0 & 0x00000000; // first clear the all pins that's why we are using
IOPIN1 = IOPIN1 & 0x00000000; // first clear the all pins that's why we are using and(&)
}
4. Write a c program for counting the no of times that a switch is pressed & released.
#include <lpc214x.h>
#define sw 1<<0 // p0.0 connected to switch
unsigned int pc,rc; //pc= pressed count , rc= released count variables
unsigned char x;
int main()
{
IODIR0=0x00000000; // declare p0 as input
#include <LPC214X.H>
#include "lcd.h"
#include "keypad.h"
int main()
{
IODIR0=0xfff000ff; // declare col as inputs and row as outputs
lcdinit();
while(1)
{
cmd(0x80); // lcd 1st row display address
disp_str("ENTER VALUES ");
cmd(0xc0); // lcd 2 nd row display address
for(i=0;i<4;i++) // ENTER 4 VALUES
{
arr[i]=get_key(); // read value from keypad and assign
to array
lcd_disp(arr[i]); // display value in array
}
}
}
IO0CLR|=RowA;IO0SET|=RowB;IO0SET|=RowC;IO0SET|=RowD;
if (!(IOPIN0&=C3)) { dela(100); while (!(IOPIN0&=C3)); return '3'; }
IO0CLR|=RowB;IO0SET|=RowA;IO0SET|=RowC;IO0SET|=RowD;
if (!(IOPIN0&=C2)) { dela(100); while (!(IOPIN0&=C2)); return '5'; }
IO0CLR|=RowB;IO0SET|=RowA;IO0SET|=RowC;IO0SET|=RowD;
if (!(IOPIN0&=C3)) { dela(100); while (!(IOPIN0&=C3)); return '6'; }
IO0CLR|=RowD;IO0SET|=RowA;IO0SET|=RowB;IO0SET|=RowC;//Test Row D
void dela(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<200;j++); //null statement
}
#define rs 0x00010000
#define en 0x00020000
IOCLR1|= rs;
IOSET1|= en;
delay_ms(10);
IOCLR1|= en;
delay_ms(10);
}
IOSET1|= rs;
IOSET1|= en;
delay_ms(10);
IOCLR1|=en;
delay_ms(10);
}
void lcdinit()
{
IODIR1=0x03FF0000;
cmd(0x38);
cmd(0x01);
cmd(0x06);
cmd(0x0c);
cmd(0x80);
}
6. Write a program to create a portable hardward delay.
#include <lpc214x.h>
int main(void)
{
initClocks();
initTimer0();
IO0DIR = 0xFFFFFFFF;
while(1)
{
IO0SET = 0xFFFFFFFF;
delayMS(500); //0.5 Second(s) Delay
IO0CLR = 0xFFFFFFFF;
delayMS(500);
}
void initTimer0(void)
{
T0CTCR = 0x0;
T0PR = PRESCALE-1;
T0TCR = 0x02;
}
T0TCR = 0x01;
while(T0TC < milliseconds);
T0TCR = 0x00;
}
void initClocks(void)
{
setupPLL0();
feedSeq();
connectPLL0();
feedSeq();
VPBDIV = 0x01;
void setupPLL0(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
void feedSeq(void)
{
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}
void connectPLL0(void)
{
PLL0CON = 0x03;
}
7. Write a c program to test loop time outs.
#include <lpc214x.h>
#define sw 1<<0
unsigned char x;
int main(void)
{
IO0DIR = 0xFFFFFFFe;
while(1)
{
mn:
x=IOPIN0;
x=x&0x00000001;
if(sw==0x00000001)
{
IO0SET = 0xFFFFFFFF;
IO0CLR = 0xFFFFFFFF;
testtime_out(1500); //1.5 Second(s) time out loop
goto mn;
}
else if(sw==0x00000000)
{
IO0SET = 0xFFFFFFFF;
IO0CLR = 0xFFFFFFFF;
testtime_out(1500); //1.5 Second(s) time out loop
goto mn;
}
}}
#include <lpc214x.h>
int main(void)
{
initClocks();
initTimer0();
IO0DIR = 0xFFFeFFFF;
while(1)
{
mn:
x= IOPIN0;
x=x&0x00010000;
if(x==0x00010000)
{
do
{
x= IOPIN0;
x=x&0x00010000;
IO0SET = 0xFFFFFFFF;
delayMS(1000); // 1 Second(s) Delay
IO0CLR = 0xFFFFFFFF;
} while(x==0x00010000&&T0TCR!=0X00);
goto mn;
}
else if(x==0x00000000)
{
do
{
IO0SET = 0xFFFF0000;
delayMS(1000); // 1 Second(s) Delay
IO0CLR = 0x0000FFFF;
}while(x==0x00000000&&T0TCR!=0X00);
goto mn;
}
}
}
void initTimer0(void)
{
T0CTCR = 0x0;
T0PR = PRESCALE-1;
T0TCR = 0x02;
}
void initClocks(void)
{
setupPLL0();
feedSeq();
connectPLL0();
feedSeq();
VPBDIV = 0x01;
void setupPLL0(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
}
void feedSeq(void)
{
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}
void connectPLL0(void)
{
PLL0CON = 0x03;
}
10. Write a program to display elapsed time over RS-232 link.
#include <lpc214x.h>
void uart0_init(void);
void uart0_tx(unsigned char);
void str_uart0(unsigned char *x);
void delayMS(unsigned int milliseconds);
void initClocks(void);
void initTimer0(void);
void setupPLL0(void);
void feedSeq(void);
void connectPLL0(void);
unsigned char timeout_loop=0,x;
int main(void)
{
uart0_init();
initClocks();
initTimer0();
IO0DIR = 0xFFFeFFFF;
while(1)
{
mn:
x= IOPIN0;
x=x&0x00010000;
if(x==0x00010000)
{
do
{
str_uart0("event 1 is start and time is counting");
x= IOPIN0;
x=x&0x00010000;
IO0SET = 0xFFFFFFFF;
delayMS(1000); // 1 Second(s) Delay
IO0CLR = 0xFFFFFFFF;
} while(x==0x00010000&&T0TCR!=0X00);
str_uart0("time is 1 sec for event 1is completed , event 1 is lights all are on");
goto mn;
}
else if(x==0x00000000)
{
do
{
str_uart0("event 2 is start and time is counting");
IO0SET = 0xFFFF0000;
delayMS(2000); // 1 Second(s) Delay
IO0CLR = 0x0000FFFF;
}while(x==0x00000000&&T0TCR!=0X00);
str_uart0("time is 2 sec for event 1is completed , event 2 is some lights are on remaining are off
");
goto mn;
}
}
}
void initTimer0(void)
{
T0CTCR = 0x0;
T0PR = PRESCALE-1;
T0TCR = 0x02;
}
void initClocks(void)
{
setupPLL0();
feedSeq();
connectPLL0();
feedSeq();
VPBDIV = 0x01;
void setupPLL0(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
void feedSeq(void)
{
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}
void connectPLL0(void)
{
PLL0CON = 0x03;
}
void uart0_init(void)
{
PINSEL0 |=0x00000005;
U0LCR=0x83;
U0DLL=98;
U0DLM=0;
U0LCR=0x03;
}
void tx_uart0(unsigned char x)
{
U0THR=x;
while(!(U0LSR&=0x20));
}