null (1)
null (1)
/defi
nes.h
#defi
neSETBI T(WORD,BITPOS)WORD| =(1<<BI TPOS)
#defi
neSSETBI T(WORD,BITPOS)WORD=( 1<<BI TPOS)
#defi
neCLRBI T(WORD,BITPOS)WORD&=( ~(1<<BI TPOS) )
#defi
neSCLRBI TSSETBI T
#defi
neCPLBI T(WORD,BITPOS)WORD^ =(
1<<BI TPOS)
#defi
neREADBI T(WORD, BITPOS)( (WORD>>BI TPOS) &1)
#defi
neREADNI BBLE(WORD, SBITPOS)( (WORD>>SBI TPOS)&15)
#defi
neWRI TEBIT(WORD, BITPOS,BIT)WORD=( WORD&( ~(1<<BITPOS)))
|(
BIT<<BITPOS)
#defi
ne WRI TEPAIRBIT(WORD,BITPAIRPOS,PAI RBI
T)
WORD=( WORD&( ~(3<<BITPAI RPOS)))|
(PAI RBIT<<BITPAI RPOS)
#defi
ne WRITENIBBLE(WORD, SBITPOS,NIBBLE)
WORD=( WORD&( ~(15<<SBI TPOS)))|
(NIBBLE<<SBI TPOS)
#defi
ne WRI TEBYTE(WORD, SBITPOS,BYTE)
WORD=( WORD&( ~(255<<SBI TPOS) )
)|(
BYTE<<SBI TPOS)
#defi
ne READWRI TEBIT(
DWORD, WBIT,
SWORD, RBI
T)
DWORD=( DWORD&( ~(
1<<WBI T))
)|
(((SWORD>>RBI T)&1)<<WBIT)
#defi
ne READWRI TEBIT2(WORD, WBIT,RBI
T)
WORD=( WORD&( ~(1<<WBI T)))|
((
(WORD>>RBI T)&1)<<WBI T)
#defi
ne CFGPIN(WORD, PIN,FUNC)
WORD=( PIN<16)?(
(WORD&( ~(3<<(PIN*2))
)) |
(FUNC<<( PIN*2))
):(
(WORD&( ~(3<<((
PIN-
16)*
2))))
|(
FUNC<<( (
PIN-16)*2)))
#defi
neFUN10
#defi
neFUN21
#defi
neFUN32
#defi
neFUN43
*
****
*****
*****
** *
****
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
**
*
****
*****
*****
** *
//t
ypes.h
t
ypedefunsi
gnedcharu8;
t
ypedefsi
gnedchars8;
t
ypedefunsi
gnedshor tu16;
t
ypedefsi
gnedshor ts16;
t
ypedefunsi
gnedi ntu32;
t
ypedefsi
gnedi nts32;
t
ypedeffl
oatf32;
t
ypedefdoubled64;
*****
*****
** *
*******
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
//del
ay.h
#incl
ude"types.h"
voiddelay_us(u32);
voiddelay_ms( u32)
;
voiddelay_s(u32);
//delay.c
#include"types.h"
voiddel ay_
us( u32dly
US)
{
dlyUS* =12;
while(dl
yUS-
-);
}
voiddel ay_
ms( u32dl
yMS)
{
dlyMS* =12000;
whi
le(dl
yMS--)
;
}
v
oiddel
ay_s(u32dlyS)
{
dl
yS*=12000000;
whi
le(dl
yS--
);
}
********
*** *********** *************
* **
* ******
* ***
********
*****
***
***
*****
***
* *
*****
***
***
***
***
***
*
I
nt erfacingLED/ LEDS:
**** *
** **
* ****
*******
1.FlashanLEDconnect edt oanypor tlineatt herateof1secondf or10ti
mesandst op.
#include<LPC21XX. h>
#include"types. h"
#include"del ay.h"
#include"def ines.h"
#defineLED0
main( )
{
u32i ;
SETBI T( IODIR0, LED) ;
for(i
=0; i
<10; i
++)
{
SSETBI T(IOSET0, LED) ;
del ay _s(1);
SCLRBI T(IOCLR0, LED) ;
del ay _s(1);
}
whi l
e(1) ;
}
********
*** *********** *************
* **
* ******
* ***
********
*****
***
***
*****
***
* *
*****
***
***
***
2.Wr it
eanECPt odi spl aybel owment ionedpat ternson8-led’
s(4-
acti
vehighled’
sand
4-acti
v el owl ed’s)
a.Swi tchONonebyonel edfrom l efttorightatt herat
eof1second
b.Swi tchONonebyonel edfrom r ighttoleftatt herat
eof1second
#include<LPC21XX. h>
#include"types. h"
#include"del ay.h"
#include"def ines.h"
#defineAL_ LED0/ /NOTE: p0. 0top0. 3act i
vel owl eds
#defineAH_ LED4/ /NOTE: p0.4top0. 7act ivehighl eds
main( )
{
u32i ,
n;
WRI TEBYTE( IODI R0, AL_LED,255) ;
whil
e( 1)
{
n=128;
for (i
=0;i
<8; i++)
{
WRI TEBYTE( I
OPIN0, AL_ LED,(n>>i)^
0x0f )
;
delay _s(1) ;
}
n=1;
for (i
=0;i
<8; i++)
{
WRITEBYTE(I
OPI
N0,
AL_
LED,
(n<<i
)^0x0f
);
del
ay_s(
1);
}
}
}
*****
* *
**** ***
**** **************************
****
********
*******
****
***
*****
****
* *
***
****
***
***
***
3.Createadi splayon8LED’ s( 4-activehighand4- acti
velow)connectedtoport0(0.
0-
0.7)int hegiv enpat ter n.
a.Firstal l
LEDsshoul dbeOFF, at1stsecond, LED0andLED7( P0.0and
P0. 7)shoul dbeON
b.At2ndsecond, LED1andLED6( P0.1andP0. 6)shouldbeON
c.At3r dsecond, LED2andLED5( P0.2andP0. 5)shouldbeON
d.At4t hsecond, LED3andLED4( P0.3andP0. 4)shouldbeON
e.Repeatabov epat t
er nfor5t i
mesandst opt heprocess.
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay.h"
#incl
ude" def i
nes.h"
#defineAH_ LED0/ /p0.0t op0. 3act ivehighleds
#defineAL_ LED4/ /
p0. 4t op0. 7act iv
el owleds
main()
{
u32i ,
j,k;
WRI TEBYTE( IODI R0,AH_ LED,255) ;
WRI TENI BBLE( I
OPI N0, AL_ LED,15) ;
for(i
=0; i
<=5;i++)
{
f
or (j
=0,k=7;j<k;j++,k--)
{
SSETBI T(IOSET0, j)
;
SCLRBI T(IOCLR0, k);
del ay_s(1);
SCLRBI T(IOCLR0, j)
;
SSETBI T(IOSET0, k);
}
}
while(1) ;
}
*****
* *
**** ***
**** **************************
****
********
*******
****
***
*****
****
* *
***
****
***
***
***
4.Wr i
teanECPt odi splaybi naryequi valentof0-255on8- leds(4-
acti
vehighled’
sand
4-activelowl ed’s)
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay.h"
#incl
ude" def i
nes.h"
#defineAL_ LED0/ /
p0. 0t op0. 3act iv
el owleds
#defineAH_ LED4/ /p0.4t op0. 7act ivehighleds
main()
{
u32i ;
WRI TEBYTE( IODI R0,AL_ LED, 255) ;
WRI TENI BBLE( I
OPI N0, AL_ LED,15) ;
while(1)
{
for(i
=0;i<=255; i++)
{
WRITENIBBLE(
IOPI
N0,
AL_
LED,~i
);
WRITENIBBLE(
IOPI
N0,
AH_LED,
(i
>>4)
);
del
ay_s(
1);
}
}
}
*****
* *
****** *
****
********
******
*******
****
*****
****
***
****
***
***
*****
***
* *
*****
***
***
*****
*
5.Wr it
eanECPt ofindthesecondhi ghestdigi
tinagiv
enint
egeranddisplayi
tsbi
nary
equivalenton8- LEDS( 4-Act
iveHighLEDS&4- Acti
veLowLEDS).
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay.h"
#incl
ude" def i
nes.
h"
#defineAL_ LED0/ /p0.0top0.3act i
velowleds
#defineAH_ LED4/ /p0.4top0.7act i
vehighleds
main( )
{
u32i ,
j,
max=0, max2=0;
WRI TEBYTE( IODIR0,AL_LED,255);
WRI TENI BBLE(IOPIN0,AL_LED,15);
i=j
=895;
whi l
e(i
)
{
if((i
%10)>max)
max=i%10;
i/=10;
}
whi l
e(j
)
{
if(((j
%10)>max2) &&((j
%10)<max))
max2=j %10;
j/=10;
}
WRI TENI BBLE(IOPIN0,AL_LED,~(max2));
WRI TENI BBLE(IOPIN0,AH_LED,max2>>4) ;
whi l
e(1) ;
}
*****
* *
****** ****
*******
********
*********
* *
*****
***
***
***
***
***
***
*****
****
*****
***
***
***
***
6.Wr i
teanECPt odispl
aybi naryequivalentofpri
menumberswit
hintherangeof0-
50
@ 5seconds.( Note:UseI sPri
me( )functi
on)
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay .
h"
#incl
ude" def i
nes.h"
#defineAL_ LED0/ /
p0.0t op0.3act i
velowl eds
#defineAH_ LED4/ /p0.4top0. 7acti
v ehighleds
u32ispr i
me( u32);
main()
{
u32num;
WRI TEBYTE( IODIR0,AL_LED,255);
WRI TENI BBLE(I
OPI N0,AL_LED,15);
while(1)
{
f
or(
num=0; num<=50;num++)
{
i
f(
isprime(num) )
{
WRI TENIBBLE(
IOPI
N0,
AL_
LED,~num);
WRI TENIBBLE(
IOPI
N0,
AH_LED,
num>>4);
delay_s(
5);
}
}
}
}
u32i
spri
me( u32num)
{
u32i;
for
(i
=2;i
<num;i++)
{
i
f(
(num%i)==0)
r
etur
n0;
}
ret
urn1;
}
*****
* *
****** **************
*****
*******
*** *
*****
***
***
*** *
****
***
****
****
****
***
***
***
***
*
7.Wr i
teanECPt opr intthebinaryequivalentofodddigit
sinagivenint
egeron8LEDS
@ 5seconds.
#incl
ude<LPC21XX. h>
#incl
ude" types. h"
#incl
ude" delay .h"
#incl
ude" def i
nes. h"
#defineAL_ LED0/ /p0.0top0.3act i
velowl eds
#defineAH_ LED4/ /p0.4top0. 7acti
vehi ghleds
main()
{
u32num=635;
WRI TEBYTE( IODIR0,AL_LED,255);
WRI TENI BBLE( IOPIN0,AL_LED,15);
while(num)
{
if(((num%10) %2)==1)
{
WRI TENI BBLE(IOPIN0,AL_LED,~(num%10) );
WRI TENI BBLE(IOPIN0,AH_ LED,(
num%10) >>4);
delay _
s(5);
}
num/ =10;
}
while(1) ;
}
*****
****
***********
*****
* *
***
***
****
******
****
*****
****
*****
****
***
***
* *
***
*****
*****
***
*
8.Wr i
teanECPt opr i
ntthebi
naryequiv
alentofsum ofthedigi
tsf
orthegiveni
nteger
onLEDS( 4-Acti
veHighLEDS&4- Acti
veLowLEDS) .
#incl
ude<LPC21XX. h>
#incl
ude"ty
pes. h"
#incl
ude"delay.h"
#incl
ude"defines.h"
#defineAL_LED0/ /
p0.0t op0.3act i
velowleds
#defineAH_LED4/ /p0.4top0.7act i
vehighleds
main()
{
u32num=6359,sum=0;
WRITEBYTE(IODIR0,AL_LED,255);
WRITENIBBLE(I
OPI N0,AL_LED,15);
whil
e(num)
{
sum+=(num%10) ;
num/=10;
}
WRITENIBBLE(I
OPI N0,AL_LED,~sum);
WRITENIBBLE(I
OPI N0,AH_LED,sum>>4);
whil
e(1);
}
*********
* **
* *
******** *****
*****
********
****
****
*****
*****
***
***
****
***
* *
*****
****
***
***
***
*
9.Wr it
eanECPt opr intthebinaryequival
entofproductoft
hedigi
tsforthegiv
en
integeronLEDS( 4-ActiveHighLEDS&4- Acti
veLowLEDS) .
Not e:iftheproducti s>255, Switchonalter
nateLEDS.
#include<LPC21XX. h>
#include"types.h"
#include"delay.h"
#include"def i
nes.h"
#def i
neAL_ LED0/ /p0. 0top0.3act i
velowleds
#def i
neAH_ LED4/ /p0.4top0. 7activ
ehighleds
main( )
{
u32num=635, pdct =1;
WRI TEBYTE( IODI R0,AL_LED,255);
WRI TENI BBLE( I
OPI N0,AL_LED,15);
whi l
e(num)
{
pdct*=(num%10) ;
num/ =10;
}
WRI TENI BBLE( I
OPI N0,AL_LED,~pdct);
WRI TENI BBLE( I
OPI N0,AH_LED,pdct>>4);
whi l
e(1) ;
}
*****
* *
***********
********
*****
********
*****
****
***
****
***
***
****
***
****
***
***
*****
*****
***
***
*
10.Wr it
eanECPt oprintthebinaryequival
entofpowerof2numberswit
hintherange
of0-255@ 5seconds.
#incl
ude<LPC21XX. h>
#incl
ude" ty
pes. h"
#incl
ude" delay.h"
#incl
ude" defines.
h"
#defineAL_ LED0/ /
p0. 0top0.3act i
velowleds
#defineAH_ LED4/ /p0.4top0. 7acti
vehighleds
main()
{
u32num;
WRI TEBYTE( IODIR0,AL_LED,255);
WRI TENI BBLE(I
OPI N0,AL_LED,15);
f
or(
num=0; num<=255;num++)
{
if
((num&( num-1)
)==0)
{
WRI TENIBBLE(I
OPIN0,
AL_
LED,~num);
WRI TENIBBLE(I
OPIN0,
AH_LED,
num>>4);
delay_s(
5);
}
}
whi
le(
1) ;
}
**********
*** *
******
** ****
* ****
**************
*****
*****
****
* **
* *
***
*****
***
****
***
**
**********
*** *
******
** ****
* ****
**************
*****
*****
****
* **
* *
***
*****
***
****
***
**
Interf
acingLED&SWI TCH:
**********
* *
* **
* *
*****
***
1.Creat eadi splayon8LED’ s(4-acti
v ehighand4- act
ivelow)using4activel
ow
switchesconnect edt opor t0(0.16-0.19)inthepat t
ernexplained:
a).FirstallLEDsshoul dbeOFF.
b)if1stswi t
chi spressed, LED0andLED7( P0.0andP0. 7)shouldbeON
c)if2ndswi tchispr essed, LED1andLED6( P0.1andP0. 6)shouldbeON
d)if3r dswi t
chi spressed, LED2andLED5( P0.2andP0. 5)shouldbeON
e)if4t hswi t
chi spressed, LED3andLED4( P0.3andP0. 4)shouldbeON
#include<LPC21XX. h>
#include"types.h"
#include"delay.h"
#include"def i
nes.h"
#defineAL_ LED0/ /p0. 0top0. 3act i
v elowleds
#defineAH_ LED4/ /p0.4t op0.7act ivehighleds
#defineAL_ SW116
#defineAL_ SW217
#defineAL_ SW318
#defineAL_ SW419
mai
n()
{
WRITEBYTE( I
ODIR0,AL_LED,255);
WRITENIBBLE( I
OPIN0,AL_LED,15);
whi
le(
1)
{
if
(READBI T(I
OPIN0,AL_SW1) ==0)
{
WRI TEBYTE(IOPIN0,AL_LED,
0X8E)
;
delay_s(1)
;
}
elseif
(READBI T(I
OPIN0,AL_SW2)==0)
{
WRI TEBYTE(IOPIN0,AL_LED,
0X4D);
}
elseif
(READBI T(I
OPIN0,AL_SW3)==0)
{
WRI TEBYTE(IOPIN0,AL_LED,
0X2B)
;
}
elseif
(READBI T(I
OPIN0,AL_SW4)==0)
{
WRI TEBYTE(IOPIN0,AL_LED,
0X17)
;
}
el
se
WRI
TEBYTE(
IOPI
N0,
AL_
LED,
0X0F)
;
}
}
*****
* *
*****
* *
****
**** *
****
* *
****
****
******
*****
***
****
****
***
*****
***
***
****
****
**
2.Wr i
teanECPt odisplaybinaryequi
valentofswit
chpresscounton8-
leds(4-
act
ive
highledsand4- activelowleds)
#incl
ude<LPC21XX. h>
#incl
ude"types.h"
#incl
ude"delay.h"
#incl
ude"def i
nes.
h"
#defineAL_ LED0/ /p0.0top0.3activelowleds
#defineAH_ LED4/ / p0.4t
op0. 7acti
vehighleds
#defineAL_ SW 8//p0. 8acti
velowswi t
ch
mai
n()
{
u8cnt=0;
WRITEBYTE( I
ODIR0,AL_LED,255);
WRITENIBBLE( I
OPIN0,AL_LED,15);
whi
le(
1)
{
if
(READBI T(I
OPIN0,AL_SW) ==0)
{
whil
e(READBI T(I
OPIN0,
AL_SW)
==0)
;
cnt++;
delay_ms(2);
if
(cnt>255)
cnt=0;
WRI TEBYTE(IOPIN0,AL_LED,cnt^
0x0F)
;
}
}
*********
***************
*** ***
****
********
* *
**********
*******
*******
******
****
**
3.PART1: I
mpl ementupanddowncount er
.Uset woswi t
chesand8l eds(4-
act
ivehigh
ledsand4- activ
el owl eds)asment ioned: sw1f orincrementingcount,sw2fordecr
ement
countanddi splayupdat edcountonl eds.
Not e:i
fcountv alueis0, atthi
st i
mei fsw2pr esseditshoulddisplay0onlyonledsandi
f
countv aluei s255t heni fsw1pr esseditshoul ddisplay255onl yonleds.
#include<LPC21XX. h>
#include"ty
pes. h"
#include"delay.h"
#include"defines.h"
#def i
neAL_ LED0/ /
p0.0t op0. 3acti
velowl eds
#def i
neAH_ LED4/ /p0.4t op0.7activehighleds
#def i
neAL_ SW_ I
NC8/ /p0. 8acti
velowswi t
chf orincrementingcount
#def i
neAL_ SW_ DEC9/ /p0.9activelowswi tchfordecr ement i
ngcount
mai
n()
{
s32cnt=0;
WRITEBYTE(I
ODIR0,AL_LED,255);
WRITENIBBLE(
IOPIN0,AL_LED,15)
;
whi
l
e(1)
{
i
f(READBI T(I
OPIN0,AL_ SW_ I
NC)==0)
{
while(READBIT(IOPIN0,
AL_
SW_
INC)
==0);
cnt++;
delay_ms(2);
}
el
sei f(
READBI T(I
OPIN0, AL_SW_ DEC)==0)
{
while(READBIT(IOPIN0,
AL_
SW_
DEC)==0)
;
cnt--
;
delay_ms(2);
}
i
f(cnt<0)
cnt=0;
elseif
(cnt>255)
cnt=255;
WRI TEBYTE( I
OPIN0,AL_ LED,cnt
^0x0F);
}
}
*****
* **
***** *
*************
******
*****
*****
*****
***
*****
***
****
****
***
*****
****
***
*
4.Wr i
teanECPt oprintthebinaryequiv
alentofswit
chpresscountwit
hin500mill
i
seconds.
Not e:Timesl iceshoul dstartaft
erthefir
stswit
chpress
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay.h"
#incl
ude" def i
nes.h"
#defineAL_ LED0/ / p0.0top0.3activelowleds
#defineAH_ LED4/ /p0.4top0. 7acti
vehighleds
#defineAL_ SW 8/ /p0.8act i
velowswi t
chforcount
mai
n()
{
u32i,
cnt=0;
WRITEBYTE( I
ODIR0,AL_LED,255);
WRITENIBBLE(IOPIN0,AL_LED,15);
whil
e(1)
{
if
(READBI T(I
OPIN0,AL_SW)==0)
{
cnt=0;
for(
i=0;
i<500;i
++)
{
if
(READBI T(I
OPIN0,AL_SW)==0)
{
whil
e(READBI
T(I
OPI
N0,
AL_
SW)
==0)
;
cnt++;
}
delay_ms(1);
}
}
WRI TEBYTE( I
OPIN0,AL_LED,cnt
^0x0f)
;
}
}
********
************** *****************
***************
******
**** *
********
****
***
** *
* *
**
5.PART2: I
mpl ementupanddowncount er.Uset woALswi tchesand8l eds(4-activ
e
highledsand4- act i
vel owl eds)asment ioned:
Asl ongassw1i spr essedi ncrementt hecountv al
uewi t
hr espectt o1sec
Asl ongassw2i spr esseddecr ementcountv aluewithrespectt o1sec
Anddi splayupdat edcountonl eds.
Ifbothswi tchesar epr essedatat i
me, don’ tdoanyoper ationoncount .
Not e:i
fcountv aluei s0, atthistimeifsw2pr esseditshoulddi splay0onlyonl edsandi
f
countv alueis255t heni fsw1pr essedi tshoul ddisplay255onl yonleds.
#include<LPC21XX. h>
#include"ty
pes. h"
#include"delay.h"
#include"defines.h"
#defineAL_ LED0/ /p0. 0t op0. 3activelowl eds
#defineAH_ LED4/ / p0.4t op0. 7act i
vehighl eds
#defineAL_ SW_ I
NC8/ /p0.8act ivelowswi tchf orcountincrement
#defineAL_ SW_ DEC9/ /p0. 9act i
velowswi tchf orcountdecr ement
mai
n()
{
s32cnt =0;
WRITEBYTE( I
ODIR0,AL_LED,255);
WRITENI BBLE(IOPIN0,AL_LED,15);
whil
e(1)
{
whi le((
READBI T(I
OPIN0,AL_SW_ I
NC)==0)
&&(READBI
T(I
OPIN0,
AL_
SW_DEC)
!=0)
)
{
cnt++;
delay_s(
1);
}
whi le((
READBI T(I
OPIN0,AL_SW_ DEC)
==0)&&(
READBIT(
IOPI
N0,
AL_SW_
INC)
!=0)
)
{
cnt--
;
delay_s(
1);
}
if(cnt<0)
cnt=0;
elsei f(
cnt>255)
cnt=255;
WRI TEBYTE(IOPIN0,AL_LED,cnt
^0x0f
);
}
}
*****
*********
**** *
******
*******
*******
**********
*******
***
***
***
****
*******
* *
*****
**
6.Wr i
teanECPt ofulf
il
lthebelowment ionedoperat i
ons.
1.Ini
ti
allyallledsshouldbeof f.
2.Displayev ennumber son8l edswi threspectto1secondifsw1i spressed.
Cont i
nuet hisprocessunt i
lsw2i spressed.
3.Displayoddnumber son8l edswi t
hr espectto1secondifsw2i spressed.
Cont i
nuet hisprocessunt i
lsw1i spressed.
Note:ifnumberi s>255t henrolloverto0andr epeatt
heoperat
ion.
#incl
ude<LPC21XX. h>
#incl
ude"types.h"
#incl
ude"delay.h"
#incl
ude"defines.h"
#def
ineAL_
LED0/ /p0.
0t op0.3acti
velowleds
#def
ineAH_LED4//p0.
4t op0.7acti
v ehi
ghleds
#def
ineAL_
SW_ EVEN8/ /p0.8act
ivelowswitchforev
ennumbers
#def
ineAL_
SW_ ODD9/ /p0.9acti
velowswitchforoddnumber
s
main()
{
s32num=0;
WRI TEBYTE( IODIR0,AL_ LED,255);
WRI TENI BBLE( I
OPIN0, AL_LED,15);
while(1)
{
if(READBI T(I
OPIN0, AL_SW_ EVEN) ==0)
{
if(
(num%2) !=0)
num+=1;
}
if(READBI T(I
OPIN0, AL_SW_ ODD) ==0)
{
if(
(num%2) ==0)
num+=1;
}
if(num==256)
num=0;
if(num==257)
num=1;
WRI TEBYTE( I
OPI N0,AL_LED,num^ 0x0f);
num+=2;
del ay_s(1);
}
}
*****
* *
****** *
******************
******
******* **
******
***
***
*****
***
***
***
***
***
*****
SEVENSEGMENTDI SPLAY
******
** *
* *
******
****
1.Wr i
teanECPt ot estsev ensegmentdi spl ay(al
lsegments)i
sworki
ngornot.
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay.h"
#incl
ude" def i
nes.h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
main()
{
WRI TEBYTE( IODIR0,CA7_ DATA_ PINS, 255);
SETBI T( I
ODIR0, DSEL1) ;
SSETBI T(I
OSET0, DSEL1) ;
while(1)
{
WRI TEBYTE( I
OPI N0,CA7_DATA_ PINS,255);
del ay_s(1);
WRI TEBYTE( I
OPI N0,CA7_DATA_ PINS,0);
del ay_s(1);
}
}
*
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
**
2.Wr i
teanECPt oShowupcount i
ngf r
om 0t o9onseg1andt henaftershowdown
counting9t o0onseg2@ of1sec( approximately
).
#incl
ude<LPC21XX. h>
#incl
ude"ty pes.h"
#incl
ude"del ay.h"
#incl
ude"def ines.h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
constu8seg_ l
ut[
]={0xc0,0xf
9,0xa4,
0xb0,0x99,
0x92,0x82,0xf8,
0x80,0x90};
main()
{
s8i;
WRITEBYTE( IODIR0,
CA7_ DATA_ PI
NS,255);
SETBI T(IODIR0,DSEL1) ;
SETBI T(IODIR0,DSEL2) ;
whil
e( 1)
{
SSETBI T(IOSET0,DSEL1);
for(i=0;i
<10;i++)
{
WRITEBYTE( I
OPIN0,CA7_DATA_ PINS,seg_lut[
i]
);
delay_s(1);
}
SCLRBI T(IOCLR0,DSEL1);
SSETBI T(IOSET0,DSEL2);
for(--i
;i
>=0;i-
-)
{
WRITEBYTE( I
OPIN0,CA7_DATA_ PINS,seg_lut[
i]
);
delay_s(1);
}
SCLRBI T(IOCLR0,DSEL2);
}
}
*********
*****************
*****
****
***
****
*******
********
******
******
*****
***
***
***
* *
3.Wr iteanECPt odi splaynumbers0-9ont wosegment s@ 1secint hebel
owgiven
format .
Ev ennumber sonsegment1andoddnumber sonsegment 2
#include<LPC21XX. h>
#include"types.h"
#include"delay.h"
#include"defines.h"
#defineCA7_ DATA_ PI NS0
#defineDSEL18
#defineDSEL29
constu8seg_ l
ut[
]={0xc0,0xf9,
0xa4,
0xb0,
0x99,0x92,0x82,0xf
8,0x80,
0x90};
main( )
{
s8i ;
WRI TEBYTE( IODIR0,CA7_DATA_PINS,255);
SETBI T(IODIR0,DSEL1) ;
SETBI T(IODIR0,DSEL2) ;
whi l
e(1)
{
f
or (
i=0;i
<10;i++)
{
i
f(
(i
%2)==0)
{
SSETBIT(I
OSET0,
DSEL1)
;
WRITEBYTE(I
OPIN0,
CA7_DATA_
PINS,
seg_
lut
[i
])
;
del
ay_s(1)
;
SCLRBIT(I
OCLR0,
DSEL1)
;
}
el
se
{
SSETBIT(I
OSET0,
DSEL2)
;
WRITEBYTE(I
OPIN0,
CA7_DATA_
PINS,
seg_
lut
[i
])
;
del
ay_s(1)
;
SCLRBIT(I
OCLR0,
DSEL2)
;
}
}
}
}
*****
* *
*******************
* **
* *
*******
*****
******
********
******
*******
****
***
***
****
**
4.Wr i
teanECPt oShowupcount ingfrom 00to99ont womul t
ipl
exedsevensegment
s
@ of1sec( appr oximat el
y).
#incl
ude<LPC21XX. h>
#incl
ude" types.h"
#incl
ude" delay.h"
#incl
ude" defines.h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
constu8seg_ l
ut[
]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf
8,0x80,
0x90};
main()
{
u32i ,
j;
WRI TEBYTE( IODIR0,CA7_ DATA_ PINS,255);
SETBI T(IODIR0, DSEL1) ;
SETBI T(IODIR0, DSEL2) ;
while(1)
{
for (
i=0;i
<100; i
++)
{
for(
j=0;j<=500;j++)
{
SSETBI T(I
OSET0, DSEL1);
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_l
ut[i
/10])
;
delay_ms(1) ;
SCLRBI T(I
OCLR0, DSEL1) ;
SSETBI T(I
OSET0, DSEL2);
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_l
ut[i
%10]);
delay_ms(1) ;
SCLRBI T(I
OCLR0, DSEL2) ;
}
}
}
}
*
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
5.Wr i
teanECPt oShowdowncount ingfr
om 99t o00ont womul ti
plexedsev
en
segment s@ of1sec( approxi
mately)
.
#incl
ude<LPC21XX. h>
#incl
ude"types.h"
#incl
ude"delay.h"
#incl
ude"defines.
h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
constu8seg_ l
ut[
]={0xc0,0xf
9,0xa4,
0xb0,
0x99,0x92,0x82,0xf
8,0x80,
0x90};
main()
{
s32i,
j;
WRITEBYTE( IODI R0,
CA7_ DATA_PINS,255);
SETBIT(IODIR0,DSEL1) ;
SETBIT(IODIR0,DSEL2) ;
whil
e(1)
{
f
or (
i=99;
i>=0;i-
-)
{
for(
j=0;j
<=500;j++)
{
SSETBI T(I
OSET0,DSEL1);
WRI TEBYTE( I
OPIN0,CA7_DATA_ PINS,seg_l
ut[i
/10]
);
delay_ms(1);
SCLRBI T(I
OCLR0,DSEL1) ;
SSETBI T(I
OSET0,DSEL2);
WRI TEBYTE( I
OPIN0,CA7_DATA_ PINS,seg_l
ut[i
%10]);
delay_ms(1);
SCLRBI T(I
OCLR0,DSEL2) ;
}
}
}
}
*****
* *
*************
* *
* **
****
******
****
***
********
***
******
****
******
****
****
***
***
***
* *
6.Display00ont womul t
ipl
exedsegmentsandmakei tfl
ashfor5t i
mesandthendisplay
88andmakei tflashf or5t i
mesthenstop.
#incl
ude<LPC21XX. h>
#incl
ude" ty
pes. h"
#incl
ude" delay.h"
#incl
ude" defines.
h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
constu8seg_ l
ut[
]={0xc0,0xf
9,0xa4,
0xb0,
0x99,0x92,
0x82,0xf
8,0x80,
0x90};
main()
{
s32i ,
j;
WRI TEBYTE( IODI R0,CA7_DATA_PINS,255);
SETBI T(IODIR0,DSEL1) ;
SETBI T(IODIR0,DSEL2) ;
for(i
=0;i
<=8; i
+=8)
{
WRI TEBYTE( IOPIN0,CA7_DATA_PINS,seg_
lut
[i]
);
f
or(
j=0;
j
<5;j
++)
{
SSETBIT(I
OSET0,
DSEL1)
;
SSETBIT(I
OSET0,
DSEL2)
;
del
ay_s(1)
;
SCLRBIT(I
OCLR0,
DSEL1)
;
SCLRBIT(I
OCLR0,
DSEL2)
;
del
ay_s(1)
;
}
}
whi
l
e(1)
;
}
********
** *
** *
**** **
* **********
********
** *
********
********
**************
***
*****
***
****
***
****
7.Wr it
eanECPt odi splayt heswitchpr esscountont womul t
iplexedsevensegment
display.(Not e: i
fswi tchi spressed,needt odisplaytheupdat edcountv al
ueaft
erswit
ch
isreleased)
#include<LPC21XX. h>
#include"types.h"
#include"delay.h"
#include"def i
nes.h"
#defineCA7_ DATA_ PI NS0
#defineDSEL18
#defineDSEL29
#defineAL_ SW 10
constu8seg_ l
ut[]={0xc0, 0xf9,
0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80, 0x90}
;
main( )
{
s32cnt =0;
WRI TEBYTE( IODI R0,CA7_ DATA_ PINS, 255);
SETBI T( I
ODI R0,DSEL1) ;
SETBI T( I
ODI R0,DSEL2) ;
whi l
e(1)
{
i
f(READBI T(IOPI N0,AL_SW) ==0)
{
whil
e(READBI T( I
OPIN0,AL_SW) ==0) ;
cnt ++;
delay _ms( 2) ;
}
SSETBI T( I
OSET0, DSEL1) ;
WRI TEBYTE( IOPI N0,CA7_DATA_ PINS,seg_lut
[cnt/10]);
delay_ms( 1) ;
SCLRBI T( I
OCLR0, DSEL1) ;
SSETBI T( I
OSET0, DSEL2) ;
WRI TEBYTE( IOPI N0,CA7_DATA_ PINS,seg_lut
[cnt%10]);
delay_ms( 1) ;
SCLRBI T( I
OCLR0, DSEL2) ;
}
}
***
****
* *
****
*****
********
****
******
*****
********
****
*****
******
***
***
***
* *
****
****
*****
***
***
**
8.I
mplementa2di gitupanddowncount er.Usetwoswi t
chesasmentioned:sw1-
i
ncrementingcount&sw2-decr ementcount .
Note:i
fcountvalueis0,thenifsw2pr esseditshoulddispl
ay00onlyandifcountval
ue
i
s99t henifsw1pr esseditshoulddi
splay99onl y.
#incl
ude<LPC21XX. h>
#incl
ude"types.h"
#incl
ude"delay.h"
#incl
ude"def i
nes.h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
#defineAL_ I
NC_ SW 10
#defineAL_ DEC_ SW 11
constu8seg_ l
ut[]
={ 0xc0,0xf9,
0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90}
;
main()
{
s32cnt =0;
WRITEBYTE( IODI R0,CA7_DATA_ PINS,255);
SETBIT( I
ODIR0, DSEL1) ;
SETBIT( I
ODIR0, DSEL2) ;
whil
e(1)
{
i
f(READBI T(IOPIN0,AL_I
NC_ SW)==0)
{
whil
e(READBIT( I
OPIN0,AL_INC_ SW)==0);
cnt++;
delay _ms( 2);
}
el
sei f(
READBI T(I
OPIN0,AL_DEC_SW) ==0)
{
whil
e(READBIT( I
OPIN0,AL_DEC_ SW)==0);
cnt--;
delay _ms( 2);
}
i
f(cnt<0)
cnt=0;
el
sei f(
cnt >99)
cnt=99;
SSETBI T(IOSET0, DSEL1);
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_lut
[cnt/10])
;
delay_ms( 1);
SCLRBI T(IOCLR0, DSEL1) ;
SSETBI T(IOSET0, DSEL2);
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_lut
[cnt%10]);
delay_ms( 1);
SCLRBI T(IOCLR0, DSEL2) ;
}
}
***
*****
***
****
****
***
***
***
***
***
***
***
***
***
***
*****
****
****
****
****
***
***
***
** *
***
***
***
***
*
9.Displ
ayfl
oatval
uesfr
om 0.
0to9.
9ontwomul t
ipl
exedsevensegmentdi
splay.
***
********
****
********
* *
* *
*****
* *
***
****
***
****
** *
***
****
****
***
****
*******
***
****
***
***
****
**
10.Wr i
teanECPf orSinglesegmentonlyshouldbelit(
star
ti
ngfrom segmentA)
cycl
ical
lyaroundtheper ipheryofsi
ngl
esev ensegment(5ti
mescl ockwiseand5ti
mes
anti
-cl
ockwi se@ pr opervisual
iz
ati
on).
#i
ncl
ude<LPC21XX.h>
#i
ncl
ude"ty
pes.h"
#i
ncl
ude"delay
.h"
#i
nclude"defi
nes.h"
#defineCA7_DATA_ PINS0
#defineDSEL18
main()
{
s32i,
j;
;
WRITEBYTE( IODIR0,CA7_DATA_ PI
NS,255)
;
SETBIT(I
ODIR0, DSEL1);
SSETBIT(I
OSET0, DSEL1);
whil
e(1)
{
f
or(i
=0;i
<5;i++)
{
for(
j=0;j<=6;j
++)
{
WRI TEBYTE(IOPI
N0,CA7_DATA_
PINS,
0XFF^
(1<<j
))
;
delay_ms(100);
}
}
f
or(i
=0;i
<5;i++)
{
for(
j=6;j>=0;j
--
)
{
WRI TEBYTE(IOPI
N0,CA7_DATA_
PINS,
0XFF^
(1<<j
))
;
delay_ms(100);
}
}
}
}
***
** ************* *
*********
*** *
* *
* **
* *
**********
***********
* *
**********
* *
****
* *
*******
** *
*******
***
11.Wr iteanECPt opr i
nttheswi t
chpr esscountwi thin500mi ll
isecondsont wo
mul tiplexedsev ensegmentdi splays.
Not e: Timesl iceshoul dst artafterthef i
rstswitchpr ess
Explanat i
on: Init
ial
ly0wi l
l displayont womul ti
plexeddi splays.Onceswi tchispressed,
thent i
mesl i
cewi l
lstart
.Ifswi tchispr essedwi thint i
mesl i
ceagaint hentimeslicest art
s
from 0.I fther eisnoswi t
chpr esswi thintimeslicet hendisplaytheupdat edcountont wo
mul tiplexedsev ensegmentdi splays.Afterswitchi spressed, withi
nt i
mesl i
cenoneedt o
Embedded- CAssi gnment s
displayanyv alueonsegment s.(Thi slogicisgivingsuppor tforSMSkey padpr ogram
i
mpl ement ati
on)
***
** ************* *
*********
*** *
* *
* **
* *
**********
***********
* *
**********
* *
****
* *
*******
** *
*******
***
12.Pr oducet hedi splayonsev ensegment susingswi tchesconnect edt oanypor tinthe
pat ternexpl ained:
a.Fi rsttwoSegment sshoul dbeOFF
b.Forev er ySW1pr esst hev alueshoul dbei ncrement edanddi splayedont hesev en
segment s.
c.Forev er ySW2pr esst hev alueshoul dbedecr ement edanddi splayedont hesev en
segment s.
d.Forev er ySW3pr esst hev alueshoul dber eset .
#i
ncl
ude<LPC21XX. h>
#i
ncl
ude"ty
pes. h"
#i
ncl
ude"delay.h"
#i
ncl
ude"defines.
h"
#def
ineCA7_ DATA_PINS0
#defineDSEL18
#defineDSEL29
#defineAL_ I
NC_ SW 10
#defineAL_ DEC_ SW 11
#defineAL_ RESET_ SW 12
constu8seg_ l
ut[]
={ 0xc0,0xf9,
0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90}
;
main()
{
s32cnt =0;
WRITEBYTE( IODI R0,CA7_DATA_ PINS,255);
SETBIT( I
ODIR0, DSEL1) ;
SETBIT( I
ODIR0, DSEL2) ;
whil
e(1)
{
i
f(READBI T(IOPIN0,AL_I
NC_ SW)==0)
{
whil
e(READBIT( I
OPIN0,AL_INC_ SW) ==0)
;
cnt++;
delay _ms( 2);
}
el
sei f(
READBI T(I
OPIN0,AL_DEC_SW) ==0)
{
whil
e(READBIT( I
OPIN0,AL_DEC_ SW)==0);
cnt--;
delay _ms( 2);
}
el
sei f(
READBI T(I
OPIN0,AL_RESET_ SW) ==0)
{
whil
e(READBIT( I
OPIN0,AL_RESET_ SW)==0);
cnt=0;
delay _ms( 2);
}
i
f(cnt<0)
cnt=0;
el
sei f(
cnt >99)
cnt=99;
SSETBI T(IOSET0, DSEL1);
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_lut
[cnt/10]);
delay_ms( 1);
SCLRBI T(IOCLR0, DSEL1) ;
SSETBI T(IOSET0, DSEL2);
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_lut
[cnt%10] )
;
delay_ms( 1);
SCLRBI T(IOCLR0, DSEL2) ;
}
}
***
*******
***
***
* *
****
* *
*****
****
****
***
*****
****
******
*****
***
***
***
***
******
***
***
***
***
***
***
***
*
**
13.Wr i
teanECPt odisplayt
heprimenumber swit
hin0-99ontwomulti
plexedsev
en
segmentdispl
ays.(Note:mustuseIsPri
me()functi
on)
#i
ncl
ude<LPC21XX. h>
#i
ncl
ude"ty
pes. h"
#i
ncl
ude"delay.h"
#i
ncl
ude"defines.
h"
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
u32ispr i
me( u32);
constu8seg_ lut[
]={0xc0,0xf9,
0xa4,0xb0, 0x99,
0x92,
0x82,
0xf
8,0x80,
0x90}
;
main()
{
u32num, i;
WRI TEBYTE( IODI R0,CA7_DATA_ PINS,255);
SETBI T(IODIR0,DSEL1) ;
SETBI T(IODIR0,DSEL2) ;
whi l
e(1)
{
for(num=0; num<100; num++)
{
if
(i
sprime( num) )
{
for(i
=0;i<500;i
++)
{
SSETBI T(
IOSET0, DSEL1) ;
WRI TEBYTE( IOPIN0,
CA7_DATA_PINS,
seg_l
ut[
num/10]
);
delay_ms( 1);
SCLRBI T(I
OCLR0, DSEL1)
;
SSETBI T(I
OSET0, DSEL2)
;
WRI TEBYTE( IOPIN0,
CA7_DATA_PINS,
seg_l
ut[
num%10])
;
delay_ms( 1);
SCLRBI T(I
OCLR0, DSEL2)
;
}
}
}
}
}
u32ispr i
me( u32num)
{
u32i ;
i
f (
num==2)
return1;
for(i
=2;i
<num; i
++)
{
if((
num%i )
==0)
ret
urn0;
}
return1;
}
***
*******
***
***
****
****
*******
***
***
*****
****
****
***
***
****
*****
***
****
***
***
*******
***
***
***
***
14.Wr i
teanECPtodisplaytheFi
bonacciser
iesnumbers(
0-99)ontwomulti
plexed
sevensegmentdi
splays.
#i
ncl
ude<LPC21XX. h>
#i
ncl
ude"ty
pes. h"
#i
ncl
ude"delay.h"
#i
ncl
ude"defines.
h"
#def
ineCA7_ DATA_PINS0
#def
ineDSEL18
#def
ineDSEL29
u32ispri
me( u32);
constu8seg_ l
ut[
]={0xc0,0xf9,
0xa4,
0xb0,0x99,
0x92,
0x82,
0xf8,
0x80,
0x90}
;
main()
{
u32sum, num1, num2,i
;
WRITEBYTE( I
ODI R0,CA7_DATA_PINS,255);
SETBI T(
IODIR0,DSEL1) ;
SETBI T(
IODIR0,DSEL2) ;
while(
1)
{
sum=0;
num1=1;
num2=0;
while(
sum<100)
{
for(
i=0;i
<500;i++)
{
SSETBI T(IOSET0,DSEL1);
WRI TEBYTE( I
OPIN0,
CA7_DATA_ PINS,
seg_l
ut[
sum/10])
;
delay_ms( 1);
SCLRBI T(IOCLR0,DSEL1);
SSETBI T(IOSET0,DSEL2);
WRI TEBYTE( I
OPIN0,
CA7_DATA_ PINS,
seg_l
ut[
sum%10]);
delay_ms( 1);
SCLRBI T(IOCLR0,DSEL2);
}
sum=num1+num2;
num1=num2;
num2=sum;
}
}
}
***
*******
***
***
****
****
*****
***
***
*****
***
****
***
****
***
***
***
***
***
***
***
***
*******
***
***
***
**
15.Wr i
teanECPtodisplayt
he10random numberswit
hin0-
99ontwomulti
plexed
sevensegmentdi
splaywit
hrespectt
o5seconds.
#incl
ude<LPC21XX. h>
#incl
ude" ty
pes. h"
#incl
ude" delay.h"
#incl
ude" defines.h"
#incl
ude<st dlib.h>
#defineCA7_ DATA_ PI
NS0
#defineDSEL18
#defineDSEL29
u32isprime( u32) ;
constu8seg_ l
ut[]={
0xc0,
0xf
9,0xa4,
0xb0,
0x99,
0x92,
0x82,
0xf
8,0x80,
0x90}
;
main()
{
u32num, i
,j
;
WRI TEBYTE( IODIR0,
CA7_DATA_PINS,
255);
SETBI T(IODI R0,
DSEL1);
SETBI T(IODI R0,
DSEL2);
for(
i=0;i
<10; i++)
{
num=r and()%99;
f
or(
j=0;
j
<2500;
j
++)
{
SSETBIT(
IOSET0,
DSEL1)
;
WRITEBYTE(IOPI
N0,
CA7_DATA_
PINS,
seg_
lut
[num/10]
);
del
ay_ms(1);
SCLRBIT(
IOCLR0,
DSEL1)
;
SSETBIT(
IOSET0,
DSEL2)
;
WRITEBYTE(IOPI
N0,
CA7_DATA_
PINS,
seg_
lut
[num%10])
;
del
ay_ms(1);
SCLRBIT(
IOCLR0,
DSEL2)
;
}
}
whi
l
e(1)
;
}
***
****
******
***
***
***
***
***
***
***
***
****
***
****
***
***
***
***
****
***
***
*****
***
***
***
***
***
***
**
16.Writ
eanECPt oimpl
ementel
ect
roni
cdiceusi
ngoneswit
chandsingl
esegment
di
splay
.
***
*****
*****
***
***
******
*****
****
****
****
****
****
*****
***
***
* *
** *
****
***
****
***
*****
****
***
***
17.Writ
eanECPt odisplay10random numberswithi
n0-9anddi spl
aythesum ofal
l
r
andom numbersont womul t
ipl
exedsevensegmentdisplays.
Note:
displ
ayrandom numbersalso.
#incl
ude<LPC21XX. h>
#incl
ude" ty
pes. h"
#incl
ude" delay.h"
#incl
ude" defines. h"
#incl
ude<st dlib.h>
#defineCA7_ DATA_ PINS0
#defineDSEL18
#defineDSEL29
u32isprime( u32) ;
constu8seg_ l
ut[]={0xc0, 0xf9,
0xa4,
0xb0,0x99,
0x92,
0x82,
0xf8,
0x80,0x90}
;
main()
{
u32ar r[
10] ,
sum=0, i,
j;
WRI TEBYTE( IODI R0,CA7_DATA_PINS,255);
SETBI T(IODI R0, DSEL1) ;
SETBI T(IODI R0, DSEL2) ;
for(
i=0;i
<10; i++)
{
arr[i]
=rand( )%10;
}
for(
i=0;i
<10; i++)
{
for( j
=0;j<500;j++)
{
SSETBI T(I
OSET0, DSEL1);
WRI TEBYTE( IOPIN0,
CA7_DATA_ PINS,
seg_l
ut[
arr
[i
]/10])
;
del ay _
ms( 1);
SCLRBI T(IOCLR0,DSEL1);
SSETBI T(I
OSET0, DSEL2);
WRI TEBYTE( IOPIN0,
CA7_DATA_ PINS,
seg_l
ut[
arr
[i
]%10]);
del ay _
ms( 1);
SCLRBI T(IOCLR0,DSEL2);
}
sum+=ar
r[
i]
;
}
f
or(
j=0;
j<500;j
++)
{
SSETBI T(
IOSET0,
DSEL1)
;
WRI TEBYTE(IOPI
N0,
CA7_DATA_
PINS,
seg_
lut
[sum/10]
);
delay_ms(1);
SCLRBI T(
IOCLR0,
DSEL1)
;
SSETBI T(
IOSET0,
DSEL2)
;
WRI TEBYTE(IOPI
N0,
CA7_DATA_
PINS,
seg_
lut
[sum%10])
;
delay_ms(1);
SCLRBI T(
IOCLR0,
DSEL2)
;
}
whi
le(
1);
}
**
***
* *
***
*********
********
***
*****
*****
*****
*****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
I
NTERFACI NGOFLCD
*
*****
*****
*****
**
/
/lcd_defines.
h
#def
ineCLRLCD0X01
#def
ineCUR_ HOME0X02
#def
ineSFT_CUR_ RGT0X06
#def
ineSFT_CUR_
#def
ineDISP_OFF_ CUR_ OFF0X08
#def
ineDISP_ON_ CUR_ OFF0X0C
#def
ineDISP_ON_ CUR_ ON0X0E
#def
ineDISP_ON_ CUR_ BLK0X0F
#def
ineSFT_DISP_ RGT0X1C
#def
ineSFT_DISP_ LFT0X18
#def
ineMODE_ 8BIT_2LINE0X38
#def
ineMODE_ 4BIT_2LINE0X28
#def
ineCGRAM_ START_ ADDR0X40
#def
ineDDRAM_ START_ ADDR0X80
#def
ineGOTO_ LINE1_ POS00X80
#def
ineGOTO_ LINE2_ POS00XC0
#def
ineGOTO_ LINE3_ POS00X94
#def
ineGOTO_ LINE4_ POS00XD4
#def
ineLCD_DATA8
#def
ineLCD_RS16
#def
ineLCD_EN17
#def
ineLCD_RW 18
//l
cd. h
#incl
ude" types. h"
voidIni
tLCD( v oid);
voidcmdLCD( u8) ;
voidwr i
teLCD( u8);
voidchar LCD( u8);
voidstrLCD( s8* )
;
voidu32LCD( u32) ;
voids32LCD( s32) ;
voidf32LCD( f
32, u8);
voidbinLCD( u32) ;
voidhexLCD( u32) ;
v
oidoct
LCD(
u32)
;
/
/lcd.c
#incl
ude"types. h"
#incl
ude"l
cd_ def i
nes.h"
#incl
ude"delay .
h"
#incl
ude"def i
nes. h"
#incl
ude<lpc21xx. h>
voidwrit
eLCD( u8dat )
{
//
SCLRBI T(IOCLR0, LCD_RW) ;
WRITEBYTE( IOPI
N0, LCD_DATA,dat
);
SSETBI T(IOSET0, LCD_ EN)
;
del
ay_us( 1) ;
SCLRBI T(IOCLR0, LCD_ EN);
del
ay_ms( 2);
}
v
oidcmdLCD(u8cmd)
{
SCLRBIT(
IOCLR0,LCD_
RS)
;
wri
teLCD(
cmd) ;
}
v
oidcharLCD(
u8ascii
)
{
SSETBIT(
IOSET0,
LCD_
RS)
;
wri
teLCD(asci
i
);
}
v
oidst
rLCD(s8*p)
{
whi
le(
*p)
{
charLCD(
*p++)
;
}
}
v
oidu32LCD( u32n)
{
u8a[10];
s32i=0;
i
f(n==0)
{
charLCD('0'
);
}
el
se
{
whil
e(n>0)
{
a[i
++]=(n%10)+48;
n/=10;
}
for
(-
-i;
i>=0;i
--
)
{
charLCD(a[i
])
;
}
}
}
v
oids32LCD( s32n)
{
i
f(n==0)
charLCD('
0'
);
el
sei f
(n<0)
{
charLCD('
-'
);
n=-n;
u32LCD(n);
}
}
v
oidf32LCD( f
32f ,
u8ndp)
{
u32i,j;
i
f(f
<0. 0)
{
charLCD('-
')
;
f=-f
;
}
i
=f;
u32LCD( i
);
charLCD( '
.
')
;
for
(j
=0; j
<ndp;j
++)
{
f=(f
-i
)*10;
i=f;
charLCD(i+48)
;
}
}
v
oidbi
nLCD( u32n)
{
s8bit
=15;
whi
le(bi
t>=0)
{
char LCD(
((n>>bi
t)
&1)
+48)
;
bit-
-;
}
}
v
oidhexLCD( u32n)
{
s32i=0,nibbl
e;
u8a[8];
i
f(n==0)
charLCD('0'
);
el
se
{
while(n)
{
nibbl
e=n%16;
ni
bble=(ni
bble>9)
?((
nibbl
e-10)
+'A'
):
(ni
bbl
e+48)
;
a[
i++]=ni
bble;
n/
=16;
}
f
or(
--
i;
i>=0;
i-
-)
{
charLCD(a[
i]
);
}
}
}
v
oidoctLCD( u32n)
{
u8a[15];
s32i=0,x;
i
f(n==0)
charLCD( '0'
);
el
se
{
while(n)
{
x=n%8;
a[i
++]=x+48;
n/=8;
}
for
(--i;
i>=0;i
--
)
charLCD( a[i]
);
}
}
v
oidInit
LCD( )
{
WRI TEBYTE( IODIR0, LCD_ DATA,0XFF)
;
SETBIT(IODI R0,LCD_ RS) ;
SETBIT(IODI R0,LCD_ EN) ;
//SETBIT(IODI R0,LCD_ RW) ;
delay_ms( 15);
cmdLCD( 0x30) ;
delay_ms( 5);
cmdLCD( 0x30) ;
delay_us(100);
cmdLCD( 0x30) ;
cmdLCD( MODE_ 8BIT_ 2LINE);
cmdLCD( SFT_ CUR_ RGT) ;
cmdLCD( DI SP_ ON_ CUR_ BLK);
cmdLCD( CLRLCD) ;
}
***
*****
***
* *
** *
***
****
******
****
**********
***
****
****
*****
****
***
***
***
***
***
***
***
***
***
***
***
1.Writ
eanECPt odevelopthedriverfor16*2al
phanumer i
cLCD.
a)Todisplayacharacter
b)Todisplayastri
ng
c)Todisplayanint
eger
d)Todisplayfl
oatnumberupt othreedeci
mal pl
aces.
e)Todisplayanycustom character.
//assign1_lcd_main.
c
#i
nclude" l
cd.h"
#i
nclude" l
cd_def i
nes.
h"
main( )
{
Init
LCD( )
;
charLCD( '
A');
charLCD( ''
);
strLCD("mahesh" )
;
charLCD( ''
);
u32LCD( 23) ;
charLCD( ''
);
cmdLCD( GOTO_ LI
NE2_POS0)
;
s32LCD( -12456);
charLCD( ''
);
f32LCD( 123.456,
3);
}
***
******
*****
*****
*****
* *
** *
****
***
***
****
****
***
*****
****
* *
******
*****
****
*****
*****
***
2.Writeaprogram todisplaythemessage“VECTOR”ont hefir
stLineand“Inst
it
ute”
onthesecondl i
neofa2x16LCD.Thenmake“ I
nsti
tut
e”flashattherateof1secfor5
ti
mes, t
hencleartheLCDscr een
//assi gn2_lcd_
mai n.
c
#i
nclude" types.h"
#i
nclude" l
cd. h"
#i
nclude" l
cd_ defines.
h"
#i
nclude" delay .
h"
main( )
{
s32i ;
Init
LCD( ) ;
strLCD( "VECTOR" );
for(i
=0;i<5;i
++)
{
cmdLCD( GOTO_ LI
NE2_POS0)
;
strLCD( "Inst
it
ute")
;
del ay_s(1);
cmdLCD( GOTO_ LI
NE2_POS0)
;
strLCD( " "
);
del ay_s(1);
}
cmdLCD( CLRLCD) ;
whi l
e(1);
}
***
***
***
***
***
***
****
****
***
*****
***
***
***
****
***
*****
***
*****
***
***
***
***
***
***
***
***
***
***
3.Wri
teanECPtorot
ateastri
ngonLCD(From r
ightt
oleft
)
//assi
gn3_main_
lcd.
c
#i
ncl
ude"
types.h"
#i
ncl
ude"
lcd.h"
#i
ncl
ude"
lcd_defi
nes.
h"
#i
ncl
ude"
delay.h"
mai
n( )
{
s32i ;
Init
LCD();
while(1)
{
for(
i=10;i
>=0;
i-
-)
{
cmdLCD( GOTO_ LI
NE1_
POS0+i
);
str
LCD( "
vector
");
delay_ms(500)
;
cmdLCD( GOTO_ LI
NE1_
POS0+i
);
str
LCD( " "
);
}
}
}
***
****
* *
*****
***
** *
*****
* *
******
***
***
***
***
***
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
4.Writ
eanECPdi splayt
hebasi cti
me(HH:MM:SS)onLCD.
Note:don’
tuseRTCr egist
ers
***
***
***
***
***
***
***
***
***
****
***
****
****
***
****
****
***
***
***
***
***
***
***
***
***
***
***
***
**
5.Wri
teanECPtodi
spl
aytheswit
chpresscountonLCD.
//assi gn5_main_l
cd.
c
#i
nclude" types. h"
#i
nclude" l
cd. h"
#i
nclude" l
cd_ def i
nes. h"
#i
nclude" delay .
h"
#i
nclude" def i
nes. h"
#i
nclude<l pc21xx. h>
#defineAL_ SW 0
main()
{
u32cnt =0;
I
nitLCD( )
;
while(1)
{
if(READBI T(IOPIN0,
AL_SW)==0)
{
while( READBIT(I
OPIN0,
AL_SW)
==0)
;
delay _ms( 2)
;
cnt++;
cmdLCD( CLRLCD);
u32LCD( cnt);
}
}
}
***
***
***
****
***
***
*****
* *
* *
*****
***
*****
***
****
***
***
*****
***
***
***
* *
***
****
***
***
***
**
6.Wri
teanECPtotake10number srandomlyi
nanarrayandfi
ndthepri
menumbersin
t
heli
stofnumberstodisplayonLCD.
Not
e:mustuserand()functi
on
/
/assi gn6_main_
lcd.
c
#incl
ude" ty
pes. h"
#incl
ude" l
cd.h"
#incl
ude" l
cd_ def i
nes.
h"
#incl
ude" delay .
h"
#incl
ude" defines.h"
#incl
ude<st dlib.h>
u32ispr i
me( u32n)
{
s32i ;
i
f (
n==2)
r
etur n1;
for(i
=2;i
<n; i
++)
{
i
f(
(n%i )==0)
return0;
}
return1;
}
main( )
{
u32ar r[
10] ;
s32i ;
Init
LCD( );
srand(23) ;
for
(i
=0;i<10;i
++)
{
arr
[i]
=rand()%100;
}
for
(i
=0;i<10;i
++)
{
u32LCD( arr
[i
]);
charLCD( '
')
;
i
f(i
==4)
cmdLCD( GOTO_ LI
NE2_
POS0)
;
}
del
ay _
s(2);
cmdLCD( CLRLCD) ;
for
(i
=0;i<10;i
++)
{
i
f(i
sprime(arr[
i])
)
{
u32LCD( arr[
i]
);
charLCD( ''
);
}
}
whil
e(1);
}
***
****
* *
***
****
****
*****
* *
*****
******
*****
* *
*****
***
***
***
***
******
***
****
***
****
* *
***
**
7.Writ
eanECPt ot
aketen3- di
gitnumbersr andomlyi
nanarrayandfi
ndpali
ndrome
numbersinthel
istofnumber stodisplayonLCD.
Note:mustuserand()funct
ion
#incl
ude" ty
pes. h"
#incl
ude" l
cd. h"
#incl
ude" l
cd_ def i
nes.h"
#incl
ude" delay.h"
#incl
ude" defines.h"
#incl
ude<st dlib.h>
u32ispal indr ome( u32n)
{
s32t emp=n, x=0;
whi le(temp)
{
x=( x*10) +t
emp%10;
temp/ =10;
}
i
f (
x==n)
retur n1;
else
retur n0;
}
i
ntmai n()
{
u32ar r[6];
s32i ;
InitLCD( );
srand( 23) ;
for(i=0;i<6;i
++)
{
arr
[i]
=rand()%1000;
}
for
(i
=0;i<6;
i++)
{
u32LCD( arr[
i]
);
charLCD( '
');
i
f(i
==2)
cmdLCD( GOTO_ LI
NE2_
POS0)
;
}
del
ay _
s(2);
cmdLCD( CLRLCD) ;
for
(i
=0;i<6;
i++)
{
i
f(i
spalindrome(arr[
i]
))
{
u32LCD( arr[
i]
);
charLCD( ''
);
}
}
whil
e(1);
}
***
***
***
***
***
***
***
*****
***
*****
***
****
*****
****
***
* *
****
***
***
****
***
***
***
***
*****
***
*
8.Wri
teanECPtocheckwhethergi
venstr
ingispal
indromeornotanddi
spl
ayresul
ton
LCD.
#incl
ude"ty
pes. h"
#incl
ude"l
cd.h"
#incl
ude"l
cd_ defines.h"
#incl
ude"delay.h"
#incl
ude"defines.h"
#incl
ude<string.h>
u32ispali
ndrome( s8* p)
{
s32i,
j;
j
=strl
en(p)-1;
for
(i
=0;i
<j ;
i++,j
--
)
{
i
f(
p[ i
]!
=p[ j
])
return0;
}
ret
urn1;
}
i
ntmai n()
{
s8a[ 20]="
madam" ;
InitLCD();
strLCD(a);
cmdLCD( GOTO_LINE2_POS0)
;
i
f (
ispali
ndrome(a))
{
st
rLCD("pal
i
ndrome");
}
else
st
rLCD("notapali
ndrome"
);
whi
l
e(1)
;
}
*****
* *
******* *
******** *
* *
** *
******
****
***
*****
***
*****
****
* *
***
* *
******
***
****
***
* *
* *
***
*
9.Wr i
teapr ogram t odi splaythemessage“ VECTOR”ont hefi
rstLineand“I
nsti
tute”
ont hesecondl ineofa2x16LCD.Thenmake“ I
nsti
tut
e”scrollf
rom ri
ghttol
efton
secondl i
neofLCDscr een.
#incl
ude" types.h"
#incl
ude" l
cd. h"
#incl
ude" l
cd_ defines.h"
#incl
ude" delay .
h"
#incl
ude" defines.h"
i
ntmai n()
{
s32i ;
Ini
tLCD( );
cmdLCD( GOTO_ LINE1_ POS0+5);
strLCD( "VECTOR" );
whi l
e(1)
{
for(i
=7; i
>=0;i-
-)
{
cmdLCD( GOTO_ LINE2_POS0+i)
;
strLCD("Inst i
tute")
;
delay_ms( 100);
cmdLCD( GOTO_ LINE2_POS0+i)
;
strLCD(" ")
;
}
}
}
***
*** ******
***** *
* ***
*****
**********
**********
***** ****
*********
* *
******
****
* *
*** *
***
****
10.Wr iteanECPt oconv ertfi
rstcharacterofeachwor di ntouppercaseanddi spl
aythe
final r
esultonLCD.
Not e:Connectt woswi tches.Basedonf ir
stswi tchpress,take“ v
ectorindia”asinputand
dot heoper ation;Basedonsecondswi tchpr ess, t
ake“ vectorHyderabad”asi nputanddo
theoper ation.I nbothcasespr i
ntinputandout putonl cdscr een.
Out put:
WhenSwi tch1i spressed, di
splay“vectorindia”ont he1stlineofLCDandwhent he
swi tch1isr eleaseddisplay“Vect orIndi
a”ont he2ndl i
neofLCD.
WhenSwi tch2i spressed, di
splay“vectorhy der abad”ont he1stl i
neofLCDandwhen
theswi t
ch2i sr el
easeddi splay“VectorHy der abad”ont he2ndl i
neofLCD.
Ifnoneoft heswi t
chispr essed,display“waitingf orinput”ont he1stli
neofLCD.
#incl
ude"types.h"
#incl
ude"lcd.h"
#incl
ude"lcd_defines.h"
#incl
ude"def i
nes.h"
#incl
ude<lpc21xx. h>
#incl
ude<st r
ing.h>
#defi
neAL_ SW10
#defi
neAL_ SW21
voidconvert_lower _
to_upper
(s8*
p)
{
i
f((
p[0]>=97) &&p[0]
<=122)
p[0]=p[
0] -
32;
whi
l
e(p=str
chr(p,''
))
{
i
f((
p[1]
>=97) &&p[1]
<=122)
p[
1]=p[1]-
32;
p++;
}
}
i
ntmain()
{
s8s1[ ]="vectorindia";
s8s2[ ]="vectorhy derabad" ;
s8s3[ 50],s4[50];
str
cpy (s3,s1);
str
cpy (s4,s2);
conv ert_
lower _to_upper (s3);
conv ert_
lower _to_upper (s4);
Ini
tLCD( );
cmdLCD( GOTO_ LINE1_ POS0) ;
str
LCD( "
wai ti
ngf orinput ");
while(1)
{
if(READBI T(IOPI N0,AL_ SW1) ==0)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
strLCD( s1);
whi l
e(READBI T(IOPIN0,AL_SW1)==0)
;
cmdLCD( GOTO_ LINE2_POS0);
strLCD( s3);
}
elsei f(
READBI T(IOPI N0,AL_SW2)==0)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
strLCD( s2);
whi l
e(READBI T(IOPIN0,AL_SW2)==0)
;
cmdLCD( GOTO_ LINE2_POS0);
strLCD( s4);
}
}
}
***
*********
***
** *
****
* *
*****
* *
***
*****
* *
***
***
****
***
***
*****
****
***
***
****
***
***
***
***
**
11.Wr it
eanECPt odisplaytheASCIItabl
einf
ormati
onforA-Z,
a-z&0-9onLCD
screenwithrespectt o5seconds.
Forexampl e,
1stline:
AD H O
2ndl i
ne:A6541101
After5secs
1stline:
ADHO
2ndl i
ne:B6642102andsoon….
#i
ncl
ude"
types.h"
#i
ncl
ude"
lcd.
h"
#i
ncl
ude"
lcd_defi
nes.
h"
#i
ncl
ude"defines.h"
#i
ncl
ude<lpc21xx. h>
#i
ncl
ude<string.h>
#i
ncl
ude"delay.h"
i
ntmain()
{
s8ch;
Ini
tLCD( )
;
cmdLCD( GOTO_ LINE1_POS0);
str
LCD( "A D H O" );
cmdLCD( GOTO_ LINE1_POS0);
for
(ch=48; ch<=122; ch++)
{
cmdLCD( GOTO_ LINE2_
POS0);
charLCD( ch);
charLCD( ''
);
u32LCD( ch);
charLCD( ''
);
hexLCD( ch) ;
charLCD( ''
);
octLCD( ch) ;
delay_s(5) ;
cmdLCD( GOTO_ LINE2_
POS0);
strLCD( " "
);
if
(ch==57)
ch=64;
elseif(ch==90)
ch=96;
}
while(
1) ;
}
*****
******
****
****
***
* *
*****
***
*****
***
* *
***
****
****
* *
***
***
***
***
***
***
****
***
*****
***
***
12.Wr i
teanECPt odisplayt
hebinaryval
ueforA-Z,a-
z&0-9onLCDscreenwith
respectto5seconds.
Forexampl e,
1stli
ne:A
2ndl i
ne:01000001
After5secs
1stli
ne:B
2ndl i
ne:01000010andsoon….
#i
ncl
ude"types.h"
#i
ncl
ude"l
cd.h"
#i
ncl
ude"l
cd_ defines.
h"
#i
ncl
ude"defines.h"
#i
ncl
ude<lpc21xx. h>
#i
ncl
ude<string.h>
#i
ncl
ude"delay.h"
i
ntmain()
{
s8ch;
Ini
tLCD(
);
cmdLCD(GOTO_
LINE1_
POS0)
;
for
(ch=48;ch<=122;ch++)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_
POS0)
;
charLCD(ch);
cmdLCD( GOTO_ LINE2_
POS0)
;
binLCD(ch);
delay_
s(5);
i
f(ch==57)
ch=64;
el
sei f
(ch==90)
ch=96;
}
cmdLCD( CLRLCD) ;
whil
e(1);
}
***
*** *
***********
* ***
* ***
***********
**********
************
*** ***
* *
******
****
* *
*******
****
13.Wr it
eanECPt oconv ertfi
rstandl astcharacterofeachwor di ntouppercaseand
displaythef i
nal resultonLCD.
Not e:Connectt woswi tches.Basedonf ir
stswi t
chpress, take“ v
ectorindia”asinputand
dot heoper ation;Basedonsecondswi tchpr ess,take“v ectorHyderabad”asi nputanddo
theoper ati
on.I nbot hcasespr i
ntinputandout putonl cdscr een.
Output :
WhenSwi t
ch1i spr essed, di
splay“vectorindia”ont he1stl ineofLCDandwhent he
switch1i sreleaseddi splay“Vect oRIndiA”ont he2ndl ineofLCD.
Embedded- CAssi gnment s
WhenSwi t
ch2i spr essed, di
splay“vectorhy derabad”ont he1stl i
neofLCDandwhen
theswi t
ch2i sr el
easeddi splay“VectoRHy derabaD”ont he2ndl ineofLCD.
Ifnoneoft heswi tchispr essed,display“waitingforinput ”ont he1stli
neofLCD.
#incl
ude" types. h"
#incl
ude" l
cd. h"
#incl
ude" l
cd_ def i
nes. h"
#incl
ude" def i
nes. h"
#incl
ude<l pc21xx. h>
#incl
ude<st ring. h>
#defineAL_ SW10
#defineAL_ SW21
voidconv ert_lower _to_ upper (s8* p)
{
s32l en;
l
en=st rlen( p)-1;
i
f (
(p[0]>=97) &&( p[0]<=122) )
p[0] =p[ 0]-
32;
i
f (
(p[l
en] >=97) &&( p[l
en] <=122) )
p[len] =p[ l
en] -
32;
while(p=st rchr (p,''
) )
{
if((p[1]>=97) &&( p[1]<=122) )
p[1]=p[ 1]-32;
if((p[-1]>=97) &&( p[-
1] <=122))
p[-1]=p[ -
1] -
32;
p++;
}
}
i
ntmain()
{
s8s1[ ]="vectorindia";
s8s2[ ]="vectorhy derabad" ;
s8s3[ 50],s4[50];
str
cpy (s3,s1);
str
cpy (s4,s2);
conv ert_
lower _to_upper (s3);
conv ert_
lower _to_upper (s4);
Ini
tLCD( );
cmdLCD( GOTO_ LINE1_ POS0) ;
str
LCD( "
wai ti
ngf orinput ");
while(1)
{
if(READBI T(IOPI N0,AL_ SW1) ==0)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
strLCD( s1);
whi l
e(READBI T(IOPIN0,AL_SW1)==0)
;
cmdLCD( GOTO_ LINE2_POS0);
strLCD( s3);
}
elsei f(
READBI T(IOPI N0,AL_SW2)==0)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
strLCD( s2);
whi l
e(READBI T(IOPIN0,AL_SW2)==0)
;
cmdLCD( GOTO_ LINE2_POS0);
strLCD( s4);
}
}
}
***
*** *
******
* ***
****
* *
* **
************
*********
********** *
*****
******** *
*****
******
*****
***
***
14.Wr it
eanECPt oremov eext raspacesi nagi venst ringanddi splayt hefi
nalresul
tonLCD.
Not e:
Connectt woswi tches.Basedonf ir
stswi t
chpr ess,take“AnAppl eaday”asinput
anddot heoper ation;Basedonsecondswi t
chpr ess, t
ake“v ect orstu!”asinputanddo
theoper ati
on.Inbot hcasespr i
ntinputandout putonl cdscr een.
Output:
WhenSwi t
ch1ispr essed, di
splay“AnAppl eaday ”onthe1stl ineofLCDandwhent he
switch1i srel
easeddi splay“ AnAppl eaday ”ont he2ndl i
neofLCD.
WhenSwi t
ch2ispr essed, di
splay“v ectorstu! ”ont he1stlineofLCDandwhent he
switch2i srel
easeddi splay“ vect
orst u!”ont he2ndl i
neofLCD.
Ifnoneoft heswi tchispr essed,display“waitingf orinput”ont he1stl i
neofLCD.
#i
ncl
ude"types.h"
#i
ncl
ude"l
cd. h"
#i
ncl
ude"l
cd_ defines.
h"
#i
ncl
ude"def i
nes.h"
#i
ncl
ude<lpc21xx. h>
#i
ncl
ude<string.h>
#def
ineAL_ SW10
#def
ineAL_ SW21
v
oidremov e_ext r
a_ spaces( s8* p)
{
while(p[0]==' '
)
memmov e(p,p+1, str
len(p+1) +1);
while(p=st rchr(p,'')
)
{
while(p[1]==' ')
memmov e(p+1,p+2, str
len(p+2)+1);
p++;
}
}
i
ntmain()
{
s8s1[ 50] ="vectori ndia";
s8s2[ 50] ="vectorhy der abad" ;
s8r s1[50] ,
rs2[50] ;
str
cpy (rs1,s1) ;
str
cpy (rs2,s2) ;
remov e_ extra_spaces( rs1);
remov e_ extra_spaces( rs2);
Ini
tLCD( );
cmdLCD( GOTO_ LI NE1_ POS0) ;
str
LCD( "wai t
ingf orinput ");
while(1)
{
i
f(READBI T(IOPI N0,AL_ SW1) ==0)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0) ;
strLCD( s1);
whi le(READBI T(IOPIN0,AL_SW1) ==0)
;
cmdLCD( GOTO_ LINE2_POS0) ;
strLCD( rs1);
}
el
sei f(READBI T(IOPI N0,AL_SW2) ==0)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0) ;
strLCD( s2);
whi le(READBI T(IOPIN0,AL_SW2) ==0)
;
cmdLCD( GOTO_ LINE2_POS0) ;
strLCD( rs2);
}
}
}
*
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
4*4key pad
*****
*****
//
kpm_def
ines.h
#def
ineROW016
#def
ineROW117
#def
ineROW218
#def
ineROW319
#def
ineCOL020
#def
ineCOL121
#def
ineCOL222
#def
ineCOL323
//kpm.h
#incl
ude"t
ypes.h"
voidI
nit
kpm( voi
d);
u32colscan(v
oid);
u32keyscan(void)
;
//kpm_ mai n.
c
#i
ncl
ude" types. h"
#i
ncl
ude" kpm. h"
#i
ncl
ude" l
cd. h"
#i
ncl
ude" l
cd_ def ines.h"
i
ntmain()
{
u32key p;
I
nitLCD( );
I
nitkpm( ) ;
while(1)
{
key p=key scan( )
;
whi le( colscan()==0)
{
cmdLCD( GOTO_LI
NE1_
POS0)
;
u32LCD( keyp);
}
}
}
//kpm. c(type1)
#incl
ude" types. h"
#incl
ude" def ines.h"
#incl
ude<l pc21xx. h>
#incl
ude" kpm_ defines.h"
#incl
ude" del ay.h"
u8kpm_ l
ut[4][4]={1,2,3,
4,
5,6,
7,8,
9,
10,
11,
12,
13,
14,
15,
16}
;
voidInitkpm( void)
{
SETBI T( IODI R1, ROW0);
SETBI T( IODI R1, ROW1);
SETBI T( IODI R1, ROW2);
SETBI T( IODI R1, ROW3);
//coumnpi nsbydef aulti
nputpi
ns
}
u32col scan( v oid)
{
u32t 0,t1, t
2, t
3;
t0=READBI T(I
OPI N1,COL0);
t1=READBI T(I
OPI N1,COL1);
t2=READBI T(I
OPI N1,COL2);
t3=READBI T(I
OPI N1,COL3);
return( t0&t 1&t 2&t3)
;
}
u32keyscan( voi
d)
{
u32r no,cno;
whi l
e( colscan())
;
SCLRBI T(IOCLR1,ROW0) ;
SSETBI T(IOSET1,ROW1) ;
SSETBI T(IOSET1,ROW2) ;
SSETBI T(IOSET1,ROW3) ;
if
(!colscan( ))
{
rno=0;
got ocolcheck;
}
SSETBI T(IOSET1,ROW0) ;
SCLRBI T(IOCLR1,ROW1) ;
SSETBI T(IOSET1,ROW2) ;
SSETBI T(IOSET1,ROW3) ;
if
(!colscan( ))
{
rno=1;
got ocolcheck;
}
SSETBI T(IOSET1,ROW0) ;
SSETBI T(IOSET1,ROW1) ;
SCLRBI T(IOCLR1,ROW2) ;
SSETBI T(IOSET1,ROW3) ;
if
(!colscan( ))
{
rno=2;
got ocolcheck;
}
SSETBI T(IOSET1,ROW0) ;
SSETBI T(IOSET1,ROW1) ;
SSETBI T(IOSET1,ROW2) ;
SCLRBI T(IOCLR1,ROW3) ;
if
(!colscan( ))
{
rno=3;
got ocolcheck;
}
colcheck:
SCLRBI T(IOCLR1,ROW0) ;
SCLRBI T(IOCLR1,ROW1) ;
SCLRBI T(IOCLR1,ROW2) ;
SCLRBI T(IOCLR1,ROW3) ;
if
(READBI T(
IOPIN1,COL0)==0)
cno=0;
elsei f(READBI T(I
OPIN1,
COL1)==0)
cno=1;
elsei f(READBI T(I
OPIN1,
COL2)==0)
cno=2;
elsei f(READBI T(I
OPIN1,
COL3)==0)
cno=3;
returnkpm_ l
ut[
rno][
cno]
;
}
//kpm. c( type2)
#incl
ude" types. h"
#incl
ude" defines. h"
#incl
ude<l pc21xx. h>
#incl
ude" kpm_ def i
nes. h"
#incl
ude" delay.h"
u8kpm_ l
ut [
4][4] ={ 1,2,3,
4,5,
6,7,8,9,
10,
11,12,
13,
14,
15,
16}
;
voidInitkpm( void)
{
WRI TENI BBLE( IODI R1,ROW0, 15);
//coumnpi nsbydef aultinputpins
}
u32col scan( void)
{
u32t ;
t=READNI BBLE( I
OPI N1,COL0) ;
t=(t==15) ?1: 0;
retur nt ;
}
u32key scan( voi d)
{
u32r no,cno;
whi le(colscan( ));
WRI TENI BBLE( IOPI N1,ROW0, 14);
i
f(!col scan( ))
{
rno=0;
got ocol check;
}
WRI TENI BBLE( IOPI N1,ROW0, 13);
i
f(!col scan( ))
{
rno=1;
got ocol check;
}
WRI TENI BBLE( IOPI N1,ROW0, 11);
i
f(!col scan( ))
{
rno=2;
got ocol check;
}
WRI TENI BBLE( IOPI N1,ROW0, 7);
i
f(!col scan( ))
{
rno=3;
got ocol check;
}
colcheck:
WRI TENI BBLE( IOPI N1,ROW0, 0);
i
f(READBI T( IOPI N1,COL0) ==0)
cno=0;
elsei f(READBI T(IOPIN1, COL1)==0)
cno=1;
elsei f(READBI T(IOPIN1, COL2)==0)
cno=2;
elseif
(READBIT(I
OPIN1,
COL3)
==0)
cno=3;
retur
nkpm_ l
ut[
rno][
cno]
;
}
*****
* *
*** *
** *
***** *****
*********
****
*****
** *
* *
*****
*****
***
***
***
***
****
***
***
****
*****
***
***
***
*
1.Wr i
teanECPf ordi splayingbinaryequivalentofakeyValuefr
om 4X4matri
xkeypad
on8- LEDS( 4-activehighl eds&4-acti
vel owleds).
#incl
ude" types.h"
#incl
ude" kpm. h"
#incl
ude" def i
nes.h"
#incl
ude<l pc21xx. h>
#defineAL_ LED0 / /p0.0-p0.3
#defineAH_ LED4 / /p0.4-p0.7
i
ntmai n()
{
u32key p;
Ini
tkpm( )
;
WRI TEBYTE( IODI R0,AL_LED,255);
while(1)
{
keyp=key scan();
while(colscan( )
==0)
{
WRI TEBYTE( IOPIN0,AL_LED,
key p^0x0f)
;
}
}
}
*****
* *
************ **** *
*******
*****
* *
*****
******
********
*******
* *
******
*****
********
***
***
***
***
*
2.Wr i
teanECPf ordi splayingakeyVal uef r
om 4X4mat ri
xkey padon2- multi
plexed
sevensegmentdi splays.
#incl
ude" types.h"
#incl
ude" kpm. h"
#incl
ude" defines.h"
#incl
ude<l pc21xx. h>
#incl
ude" delay.h"
#defineCA7_ DATA_ PINS0 / /P0.0-P0.7
#defineDSEL18
#defineDSEL29
constu8seg_ l
ut[]={0xc0, 0xf9,
0xa4,0xb0,
0x99,0x92,0x82,0xf
8,0x80,0x90};
i
ntmai n()
{
u32key p;
Ini
tkpm( );
WRI TEBYTE( IODI R0,CA7_DATA_ PINS,255);
SETBI T(IODI R0,DSEL1) ;
SETBI T(IODI R0,DSEL2) ;
while(1)
{
key p=key scan( );
whi le(colscan( )
==0)
{
SSETBI T(IOSET0, DSEL1) ;
WRI TEBYTE( IOPIN0,CA7_DATA_ PINS,seg_l
ut[keyp/10])
;
del
ay_ms(1);
SCLRBIT(
IOCLR0,
DSEL1)
;
SSETBIT(
IOSET0,
DSEL2)
;
WRITEBYTE(IOPI
N0,
CA7_DATA_
PINS,
seg_
lut
[key
p%10]
);
del
ay_ms(1);
SCLRBIT(
IOCLR0,
DSEL2)
;
}
}
}
******************* ***
* ****
* ************
*****************
** ***
* *
** *
*******
****
***
***
***
***
***
***
***
3.Wr it
eanECPf ori mpl ement ingabasi ccal culatorusing4X4mat r
ixkeypadand2-
mul tiplexedsev ensegmentdi splays.
#include" types.h"
#include" kpm. h"
#include" defines.h"
#include<l pc21xx. h>
#include" delay.h"
#def i
neCA7_ DATA_ PI NS0 / /P0.0-P0.7
#def i
neDSEL18
#def i
neDSEL29
constu8seg_ l
ut[]={0xc0,0xf 9,0xa4,0xb0,0x99,0x92, 0x82,0xf8,0x80,0x90};
/**keypad_ l
utshoul dbei nt hisf orm, u8kpm_ l
ut [
4][
4]={1,2,3,
'+'
,
4,5,6,
'-
',
7,8,9,
'*'
,
'#',
0,'
/'
,'
='
};*
**/
i
ntmai n()
{
u32op1, op2,op, r
esul t
=0, i;
Initkpm( );
WRI TEBYTE( IODI R0,CA7_ DATA_ PINS,255) ;
SETBI T(IODI R0,DSEL1) ;
SETBI T(IODI R0,DSEL2) ;
whi le(1)
{
op1=key scan( )
;
op=key scan( );
op2=key scan( )
;
swi tch(op)
{
case' +':
r esult=op1+op2;
break;
case' -'
:result=op1- op2;
break;
case' *':
result=op1* op2;
break;
case' /'
:
result=op1/ op2;
break;
}
for (
i=0;i
<500; i++)
{
SSETBI T( I
OSET0, DSEL1) ;
WRI TEBYTE( I
OPI N0, CA7_ DATA_ PINS,seg_ l
ut[result/10]);
delay_ ms( 1);
SCLRBI T(I
OCLR0, DSEL1) ;
SSETBI T(I
OSET0, DSEL2) ;
WRITEBYTE(I
OPIN0,
CA7_DATA_
PINS,
seg_
lut
[r
esul
t%10]
);
del
ay_ms(1)
;
SCLRBIT(
IOCLR0,
DSEL2)
;
}
}
}
***
****
* *
*****
***
* *
******
***
***
****
*****
***
***
***
*****
***
***
***
***
***
****
*****
***
***
***
****
***
***
**
4.Writ
eanECPf orimplement
ingabasiccal
cul
atorusi
ng4X4matri
xkeypadand16x2
l
cd.
Note:1+2=3,3*8=24,3-
6=-
3etc..
.
#i
nclude" types.h"
#i
nclude" kpm. h"
#i
nclude" l
cd_ defines. h"
#i
nclude" l
cd.h"
#i
nclude" defines.h"
#i
nclude<l pc21xx. h>
#i
nclude" delay.h"
/*
*keypad_ l
utshoul dbei nt hisform,u8kpm_
lut
[4]
[4]
={1,
2,
3,
'+'
,
4,
5,6,
'-
'
,
7,
8,9,
'*
',
'
#'
,0,'
/'
,
'='
};
***
/
i
ntmai n()
{
u32op1, op2,op, r
esul t=0;
Initkpm( );
InitLCD( );
cmdLCD( GOTO_ LINE1_ POS0) ;
whi le(1)
{
op1=key scan( )
;
cmdLCD( CLRLCD) ;
u32LCD( op1) ;
op=key scan( );
char LCD( op) ;
op2=key scan( )
;
u32LCD( op2) ;
swi tch(op)
{
case' +'
:result=op1+op2;
break;
case' -
':
result=op1- op2;
break;
case' *'
:
resul t=op1* op2;
break;
case' /'
:
resul t=op1/ op2;
}
char LCD( keyscan()
);
s32LCD( r esult);
}
}
***
****
* *
*****
*****
****
******
****
****
*********
******
*****
***
***
***
***
***
***
****
***
***
***
***
*****
***
5.Writ
eanECPf orimplementi
ngmul ti
digitcalcul
atorusi
ng4X4matr
ixkey
padand16x2lcd.
Note:123+23=146,33*38=1254,345-627=-282etc…
#i
nclude" types. h"
#i
nclude" kpm. h"
#i
nclude" l
cd_ def ines.h"
#i
nclude" l
cd. h"
#i
nclude" defines. h"
#i
nclude<l pc21xx. h>
#i
nclude" delay .h"
/*
*keypad_ l
utshoul dbei nt hisform,u8kpm_
lut
[4]
[4]
={1,
2,
3,
'+'
,
4,
5,6,
'-
'
,
7,
8,9,
'*
',
'
#'
,0,'
/'
,
'='
};
***
/
i
ntmai n()
{
u32key p,op1, op2,op,resul t
=0;
Initkpm( ) ;
InitLCD( );
whi le(1)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LI NE1_POS0);
op1=0;
op2=0;
whi le( (
key p=key scan( ))
<=9)
{
u32LCD( keyp) ;
op1=( op1* 10) +keyp;
}
char LCD( keyp);
op=key p;
whi le( (
key p=key scan( ))
<=9)
{
u32LCD( keyp) ;
op2=( op2* 10) +keyp;
}
charLCD( keyp);
swi tch( op)
{
case' +'
:resul t=op1+op2;
br eak;
case' -'
:
resul t=op1-op2;
br eak;
case' *'
:r
esul t=op1* op2;
br eak;
case' /'
:r
esul t=op1/ op2;
}
s32LCD( result);
}
}
***
****
* *
***
****
***
***
****
****
*****
*****
*****
*****
*****
***
****
****
*****
****
***
****
****
****
***
***
***
6.Writ
eanECPt oaccessadevice(door/
fan/bul
b)withavali
dpasswordentryfr
om 4x4
matri
xkeypad.
Note:musti
ncl
udeallt
estcaseswhichar egi
vingsuppor
tforreal
-t
imeappli
cati
ons
***
***
****
*****
***
****
*****
***
***
*****
****
***
***
*****
***
***
***
***
***
****
*****
***
***
***
***
* *
***
***
**
7.Wri
teanECPt odesignamenu-basedcal
cul
atorusi
ng4x4matr
ixkeypadand16X2lcd.
Menu:1.Addi
ti
on2.Subtr
acti
on….etc
#i
nclude" types. h"
#i
nclude" kpm. h"
#i
nclude" l
cd_ def ines. h"
#i
nclude" l
cd. h"
#i
nclude" defines. h"
#i
nclude<l pc21xx. h>
#i
nclude" delay .h"
/*
*keypad_ l
utshoul dbei nthisform,u8kpm_
lut
[4]
[4]
={1,
2,
3,
'+'
,
4,
5,6,
'-
'
,
7,
8,9,
'*
',
'
#'
,0,'
/'
,
'='
};
***
/
i
ntmai n()
{
u32key p,op1, op2, op,result
=0;
Initkpm( ) ;
InitLCD( );
whi le(1)
{
op1=0;
op2=0;
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
st r
LCD( "1. add,2.sub");
cmdLCD( GOTO_ LINE2_POS0);
st r
LCD( "ent erchoi ce:"
);
op=key scan( );
u32LCD( op) ;
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
st r
LCD( "ent eroper and1");
cmdLCD( GOTO_ LINE2_POS0);
whi le( (
key p=key scan())
<=9)
{
u32LCD( keyp);
op1=( op1* 10)+keyp;
}
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0);
st r
LCD( "ent eroper and2");
cmdLCD( GOTO_ LINE2_POS0);
whi le( (
key p=key scan())
<=9)
{
u32LCD( keyp);
op2=( op2* 10)+keyp;
}
swi tch( op)
{
case1: result=op1+op2;
break;
case2: result=op1-op2;
break;
}
cmdLCD(CLRLCD)
;
cmdLCD(GOTO_LI
NE1_
POS0)
;
u32LCD(r
esul
t)
;
del
ay_s(
2);
}
}
*****
****
***
***
***
****
****
****
****
***
******
***
***
***
*****
***
****
****
***
***
* *
*****
****
***
***
***
***
**
8.Wr i
teanECPtocheckwhethergi
vennumberisprimeornotanddi
splayresul
tonLCD.
Note:usekeypad t
ot akethei nput(mul
tidigi
t)and mustuseI sPri
me()& ReadNum()
functi
ons
***
****
* *
***
****
****
* *
***
***
* *
*****
****
***
****
* *
***
****
****
****
****
***
* *
*****
***
*****
***
***
***
***
**
9.Writ
eanECPt otakeonenumber( mult
idigi
t)fr
om keypadanddisplayi
tsbinar
yequi
val
enton
LCD.
Note:mustuseBinLCD()funct
ion
#include" types.h"
#include" kpm. h"
#include" l
cd_ defi
nes. h"
#include" l
cd. h"
#include" defines.
h"
#include<l pc21xx.h>
#include" delay.h"
/**keypad_ l
utshoul dbei nthi
sform,u8kpm_
lut
[4]
[4]
={1,
2,
3,
'+'
,
4,
5,6,
'-
'
,
7,
8,9,
'*
',
'
#'
,0,'
/'
,
'\n'
};
***
/
u32Readnum( )
{
u32n=0, kp;
whi le((kp=key scan())
<=9)
{
u32LCD( kp);
n=( n*10)+kp;
}
retur nn;
}
i
ntmai n()
{
u32num;
Initkpm( );
InitLCD( );
whi le(1)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0)
;
st r
LCD( "
ent ernumber "
);
cmdLCD( GOTO_ LINE2_POS0)
;
num=Readnum( );
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LINE1_POS0)
;
u32LCD( num) ;
cmdLCD( GOTO_ LINE2_POS0)
;
binLCD( num) ;
del ay_s(
1) ;
}
}
***
*****
*****
***
***
***
****
*****
** *
****
****
***
***
***
*****
****
****
***
***
******
****
****
****
****
***
***
*
10.Writ
eanECPt ocheckwhet hergi
vennumberispal
indr
omeornotanddi spl
ayresul
tonLCD.
Note:
usekeypadtotaketheinputandmustuseIsPal
indr
ome()funct
ion
#include" types.h"
#include" kpm. h"
#include" l
cd_ defines. h"
#include" l
cd.h"
#include" defines.h"
#include<l pc21xx. h>
#include" delay.h"
/**keypad_ l
utshoul dbei nthi
sform,
u8kpm_
lut
[4]
[4]
={1,
2,
3,
'+'
,
4,
5,6,
'-
'
,
7,
8,9,
'*
',
'
#'
,0,'
/'
,
'\n'
};
***
/
u32Readnum( )
{
u32n=0, kp;
whi le((kp=key scan()
)<=9)
{
u32LCD( kp);
n=( n*10) +kp;
}
retur nn;
}
u32I spal i
ndr ome( num)
{
u32t emp, num1=0;
temp=num;
whi le(temp)
{
num1=( num1* 10)+(t
emp%10);
temp/ =10;
}
if(
num==num1)
retur n1;
retur n0;
}
i
ntmai n()
{
u32num;
Initkpm( ) ;
InitLCD( );
whi le(1)
{
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LI
NE1_POS0);
st r
LCD( "ent ernumber")
;
cmdLCD( GOTO_ LI
NE2_POS0);
num=Readnum( )
;
cmdLCD( CLRLCD) ;
cmdLCD( GOTO_ LI
NE1_POS0);
u32LCD( num) ;
cmdLCD( GOTO_LINE2_POS0);
i
f(I
spali
ndrome(num))
str
LCD("i
spali
ndrome")
;
el
se
str
LCD("i
snotpali
ndrome")
;
del
ay _
s(1);
}
}
*****
* *
* *
****** *
*** *
******
* *
******
***
***
*****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
/
/wor kshoppr ogram
passwor dv alidati
onusi ngkey padandlcd
#incl
ude<LPC214X. H>
#incl
ude" defines.h"
#incl
ude" delay .
h"
#incl
ude<st ring.h>
#incl
ude" l
cd_ def i
nes.h"
#incl
ude" l
cd.h"
#incl
ude" ty
pes. h"
#defineSuccuss_ LED16/ /
P0.16
#defineFai l
ur e_LED17/ /P0.17
i
ntmy _str
cmp( s8* ,
consts8* )
;
u8Key Scan( void);
voidInitKPM( void);
constu8Cur r
ent_Passwor d[
]="12345";
i
ntpasswor d(
void);
/*
'
*'-delete_ key
'
#'-ent er_key
*/
i
ntmai n( )
{
i
ntr es,i;
//i
ni t
iali
zether equi
reddev i
cesfort
hisappl
i
cat
ion
I
nitKPM( );
I
nitLCD( )
;
SETBI T( I
ODIR0,Succuss_LED);
SETBI T( I
ODIR0,Fail
ure_LED);
//
Display"Enterpasswor d"on1stl i
neofLCD
Str
LCD( "
Enterpasswor d");
whil
e(1)
{
r
es=passwor d()
;//callpasswor df uncti
onwhi chretur
nssuccussorfai
l
ure
i
f(r
es)
{
CmdLCD( CLEAR_ LCD) ;/
/ClearLCDdi splay
Str
LCD( "val
idpasswor d")
;/
/Display"ValidPassword"onLCD
delay_s(
1) ;
//wai tforsomedel ayforvisuali
zat
ion
/
/ bli
nkt heSuccuss_ Ledf or5times@ 0.5seconds
for
( i
=0;i
<5;i
++)
{
SSETBI T(I
OSET0, Succuss_ LED);
delay_ms(500) ;
SCLRBI T(I
OCLR0, Succuss_ LED);
del
ay_
ms(
500)
;
}
}
el
se
{
CmdLCD( CLEAR_ LCD);//Cl
earLCDdi splay
StrLCD("WrongPasswor d");
//Di
splay"Wr ongPasswor
d"onLCD
delay_s(1);
//wai
tf orsomedel ayforvisuali
zat
ion
//bli
nktheFai l
ure_Ledfor5t imes@ 0. 5seconds
for(i
=0;i
<5;i
++)
{
//bli
nktheSuccuss_ Ledfor5times@ 0. 5seconds
SSETBI T(I
OSET0, Fai
lure_LED);
delay_ms(500) ;
SCLRBI T(I
OCLR0, Fail
ure_LED);
delay_ms(500) ;
}
}
}
}
i
ntpasswor d(voi d)
{
//declaret her equiredv ariables
u8ch;
s8index=0, User _Passwor d[10];
CmdLCD( GOTO_ LI
NE2_ POS0) ;
while(1)
{
ch=Key Scan() ;
//r
eadt hekeyf rom key pad
i
f(ch==' *'
)//delete_keyispr essed
{
//del etelatestkeyi nformat ionf r
om User _ Passwor dar r
ary&onLCD
i
f(index==0)
cont inue;
i
ndex- -;
CmdLCD( GOTO_ LI
NE2_POS0+i ndex);
char LCD( '
');
CmdLCD( GOTO_ LI
NE2_POS0+i ndex);
}
el
sei f(ch==' #'
)//enter_keyispr essedorar raymaxi mum si zereached
{
User _ Passwor d[i
ndex] ='
\ 0';
/ /updatethe' \0' characterattheupdat edi
ndexposi
ti
on
ofUser_Passwor d
break;
}
el
se
{
User _ Passwor d[i
ndex++] =ch; //updatet hekeyv al
uei ntoUser_Passwor
darray
//CmdLCD( GOTO_ LINE2_ POS0) ;
char LCD( '
*');
// displ
ay'*'onl cd
//St rLCD(User _Passwor d);
}
}
i
f(
my_ str
cmp(User
_Passwor
d,Cur
rent
_Passwor
d))
return1;
el
se
return0;
i
ntmy_str
cmp( s8* s1,consts8*s2)
{
i
nti;
i
f(
strl
en(s1)!=strl
en(s2))
r
eturn0;
/
/writ
et hefunct i
onbodyf orst
ri
ngcompar
isi
onwi
thpr
operr
etur
nval
ues
f
or(i
=0;s1[
i]!
='\0'
;i
++)
{
i
f(
s1[i]
!=s2[i]
)
r
eturn0;
}
r
eturn1;
}
/
*ALLTHEBEST*
/
**
*****
****
** *
*** *** *
****
****
****
*****
*****
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
**
**
UARTPROTOCOL
****
*****
****
//uar
t_def ines.h
#def
ineTXD0_ 0_ 0FUN2
#def
ineRXD0_ 0_ 1FUN2
//
div
isorcalculaat ion
#def
ineFOSC12000000
#def
ineCCLKFOSC* 5
#def
inePCLKCCLK/ 4
#def
ineBAUD9600
#def
ineDIVISOR( PCLK/ (
16*BAUD))
//
UOLCRdef i
nes
#def
ineWORD_ LEN_ 83
#def
ineDLAB_ BI T7
//
UOLSRdef i
nes
#def
ineDR_ BIT0
#def
ineTHRE_ BI T5
#def
ineTEMT_ BI T6
**
***
*****
*******
***
***
***
*****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
**
//
uart
_tx
//uar
t_tx.
h
#i
ncl
ude"types.
h"
v
oidIni
tUART(void);
v
oidcharUART_tx(u8);
v
oidstrUART_
tx(s8* )
;
v
oids32UART_tx(s32);
v
oidf32UART_tx(f32,
u32)
;
v
oidhexUART_t
x(u32);
v
oidoctUART_
tx(u32)
;
//uart_t
x.c
#incl
ude<LPC21xx. h>
#incl
ude" defines.h"
#incl
ude" uart_defines.h"
#incl
ude" types.h"
voidInit
UART( )
{
CFGPI N( PINSEL0, 0,
TXD0_ 0_0);
CFGPI N( PINSEL0, 1,
RXD0_ 0_
1);
U0LCR=( 1<<DLAB_ BIT)|WORD_ LEN_8;
U0DLL=DI VISOR;
U0DLM=DI VISOR>>8;
CLRBI T(U0LCR, DLAB_ BIT);
}
voidchar UART_ tx(u8sdat )
{
U0THR=sdat ;
whi l
e(READBI T(U0LSR, TEMT_ BIT)
==0)
;
}
voidstrUART_ t
x(s8* p)
{
whi l
e(*p)
{
char UART_ tx(
*p++) ;
}
}
voids32UART_ t
x(s32n)
{
u8a[ 10];
s32i =0;
i
f(n<0)
{
char UART_ tx(
'-
'
);
n=- n;
}
whi l
e(n)
{
a[i++]=(n%10) +48;
n/ =10;
}
for(-
-i;
i
>=0; i
--
)
{
char UART_ tx(
a[i
]);
}
}
voidf32UART_ tx(f32f ,
u32ndp)
{
u32i ,
j;
i
f(f<0.0)
{
char UART_ tx(
'-
'
);
f=-f;
}
i
=f;
s32UART_ tx(i
);
charUART_ tx(
'.
'
);
for
(j
=0;j<ndp;j
++)
{
f=(
f-i
)*10;
i
=f;
charUART_ t
x(i
+48)
;
}
}
v
oidhexUART_ tx(u32n)
{
s32i=0,nibble;
u8a[8];
i
f(n==0)
charUART_ t
x( '
0');
el
se
{
while(n)
{
nibbl e=n%16;
nibbl e=(ni bble>9)?(
(ni
bbl
e-10)
+'A'
):
(ni
bbl
e+48)
;
a[i++] =nibble;
n/ =16;
}
for
(--i
;i
>=0; i-
-)
{
char UART_ tx(a[
i]
);
}
}
}
v
oidoctUART_ tx(u32n)
{
u8a[15];
s32i=0,x;
i
f(n==0)
charUART_ t
x( '
0');
el
se
{
while(n)
{
x=n%8;
a[i++] =x+48;
n/ =8;
}
for
(--i
;i
>=0; i-
-)
charUART_ t
x( a[i
]);
}
}
//
uart
_tx_mai
n.c
#i
ncl
ude<LPC21xx.h>
#i
ncl
ude"uar
t_t
x.h"
i
ntmain()
{
I
nitUART()
;
charUART_ tx('
A')
;
strUART_tx("vect
r or"
);
s32UART_ tx(1234) ;
f32UART_ tx(123.456,3)
;
hexUART_ tx(6553) ;
octUART_ t
x( 1234) ;
whi l
e(1)
;
}
*****
* *
** **
* *
*************
* *
* *
* *
* *
*******
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
*
//uart_rx
//uart
_ r
x_ main.c
#incl
ude<LPC21xx. h>
#incl
ude" defines.h"
#incl
ude" uart_defines.h"
#incl
ude" delay.h"
#incl
ude" types.h"
#defineOPI N2
voidInitUART( )
{
CFGPI N(PINSEL0, 0,
TXD0_ 0_0);
CFGPI N(PINSEL0, 1,
RXD0_ 0_1);
U0LCR=( 1<<DLAB_ BIT)|WORD_ LEN_8;
U0DLL=DI VISOR;
U0DLM=DI VISOR>>8;
CLRBI T(U0LCR, DLAB_ BIT);
}
voiduar t_t
x(u8sdat )
{
U0THR=sdat ;
whi l
e(READBI T(U0LSR, TEMT_ BIT)
==0);
}
u8uar t_rx()
{
whi l
e(READBI T(U0LSR, DR_ BIT)==0);
returnU0RBR;
}
i
ntmai n( )
{
Ini
tUART( )
;
SETBI T(IODIR0, OPIN);
whi l
e(1)
{
if
(uart_r
x( )
=='A')
CPLBI T(IOPIN0, OPIN) ;
}
}
***
*****
***
* *
** *
***
****
******
***
****
*******
*****
***
***
*****
***
***
***
***
***
***
***
***
***
***
***
***
***
**
**
1.Writ
eanECPt odevelopthedri
verforon-chi
pUART.
a)Todisplayacharacter
b)Todisplayastri
ng
c)Todisplayanint
eger
d)Todisplayfl
oatnumberupt othreedecimalpl
aces.
#i
ncl
ude<LPC21xx. h>
#i
ncl
ude" del
ay.
h"
#i
ncl
ude" uar
t_t
x.h"
i
ntmain()
{
Ini
tUART( )
;
charUART_ tx('
A')
;
delay_s(
1);
strUART_tx("vect
or"
);
delay_s(
1);
s32UART_ tx(1234);
delay_s(
1);
f32UART_ tx(123.
456,3)
;
delay_s(
1);
whi l
e(1)
;
}
*****
* *
******
*******
** *
***
***
****
****
*****
*****
***
***
***
***
*****
***
****
***
*****
***
*****
***
***
***
***
*
2.Wr i
teanECPf ortransmit
ti
ngstri
ngthroughUARTanddispl
ayitonhypert
erminal
with9600baudr ate.
Exampl e:“Welcomet oVECTOR”
#incl
ude<LPC21xx. h>
#incl
ude" del
ay.h"
#incl
ude" uar
t_tx.
h"
i
ntmai n()
{
Ini
tUART( )
;
strUART_ t
x("
wel cometovector
");
whi l
e(1)
;
}
********
* *****************
* *
* *
*** ***
******
***
****
****
*****
****
****
******
***
***
***
****
****
***
***
***
3.Wr it
eanECPf orr eceiv
ingonechar act
erfr
om PCt hr
oughUARTwi t
h9600baudrate
anddi splayi t
sbi naryequi v
al enton8–LEDS( 4–Act i
vehighleds&4–Act i
velow
leds).
#include<LPC21xx. h>
#include"def ines.h"
#include"uar t_defines.h"
#include"del ay.h"
#include"t y
pes. h"
#def i
neAL_ LED3
#def i
neAH_ LED7
voidInitUART( )
{
CFGPI N( PINSEL0, 0,
TXD0_ 0_ 0)
;
CFGPI N( PINSEL0, 1,
RXD0_ 0_ 1)
;
U0LCR=( 1<<DLAB_ BIT)|WORD_ LEN_8;
U0DLL=DI VISOR;
U0DLM=DI VISOR>>8;
CLRBI T(U0LCR, DLAB_ BIT);
}
u8uar t_rx()
{
whi l
e( READBI T(U0LSR, DR_ BIT)==0);
returnU0RBR;
}
i
ntmain()
{
u8ch;
WRI TEBYTE(I
ODIR0,
AL_LED,
255)
;
I
nitUART();
while(
1)
{
ch=uar
t_r
x()
;
WRITEBYTE(IOPI
N0,AL_
LED,
ch^
0x0f
);
}
}
************* *
******* *
** *******
* **
****
*****
*****
* *
*****
*******
***
****
****
***
* *
****
***
***
***
***
***
4.Wr it
eanECPt oi nfinitel
yr eceiveanycharacterthenre-t
ransmiti
tsASCIIval
uein
deci mal andhexf or mat.Out putshouldbeseenasf orexamplebel
ow
Out put :ifReceiv edchar acteris‘
A’,t
henout putshouldbe650x41
IfRecei v edchar act eris‘a’,thenoutputshouldbe970x61
IfRecei v edchar act eris‘Z’,thenoutputshouldbe900x5Aandsoon…
#include<LPC21xx. h>
#include" def i
nes.h"
#include" uar t
_defines.h"
#include" delay.h"
#include" types.h"
#include" uar t
_tx.
h"
u8uar t_rx()
{
whi le(READBI T(U0LSR, DR_ BIT)
==0);
retur nU0RBR;
}
i
ntmai n()
{
u8ch;
InitUART( );
whi le(1)
{
ch=uar t
_rx();
s32UART_ tx(ch) ;
char UART_ tx(''
) ;
hexUART_ tx(ch) ;
}
}
********
*****************
****
* *
******
****
*******
* *
****
***
***
***
***
***
***
***
***
***
****
***
***
***
5.Wr it
eanECPt oi nfi
nit
elyrecei
v eanycharacterfr
om PCthr
oughUARTanddispl
ay
thatcharact eronLCD1stl ine1stposit
ion.
#include<LPC21xx. h>
#include"defines.h"
#include"uart_defines.h"
#include"delay.h"
#include"ty
pes. h"
#include"l
cd.h"
#include"l
cd_ defi
nes. h"
voidInit
UART( )
{
CFGPI N( PINSEL0, 0,
TXD0_ 0_0);
CFGPI N( PINSEL0, 1,
RXD0_ 0_1);
U0LCR=(1<<DLAB_ BI
T)|
WORD_
LEN_
8;
U0DLL=DIVISOR;
U0DLM=DI VISOR>>8;
CLRBIT(
U0LCR, DLAB_BI
T);
}
u8uart
_rx()
{
while(READBIT(U0LSR,
DR_BI
T)==0);
ret
ur nU0RBR;
}
i
ntmain()
{
u8ch;
Ini
tUART( );
Ini
tLCD( )
;
while(1)
{
ch=uart_r
x();
cmdLCD(CLRLCD);
cmdLCD(GOTO_LINE1_POS0)
;
charLCD(ch);
}
}
*****
**** ******
* *
***********
* **
** *
*******
*****
***
***
***
****
***
***
***
***
***
***
****
***
* *
***
***
*
6.Wr i
teanECPt oinfi
nitel
yr eceiveanyst r
ingfr
om PCthr
oughUARTanddispl
aythat
stri
ngonLCD1stl ine1stposi ti
on.
#incl
ude<LPC21xx. h>
#incl
ude" def i
nes.h"
#incl
ude" uar t
_defines.h"
#incl
ude" delay.h"
#incl
ude" types.h"
#incl
ude" lcd.h"
#incl
ude" lcd_defines.h"
s8str[20] ;
voidInitUART( )
{
CFGPI N(PINSEL0, 0,
TXD0_ 0_0);
CFGPI N(PINSEL0, 1,
RXD0_ 0_1);
U0LCR=( 1<<DLAB_ BIT)|WORD_ LEN_8;
U0DLL=DI VISOR;
U0DLM=DI VISOR>>8;
CLRBI T(U0LCR, DLAB_ BIT);
}
u8uar t_rx()
{
whi l
e( READBI T(U0LSR, DR_ BIT)==0);
returnU0RBR;
}
voidstrUART_ rx()
{
u8ch;
s32i =0;
do
{
ch=uar t_r
x();
st
r[i
++]
=ch;
}whil
e(st
r[
i-
1]!
='
\r
')
;
str
[i
-1]
='\
0';
}
i
ntmain()
{
I
nitUART();
I
nitLCD();
while(
1)
{
str
UART_rx(
);
cmdLCD(CLRLCD)
;
cmdLCD(GOTO_LI
NE1_
POS0)
;
str
LCD(st
r);
}
}
************* **
* *
* ******* *
* *
* *
*****
**********
****
*****
*****
****
****
****
***
***
***
***
*****
***
*
7.Wr it
eanECPf ort ransmi tti
ngchar actersA-Zfrom LPC21xxbasedCPUtothehyper
termi nal ofPCt hroughUARTf orev eryswit
chpressf r
om user.
#include<LPC21xx. h>
#include" def i
nes. h"
#include" uart_def ines.h"
#include" delay.h"
#include" types.h"
#defineAL_ SW 3
voidInitUART( )
{
CFGPI N( PINSEL0, 0,TXD0_ 0_
0);
CFGPI N( PINSEL0, 1,RXD0_ 0_
1);
U0LCR=( 1<<DLAB_ BIT)|WORD_ LEN_ 8;
U0DLL=DI VISOR;
U0DLM=DI VI SOR>>8;
CLRBI T( U0LCR, DLAB_ BIT);
}
voidchar UART_ tx(u8sdat )
{
U0THR=sdat ;
whi le(READBI T(U0LSR, TEMT_ BIT)==0);
}
i
ntmai n()
{
u8ch;
InitUART( );
for(ch=65; ch<=90; ch++)
{
char UART_ tx(ch);
del ay_ms( 100) ;
whi le(READBI T( IOPIN0,AL_SW) );
}
whi le(1);
}
***
*****
***
***
***
****
***
***
****
*****
***
***
******
***
******
***
***
****
****
****
****
****
***
***
*
9.Writ
eanECPt odi
spl
aythebelowmentionedpatt
ernsonhyperter
minalwit
hthehel
p
ofUART0wit
h19200baudr at
es.
//patterns
#incl
ude<LPC21xx. h>
#incl
ude" defines.h"
#incl
ude" uart_defines. h"
#incl
ude" delay.h"
#incl
ude" ty
pes. h"
#defineAL_ SW 3
voidIni
tUART( )
{
CFGPI N( PINSEL0, 0,
TXD0_ 0_0);
CFGPI N( PINSEL0, 1,
RXD0_ 0_1);
U0LCR=( 1<<DLAB_ BIT)|WORD_LEN_ 8;
U0DLL=DI VISOR;
U0DLM=DI VISOR>>8;
CLRBI T(U0LCR, DLAB_ BIT)
;
}
voidchar UART_ t
x(u8sdat )
{
U0THR=sdat ;
while(READBI T(U0LSR, TEMT_BIT)==0)
;
}
i
ntmai n()
{
s32r ow, col;
Ini
tUART( );
for(
row=1; r
ow<=5; row++)
{
for(col=1;col <=(5-row+1);
col++)
{
char UART_ t
x( '
*'
);
}
char UART_ tx('\
r'
);
char UART_ tx('\
n');
}
}