Microcontroller Lab
Microcontroller Lab
PROGRAMS
1
a) Write an ALP to move n bits of data from memory location
X to memory location Y
b) Write an ALP to move n bits of data from memory location
X to memory location Y with overlap
c) Write an ALP to exchange n bits of data from memory
location X to memory location Y:
d) Write an ALP to exchange a block of data bytes between
external memory location starting from 500h to internal
memory location starting from 30h:
2
a) Write an ALP to add n bytes numbers:
b)Write an ALP to add two multibyte numbers:
c)Write an ALP to subtract 2 multibytes:
d) Write an ALP to add n 2 digits BCD numbers
e) Write an ALP to divide two bytes
3
a) Write a program to convert a given binary no to BCD
equivalent
b)Write an ALP to convert given packed BCD into binary
equivalent
4
4a) Write an ALP to convert an ASCII number to hex
equivalent
4b) Write an ALP to convert given packed BCD to ASCII
5
5a)Write an ALP for 4:1 mux
5B) Write an ALP to 2: 4 decoder
5C)Write an ALP to realize 8: 3 priority encoder
6
6a) Write an ALP to realize 8 bit binary Up Counter?
6b)Write an ALP to realize 8 bit binary Down Counter
6c) Write an ALP to realize 8 bit BCD Up Counter?
6d) Write an ALP to realize 8 bit BCD Down Counter?
7
Write a C program to interface LCD to 8051
8
Write a C program to interface a stepper motor to 8051
9
Write a C program to interface an seven segment display to
8051
10
Write a C program to interface a DC MOTOR to 8051
11
Write a C program to interface a DAC to 8051
12
Write a C program to interface an ADC to 8051
13
Write a C program to interface an elevator to 8051
14
Write a C program to interface hex keypad to 8051
Algorithm:
1) initialize r0,r1 as pointers for source and destination memory.
2) initialize r2 as counter for no. of bytes in block.
3) move data from source location to accumulator.
4) move data from accumulator to destination location.
5) increment source and destination address.
6) decrement the counter value, repeat steps 3,4,5.
7) end the program.
Program:
src equ 30h
;src =30h
;dst =50h
;cnt=5
mov r0,#src
mov r1,#dst
mov r2,#cnt
;r2 counter
; a ( r0)
mov @r1,a
;(r0) a
inc r0
; (r0) (r0)+1
inc r1
;(r1) (r1)+1
djnz r2,back
;(r2) (r2)-1
;if r2!=0 then go to label back
end
Before execution:
0X30h=11h
0X50h=00h
0X31h=22h
0X51h=00h
0X32h=33h
0X52h=00h
0X33h=44h
0X53h=00h
0X34h=55h
0X54h=00h
After execution :
0X30h=11h
0X50h=11h
0X31h=22h
0X51h=22h
0X32h=33h
0X52h=33h
0X33h=44h
0X53h=44h
0X34h=55h
0X54h=55h
;src =30h
;dst =33h
;cnt=5
mov r2,#cnt
;r2 counter
; a ( r0)
mov @r1,a
;(r0) a
dec r0
; (r0) (r0)+1
dec r1
;(r1) (r1)+1
djnz r2,back
end
Before execution:
0X30h=11h
0X31h=22h
0X32h=33h
0X33h=44h
0X34h=55h
0X35h=66h
After execution :
0X30h=11h
0X31h=22h
0X32h=33h
0X33h=11h
0X34h=22h
0X35h=33h
0X36h=44h
0X37h=55h
0X38h=66h
Program:
src equ 30h
;src =30h
;dst =50h
;cnt=5
mov r0,#src
mov r1,#dst
mov r2,#cnt
;r2 counter
; a ( r0)
;a (r1)
; a ( r0)
inc r0
; (r0) (r0)+1
inc r1
;(r1) (r1)+1
djnz r2,back
;(r2) (r2)-1
;if r2!=0 then go to label back
end
Before execution:
0X30h=11h
0X50h=aah
0X31h=22h
0X51h=bbh
0X32h=33h
0X52h=cch
0X33h=44h
0X53h=ddh
0X34h=55h
0X54h=eeh
After execution :
0X30h=aah
0X50h=11h
0X31h=bbh
0X51h=22h
0X32h=cch
0X52h=33h
0X33h=ddh
0X53h=44h
0X34h=eeh
0X54h=55h
;src =30h
;dst =500h
;cnt=5
`
mov DPTR,#ext
mov r2,#cnt
;r2 counter
; a ( DPTR)
;a (r0)
; (DPTR) a
inc r0
; (r0) (r0)+1
inc DPTR
;(DPTR) (DPTR)+1
djnz r2,back
;(r2) (r2)-1
;if r2!=0 then go to label back
end
Before execution:
0X30h=11h
0X500h=aah
0X31h=22h
0X501h=bbh
0X32h=33h
0X502h=cch
0X33h=44h
0X503h=ddh
0X34h=55h
0X504h=eeh
After execution :
0X30h=aah
0X500h=11h
0X31h=bbh
0X501h=22h
0X32h=cch
0X502h=33h
0X33h=ddh
0X503h=44h
0X34h=eeh
0X504h=55h
1F) Write an ALP to move a string from code memory to data memory
Algorithm:
1)store the code from memory location 0000h.
2)initialize r0 as pointer for data memory.
3)initialize DPTR as pointer for code memory.
4)initialize r2 as counter for no, of blocks.
5)move the contents from code memory to data memory.
6)increment code and data memory addresses.
7)decrement counter,repeat steps 5,6.
8)end the program.
Program:
ORG 0000h
Mov DPTR,#string
mov r0,#50H
;r0=50h
mov r2,#5h
;r2=05h
back: CLR a
;a=00h
movc a,@a+DPTR
;a a+(DPTR)
mov @r0,a
;(r0)a
inc r0
;(r0)(r0)+1
inc DPTR
;(DPTR)(DPTR)+1
djnz r2,back
;(r2) (r2)-1
;if r2!=0 then go to label back
ORG 100h
string: db HELLO
end
;end of program
Before execution:
0X100h=H
0X50h=00h
0X101h=E
0X51h=00h
0X102h=L
0X52h=00h
0X103h=L
0X53h=00h
0X104h=0
0X54h=00h
After execution :
0X100h=H
0X50h=H
0X101h=E
0X51h=E
0X102h=L
0X52h=L
0X103h=L
0X53h=L
0X104h=O
0X54h=O
Algorithm:
1)Initialize source location and the location of the result.
2)mov r0 into source location and mov no of bytes into r2
3) use ADD instruction for adding n multibytes
4) mov result into desired location
5) account for the carry if it is generated
6) end the program.
Program:
; src=30h
;cnt=05h
;res=50h
mov r0,#30h
;r030h
mov r1,#05h
;r105h
clr a
;a=00h
;aa+(r0)
inc r0
;r0r0+1
jnc next
inc r2
;r2r2+1
;r1r1-10,back
mov 51h,a
;51ha
mov 50h,r2
;50hr2
end
Output:
Before execution:
After execution:
d: 0X30h =10h
d: 0x50h=02h
0X31h =A0h
0X 51h=f0h
0X32h =B0h
0X33h =C0h
0X34h =D0h
Program:
add1 equ 30h
; add1=30h
;add2=40h
;cnt =04h
mov r0,#add1+cnt-1
;r033h
mov r1,#add2+cnt-1
;r143h
mov r4,#cnt
;r4cnt
clr c
;c=0
back:mov a,@r0
;a(r0)
addc a,@r1
;aa+r1+c
mov @r0,a
;(r0)a
dec r0
;r0r0+1
dec r1
;r1r1+1
djnz r4,back
mov a,#00h
;a00h
addc a,#00h
;aa+00+cy
mov @r0,a
;(r0)a
end
Output:
Before Execution:
D:
0X30h
ffh
D:
0X40h
feh
After execution:
0X31h
0X32h
ffh
ffh
0X41h
0X42h
feh
feh
0X33h
ffh
0X43h
feh
D: 0X29h
0X30h
01h
0X31h
feh
0X32h
feh
feh
0X33h
fdh
Algorithm:
Program:
sub1 equ 30h
; add1=30h
;add2=40h
;cnt =04h
mov r0,#sub1+cnt-1
;r033h
mov r1,#sub2+cnt-1
;r143h
mov r4,#cnt
;r4cnt
clr c
;c=0
back:mov a,@r0
;a(r0)
sbbc a,@r1
;aa-r1+c
mov @r0,a
;(r0)a
dec r0
;r0r0+1
dec r1
;r1r1+1
djnz r4,back
mov a,#00h
;a00h
sbbc a,#00h
;aa-00+cy
mov @r0,a
;(r0)a
end
Output:
Before Execution:
D:
0X30h
01h
D:
0X40h
ffh
0X31h
02h
0X32h
03h
0X41h
0X42h
ffh
ffh
0X33h
04h
0X43h
ffh
After execution:
D: 0X29h
01h
0X30h
0X31h
01h
02h
0X32h
03h
0X33h
05h
Program:
; src=30h
;cnt=05h
;res=50h
mov r0,#30h
;r030h
mov r1,#05h
;r105h
clr a
;a=00h
;aa+(r0)
inc r0
;r0r0+1
jnc next
inc r2
;r2r2+1
;r1r1-10,back
mov 51h,a
;51ha
mov a,r2
;ar2
da a
mov r2,a
; r2a
mov 50h,r2
;50hr2
end
Output:
Before execution:
After execution:
d: 0X30h =10h
d: 0x50h=01h
0X31h =20h
0X 51h=80h
0X32h =30h
0X33h =40h
0X34h =80h
;div1=30h
;div2=40h
mov r0,#div1
;r0div1
mov r1,#div2
;r1div2
mov a,@r0
;a(r0)
mov 0f0h,@r1
;b(r1)
div ab
;aa/b
inc r1
;r1r1+1
mov @r1,a
;(r1)a
inc r1
;r1r1+1
mov @r1,0f0h
;(r1)b
end
Output:
Before execution:
After Execution:
D: 0X 30h= Fb h
D: 0X 32h= Fb h
0X 40h= 05 h
0X33h=01h
Algorithm:
1)
2)
3)
4)
5)
Program:
; num=30h
mov r0,#num
; r0 num
mov a,@r0
; a(r0)
mov 0f0h,#100d
;b100d
div ab
;aa/b, ba-(a*b)
mov @r0,a
;(r0)a
mov a,0f0h
;ab
mov 0f0h,#10d
;b10d
div ab
; aa/b, ba-(a*b)
inc r0
;r0r0+1
mov @r0,a
;(r0)a
inc r0
; r0r0+1
mov @r0,0f0h
;(r0)b
end
Output:
Before Execution:
After Execution:
D:0X30h=FFh
d:0X30h=02h
0X31h=05h
0X32h=05h
Algorithm:
Program:
;num =40h
mov r0,#num
;r0 num
mov a,@r0
;a(r0)
ANl a,#0f0h
;a(a.f0h)
swap a
mov 0f0h,#10d
;b10d
mul ab
;a(a*b)
mov r1,a
;r1a
push 1
mov a,@r0
;a(r0)
ANL a,#0fh
;a(a.0fh)
pop 1
ADD a,r1
;aa+r1
mov @r0,a
;(r0)a
end
Output:
Before Execution:
After Execution:
D:0X30h=45h
d:0X30h=2Dh
Algorithm:
1)check if the number is less than 39 or greater than 46, if yes set r2
2) check if number is between 30-39, if yes, then subtract 30 from it
3)if number is between 40-46, frst subtract 7 then subtract 30
4)store the result i n the required memory location
5)end the program
Program:
; num =30h
mov r0,#num
;r0 num
mov a,@r0
;a(r0)
SUBB a,#30h
;a a-30h
mov @r0,a
;(r0)a
sjmp end2
;jump to end2
;a a-07h
SUBB a,#30h
;a a-30h
mov @r0,a
;(r0)a
sjmp end2
;jump to end 2
invalid: jc end1
CJNE a, #46h,invalid2
sjmp start
invalid2:jnc end1
sjmp start
end1: mov a,#0ffh
;jump to start
;cy=0, jump to end1
;jump to start
;affh
end2:
end
Output:
Before Execution:
d: 0X 30h=43h
After Execution:
d: 0X 30h=2Dh
Algorithm:
1) Store the number in the location pointed to by r0
2) Mask the lower bit, swap it and store it in the required memory location
3) Mask the upper bit and store it in the consecutive location
;num= 30h
mov r0,#num
;r0 num
mov a,@r0
;a(r0)
mov r1,a
;r1a
push 1
ANL a,#0fh
;a(a.00001111b)
mov @r0,a
;(r0)a
pop 1
mov a,r1
;ar1
ANL a,#0f0h
;a (a.11110000b)
swap a
dec r0
;r0r0-1
mov @r0,a
;(r0)a
end
Output:
Before Execution:
d: 0X 30h=43h
After Execution:
d: 0X29h=04h
0X30h=05h
ALGORITHM:
1) configure P1 as input port
2) configure P1.7 and P1.6 be the two select lines ( say S1 and S0).
3) check if S1 is high, if yes branch to another location and check for S0.
( u have checked for 11 and 10 combination)
4) if no S1 is 0 then again check for S0. ( this time u have checked for 01
and 00)
5) end the program.
PROGRAM
mov a,#0ffh
mov p1,a
back: jb p1.7,i2
jb p16,i1
mov c,p1.0
mov p2.1,c
sjmp back
OUTPUT
P1.7(S1) P1.6(S0)
1
0
P2.7
-
P2.6
-
P1.5
P2.5
-
P1.4
P2.4
-
P1.3
0
P2.3
-
P1.2
1
P2.2
-
P1.1
1
P1.0
0
P2.1(o/p)
1
P2.0
-
PROGRAM
mov a,#0ffh
mov p1,a
back: jb p1.1,cond1
jb p1.0,cond3
setb p2.0
cond1: jb p1.0,cond2
setb p2.2
sjmp back
sjmp back
cond3: setb p2.1
sjmp back
end
OUTPUT:
P1.7(S1) P1.6(S0)
P2.7
-
P2.6
-
P1.5
P2.5
-
P1.4
P2.4
-
P1.3
P2.3
0
P1.2
P2.2
1
P1.1
1
P1.0
0
P2.1(o/p)
0
P2.0
0
PROGRAM:
mov a,#0ffh
mov p1,a
mov p2,a
sjmp back
cno1: mov a, #01h
mov p2,a
sjmp back
mov p2,a
sjmp back
cno3: mov a, #03h
mov p2,a
sjmp back
cno4: mov a, #01h
mov p2,a
sjmp back
cno5: mov a, #01h
mov p2,a
sjmp back
cno6: mov a, #01h
mov p2,a
sjmp back
cno7: mov a, #01h
mov p2,a
sjmp back
end
OUTPUT:
P1.7(S1) P1.6(S0)
1
1
P1.5
0
P1.4
0
P1.3
1
P1.2
1
P1.1
1
P1.0
0
P2.7
-
P2.6
-
P2.5
-
P2.4
-
P2.3
-
P2.2
0
PROGRAM
Back:
Mov a,#00h ;
a=00h
mov p0, a
p0=a
Acall delay
Add a, #01h ;
a=a+1
OUTPUT:
Delay Generation
P2.1(o/p)
0
P2.0
0
PROGRAM
Back:
Mov a,#0ffh ;
a=00h
mov p0, a
p0=a
Acall delay
Subb a, #01h ;
a=a+1
a= FF FE FD.03 02 01.
Delay Generation
PROGRAM
Back:
Mov a,#00h ;
a=00h
mov p0, a
p0=a
Acall delay
Add a, #01h ;
a=a+1
Da a
Sjmp back
Delay: mov r1, #71h
Back2: mov r2, #255h
Back1: mov r3, #255h
Back:
Delay Generation
Ret
OUTPUT: a=00 0198 99
PROGRAM
Back:
Mov a,#00h ;
a=00h
mov p0, a
p0=a
Acall delay
Add a, #99h ;
a=a+99
Da a
Delay: mov r1, #71h
Back2: mov r2, #255h
Back1: mov r3, #255h
Back:
Delay Generation
Select command
register
Select data register
R/W = 0
1
E
Write on LCD
Read from LCD
low to high
Before every read from LCD
High to low
Before every write to LCD
D7- Busy check pin. If high data cant be sent out
PROGRAM:
# include <reg51Xd2.h>
void delay( unsigned int x)
{
for ( i=0;i<x;i++)
for ( j=0;j<2000;j++)
}
void lcdready( )
{
busy=1;
rs=0;
rw=1;
while(busy==1)
{ E=0;
delay(1)
E=1;
}
}
void cmdwrite( unsigned char val)
{ { lcdready( )
P2=val;
rs=0;
rw=0;
E=1;
delay(1);
E=0;
}
void datawrite( unsigned char val)
{ lcdready( )
P2=val;
rs=1;
rw=0;
E=1;
delay(1);
E=0;
}
void main()
{
unsigned char a;
unsigned char disp [ ] = PESIT
unsigned cmd[5] = { 0X38,0X0E,0X06,0X01,0X80h}
for(a=0; a<5 ;a++)
cmd write (cmd[a]);
for(a=0; a<5 ;a++)
data write (disp[a]);
}
0
0
0
0
0
1
0
1
0
1
0
0
1
0
0
0
0
1
0
0
PROGRAM:
#include<reg51Xd2.h>
sbit dir=P3^2;
sbit speed=P3^3;
void delay(unsigned int x)
{
for( ;x>0;x--);
}
// function of delay
void main()
{
unsigned char val, i;
unsigned int cnt=500;
while(1)
{
if (dir==0)
{val =0X08;
for(i=0;i<4;i++)
{
P0=val;
val=val>>1;
delay(cnt);
}
0
0
1
0
0
0
0
1
}
else
{
val=0X01;
for(i=0;i<4;i++)
{
P0=val;
val=val<<1;
delay(cnt);
}
}
if (speed==0)
{
while (speed ==0)
{while (speed ==0);
cnt=cnt-100;
if(cnt<200)
cnt=500;
}
}
}
}
// increase speed
PROGRAM
#include<reg51Xd2.h>
sbit dataout=P1^0;
sbit clock = P2^0;
void delay( unsigned int i)
{ unsigned int i;
for(;i>0;i--);
// delay function
}
main()
{
unsigned char regcode[4]={0x88,0x091,0x00,0x88}; // character array
unsigned char a,b,c;
for (a=0;a<4;a++)
{
c = regcode[a];
for(b=0;b<8;b++)
{
c=c<<1;
dataout = CY;
// displaying on 1 led
clock=0;
clock=1;
delay(2);
clock=0;
}
}
}
Vref
80
PROGRAM:
#include <reg51Xd2.h>
sbit inc =P3^2;
sbit dec = P3^3;
main ( )
{
unsigned char i = 0X80;
while(1);
P0=i;
if (inc ==0)
{
while (inc==0);
80
if(i>0X10)
i=i-10;
}
// increase speed
if (dec ==0)
{ while (dec==0);
if(i<0XF0)
i=i+10;
}
}
// decrease speed
AMPLITUDE VARIATIONS
PROGRAM:
SQUARE:
#include<reg51Xd2.h>
#include "lcd.h"
sbit amp = P3^2;
sbit freq = P3^3;
void delay (unsigned int x)
{
for ( ;x>0;x--);
}
void main()
{unsigned char on=127,off=0;
unsigned int cnt =100;
while(1)
{
P0=on;
P1=on;
delay(cnt);
P0=off;
P1=off;
delay( cnt) ;
if(amp==0)
{ while(amp==0);
on=on+16;
if(on>254)
on=127;
}
if(freq==0)
{ while(freq==0);
cnt=cnt+50;
if(cnt>500)
cnt=100;
}
}
}
RAMP:
#include<reg51Xd2.h>
sbit amp = P3^2;
sbit freq = P3^3;
void delay (unsigned int x)
{
for ( ;x>0;x--);
}
void main()
{unsigned char i,x=127;
unsigned int cnt =5;
while(1)
{
for(i=0;i<x;i++)
{
P0=i;
P1=i;
delay(cnt);
}
if(amp==0)
{ while(amp==0);
x=x+32;
if(x>254)
x=127;
}
if(freq==0)
{ while(freq==0);
cnt=cnt+5;
if(cnt>20)
cnt=5;
}
}
}
STAIRCASE:
#include<reg51Xd2.h>
sbit amp = P3^2;
sbit freq = P3^3;
void delay (unsigned int x)
{
for ( ;x>0;x--);
}
void main()
{unsigned char i=0,on=127;
unsigned int cnt =100;
while(1)
{
for(i=0;i<on;i++)
{
P0=i;
P1=i;
delay(cnt);
i=i+20;
if(amp==0)
{ while(amp==0);
on=on+16;
if(on>254)
on=127;
}
if(freq==0)
{ while(freq==0);
cnt=cnt+50;
if(cnt>500)
cnt=100;
}
}
}
}
TRIANGULAR:
#include<REG51XD2.h>
sbit amp = P3^2;
sbit freq = P3^3;
void delay (unsigned int x)
{
for ( ;x>0;x--);
}
void main()
{unsigned char i,x=128;
unsigned int cnt =5;
while(1)
{
for(i=0;i<x;i++)
{
P0=i;
P1=i;
delay(cnt);
}
for( i=x;i>0;i--)
{ P0=i;
P1=i;
delay(cnt);
}
if(amp==0)
{ while(amp==0);
cnt+=25;;
if(x>253)
x=128;
}
if(freq==0)
{ while(freq==0);
cnt+=5;
if(cnt>50)
cnt=5;
}
write a C program to interface an ADC:
ALGORITHM:
1) Analog to digital conversion is done by the IC and the digital data is
available at pin D0-D7 of IC at the end of process below
CS
SOC
INTR
RD
if ( y> 0X09)
y= y+0X37;
else
y= y + 0X30;
writechar(y);
// starts conversion
// waits till conversion ends
RD=1;
delay(1);
RD=0;
digip=P1;
InitLcd()
WriteString (digital o/p = ) ;
display(digip);
delay(2000);
// display on LCD
RD=1;
}
}
#include <reg51Xd2.h>
void delay (unsigned int x)
{
for( ;x>0; x--)
}
void main( )
{ unsigned char ptr [9] ={ 0,0X00,0X03.0.0X06,0,0,0,0X09};
unsigned char ptr_clr[9]= {0X0,0X0E0,)X003,0,00X0B6,0,0,00X79};
while(1)
{
P1=0X0f;
req =P1| 0X0f0;
while(reqflr= =0X0ff)
reqflr=~reqflr;
if( curflr ==reqflr)
{P0=flr_clr(curflr);
{
continue
;}
else if (curflr>reqflr)
{
i=flr[curflr] flr [reqflr];
j=flr[curflr];
for( ;i>0;i--)
{
P0= 0X0f0;
j--;
delay(25000);
}
}
else
i=flr[reqflr]-flr[curflr]
j=flr[curflr];
for( ;i>0;i--)
P0= 0X0f0/j;
j++;
delay(25000);
}
}
curflr =reqflr;
}
void display()
{
If (code>0X09)
Code=code+37;
Write Char(code);
}
void keyscan()
{
Repeat
Row=0X77;
Count=0X04;
Code=0X0C;
next: P1=row;
Row=row>>1;
//right shift by 1
Col=P0;
//read columns
if(col!=0X0F)
{
col=col>>1;
if(!cy)
{
ClrLcd ();
return;
}
else
{
code=code+1;
goto rot;
}}
else
{
code=code-0X04;
count --;
if(count==0)
goto repeat;
//decrement code by 4
else
goto next;
}
}