You are on page 1of 68

5th Semester Microcontroller Lab (15EEL57) Manual

M I C RO C O N T R O L L E R L A B . ( 1 5 E E L 5 7 )
S y l l a b u s : [ U s e o f 8 0 5 1 M i c r o c o n t r o l l e r a n d i t s a d va n c e d ve r s i o n AT 8 9 S 5 2 ]
A. Use of Simulation Software for writing programs . .
1. D a t a Tr a n s f e r – W r i t i n g A L P s f o r :
Block data move ment , bl ock data e xc h a n g e , sorting of data, finding
l a r g e s t / s m a l l e s t e l e m e n t i n a g i ve n a r r a y e t c , e t c , .

2. Arithme tic I nstruc ti ons – Wri ti ng A LPs for:


Addi ti on, Subtrac ti on, Mul ti plic ation a nd Division i nvo lvi ng 8 bit a nd 16 bi t
n u m b e r s . F i n d i n g t h e s q u a r e a n d c u b e o f a g i v e n n u m b e r.

3. Wr i t i n g A L P s f o r :
Counters: Binary Counters, Decimal Counters etc.,

4. Wr i t i n g A L P s f o r :
Boolean and Logical Instructions (bit manipulations).

5. Wr i t i n g A L P s f o r :
Understanding conditional CALL and RETURN instructions.

6. Wr i t i n g A L P s f o r :
Unde rsta ndi ng Code conver si on program s – BC D to ASC II, AS CII to BCD, AS CII to
decimal, Dec imal to ASCII, Hexa to dec imal and Decimal to Hexa

7. Wr i t i n g A L P s f o r :
Generation of time-delay (delay programs), Generation of time delay(use of
on chip timer/counter), serial port communication.

B. Use of Simulation Software for writing prog rams and interfacing . .

8. Wr i t i n g ‘ c ’ p r o g r a m f o r : S t e p p e r m o t o r s p e e d a n d d i r e c t i o n c o n t r o l .

9. Wr i t i n g ‘ c ’ p r o g r a m f o r : D C M o t o r s p e e d a n d d i r e c t i o n c o n t r o l ( P W M o d u l a t i o n ) .

1 0. Wri ti ng ‘c ’ program for: Al pha nu meric al displ ay on LCD Pa ne l.

1 1 . W r i t i n g ‘ c ’ p r o g r a m f o r : G e n . o f Wa v e f o r m s ( S i n e , T r i a n g u l a r, R a m p ) u s i n g DA C .

1 2 . W r i t i n g ‘ c ’ p r o g r a m f o r : E x t e r n a l A D C a n d Te m p e r a t u r e c o n t r o l .

1 3. Wri ti ng ‘c ’ program for: El evator I nter faci ng.

**************

Department of Electrical & Electronics Engineering


1
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

INTRODUCTION

MCS 8051 is an 8-bit single chip microcontroller with many built-in functions and
is the core for all MCS-51 devices.

The main features of the 8051 core are:


 Operates with single Power Supply +5V.
 8-bit CPU optimized for control applications.
 16-bit program counter (PC) and 16-bit data pointer (DPTR).
 8-bit program status word (PSW).
 8-bit stack pointer (SP).
 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
 128 bytes of On-Chip Data Memory (Internal RAM):
o Four Register Banks, each containing 8 registers (R0 to R7)
(Total 32 registers).
o 16 bytes of bit addressable memory.
o 80 bytes of general-purpose data memory (Scratch Pad Area).
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3).
 Two 16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 5-vector interrupts (2 external and 3 internal) with two priority levels.
 On-Chip oscillator and clock circuitry.

Figure below shows the general block diagram

Department of Electrical & Electronics Engineering


2
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

8051 Pin Diagram

P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0
P1.2 3 38 )P0.1(AD1)
P1.3 4 37 P0.2(AD2
P1.4 5 8051 36 )P0.3(AD3)
P1.5 6 35 P0.4(AD4)
P1.6 7
(8031) 34 P0.5(AD5)
P1.7 8 33 P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14
(T1)P3.5 15 26 )P2.5(A13
(WR)P3.6 16 25 )P2.4(A12
(RD)P3.7 17 24 )P2.3(A11)
XTAL2 18 23 P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)

Program Status Word


• Program status word (PSW) at address DOH contains status bits as
summarized in the following table

Department of Electrical & Electronics Engineering


3
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

SAMPLE PROGRAMS:

Write the result of the following instructions for each example

Data Movement Operation

Example: 01
org 0000h
mov r0,#88h ; R0 = ;Memory Location(D:0x00)=
mov a,#24h ; A = ;Memory Location(D:0xE0)=
mov a,r0 ; Check the change in the content of A and R0 if any
end

Example: 02
org 0000h
mov a,#88h ; (A)= and (R0)=
mov r0,#24h
mov 24h,#20h ; contents of RAM location 24h=
mov a,@r0 ; Check the change in the content of A and R0 if any
end

Example: 03
org 0000h
mov a,#40h ; (A)=
mov DPTR,#34h ; (DPTR)=
movx @DPTR, a ; Comments on this instruction
mov a,#00h
movx a,@DPTR
end

Example: 04
org 0000h
mov DPTR,#1234h ; (DPTR)=
mov a,#00h ; (a)=
movc a,@a+DPTR ; Comment on this instruction and check out the
end ; respective register and memory location content

Department of Electrical & Electronics Engineering


4
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Addition operation:
Example 5
org 0000h
mov r1,#27h ; (r1)=
mov a,#45h ; (a) =
add a,#34h ; (a) =
add a,r1 ; (a) = (r1)=
add a,01h ; (a) = (01)=
add a,@r1 ; Comment on this instruction
addc a,#34h ; (PSW) = (a)= (r1)=
addc a,r1 ; (PSW) = (a)= (r1)=
addc a,01h ; (PSW) = (a)= (r1)=
addc a,@r1 ; Comment on this instruction
end

Example 06
org 0000h
mov r1,#27h ; (r1)=
mov a,#45h ; (a) =
subb a,#34h ; (a) = (psw)
subb a,r1 ; (a) = (r1)=
subb a,01h ; (a) = (psw)=
subb a,@r1 ; Comment on this instruction and check the
end ; content of the required registers

Multiplication and Division Operation


Example 7
org 0000h
mov b,#27h ; (b)=
mov a,#45h ; (a) =
mul ab ; (a) = (b)=
end

Example 8
org 0000h
mov b,#27h ; (b)=
mov a,#45h ; (a) =
div ab ; (a) = (b)=
end

Department of Electrical & Electronics Engineering


5
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Rotate Operation:
Example 9
org 0000h
mov a,#66h ; (a)=
clr c
rl a ; (a)= flag C=
rlc a ; (a)= flag C=
rr a ; (a)= flag C=
rrc a ; (a)= flag C=
end

Boolean Operations
Example 10
AND Operations
org 0000h
mov a,#23h ; (a)=
mov r1,#45h ; (r1)=
anl a,#56h ; (a)=
anl a,r1 ; (a)= (r1)=
anl a,01h ; Comment on this instruction and check out of the
end ; respective registers

Example 11
OR Operations
org 0000h
mov a,#23h ; (a)=
mov r1,#41h ; (r1)=
orl a,#56h ; (a)=
orl a,r1 ; (a)= (r1)=
orl a,@r1 ; Comments on this instruction
orl a,01h ; (a)= (r1)=
end ;

Example 12
Ex-OR Operations
org 0000h
mov a,#23h ; (a)=
mov r1,#45h ; (r1)=
xrl a,#56h ; (a)=
xrl a,r1 ; (a)= (r1)=
xrl a,@r1 ; (a)= Comments on this instruction, check the registers
xrl a,01h ; (a)= (r1)=
end ;
Department of Electrical & Electronics Engineering
6
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Example 13
Compliment Instruction

org 0000h
mov a,#23h ; (a)=
cpl a ; (a)=
end

Example 14
Clear Instruction

org 0000h
mov a,#34h ; (a)=
clr a ; (a)=
end

Example 15
Bit level Operations

org 0000h
mov 0d0h,#0ffh ; (0d0h)=
anl c,08h ; c flag= (080)=
anl c,/08h ; c flag= (080)=
orl c,08h ; c flag= (080)=
orl c,/08h ; c flag= (080)=
cpl c ; c flag=
cpl 08h ; (o8oh)=
clr c ; c flag =
clr 08h ; (080h)=
clr acc.7 ; (a)=
setb c ; c flag =
setb 08h ; (08h)=
mov c,08h ; c flag = (080)=
mov 08h,c ; c flag = (080)=
end.

Example 16
Swap Operations

org 0000h
mov a,#23h
swap a
end

Department of Electrical & Electronics Engineering


7
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Example 17

Exchange Operations

org 0000h
mov a,#23h
mov r1,#45h
xch a,@r1
xch a,01h
xch a,@r1
end

Example 18
Increment & Decrement Operations

