You are on page 1of 55

Prg no

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

1A) Write an ALP to move n bits of data from memory location X to


memory location Y:

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 equ 50h

;dst =50h

cnt equ 05h

;cnt=5

mov r0,#src

;r0 pointer for source location

mov r1,#dst

;r1 pointer for source location

mov r2,#cnt

;r2 counter

back: mov a,@r0

; 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

;end the program

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

1B) Write an ALP to move n bits of data from memory location X to


memory location Y with overlap:
Algorithm:
1)initialize r0,r1 as pointers pointing to the end of source and destination
memory.
2) choose destination memory such that it overlaps with source memory.
3) initialize r2 as counter for no. of bytes in block.
4) move data from source location to accumulator.

5) move data from accumulator to destination location.


6) decrement source and destination address.
7) decrement the counter value, repeat steps 4,5,6.
8) end the program.
Program:
src equ 30h

;src =30h

dst equ 33h

;dst =33h

cnt equ 06h

;cnt=5

mov r0,#src +cnt-1

;r0 pointer for source location

mov r1,#dst +cnt-1

;r1 pointer for source location

mov r2,#cnt

;r2 counter

back: mov a,@r0

; 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

;(r2) (r2)-1 if r2!=0 then go to back


;end the program

After execution :
0X30h=11h
0X31h=22h
0X32h=33h
0X33h=11h
0X34h=22h
0X35h=33h
0X36h=44h
0X37h=55h
0X38h=66h

1C) Write an ALP to exchange n bits of data from memory location X to


memory location Y:
Algorithm:
1) initialize r0,r1 as pointers for source and destination memory.
2) initialize r2 as counter for no. of bytes in block.
3) exchange data between source location and accumulator.
4) exchange data between accumulator and destination location.
5) exchange data between source location and accumulator again.
6) increment source and destination address.
7) decrement the counter value, repeat steps 3,4,5.
8) end the program.

Program:
src equ 30h

;src =30h

dst equ 50h

;dst =50h

cnt equ 05h

;cnt=5

mov r0,#src

;r0 pointer for source location

mov r1,#dst

;r1 pointer for source location

mov r2,#cnt

;r2 counter

back: XCH a,@r0


XCH a,@r1
XCH a,@r0

; 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

;end the program

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

1D) Write an ALP to exchange a block of data bytes between external


memory location starting from 500h to internal memory location starting
from 30h:
Algorithm:
1) initialize r0 as pointer for internal memory.
2) initialize DPTR as pointer for external memory.
3) initialize r2 as counter for no. of bytes in block.
3) move data from external memory location to accumulator
4) exchange data between accumulator and internal memory.
5) ) move data from accumulator to external memory location
6) increment external and internal memory location.
7) decrement the counter value, repeat steps 3,4,5,6.
8) end the program.
Program:
ext equ 30h
int equ 50h

;src =30h
;dst =500h

cnt equ 05h


mov r0,#int