org 0000h
mov a,#45h ; (a)
mov r1,#27h ; (r1)
dec r1 ; (r1)
dec a ; (a)
dec@r1
inc r1 ; (r1)=
inc a ; (a)=
inc@r1 ;
inc dptr ; (dptr)
end

Example 19
Decimal Adjustment Operations

org 0000h
mov b,#27h ; (b)
mov a,#45h ; (a)
add ab ; (a)
da a ; (a)
end

Example 20
Call Operation
main: mov 81h,#30h ; (81h)=
Lcall sub ; (pc)=
Nop ; comment on this
sub: mov a,#45h ; (a) =
Ret ; (pc) =
end

Department of Electrical & Electronics Engineering


8
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Example 21
Jump Operations

org 0000h
mov a,#55h ; (a)=
mov r1,#0ffh ; (r1)=
add a,r1 ; (a)= (r1)=

jc loop ;
Main: jnc loop1 ;
jbc 08h,loop ;

loop: clr c ; c flag=


loop1:setb c ; c flag=
mov a,#15h ; (a)=
mov r1,#15h ; (r1)=
mov 15h,#15h ; (45h)=
cjne a,01h,loop ;
cjne r1,#15h,loop ;
cjne @r1,#15h,loop ;
loop2:djnz 15h,loop2 ;
loop3:dec a ; (a)
jz main ; (pc)=
jnz loop3 ; (pc)=
end

Example 21
Delay Program:

org 0000h
start: mov a,#00h
main: mov P0,a
acall delay
inc a
jnz main
sjmp start
delay: mov r0,#0fh
d0: mov r1,#0ffh
d1: mov r2,#0ffh
d2: djnz r2,d2
djnz r1,d1
djnz r0,d0

Department of Electrical & Electronics Engineering


9
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

ret

Use of Simulation Software for writing programs.

1 . D a t a Tr a n s f e r – Wr i t i n g A L P s f o r :
B l o c k d a t a m o v e m e n t , b l o c k d a t a e xc h a n g e , s o r t i n g o f d a t a , f i n d i n g
l a r g e s t / s m a l l e s t e l e m e n t i n a g i ve n a r r a y e t c ,

a. ALP to transfer block of data from location X (internal RAM) to location Y


(internal RAM)
Note: It is assumed that location X starts from RAM location 30H & location Y
starts from 40H

org 0000 ; Defining the code location to start from location 0000
mov r0,#0x30 ; Initializing r0 as a memory pointer to location X
mov r1,#0x40 ; Initializing r1 as a memory pointer to location Y
mov r5,#0x05 ; Number of elements in the array is defined as a count
bck: mov a,@r0 ; Reads a data from location X
mov @r1,a ; Transfers data to location Y
inc r0 ; Points to next location in X
inc r1 ; Points to next location Y
djnz r5,bck ; Repeats data transfer until all the data are transferred
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program

; This program transfers a block of data from location X to location Y

Before Execution After Execution


Loc-X Loc-Y Loc-X Loc-Y
30 - 01 40 – 00 30 - 01 40 - 01
31 - 02 41 – 00 31 - 02 41 - 02

Department of Electrical & Electronics Engineering


10
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

32 - 03 42 – 00 32 - 03 42 - 03
33 - 04 43 – 00 33 - 04 43 - 04
34 - 05 44 – 00 34 - 05 44 - 05

b. ALP to exchange a block of data in location X (internal RAM) with location Y


(internal RAM)

Note: It is assumed that location X starts from RAM location 30H & location Y
starts from 40H

org 0000 ; Defining the code location to start from location 0000
mov r0,#0x30 ; Initializing r0 as a memory pointer to location X
mov r1,#0x40 ; Initializing r1 as a memory pointer to location Y
mov r2,#0x05 ; Number of elements in the array is defined as a count
bck: mov a,@r0 ; Reads a data from location X
mov r3,a ; Copies data to a temporary register
mov a,@r1 ; Reads a data from location Y
mov @r0,a ; Transfers data to location X
mov a,r3
mov @r1,a ; Transfers data to location Y
inc r0 ; Points to next location in X
inc r1 ; Points to next location Y
djnz r2,bck ; Repeats data transfer until all the data are transferred
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program

; This program exchange a block of data from location X with location Y

Before Execution After Execution


Loc-X Loc-Y Loc-X Loc-Y
30 - 01 40 – 0A 30 – 0A 40 - 01
31 - 02 41 – 0B 31 – 0B 41 - 02
32 - 03 42 – 0C 32 – 0C 42 - 03
33 - 04 43 – 0D 33 – 0D 43 - 04
Department of Electrical & Electronics Engineering
11
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

34- 05 44 – 0E 34 – 0E 44 - 05

c. ALP to perform ascending order sort operation using "Bubble Sort" Technique

org 0000
mov r0,#0x30 ; initializing array to point location specified
mov 01,r0 ; copying array location address.
mov r2,#0x05 ; initializing number of elements in the array 'n'
dec r2 ; performs n-1 operation

bck2: mov 03,r2 ; copying count value for number of pass & comparison value

mov a,@r0
bck1: mov r6,a ; reads first element in the array
inc r0 ; points to next location in the array
mov a,@r0
mov r7,a ; reads second element in the array
mov a,r6

cjne a,07,cont ; compares first element with the second element


cont: jc skip ; checks for whether first element < second element
xch a,r7
dec r0
mov @r0,a ; if no exchange first & second element value
inc r0
mov a,r7
mov @r0,a
skip: mov a,r7

djnz r3,bck1 ; decrements the number of comparison count value & checks
for zero
mov r0,01 ; if yes, initialize array to starting addr. to perform next pass
djnz r2,bck2 ; decrement number of pass count value & checks for zero
exit: sjmp exit ; if yes exits the program
end

Department of Electrical & Electronics Engineering


12
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

; This program sorts the entered data elements in the array in ascending order using
"bubble sort" Technique

Array location starts from the location 30h

Eg: Before Execution

30h - 0x05
31h - 0x04
32h - 0x03
33h - 0x02
34h - 0x01

After Execution

30h - 0x01
31h - 0x02
32h - 0x03
33h - 0x04
34h - 0x05

Bubble sort mechanism works as follows:


Number of elements in the array - (n) = 05

Number of passes value is set - (n-1) = 04

Number of comparison value is set - (n-1) = 04

1. First element in the array is read and compared with the second element
2. If first element < second element
yes: make second element as first element & go to next step
No: exchange first & second element
3. Continue this process until all the elements in the array are compared or until the end
of current pass
4. Decrement the comparison pointer value & check for end of comparison
If end of comparison is reached
yes: go to step 5
no: go to step 2
5. Decrement the number of pass value & check for end of pass
If end of pass value is reached
yes: exit the program
no: initialize sort for starting address of array for next pass

Department of Electrical & Electronics Engineering


13
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

d. ALP to perform descending order sort operation using "Bubble Sort"


mechanism

org 0000

mov r0,#0x30 ; initializing array to point location specified


mov 01,r0 ; copying array location address.
mov r2,#0x05 ; initializing number of elements in the array 'n'
dec r2 ; performs n-1 operation
bck2: mov 03,r2 ; copying count value for number of pass & comparison
value
mov a,@r0
bck1: mov r6,a ; reads first element in the array
inc r0 ; points to next location in the array
mov a,@r0
mov r7,a ; reads second element in the array
mov a,r6

cjne a,07,cont ; compares first element with the second element


cont: jnc skip ; checks for whether first element < second element
xch a,r7
dec r0
mov @r0,a ; if no exchange first & second element value
inc r0
mov a,r7
mov @r0,a
skip: mov a,r7

djnz r3,bck1 ; decrements the number of comparison count value &


checks for zero
mov r0,01 ; if yes, initialize array to starting address to perform next
pass
djnz r2,bck2 ; decrement number of pass count value & checks for zero
exit: sjmp exit ; if yes exits the program

end

Department of Electrical & Electronics Engineering


14
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

; This program sorts the entered data elements in the array in descending order using
"bubble sort" mechanism

Array location starts from the location 30h

Eg: Before Execution:

30h - 0x01
31h - 0x02
32h - 0x03
33h - 0x04
34h - 0x05

After Execution

30h - 0x05
31h - 0x04
32h - 0x03
33h - 0x02
34h - 0x01

Bubble sort mechanism works as follows:


Number of elements in the array - (n) = 05

Number of passes value is set - (n-1) = 04

Number of comparison value is set - (n-1) = 04

1. First element in the array is read and compared with the second element
2. If first element > second element
yes: make second element as first element & go to next step
No: exchange first & second element
3. Continue this process until all the elements in the array are compared or until the end
of current pass
4. Decrement the comparison pointer value & check for end of comparison
If end of comparison is reached
yes: go to step 5
no: go to step 2
5. Decrement the number of pass value & check for end of pass
If end of pass value is reached
yes: exit the program
no: Initialize sort for starting address of array for next pass

Department of Electrical & Electronics Engineering


15
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

e. ALP to find the largest element in the block of data


org 0000 ; Defining the code location to start from location 0000
mov r0,#0x30 ; Initializing r0 as a memory pointer to point for starting addr.
of array.
mov r5,#0x05 ; Number of elements in the array is defined as a count
mov a,@r0
mov r3,a ; Copies the first element in the array to R3
dec r5 ; Performs n-1 operation
bck: inc r0 ; Points to next location in the array
mov a,@r0
mov r4,a ; Copies next element to register r4
mov a,r3
cjne a,04,cont ; Compares first element with next element in the array
cont: jnc skip ; If first element > than next element,
; Yes: goes to specified location
mov a,r4 ; No: copy next element as largest
skip: mov r3,a ; Storing largest value in R3
djnz r5,bck ; Decrement count value and repeat the operation for next
comparison
mov 40h,r3 ; Save the result in 40th memory location
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program
; This program is used to find the largest element in the array.
Array starts from location 0x30
30h - 0x20
31h - 0x30
32h - 0x50
33h - 0x40
34h - 0x10
Largest element R3 - 50

Department of Electrical & Electronics Engineering


16
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

; In 8051 we don’t have simple compare instruction, at first the numbers are checked for
equality if both the elements in the array are equal no task is performed.

; "cjne" instruction is used to check for equality of both the numbers


i.e. "cjne a, directaddr, location"

; First element is copied to R3 & second element to R4, initially the value in R3 is
assumed largest and it is compared with next number in R4

; If R3 > R4
yes: continue comparing with next elements
no: copy r4 to r3 and r3 is treated as largest, comparing with next elements done

; Repeat the compare operation until the specified number of elements


Alternate method:
org 0000h
mov dptr,#9000h
mov r0,#05h
dec r0
movx a,@dptr
mov 7fh,a
back2: inc dptr
movx a,@dptr
cjne a,7fh,back1
sjmp back3
back1: jc back3
mov 7fh,a
back3: djnz r0,back2
mov a,7fh
end

Department of Electrical & Electronics Engineering


17
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

f. ALP to find the smallest element in the block of data


org 0000 ; Defining the code location to start from location 0000
mov r0,#0x30 ; Initializing r0 as a memory pointer to point for starting addr. of
array
mov r5,#0x05 ; Number of elements in the array is defined as a count
mov a,@r0
mov r3,a ; Copies the first element in the array to R3
dec r5 ; Performs n-1 operation
bck: inc r0 ; Points to next location in the array
mov a,@r0
mov r4,a ; Copies next element to register r4
mov a,r3
cjne a,04,cont ; Compares first element with next element in the array
cont: jc skip ; If first element < than next element,
; Yes: goes to specified location
mov a,r4 ; No: copy next element as smallest
skip: mov r3,a ; Storing smallest value in R3
djnz r5,bck ; Decrement count value and repeat the operation for next
comparison
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program

; This program is used to find the smallest element in the array.


Array starts from location 0x30
30h - 0x20
31h - 0x30
32h - 0x50
33h - 0x40
34h - 0x10
smallest element R3 - 10

Department of Electrical & Electronics Engineering


18
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

; In 8051 we don’t have simple compare instruction, at first the numbers are checked for
equality if both the elements in the array are equal no task is performed.

; "cjne" instruction is used to check for equality of both the numbers i.e.
"cjne a, directaddr., location"

; First element is copied to R3 & second element to R4, initially the value in R3 is
assumed as smallest and it is compared with next number in R4

; If R3 < R4
Yes: continue comparing with next elements
No: copy r4 to r3 and r3 is treated as smallest, comparing with next elements done

; repeat the compare operation until the specified number of elements

Alternate method:
org 0000h
mov dptr,#9000h
mov r0,#05h
dec r0
movx a,@dptr
mov 7fh,a
back2: inc dptr
movx a,@dptr
cjne a,7fh,back1
sjmp back3
back1: jnc back3
mov 7fh,a
back3: djnz r0,back2
mov a,7fh
end

Department of Electrical & Electronics Engineering


19
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

2. Arithme tic I nstruc ti ons – Wri ti ng A LPs for:


Addi ti on, Subtrac ti on, Mul ti plic ation a nd Division i nvo lvi ng 8 bit a nd 16 bi t
n u m b e r s . F i n d i n g t h e s q u a r e a n d c u b e o f a g i v e n n u m b e r.

a. ALP to perform the addition of two 8-bit numbers


Note: Numbers are stored from the location 40h [internal RAM]

org 0000 ; Defining the program to start from location 0000


mov r0,#0x40 ; Initializing r0 as a memory pointer to point to first number
mov r4,#00 ; Initializing a register as a carry pointer
bck: mov a,@r0 ; Reads first number from the specified location
inc r0 ; Points to next number in the location
add a,@r0 ; Performs the addition of 2 numbers
jnc skip ; Checks for the carry resulted from the addition
inc r4 ; If addition results in carry increment carry pointer
skip: inc r0 ; Points to the location where is sum value stored
mov @r0,a ; Stores the sum value
inc r0 ; Points to the location where carry value is stored
mov a,r4
mov @r0,a ; Storing the carry value
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program

; This program performs the addition of 2 8-bit number

40h - 25 ; first number

41h - 25 ; second number

42h - 4A ; sum

43h - 00 ; carry

Department of Electrical & Electronics Engineering


20
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

b. ALP to perform 8-bit subtraction and specifying both negative & positive
difference value

org 0000

mov r0,#0x40 ; Reading the address of 8-bit subtrahend

mov r5,#00 ; Pointer to indicate the status of the result

mov a,@r0 ; Reads 8-bit subtrahend value

inc r0 ; Points to next location to read minuend value

clr c ; For subtraction first we have clear the C-flag

subb a,@r0 ; Subtracts the value of minuend from subtrahend

jnc skip ; Checks for whether borrow required for subtraction

dec r5 ; If no borrow, the result is positive

; If borrow required result is negative and will be 2's complement


value
; Value 0xff is stored in pointer reg r5

skip: inc r0 ; Points to next location to store difference value

mov @r0,a ; Storing of difference value

exit: sjmp exit

end

Ex: 1. 40h - 08H ; first number

41h - 02H ; second number

42h - 06H ; Difference

R5 - 00H : Borrow

2. 40h - 02H ; first number

41h - 08H ; second number