;cnt=5
`

;r0 pointer for source location

mov DPTR,#ext

;r1 pointer for source location

mov r2,#cnt

;r2 counter

back: mov a, @DPTR


XCH a,@r0
mov @DPTR ,a

; 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

;end the program

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

;stores code from 0000h


;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

;starts storing from 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

2a) Write an ALP to add n bytes numbers:

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 equ 30h

; src=30h

cnt equ 05h

;cnt=05h

res equ 50h

;res=50h

mov r0,#30h

;r030h

mov r1,#05h

;r105h

clr a

;a=00h

back: add a,@r0

;aa+(r0)

inc r0

;r0r0+1

jnc next

;cy=0, jump to next

inc r2

;r2r2+1

next: djnz r1, back

;r1r1-10,back

mov 51h,a

;51ha

mov 50h,r2

;50hr2

end

;end the program

Output:
Before execution:

After execution:

d: 0X30h =10h

d: 0x50h=02h

0X31h =A0h

0X 51h=f0h

0X32h =B0h
0X33h =C0h
0X34h =D0h

2b)Write an ALP to add two multibyte numbers:


Algorithm:
1)Store location of 2 multibyte numbers in R0 and R1 such that the number
denoting the location is pointing to the last byte
2)Store the no of bytes in a number in R2
3)add with carry the last two bytes
4)decrement R0 and R1
5)decrement R2
6)repeat steps 3,4 and 5
7) account for carry from the last byte.
8)end the program

Program:
add1 equ 30h

; add1=30h

add2 equ 40h

;add2=40h

cnt equ 04h

;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

;r4 r4-10,jump back

mov a,#00h

;a00h

addc a,#00h

;aa+00+cy

mov @r0,a

;(r0)a

end

;end the program

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

2c)Write an ALP to subtract 2 multibytes:

Algorithm:

1)Store location of 2 multibyte numbers in R0 and R1 such that the number


denoting the location is pointing to the last byte
2)Store the no of bytes in a number in R2
3)subtract with borrow the last two bytes
4)decrement R0 and R1
5)decrement R2
6)repeat steps 3,4 and 5
7 )account for borrow from the last byte.
8)end the program

Program:
sub1 equ 30h

; add1=30h

sub2 equ 40h

;add2=40h

cnt equ 04h

;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

;r4 r4-10,jump back

mov a,#00h

;a00h

sbbc a,#00h

;aa-00+cy

mov @r0,a

;(r0)a

end

;end the program

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

2d) Write an ALP to add n 2 digits packed BCD numbers


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)use decimal adjust to get back BCD number
7) end the program

Program:

src equ 30h

; src=30h

cnt equ 05h

;cnt=05h

res equ 50h

;res=50h

mov r0,#30h

;r030h

mov r1,#05h

;r105h

clr a

;a=00h

back: add a,@r0

;aa+(r0)

inc r0

;r0r0+1

jnc next

;cy=0, jump to next

inc r2

;r2r2+1

next: djnz r1, back

;r1r1-10,back

mov 51h,a

;51ha

mov a,r2

;ar2

da a

;aa+66h,under certain conditions

mov r2,a

; r2a

mov 50h,r2

;50hr2

end

;end the program

Output:
Before execution:

After execution:

d: 0X30h =10h

d: 0x50h=01h

0X31h =20h

0X 51h=80h

0X32h =30h
0X33h =40h
0X34h =80h

2e) Write an ALP to divide two bytes:


Algorithm:
1) Store the dividend and divisor in location pointed to by R0 and R1
respectively.
2) Move them to accumulator and reg B
3) Divide them using DIV instruction
4) Store quotient and remainder in desired locations
5) End the program
Program:
div1 equ 30h

;div1=30h

div2 equ 40h

;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

;end the program

Output:
Before execution:

After Execution:

D: 0X 30h= Fb h

D: 0X 32h= Fb h

0X 40h= 05 h

0X33h=01h

3a) Write a program to convert a given no to BCD equivalent:

Algorithm:
1)
2)
3)
4)
5)

Store the number in the location pointed to by r0


Divide the number by 100d
The quotient in A and remainder in B
Now divide remainder by 10d
Store the first quotient,second quotient and the remainder in increasing
manner of memry location.
6) End the program

Program:

num equ 30h

; 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

;end the program

Output:
Before Execution:

After Execution:

D:0X30h=FFh

d:0X30h=02h
0X31h=05h
0X32h=05h

3b)Write an ALP to convert given packed BCD into binary equivalent:

Algorithm:

1)store the number in the location pointed to by ro


2)mask the lower bit of the number
3)swap the upper and lower nibble and store it in a memory location
4)mask the upper nibble
5)store it in the consecutive memory location
6)end the program

Program:

num equ 40h

;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

;spsp+1, stack memr1

mov a,@r0

;a(r0)

ANL a,#0fh

;a(a.0fh)

pop 1

;spsp-1, r1stack mem

ADD a,r1

;aa+r1

mov @r0,a

;(r0)a

end

;end the program

Output:
Before Execution:

After Execution:

D:0X30h=45h

d:0X30h=2Dh

4a) Write an ALP to convert an ASCII number into hex equivalent:

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 equ 30h

; num =30h

mov r0,#num

;r0 num

mov a,@r0

;a(r0)

new: CJNE a,#30h,invalid

; a-30h0 then jump to invalid

start: CJNE a,#39h,start1

;a-39h0, jump to start1

start1: jnc start2

;cy=0 jump to start2

SUBB a,#30h

;a a-30h

mov @r0,a

;(r0)a

sjmp end2

;jump to end2

start2: SUBB a,#07h

;a a-07h

SUBB a,#30h

;a a-30h

mov @r0,a

;(r0)a

sjmp end2

;jump to end 2

invalid: jc end1

;cy=1 jump to end1

CJNE a, #46h,invalid2

;a-46h0 then jump to 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

;end the program

Output:
Before Execution:
d: 0X 30h=43h

After Execution:
d: 0X 30h=2Dh

4b) Write an ALP to convert given packed BCD to ASCII

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

4) End the program


Program:
num equ 30h

;num= 30h

mov r0,#num

;r0 num

mov a,@r0

;a(r0)

mov r1,a

;r1a

push 1

;spsp+1, stack memr1

ANL a,#0fh

;a(a.00001111b)

mov @r0,a

;(r0)a

pop 1

;spsp-1, r1stack memory

mov a,r1

;ar1

ANL a,#0f0h

;a (a.11110000b)

swap a

dec r0

;r0r0-1

mov @r0,a

;(r0)a

end

;end the program

Output:
Before Execution:
d: 0X 30h=43h

After Execution:
d: 0X29h=04h
0X30h=05h

5a)Write an ALP for 4:1 mux

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

i1: mov c,p1.1


mov p2.1,c
sjmp back
i2: jb p1.6,i3
mov c,p1.2
mov p2.1,c
sjmp back

i3: mov c,p1.3


mov p2.1,c
sjmp back
end

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
-

5B) Write an ALP to 2: 4 decoder


ALGORITHM:
1) configure P1 as input port
2) check if P1.1 is set , if yes branch to another location check if P1.0 is set
if yes , then make P2.3 high
3) similarly if P1.1 P1.0 = 1 0 set p2.2 and so on
4) end the program

PROGRAM
mov a,#0ffh
mov p1,a

; configure P1 as input port

back: jb p1.1,cond1
jb p1.0,cond3
setb p2.0

; P1.1 P1.0 = 00 , set P2.0

cond1: jb p1.0,cond2
setb p2.2

; P1.1 P1.0 = 10 , set P2.2

sjmp back

cond2: setb p2.3

; P1.1 P1.0 = 11 , set P2.3

sjmp back
cond3: setb p2.1

; P1.1 P1.0 = 01 , set 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

5C)Write an ALP to realize 8: 3 priority encoder


ALGORITHM:
1) configure P1 as input port
2) this is an active low encoder, priority increases from left to right
3) start checking ,from extreme right, if any bit is 0
4) if yes, then jump to the specified ( for that condition) location and send the
corresponding number on P2

5) end the program

PROGRAM:
mov a,#0ffh
mov p1,a

; configure P1 as input port

back: clr p2.0


clr p2.1
clr p2.2
jnb p1.1,cno0
jnb p1.1,cno1
jnb p1.2,cno2
jnb p1.3,cno3
jnb p1.4,cno4
jnb p1.5,cno5
jnb p1.6,cno6
jnb p1.7,cno7
sjmp back
cno0: mov a, #00h

; send 00 when P1.0 is low

mov p2,a
sjmp back
cno1: mov a, #01h
mov p2,a
sjmp back

; send 001 on P2 when P1.1 is low

cno2: mov a, #02h

; send 010 on P2 when P1.2 is low

mov p2,a
sjmp back
cno3: mov a, #03h

; send 011 on P2 when P1.3 is low

mov p2,a
sjmp back
cno4: mov a, #01h

; send 100 on P2 when P1.4 is low

mov p2,a
sjmp back
cno5: mov a, #01h

; send 101 on P2 when P1.5 is low

mov p2,a
sjmp back
cno6: mov a, #01h

; send 110 on P2 when P1.6 is low

mov p2,a
sjmp back
cno7: mov a, #01h

; send 111 on P2 when P1.7 is low

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

6a) Write an ALP to realize 8 bit binary Up Counter?


ALGORITHM
1. Clear register A.
2. Move the contents of reg.A to port 0.
3. Call delay.
4. Keep adding 1 to contents of reg.A
5. End the program.

PROGRAM

Back:

Mov a,#00h ;

a=00h

mov p0, a

p0=a

Acall delay
Add a, #01h ;

a=a+1

Delay: mov r1, #71h


Back2: mov r2, #255h
Back1: mov r3, #255h
Back:

djnz r3, back


djnz r3, back
djnz r3, back
Ret

OUTPUT:

Delay Generation

P2.1(o/p)
0

P2.0
0

a=01 02 03 04.FD FE FF.

6b)Write an ALP to realize 8 bit binary Down Counter


ALGORITHM
1. Move value ffh to register A.
2. Move the contents of reg.A to port 0.
3. Call delay.
4. Keep subtracting 1 from contents of reg.A
5. End the program.

PROGRAM

Back:

Mov a,#0ffh ;

a=00h

mov p0, a

p0=a

Acall delay
Subb a, #01h ;

a=a+1

Delay: mov r1, #71h


Back2: mov r2, #255h
Back1: mov r3, #255h
Back:

djnz r3, back


djnz r3, back
djnz r3, back
Ret

a= FF FE FD.03 02 01.

Delay Generation

6c) Write an ALP to realize 8 bit BCD Up Counter?


ALGORITHM
1. Clear register A.
2. Move the contents of reg.A to port 0.
3. Call delay.
4. Keep adding 1 to contents of reg.A and Daa(decimal adjust after addition)
5. End the program.

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:

djnz r3, back


djnz r3, back
djnz r3, back

Delay Generation

Ret
OUTPUT: a=00 0198 99

6d) Write an ALP to realize 8 bit BCD Down Counter?


ALGORITHM
1. Clear register A.
2. Move the contents of reg.A to port 0.
3. Call delay.
4.Add 99 to contents of reg.A
5. End the program.

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:

djnz r3, back


djnz r3, back
Djnz r3, back
Ret

Delay Generation

OUTPUT: a=99 98 ..02 01 00

7)Write a C program to interface an LCD to 8051


Algorithm:
1) Include header files <reg51Xd2.h> . This provides all the hardware
information to the assembler.
2) Assign pins of LCD to p[ort pins
3) Write a function for delay.
4) Write two separate functions for writing data and command
a) Every write instruction occurs after Enable undergoes a high to
low transition
b) Also include checking of busy pin. (as long as busy pin is high,
we cannot do read or write operation).
c) Take care that R/W and Rs are as specified for command and
data.
5) Call the above functions as required.
6) End the program
Rs = 0
1

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;
}

// function for delay

// function for checking if LCD is busy

// function for writing command

// function for writing data

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]);
}

10) WRITE A C PROGRAM TO INTERFACE A STEPPER MOTOR to


8051
ALGORITHM:
1) a steeper motor is something which rotates in steps , depending on which
coil is energized
2) if u energize coil A, the mean of north of rotor aligns itself with A
3) so energize coils sequentially to rotate motor
4) change the direction by reversing the sequence of energizing coils as
shown in table.
5) change the speed by varying the time delay i.e., the amount of time the
rotor is supposed to wait in one position.
6) observe speed and direction changes.

anti -clockwise rotation Left shift by 1;

0
0
0

0
0
1

0
1
0

1
0
0

clockwise rotation right shift by 1

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);
}

// for clockwise rotation

0
0
1
0

0
0
0
1

}
else
{
val=0X01;
for(i=0;i<4;i++)
{
P0=val;
val=val<<1;
delay(cnt);

// for anticlockwise rotation

}
}
if (speed==0)
{
while (speed ==0)
{while (speed ==0);
cnt=cnt-100;
if(cnt<200)
cnt=500;
}
}
}
}

// increase speed

WRITE A C PROGRAM TO INTERFACE a seven segment display to


8051
ALGORITHM:
1) store the data u need displayed in a character array.
2) since we are using shift registers, on every clock pulse data keeps shifting
left.
3) each character is a 8 bits, therefore after 8 clock pulses the first data will be
displayed on the first led
4) repeat this process 4 times.
5) observe the output on the seven segment display.

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++)
{

// repeat for all 4 values in regcode array

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;
}
}
}

WRITE A C PROGRAM TO INTERFACE A DC MOTOR:


ALGORITHM:
1) dc motor once energized keeps rotating (unlike stepper, we dont have to
energize coils for each rotation)
2) we can, therefore, only increases or decrease speed.
3) his can be done by decrementing the value we are sending on port **
4) observe the speed variations
**

speed of the dc motor is controlled using a PWM pulse.


the duration for which pulse is high determines the speed ( i.e., if pulse is high
for a longer duration speed is more)
this can be done by decreasing the reference value of the comparator ( i.e., by
reducing the value of i( in our case))
Vref = 80
Vref
Vref = 80

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

WRITE A C PROGRAM TO INTERFACE A DAC:


ALGORITHM:
FREQUENCY VARIATIONS

1) this can be done by increasing or decreasing the parameter , we pass to


the delay function

AMPLITUDE VARIATIONS

1) DAC 0808, the value of FF corresponds to 5V


2) therefore choose a value say 7F , so we can include the option of
increasing amplitude
3) wait fir the amplitude key to be pressed and released. After that increment
your amplitude variable by some value
4) once it goes out of range , bring it back to its original value of 7F

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

1) conversion starts when SOC goes low to high


2) process completes when INTR goes low
3) when RD goes high to low , process completes
4) data is available on PORT pins
2) write a function to display the received bits from ADC to LCD
3) observe the output
PROGRAM
# include <reg51Xd2.h>
#include <lcd.h>
sbit SOC = P0^5

sbit EOC = P2^6


sbit RD= P0^6
sbit SO= P0^0
sbit S1 = P0^1
sbit S2= P0^3

void display (unsigned char dig)


{
unsigned char x,y;
x= dig & 0X0f0;
x=x>>4;
y= dig & 0X0f;
if ( x > 0X09)
x= x+0X37;
else
x= x + 0X30;
writechar(x);

// hex to ASCII conversion

if ( y> 0X09)
y= y+0X37;
else
y= y + 0X30;
writechar(y);

// function to display the digital input on LCD

void delay( unsigned int i)


{
for ( ;i>0; i--)
return;
}
void main( )
{
unsigned char digip;
P1 = 0X0ff;
EOC =1;
while(1)
{ S0=0; S1=0; S2=0; S3=0;
SOC =1;
delay(2)
SOC=0;
while (EOC=1)

// starts conversion
// waits till conversion ends

RD=1;
delay(1);
RD=0;
digip=P1;

// data available on D0-D7 of IC


// data transferred to PORT 1

InitLcd()
WriteString (digital o/p = ) ;
display(digip);

delay(2000);

// display on LCD

RD=1;
}
}

Write a C program to interface an elevator to 8051

#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;

P0= flr_clr[ curflr]


}
}

Write a C program to interface hex keypad to 8051


#include <reg51Xd2.h>
#include <lcd.h>
unsigned char row, col, code , count
void delay()
{
Unsigned int x;
for (x=0;x<2000;x++)

//delay for 2000 counts

}
void display()
{
If (code>0X09)
Code=code+37;
Write Char(code);
}
void keyscan()
{
Repeat
Row=0X77;
Count=0X04;

//from A onwards do decimal addition

Code=0X0C;
next: P1=row;
Row=row>>1;

//right shift by 1

Col=P0;

//read columns

col=col & 0X0f

//masking p0.7 to p0.4

if(col!=0X0F)

//when no key is pressed

{
col=col>>1;
if(!cy)
{
ClrLcd ();

//clear lcd in lcd.h

return;
}
else
{
code=code+1;
goto rot;
}}
else
{
code=code-0X04;
count --;
if(count==0)
goto repeat;

//decrement code by 4

else
goto next;
}
}