42h - FAH ; Difference (2's Compliment)


Department of Electrical & Electronics Engineering
21
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

R5 - FFH : Borrow (2's Compliment)

c. ALP to perform 16-bit addition.


org 0000
mov r0,#0x30 ; Reading the address of data
mov r6,#00 ; Pointer to indicate the status of the result
mov a,@r0 ; Reads lower order byte value of first 16-bit data
mov r2,a ; Copies lower order byte to a register
inc r0 ; Points to next location
mov a,@r0 ; Reads higher order byte value of first 16-bit data
mov r3,a ; Copies higher byte value to the register
inc r0 ; Points to next location
mov a,r2
add a,@r0 ; Add the lower order bytes of first and second 16 bit data
mov r4,a ; Copies lower bytes result
mov a,r3 ; Copies higher byte value to the accumulator
inc r0
addc a,@r0 ; Add the higher order bytes of first and second 16 bit data
jnc skip ; Checks for whether carry is generated or not
inc r6 ;
skip: mov r5,a ; Copies higher order result
inc r0 ; Points to next location to store higher order sum value
mov a,r4 ; Lower order sum value
mov @r0,a ; Storing of lower order difference value
inc r0
mov a,r5
mov @r0,a ; Storing of higher order difference value
exit: sjmp exit
end

Input: 30h - 21H ; first number (LSB) Result:34h -32H ; Sum (LSB)

31h - 43H ; first number (MSB) 35h -55H : Diff (MSB)

Department of Electrical & Electronics Engineering


22
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

32h - 11H ; second number (LSB)

33h - 12H ; second number (MSB)

d. ALP to perform 16-bit subtraction and specifying both negative & positive
difference value

org 0000

mov r0,#0x30 ; Reading the address of 8-bit subtrahend

mov r6,#00 ; Pointer to indicate the status of the result

mov a,@r0 ; Reads lower order byte value of 16-bit subtrahend value

mov r2,a ; Copies lower order byte subtrahend to a register

inc r0 ; Points to next location

mov a,@r0 ; Reads higher order byte value of 16-bit subtrahend value

mov r3,a ; Copies higher byte subtrahend value to e register

inc r0 ; Points to next location

mov a,r2

clr c ; Clear C- flag

subb a,@r0 ; Subtracts the lower order byte value of minuend from lower
order subtrahend

mov r4,a ; Copies lower byte difference

mov a,r3

inc r0

subb a,@r0 ; Subtracts the higher order byte value of minuend from higher
; order subtrahend with borrow

jnc skip ; Checks for whether borrow required for subtraction

dec r6 ; If no borrow, the result is positive

; If borrow required result is negative and will be 2's complement

; Value 0xff is stored in pointer reg r6

Department of Electrical & Electronics Engineering


23
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

skip: mov r5,a ; Copies higher order difference

inc r0 ; Points to next location to store difference value

mov a,r4 ; Lower order difference

mov @r0,a ; Storing of lower order difference value

inc r0

mov a,r5

mov @r0,a ; Storing of higher order difference value

exit: sjmp exit

end

; This program performs subtraction of two 8-bit numbers. The subtraction operation is
done by using "Subb" instruction

Different formats of subtract instructions are as follows:

a. SUBB A,Rn Subtract register from accumulator with borrow

b. SUBB A,dir8 Subtract direct byte from accumulator with borrow

c. SUBB A,@Ri Subtract indirect byte from accumulator with borrow

d. SUBB A,#data8 Subtract immediate data from accumulator with borrow

Eg:
43 21
12 11
31 10

Input: 30h - 21H ; first number (LSB)

31h - 43H ; first number (MSB)

32h - 11H ; second number (LSB)

33h - 12H ; second number (MSB)

Result: 34h - 10H ; Difference (LSB)

35h - 31H : Difference (MSB)

Department of Electrical & Electronics Engineering


24
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

e. PROGRAM FOR MULTIPLICATION OF TWO 8 BIT NUMBERS .(OR)


PROGRAM FOR FINDING SQUARE OF AN 8 BIT NUMBER

ORG 0000h ;
MOV A, 30h ; Take one of the numbers in A. (p x q = q x p)
MOV B, 31h ; Take the other number in B. Use of A and B is a must.

MUL AB ; After execution of this statement, A LB & B HB

MOV P0, A ; ( note comma in ADD A,B and no comma in MUL AB )


MOV P1, B ; observe the flags CY and OV
here: SJMP here ;
END ;

f. PROGRAM FOR MULTIPLICATION OF THREE 8 bit NUMBERS .OR.


PROGRAM FOR FINDING CUBE OF AN 8 BIT NUMBER
Finding the product: FAh x FBh x FCh.
N o t e : (set the procedure/workout in your mind first for program writing).
ORG 0000h ; 40 41 42h
First stage multiplication. h h
FC FB FA h
MOV A, 42h ; h 47h h 48 49
MOV B, 41h ; h h
FA FB FC
h h h
MUL AB ; A LB and B HB. But

neither will be in final result.

MOV R0, B ; Le ave alone A for immediate x n; R0 for later xn.

Second stage multiplication.


MOV B, 40h ;
MUL AB ;
MOV 49h, A ; This forms one of the outputs. This is LB of result .

MOV R1, B ; R1 B , i t i s r e q u i r e d l a t e r.

Stage 3 multiplication.
MOV A, R0 ;
MOV B, 40h ;

Department of Electrical & Electronics Engineering


25
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

MUL AB ;
ADD A, R1 ;
MOV 48h, A ;
JNC next ; (during above addition, to account for the carry.,)
INC B ; ie., add carry if resulted. Carry is always 1h in hex.
Next: NOP ;
MOV 47h, B ;
here: SJMP here ; END ;

g. PROGRAM FOR DIVISION INVOLVING TWO 8 BIT NUMBERS.

ORG 0000h ;
MOV A, 50h ; rule: Dividend is taken in A.
MOV B, 51h ; rule: Divisor is taken in B.

DIV AB ; A is divided by B. A quotient, B remainder. Observe PSW.


MOV R0, A ; Move the quotient to register R0.
MOV R1, B ; Move the remainder to register R1.
here: SJMP here ;
END ;

Department of Electrical & Electronics Engineering


26
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

4. Wri ti ng ALPs for : C ou nte rs: Bi na ry C ou nte rs, Decim al C ou nters e tc.,
5. Wri ti ng ALPs for : Unde rsta ndi ng conditi ona l CA LL and RETURN i nstr uctions.

a. COUNTERS: BINARY UP COUNTER  / BINARY DOWN COUNTER


ORG 0000h ;
MOV A, #00h ; #0FFh for down counter.
COUNT: MOV P0, A ;
ACALL DELAY ;
INC A ; DEC A instruction for down counter.
JNZ COUNT ; JNZ target. Jump if accumulator is not zero.
HERE: SJMP HERE ;
; Time Del ay su brouti ne for a pprox. 1 0 sec. del ay
DELAY: MOV R1, #0AAh ; (use #0FF for longer time delays).
LOOP3: MOV R2, #0AAh ;
LOOP2: MOV R3, #0AAh ;
LOOP1: DJNZ R3, LOOP1 ; complete innermost loop first.
DJNZ R2, LOOP2 ; complete the middle loop incl. the first for every R2
DJNZ R1, LOOP3 ;
RET ; RETURN TO THE MAIN PROGRAM.
END

Department of Electrical & Electronics Engineering


27
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual


b. C O U N T E R S : BCD UP COUNTER / BCD DOWN COUNTER*

ORG 0000h ;
MOV A, #00h ;
COUNT: MOV P0, A ;
ACALL DELAY ;
ADD A, #01h ; ADD A, #99h for down counter.
DA A ; decimal adjust the value
JNZ COUNT ;
HERE: SJMP HERE ;
; Time Del ay su brouti ne for a pprox. 1 0 sec. del ay
DELAY: MOV R1, #0AAh ; (use #0FF for longer time delays).
LOOP3: MOV R2, #0AAh ;
LOOP2: MOV R3, #0AAh ;
LOOP1: DJNZ R3, LOOP1 ; complete innermost loop first.
DJNZ R2, LOOP2 ; complete the middle loop incl. the first for every R2
DJNZ R1, LOOP3 ;
RET ; RETURN TO THE MAIN PROGRAM.
END

c. ALP to perform operation of 8-bit Up-Down counter


org 0000h
mov r6,#00h
up: inc r6
mov P1,r6
call delay
cjne r6,#0ffh,up
down: dec r6
mov P1,r6
Department of Electrical & Electronics Engineering
28
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

call delay
cjne r6,#00h,down
sjmp up

delay: mov r0,#0fh


d0: mov r1,#0ffh
d1: mov r2,#0ffh
d2: djnz r2,d2
djnz r1,d1
djnz r0,d0
ret
end

6. Wri ti ng ALPs for :


a. Boolean and Logical Instructions (bit manipulations).
ORG 0000H ; (D:41h and enter values in memory locations)
MOV A, 41h ; Let A register contain one num for Logical operation
MOV B, 42h ; and B Register contains the other number.
MOV R1, 43h ; R1= 1h/ANL; R1= 2h/ORL; R1= 3h/XRL; R1= 4h/NOT
CJNE R1, #01h, ORop ;( 01h and 1d both are equal.)
ANL A, B ;
SJMP LAST ;
ORop : CJNE R1, #02h, XORop ;
ORL A, B ;
SJMP LAST ;
XORop: CJNE R1, #03h, NOTop ;
XRL A, B ;
SJMP LAST ;
NOTop: CJNE R1, #04h, NILop ;
CPL A ;
SJMP LAST ;
NILop: CLR A ;
CLR B ;
CLR 43h ;
LAST: MOV 44H, A ; 44h memory location holds the result
HERE: SJMP HERE ;
END ;

b. # ALP to find number of 1’s and 0’s in a given byte

org 0000h
mov a,#76h
mov r0,#00h
mov r1,#00h
mov r2,#08h
clr c
back: rrc a
jc ones
inc r1
djnz r2, back
Department of Electrical & Electronics Engineering
29
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

axis1: sjmp axis1


ones: inc r0
djnz r2,back
mov 30h,r0
mov 31h,r1
axis: sjmp axis
end

c . ALP to find number of even numbers and odd numbers from given array

org 0000h
mov r0,#40h
mov r1,#00h
mov r2,#00h
mov r3,#08h
back: mov a,@r0
clr c
rlc a
jc odd
inc r1
inc r0
djnz r3, back
axis1: sjmp axis1
odd: inc r2
inc r0
djnz r3,back
axis: sjmp axis
end

Department of Electrical & Electronics Engineering


30
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

6. Wr i t i n g A L P s f o r :
Unde rsta ndi ng Code conver si on program s – BC D to ASC II, AS CII to BCD, AS CII to
decimal, Dec imal to ASCII, Hexa to dec imal and Decimal to Hexa

a. ALP to perform conversion of BCD to ASCII

org 0000

mov a,30h ; initialize the data from memory location

mov r1,a ; copy contents of ‘a’ BCD value in r1

anl a,#0fh ; mask higher nibble

orl a,#30h ;

mov r7,a ; copy in r7

mov a.r1 ;

anl a,#0f0h

rr a

rr a

rr a ; swap the lower and higher nibble of the accumulator

rr a

orl a,#30h

mov r1,a

end

Department of Electrical & Electronics Engineering


31
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

input: Output

A=16H R7=36h
R1=31

Note: Instead of rotate right (RR ) instruction, we can use SWAP instruction. (swap a)

b. ALP to perform conversion from ASCII to DECIMAL

org 0000h

mov r5,#“5” ; save the immediate ASCII value of “5” in r5

mov r6,#”9” ; save the immediate ASCII value of “9” in r6

mov a,r5 ; load the accumulator from r5

anl a,#0fh ; mask higher nibble

mov r5,a ; save the masked value in r5

mov a,r6 ; load the accumulator from r6

anl a,#0fh ; mask higher nibble

mov r6,a ; save the masked value in r6

mov a,r5 ; mov fist value from r5 to accumulator

rl a

rl a

rl a ; swap the accumulator value

rl a

orl a,r6 ; OR the both values

mov P1,a

Department of Electrical & Electronics Engineering


32
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

last: sjmp last

end

Input: 5, 9 Output: 59 at Port1

c. ALP to perform conversion of HEXADECIMAL to DECIMAL

org 0000

mov a,30h ; initializing the data from memory

mov r0,#52h ; memory location to store the result

mov b,#10 ; initialize 'b' value

div ab ; quotient in "a" register, remainder in "b" register

mov @r0,b ;

mov b,#10

div ab

dec r0

mov @r0,b

dec r0

mov @r0,a

last: sjmp last

end

Input Output
Department of Electrical & Electronics Engineering
33
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

(30) = FB h(251d) (50) = 2


(51) = 5
(52) = 1

d. ALP to perform conversion from DECIMAL to HEXADECIMAL


org 0000
mov r5,#00h
mov r4,#00h
mov a,#251 ; initializing accumulator with decimal value
mov b,#02 ; initialize ‘b register ’ by 2
clr c
div ab ; quotient in “a” register, remainder in “b” register
mov r5,a ;
mov a,b
orl a,r4
mov r4,a
mov b,#02
mov a,r5
div ab
mov r5,a
mov a,b
rl a
orl a,r4
mov r4,a
mov b,#02
mov a,r5
div ab
mov r5,a
mov a,b
rl a
rl a
orl a,r4
mov r4,a
mov b,#02
mov a,r5
div ab
mov r5,a
Department of Electrical & Electronics Engineering
34
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

mov a,b
rl a
rl a
rl a
orl a,r4
mov r4,a
mov a,r5
swap a
orl a,r4
mov P1,a
here: sjmp here
end

e. ALP to perform conversion from ASCII to BCD

org 0000h

mov r5,#“4” ; save the immediate ASCII value of “4” in r5

mov r6,#”7” ; save the immediate ASCII value of “7” in r6

mov a,r5 ; load the accumulator from r5

anl a,#0fh ; mask higher nibble

mov r5,a ; save the masked value in r5

mov a,r6 ; load the accumulator from r6

anl a,#0fh ; mask higher nibble

mov r6,a ; save the masked value in r6

mov P1,r5 ; mov fist value from r5 to accumulator

mov P2,r6

last: sjmp last

end

Input: ASCII value 0f 4 (34) Output: at Port1 = 4


ASCII value 0f 7 (37) at port 2 = 7

Department of Electrical & Electronics Engineering


35
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

7. Wri ti ng ALPs for:


Generation of time-delay(delay programs), Generation of time delay(use of
on chip timer/counter), serial port communication.

Department of Electrical & Electronics Engineering


36
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Interfacing Programs

Department of Electrical & Electronics Engineering


37
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

PROGRAM 01:

A. Blinking of LED

org 0000h
mov P2,#00h ; P0—initialize as output port
back: mov a,#0aah
mov P2,a ; send 0xaa to P0
acall delay ; call delay
mov a,#55h
mov P2,a ; send 0x55 to P0
acall delay
sjmp back

delay: mov r1,#0ffh


d3: mov r2,#0ffh ; delay Program
d2: djnz r2,d2
djnz r1,d3
ret
end

B. ALP to perform up counter program using i/p and o/p devices

org 0000h
mov P2,#00h
mov a,#00h
next: mov P2,a
acall dealy
inc A
sjmp next
delay: mov r1,#0ffh

Department of Electrical & Electronics Engineering


38
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

d3: mov r2,#0ffh


d2: djnz r2,d2
djnz r1,d3
ret
end

C. ALP to perform down counter program using i/p and o/p devices

org 0000h
mov P2,#00h
mov a,#0ffh
next: mov P2,a
acall dealy
dec a
sjmp next
delay: mov r1,#0ffh
d3: mov r2,#0ffh
d2: djnz r2,d2
djnz r1,d3
ret
end
PROGRAM 02:
Aim: Interfacing of 8051 and stepper motor.
/*Program to control Stepper Motor Direction*/

#include <reg52.h>
#include <intrins.h>
sbit START = P1^0;
sbit STOP = P1^4;
#define MOTOR P2
unsigned char run=1;
unsigned char del=2;
/*------------------------------------------------
Stepper delay
------------------------------------------------*/
void stp_delay(unsigned char dly)
{
unsigned int i,j;
for (j = 0;j<dly;j++)
for(i = 0;i<100;i++);
}

/*------------------------------------------------
Key functions
------------------------------------------------*/
void chk_key(void)
{
if (START == 0) //Start key pressed
{
run = 1;
Department of Electrical & Electronics Engineering
39
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

goto rotate;
}
if (STOP == 0) //Stop key pressed
{
run = 0;
goto rotate;
}

rotate:
_nop_();
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char stpval=1;

START = 1; // all keys as inputs


STOP = 1;
MOTOR = 0x00;
P3=0x00;
while (1)
{
chk_key();
if (run == 1)
{
{
if (stpval == 8) //rotate step value bitwise left
stpval = 1;
else
stpval <<= 1;
}

MOTOR = stpval;
stp_delay(del);
}
}
}

Department of Electrical & Electronics Engineering


40
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

PROGRAM 03:
Aim: Interfacing of 8051 and DC Motor
/* Progaram to control DC Motor Direction */
#include <reg52.h>
#include <intrins.h>
sbit DIR1 = P2^0;
sbit DIR2 = P2^1;
sbit PWM = P3^0;

sbit AKMIN = P1^0; //Anti Clockwise


sbit CKMIN = P1^4; //Clockwise
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char MOTOR;
unsigned char i;
IE=0x00;
P3=0x00;
AKMIN = 1; // All Ports as Inputs
CKMIN = 1;
MOTOR=0;
DIR1=1; DIR2=1; //Motor stopped
PWM=1;

while (1)
{
Department of Electrical & Electronics Engineering
41
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

if (AKMIN == 0) //Anti Clockwise Minimum speed


{
MOTOR = 10;
DIR1=1; DIR2=0;

goto quit;
}

if (CKMIN == 0) //Clockwise Minimum speed


{
MOTOR = 10;
DIR1=0; DIR2=1;
goto quit;
}
quit:
PWM=0;
for (i=0;i<MOTOR;i++)
{
_nop_(); _nop_(); _nop_();
}
PWM=1;
}}
PROGRAM 04: AIM: Sending Message to LCD (Alternate Program)
#include<reg52.h>
sfr ldata = 0x80; //Variables declaration for LCD
sbit rs = P3^3;
sbit en = P3^2;
void lcdcmd (unsigned char value); //LCD command
void lcddata (unsigned char value); //LCD data
void delay(unsigned int itime); //Delay
unsigned char msg1[] = "ACHARYA INSTITUTES";
unsigned char msg2[] = "DEPT.OF EEE-VLKR";
void main()
{
unsigned int i;
lcdcmd(0x38); //LCD command
lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
while(1)
{
delay(100);
lcdcmd(0x80);
for(i=0;i<20;i++)
{
lcddata( msg1[i]);
}
lcdcmd(0xC0);
for(i=0;i<18;i++)
{
lcddata( msg2[i]);
}

Department of Electrical & Electronics Engineering


42
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

}
}
void lcdcmd (unsigned char value)
{
ldata = value; //issue command code
rs = 0; //RS=0 for command
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0 ,latch in
return;
}
void lcddata (unsigned char value)
{
ldata = value; //issue data
rs = 1; //RS=1 for data
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0, latch in
return;
}
void delay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++); }
PROGRAM 05:
Aim: Interfacing of 8051 and DAC to generate different types of waveforms.
A. Generation of Ramp wave using DAC
/* Program to generate ramp wave using DAC*/
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR = P3^0; //DAC Write Pin

void delay(unsigned char tim)


{
unsigned char i,j;

for (j=0;j<tim;j++)
for (i=0; i<10;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval;

P3=0x00;
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
Department of Electrical & Electronics Engineering
43
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

dval = 0x00;

while (1)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); //varying this value will wary the frequency of the waveform
dval++;
}
}

B. Generation of Square wave using DAC


/*Program to generate Square wave using DAC with Variable Amplitude /Frequency */

#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin

void delay(unsigned char tim)


{
unsigned char i,j;

for (j=0;j<tim;j++)
for (i=0; i<200;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char amp;

P3 = 0x00;
DAC = 0x00;
amp = 0x7F; //vary the value to change the amplitude of the waveform

while (1)
{
Department of Electrical & Electronics Engineering
44
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

DAC = amp;
DWR = 0;
_nop_();
DWR = 1;
delay(1); //vary the value to change the frequency of the waveform

DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
}
}

C. Generation of Triangular wave using DAC


/*Generation of Triangular wave using DAC with variable Amplitude/Frequency*/

#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data
sbit DWR =P3^0; //DAC Write Pin

void delay(unsigned char tim)


{
unsigned char i,j;

for (j=0;j<tim;j++)
for (i=0; i<10;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval, amp;
P3=0x00;
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
amp= 0X7F; // vary the value to change the amplitude of the waveform
dval = 0x00;
Department of Electrical & Electronics Engineering
45
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

while (1)
{
while(dval != amp)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
dval++;
}
while(dval != 0x00)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
dval--;
}
}
}

D. Generation of Rectangular wave using DAC

/*Program to generate Rectangular wave using DAC with Variable Amplitude /Frequency */

#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin

void delay(unsigned char tim)


{
unsigned char i,j;

for (j=0;j<tim;j++)
for (i=0; i<200;i++);
}

/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char amp;

P3 = 0x00;
DAC = 0x00;
amp = 0x7F; //vary the value to change the amplitude of the waveform

Department of Electrical & Electronics Engineering


46
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

while (1)
{
DAC = amp;
DWR = 0;
_nop_();
DWR = 1;
delay(2); //vary the value to change the frequency of the waveform

DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
}
}

E. Generation of Sawtooth wave using DAC


/* Program to generate sawtooth wave using DAC with variable Amplitude/Frequency*/

#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin

void delay(unsigned char tim)


{
unsigned char i,j;

for (j=0;j<tim;j++)
for (i=0; i<10;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval;

P3=0x00;
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;

Department of Electrical & Electronics Engineering


47
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

dval = 0x7F; //vary the value to change the amplitude of the waveform
while (1)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
dval--;
}
}

F. Generation of Sine wave using DAC


/* Progaram to generate Sine wave using DAC*/

#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin
unsigned char code tab[] =
{128,131,134,137,140,144,147,150,153,156,159,162,165,168,171,174,177,179,
182,185,188,191,193,196,199,201,204,206,209,211,213,216,218,220,222,224,
226,228,230,232,234,235,237,239,240,241,243,244,245,246,248,249,250,250,
251,252,253,253,254,254,254,255,255,255,255,255,255,255,254,254,254,253,
253,252,251,250,250,249,248,246,245,244,243,241,240,239,237,235,234,232,
230,228,226,224,222,220,218,216,213,211,209,206,204,201,199,196,193,191,
188,185,182,179,177,174,171,168,165,162,159,156,153,150,147,144,140,137,
134,131,128,125,122,119,116,112,109,106,103,100,97,94,91,88,85,82,79,77,74,7
1,68,65,63,60,57,55,52,50,47,45,43,40,38,36,34,32,30,28,26,24,22,21,19,17,16,15
,13,12,11,10,8,7,6,6,5,4,3,3,2,2,2,1,1,1,1,1,1,1,2,2,2,3,3,4,5,6,6,7,8,10,11,12,13,1
5,16,17,19,21,22,24,26,28,30,32,34,36,38,40,43,45,47,50,52,55,57,60,63,65,68,71
,74,77,79,82,85,88,91,94,97,100,103,106,109, 112,116,119,122,125};

void delay(unsigned char tim)


{
unsigned char i,j;

for (j=0;j<tim;j++)
for (i=0; i<10;i++);
Department of Electrical & Electronics Engineering
48
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval;
IE = 0x00;
DAC = 0x00;
P3=0x00;
dval = 0x00;
while (1)
{
DAC = tab[dval];
DWR = 0;
_nop_();
DWR = 1;
delay(1); //varying this value will vary the frequency of the wave
dval++;
}
}

PROGRAM 07:

Aim: Sending Character to Seven segment display.

/* Program to display 4 digit value on the 7-Seg Display */

#include <reg52.h>
#include <intrins.h>
sbit DSP1 = P3^4; //display 1 enable port line
sbit DSP2 = P3^5; //display 2 enable port line
sbit DSP3 = P3^6; //display 3 enable port line
sbit DSP4 = P3^7; //display 4 enable port line

#define DIGDAT P0 //common digit lines

unsigned char code digit[ ] =


{0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0xa,0x38,0x98,0x68,0x1c,0x1e};

/*------MAIN C function--------*/
void main (void)
{
unsigned int cnt = 1234; //value to be displayed
unsigned char dig1, dig2, dig3, dig4;
unsigned int temp;

DIGDAT = 0x00;

dig1 =cnt % 10;


temp = cnt / 10;
cnt=temp;
dig2= cnt%10;
Department of Electrical & Electronics Engineering
49
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

temp=cnt/10;
cnt=temp;
dig3= cnt%10;
temp=cnt/10;
cnt=temp;
dig4= cnt%10;

while(1)
{
DIGDAT = digit[dig1];
DSP1 = 1;
DSP1 = 0;

DIGDAT = digit[dig2];
DSP2 = 1;
DSP2 = 0;

DIGDAT = digit[dig3];
DSP3 = 1;
DSP3 = 0;

DIGDAT = digit[dig4];
DSP4 = 1;
DSP4 = 0;
}
}

PROGRAM 08:
Aim: Interfacing of 8051 and ADC.

/*Progam to display analog voltage in Seven segment Display using ADC*/

#include <reg52.h>
#include <intrins.h>
sbit DSP1 = P3^4; //display 1 enable port line
sbit DSP2 = P3^5; //display 2 enable port line
sbit DSP3 = P3^6; //display 3 enable port line
sbit DSP4 = P3^7; //display 4 enable port line

#define DIGDAT P0 //common digit lines of seven segment display

unsigned char code digit[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0xf7};

sbit ASTART = P3^0; // ADC Start Conversion Output


sbit AEOC = P3^1; // ADC End of Conversion Input
#define ADATA P2 // ADC Data Input
/*------------------------------------------------
Read ADC C function
------------------------------------------------*/
unsigned char rdadc()
{
ASTART = 0;
_nop_();
_nop_();
Department of Electrical & Electronics Engineering
50
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

_nop_();
ASTART = 1;
_nop_();
_nop_();
_nop_();

while(AEOC == 1);

return(ADATA);
}
void delay10ms() //05 msec
{
unsigned int m,n;
for(n=0;n<2;n++)
for(m=0;m<600;m++);
}
void ssegdisp(unsigned char dig1,dig2)
{
DIGDAT = digit[dig1];
DSP1 = 1;
delay10ms();
DSP1 = 0;
DIGDAT = digit[6];
DSP2 = 1;
delay10ms();
DSP2 = 0;
DIGDAT = digit[dig2];
DSP3 = 1;
delay10ms();
DSP3 = 0;
}

/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char aval, dig1,dig2;
P3=0x00;
ADATA=0xFF;
ASTART=0;
AEOC=0;

while (1)
{
aval=rdadc();
delay10ms();
delay10ms();

if((aval<0x19))
Department of Electrical & Electronics Engineering
51
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

{
dig1=0;
dig2=0;
ssegdisp(dig1,dig2);
}
if((aval>0X19)&&(aval<0X32))
{
dig1=5;
dig2=0;
ssegdisp(dig1,dig2);
}
if((aval>0X32)&&(aval<0X4b))
{
dig1=0;
dig2=1;
ssegdisp(dig1,dig2);
}
if((aval>0X4b)&&(aval<0X64))
{
dig1=5;
dig2=1;
ssegdisp(dig1,dig2);
}
if((aval>0X64)&&(aval<0X7d))
{
dig1=0;
dig2=2;
ssegdisp(dig1,dig2);
}
if((aval>0X7d)&&(aval<0X96))
{
dig1=5;
dig2=2;
ssegdisp(dig1,dig2);
}
if((aval>0X96)&&(aval<0Xaf))
{
dig1=0;
dig2=3;
ssegdisp(dig1,dig2);
}
if((aval>0Xaf)&&(aval<0Xc8))
{
dig1=5;
dig2=3;
ssegdisp(dig1,dig2);
}
if((aval>0Xc8)&&(aval<0Xe1))
{
dig1=0;
Department of Electrical & Electronics Engineering
52
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

dig2=4;
ssegdisp(dig1,dig2);
}
if((aval>0Xe1)&&(aval<0Xfa))
{
dig1=5;
dig2=4;
ssegdisp(dig1,dig2);
}
if((aval>0Xfa))
{
dig1=0;
dig2=5;
ssegdisp(dig1,dig2);
}
}
}

Instruction in simplified format

Sl.No. DATA MOVEMENT INSTRUCTION


01. MOV A,#n MOV A,add MOV @Rp,#n
02. MOV A,Rr MOV add,A MOV @Rp,add
03. MOV Rr,A MOV Rr,add MOV @Rp,A
04. MOV Rr,#n MOV add,Rr MOV add,@Rp
05. MOV DPTR,#nn MOV add,#n MOV A,@Rp
06. MOV add1,add2

Sl.No. EXTERNAL DATA MOVEMENT & EXCHANGE INSTRUCTIONS


01. MOVX A,@Rp MOVC A,@A+DPTR XCH A,Rr
02. MOVX A,@DPTR MOVC A,@A+PC XCH A,add
03. MOVX @Rp,A PUSH add XCH A,@Rp
04. MOVX @DPTR,A POP add XCHD A,@Rp

Sl.No. BYTE LEVEL LOGICAL INSTRUCTIONS


01. ANL A,#n ORL A,#n XRL A,#n
02. ANL A,add ORL A,add XRL A,add
03. ANL A,Rr ORL A,Rr XRL A,Rr
04. ANL A,@Rp ORL A,@Rp XRL A,@Rp
05. ANL add,A ORL add,A XRL add,A
06. ANL add,#n ORL add,#n XRL add,#n
Department of Electrical & Electronics Engineering
53
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

Sl.No. BIT LEVEL LOGICAL ROTATE $ SWAP INCREMENT & DECRE


01. ANL C,b RL A INC A
02. ANL C,/b RLC A INC Rr
03. ORL C,b RR A INC add
04. ORL C,/b RRC A INC @Rp
05. CPL C SWAP A INC DPTR
06. CPL b CLR A DEC A
07. CLR C CPL A DEC Rr
08. CLR b NOP DEC add
09. MOV C,b DEC @Rp
10. MOV b,C
11. SETB C
12. SETB b

Sl.No. ADDITION ADDITION WITH CARRY SUBTRACTION


01. ADD A,#n ADDC A,#n SUBB A,#n
02. ADD A,Rr ADDC A,Rr SUBB A,add
03. ADD A,add ADDC A,add SUBB A,Rr
04. ADD A,@Rp ADDC A,@Rp SUBB A,@Rp
MULTIPLICATION DIVISION DECIMAL ADJUSTMEN
01. MUL AB DIV AB DA A

Sl.No. JUMP,CALL & RETURN INSTRUCTIONS


01. JC radd CJNE A,add,radd JMP @A+DPTR
02. JNC radd CJNE A,#n,radd AJMP sadd
03. JB b,radd CJNE Rn,#n,radd LJMP ladd
04. JNB b,radd CJNE @Rp,#n,radd SJMP radd
05. JBC b,radd DJNZ Rn,radd ACALL sadd
06. JZ radd DJNZ add,radd LCALL ladd
07. JNZ radd RET RETI

ACSII CHART
ACSII VALUE CHARACTER ACSII VALUE CHARECTER ACSII VALUE CHAREC
TER
48(30) 0 65(41) A 97(61) a
49(31) 1 66(42) B 98(62) b
50(32) 2 67(43) C 99(63) c
51(33) 3 68(44) D 100(64) d
52(34) 4 69(45) E 101(65) e
53(35) 5 70(46) F 102(66) f
54(36) 6 71(47) G 103(67) g
55(37) 7 72(48) H 104(68) h
Department of Electrical & Electronics Engineering
54
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

56(38) 8 73(49) I 105(69) i


57(39) 9 74(4A) J 106(6A) j
75(4B) K 107(6B) k
76(4C) L 108(6C) l
77(4D) M 109(6D) m
78(4E) N 110(6E) n
79(4F) O 111(6F) o
80(50) P 112(70) p
81(51) Q 113(71) q
82(52) R 114(72) r
83(53) S 115(73) s
84(54) T 116(74) t
85(55) U 117(75) u
86(56) V 118(76) v
87(57) W 119(77) w
88(58) X 120(78) x
89(59) Y 121(79) y
90(5A) Z 122(7A) z

Note: 65 (41) ------ A


Decimal(hex) ------ Character

Microcontrollers Special Function Registers

Special function
S.No. Address Description
Register
01. 80* Port-0 Port-0 Latch
02. 90* Port-1 Port-1 Latch
03. A0* Port-2 Port-2 Latch
04. B0* Port-3 Port-3 Latch
05. C0*
06. D0* PSW Program Status Word
07. E0* A Register A
08. F0* B Register B
09. 81 SP Stack Pointer
10. 82 DPL Data pointer Lower byte
11. 83 DPH Data pointer higher byte
12. 87 PCON Power Control Register
Department of Electrical & Electronics Engineering
55
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

13. 88* TCON Timer Control Register


14. 98* SCON Serial Control Register
15. A8* IE Interrupt Enable Register
16. B8* IP Interrupt Priority Register
17. 89 TMOD Timer Mode Register
18. 99 SBUF Serial Buffer Register
19. 8A TL0 Timer‘0’ Lower Byte
20. 8B TL1 Timer‘1’ Lower Byte
21. 8C TH0 Timer‘0’ Higher Byte
22. 8D TH1 Timer‘1’ Higher Byte

Note: * → Byte and Bit addressable.

Internal RAM 128 Bytes:

Department of Electrical & Electronics Engineering


56
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

30 – 7F General Purpose Ram


2F 7F 7E 7D 7C 7B 7A 79 78
2E 77 76 75 74 73 72 71 70
2D 6F 6E 6D 6C 6B 6A 69 68
2C 67 66 65 64 63 62 61 60 Bit addressable
memory
2B 5F 5E 5D 5C 5B 5A 59 58 locations

2A 57 56 55 54 53 52 51 50
29 4F 4E 4D 4C 4B 4A 49 48
28 47 46 45 44 43 42 41 40
27 3F 3E 3D 3C 3B 3A 39 38
26 37 36 35 34 33 32 31 30
25 2F 2E 2D 2C 2B 2A 29 28
24 27 26 25 24 23 22 21 20
23 1F 1E 1D 1C 1B 1A 19 18
22 17 16 15 14 13 12 11 10
21 0F 0E 0D 0C 0B 0A 09 08
20 07 06 05 04 03 02 01 00
18 – 1F Register Bank 3
10 – 17 Register Bank 2
08 – 0F Register Bank 1
00 – 07 Register Bank 0

Special function Registers

Department of Electrical & Electronics Engineering


57
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

F8 FF

F0 B F7

E8 EF

E0 ACC E7

D8 DF

D0 PSW D7

C8 CF

C0 C7

B8 IP BF

B0 P3 B7

A8 IE AF

A0 P2 A7

98 SCON SBUF 9F

90 P1 97

88 TCON TMOD TL0 TL1 TH0 TH1 8F

80 P0 SP DPL DPH PCON 87

Viva Questions

1. What do you mean by Embedded System? Give examples.


Department of Electrical & Electronics Engineering
58
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

2. Why are embedded Systems useful?


3. What is Embedded Controller?
4. What is Microcontroller?
5. List out the differences between Microcontroller and Microprocessor.
6. How are Microcontrollers more suitable than Microprocessor for Real Time
Applications?
7. What are the General Features of Microcontroller?
8. Explain briefly the classification of Microcontroller.
9. Explain briefly the Embedded Tools.
10. Explain the general features of 8051
Microcontroller.
11. How many pin the 8051 has?
12. Differentiate between Program Memory and Data
Memory.
13. What is the size of the Program and Data memory?
14. Write a note on internal RAM. What is the
necessity of register banks? Explain.
15. How many address lines are required to address 4K of memory? Show the
necessary calculations.
16. What is the function of accumulator?
17. What are SFR’s? Explain briefly.
18. What is the program counter? What is its use?
19. What is the size of the PC?
20. What is a stack pointer (SP)?
21. What is the size of SP?
22. What is the PSW? And briefly describe the function
of its fields.
23. What is the difference between PC and DPTR?
24. What is the difference between PC and SP?
25. What is ALE? Explain the functions of the ALE in
8051.
26. Describe the 8051 oscillator and clock.
27. What are the disadvantages of the ceramic
resonator?
Department of Electrical & Electronics Engineering
59
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

28. What is the function of the capacitors in the


oscillator circuit?
29. Show with an example, how the time taken to execute an instruction can be
calculated.
30. What is the Data Pointer register? What is its use
in the 8051?
31. Explain how the 8051 implement the Harvard
Architecture?
32. Explain briefly the difference between the Von
Neumann and the Harvard Architecture.
33. Describe in detail how the register banks are
organized.
34. What are the bit addressable registers and what is
the need?
35. What is the need for the general purpose RAM
area?
36. Write a note on the Stack and the Stack Pointer.
37. Why should the stack be placed high in internal
RAM?
38. Explain briefly how internal and external ROM gets
accessed.
39. What are the different addressing modes
supported by 8051 Microcontroller ?
40. Explain the Immediate Addressing Mode.
41. Explain the Register Addressing Mode.
42. Explain the Direct Addressing Mode.
43. Explain the Indirect Addressing Mode.
44. Explain the Code Addressing Mode.
45. Explain in detail the Functional Classification of
8051 Instruction set
46. What are the instructions used to operate stack?
47. What are Accumulator specific transfer
instructions?

Department of Electrical & Electronics Engineering


60
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

48. What is the difference between INC and ADD


instructions?
49. What is the difference between DEC and SUBB
instructions?
50. What is the use of OV flag in MUL and DIV
instructions?
51. What are single and two operand instructions?
52. Explain Unconditional and Conditional JMP and
CALL instructions.
53. Explain the different types of RETURN
instructions.
54. What is a software delay?
55. What are the factors to be considered while
deciding a software delay?
56. What is a Machine cycle?
57. What is a State?
58. Explain the need for Hardware Timers and
Counters?
59. Give a brief introduction on Timers/Counter.
60. What is the difference between Timer and Counter
operation?
61. How many Timers are there in 8051?
62. What are the three functions of Timers?
63. What are the different modes of operation of
timer/counter?
64. Give a brief introduction on the various Modes.
65. What is the count rate of timer operation?
66. What is the difference between mode 0 and mode
1?
67. What is the difference Modes 0,1,2 and 3?
68. How do you differentiate between Timers and
Counters?
69. Explain the function of the TMOD register and its
various fields?
Department of Electrical & Electronics Engineering
61
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

70. How do you control the timer/counter operation?


71. What is the function of TF0/TF1 bit
72. Explain the function of the TCON register and its
various fields?
73. Explain how the Timer/Counter Interrupts work.
74. Explain how the 8051 counts using Timers and
Counters.
75. Explain Counting operation in detail in the 8051.
76. Explain why there is limit to the maximum external frequency that can be counted.
77. What’s the benefit of the auto-reload mode?
78. Write a short note on Serial and Parallel communication and highlight their
advantages and disadvantages.
79. Explain Synchronous Serial Data Communication.
80. Explain Asynchronous Serial Data
Communication.
81. Explain Simplex data transmission with examples.
82. Explain Half Duplex data transmission with
examples.
83. Explain Full Duplex data transmission with
examples.
84. What is Baud rate?
85. What is a Modem?
86. What are the various registers and pins in the 8051 required for Serial
communication? Explain briefly.
87. Explain SCON register and the various fields.
88. Explain serial communication in general (synchronous and asynchronous). Also
explain the use of the parity bit.
89. Explain the function of the PCON register during
serial data communication.
90. How the Serial data interrupts are generated?
91. How is data transmitted serially in the 8051?
Explain briefly.
92. How is data received serially in the 8051? Explain
briefly.
Department of Electrical & Electronics Engineering
62
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

93. Explain various modes of Serial Data Transmission?


94. Explain with a timing diagram the shift register
mode in the 8051.
95. What is the use of the serial communication mode
0 in the 8051?
96. Explain in detail the Serial Data Mode 1 in the
8051.
97. Explain how the Baud rate is calculated for the
Serial Data Mode 1.
98. How is the Baud rate for the Multiprocessor
communication Mode calculated?
99. Explain in detail the Multiprocessor
communication Mode in the 8051.
100. Explain the Serial data mode 3 in the 8051.
101. What are interrupts and how are they useful in
Real Time Programming?
102. Briefly describe the Interrupt structure in the
8051.
103. Explain about vectored and non-vectored
interrupts in general.
104. What are the five interrupts provided in the 8051?
105. What are the three registers that control and
operate the interrupts in 8051?
106. Describe the Interrupt Enable (IE) special function
register and its various bits.
107. Describe the Interrupt Priority (IP) special function
register and its need.
108. Explain in detail how the Timer Flag interrupts are
generated.
109. Explain in detail how the Serial Flag interrupt is
generated.
110. Explain in detail how the External Flag interrupts
are generated.

Department of Electrical & Electronics Engineering


63
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

111. What happens when a high logic is applied on the


Reset pin?
112. Why the Reset interrupt is called a non-maskable
interrupt?
113. Why do we require a reset pin?
114. How can you enable/disable some or all the
interrupts?
115. Explain how interrupt priorities are set? And how interrupts that occur
simultaneously are handled.
116. What Events can trigger interrupts, and where do
they go after getting triggered?
117. What are the actions taken when an Interrupt
Occurs?
118. What are Software generated interrupts and how
are they generated?
119. What is RS232 and MAX232? What is the function
of MAX232
120. What is the function of RS and E pins in an LCD?
121. What is the use of R/W pin in an LCD?
122. What is the significance of DA instruction?
123. What is packed and unpacked BCD?
124. What is the difference between CY and OV flag?
125. When will the OV flag be set?
126. What do you mean by step angle?
INSTRUCTIONS

ACALL: Absolute Call


ADD, ADDC: Add Acc. (With Carry)
AJMP: Absolute Jump
ANL: Bitwise AND
CJNE: Compare & Jump if Not Equal
CLR: Clear Register
CPL: Complement Register
DA: Decimal Adjust
DEC: Decrement Register
DIV: Divide Accumulator by B
DJNZ: Dec. Reg. & Jump if Not Zero
INC: Increment Register

Department of Electrical & Electronics Engineering


64
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

JB: Jump if Bit Set


JBC: Jump if Bit Set and Clear Bit
JC: Jump if Carry Set
JMP: Jump to Address
JNB: Jump if Bit Not Set
JNC: Jump if Carry Not Set
JNZ: Jump if Acc. Not Zero
JZ: Jump if Accumulator Zero
LCALL: Long Call
LJMP: Long Jump
MOV: Move Memory
MOVC: Move Code Memory
MOVX: Move Extended Memory
MUL: Multiply Accumulator by B
NOP: No Operation
ORL: Bitwise OR
POP: Pop Value From Stack
PUSH: Push Value Onto Stack
RET: Return From Subroutine
RETI: Return From Interrupt
RL: Rotate Accumulator Left
RLC: Rotate Acc. Left Through Carry
RR: Rotate Accumulator Right
RRC: Rotate Acc. Right Through Carry
SETB: Set Bit
SJMP: Short Jump
SUBB: Sub. From Acc. With Borrow
SWAP: Swap Accumulator Nibbles
XCH: Exchange Bytes
XCHD: Exchange Digits
XRL: Bitwise Exclusive OR

Augmented Programs

PROGRAM 09:
Aim: Interfacing of 8051 and Relay/ Buzzer.
A. Program to interface Buzzer.

#include <reg52.h>
#include <intrins.h>
sbit Buzzer = P3^0; //BUZZER CONTROL Pin
sbit ON = P1^0; //ON KEY
Department of Electrical & Electronics Engineering
65
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

sbit OFF = P1^4; //OFF KEY


/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
P2 = 0x00;
P3 = 0x00;
ON = 1; //Port as Input
OFF = 1; //Port as Input
while (1)
{
if (ON == 0) //ON key pressed
{
Buzzer=0;
}

if (OFF == 0) //OFF key pressed


{
Buzzer=1;
}
}
}

AIM: Key pad interfacing program


#include <reg52.h>
#include <intrins.h>
void lcddisplay();

sfr ldata = 0x80; //Variables declaration for LCD


sbit rs = P3^3;
sbit en = P3^2;
//--------------Funtion declaration
void lcdcmd (unsigned char value); //LCD command
void lcddata (unsigned char value); //LCD data
void delay(unsigned int itime); //Delay

Department of Electrical & Electronics Engineering


66
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

unsigned char msg1[] = "ACHARYA INSTITUTE ";


unsigned char msg2[] = "KEY PRESSED = ";

#define ROWPORT P1 //P1.0 - P1.3 are Inputs


sbit COL1 = P1^4;
sbit COL2 = P1^5;
sbit COL3 = P1^6;
sbit COL4 = P1^7;
/*---------MAIN C function ------------------------------------------------*/
main ()
{
unsigned char key, i,j;
unsigned char rval[] = {0xFE,0xFD,0xFB,0xF7,0x0};
unsigned char keyPadMatrix[] =
{
'F','B','7','3',
'E','A','6','2',
'D','9','5','1',
'C','8','4','0'
};
COL1 = 1;
COL2 = 1;
COL3 = 1;
COL4 = 1;

IE=0x00;
P3=0x00;
lcdcmd(0x38); //LCD command
lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
for(j=0;j<16;j++)
{
lcddata( msg1[j]);
}
lcdcmd(0xC0);
for(j=0;j<16;j++)
{
lcddata( msg2[j]);
}
while (1)
{
key = 0;
for( i = 0; i < 4; i++ )
{
// turn on row output
ROWPORT = rval[i];

// read colums - break when key press detected


if( COL1 == 0)
break;

Department of Electrical & Electronics Engineering


67
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual

key++;
if( COL2 == 0)
break;
key++;
if( COL3 == 0)
break;
key++;
if( COL4 == 0)
break;
key++;
}
lcdcmd(0xCF);
lcddata(keyPadMatrix[key]);
}
}
void lcdcmd(unsigned char value)
{
ldata = value; //issue command code
rs = 0; //RS=0 for command
// rw = 0; //R/W=0 to write to LCD
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0 ,latch in
return;
}
void lcddata (unsigned char value)
{
ldata = value; //issue data
rs = 1; //RS=1 for data
// rw = 0; //R/W=0 to write to LCD
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0, latch in
return;
}
void delay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}

Department of Electrical & Electronics Engineering


68
Acharya Institute of technology

You might also like