You are on page 1of 127

CONTENTS

EXP.NO NAME OF THE EXPERIMENT PAGE.NO


1 ADDITION OF TWO 8-BIT NUMBERS 1-2
2 ADDITION OF TWO 16-BIT NUMBERS 3-4
3 ADDITION OF TWO 32-BIT NUMBERS 5-6
4 16-BIT ADDITION WITH THE OPREANDS PRESENT IN THE ROM 7-8
5 8-BIT SUBTRACTION 9-10
6 16-BIT SUBTRACTION 11-12
7 ADDITION OF TEN 8-BIT NUMBERS 13-14
8 TO FIND THE SQUARE OF THE NUMBERS 15-16
9 ADDITION OF TWO SIGNED NUMBERS 17-18
10 ADDITION OF PACKED BCD NUMBERS 19-20
11 SETTING THE ODD BITS OF THE ACCUMULATOR 21
12 MASK THE LOWER NIBBLE OF THE ACCUMULATOR 22
13 TO SEND '1' ON P1.2 WHEN '0AAH' IS RECEIVED ON P0 23-24
14 TO FIND THE LARGEST OF THE GIVEN SET OF NUMBERS 25-26
15 TO FIND THE SMALLEST OF THE GIVEN SET OF NUMBERS 27-28
16 MULTIPLICATION OF 8-BIT NUMBERS 29-30
17 LCM OF THE GIVEN SET OF NUMBERS 31-32
18 BLOCK TRANSFER OF DATA 33-34
19 EXCHANGE OF DATA 34-36
20 VERIFICATIOB OF NUMBERS READ FROM PORT-1 37-39
21 PACKED BCD TO UNPACKED BCD 40-41
22 PACKED BCD TO ASCII 42-43
23 ASCII TO PACKED BCD 44-45
24 8-BIT BINARY NUMBER TO BCD AND ASCII 46-47
25 BCD TO BINARY 48-49
26 GENERATION OF 50% DUTY CYCLE 50-51
27 MAKING LED'S GLOW ALTERNATIVELY 52-53
TIMERS AND COUNTERS IN 8051 MICROCONTROLLER 54-56
28 GENERATION OF SQUARE WAVE WITH A TIME PERIOD OF 1-SEC 57-58
29 GENERATION OF 10KHZ SQUARE WAVE 59-60
30 GENERATION OF 10KHZ SQUARE WAVE IN MODE-1 61-62
31 GENERATION OF 66% DUTY CYCLE 63-64
32 GENERATION OF A PULSE OF 2-SECONDS 65-66
33 TOGGLE THE BIT OF PIN P2.6 67-68
34 PULSE COUNTER 69-70
35 DISPLAYING 0-99 ON 7-SEGMENT DISPLAY 71-73
SERIAL COMMUNICATION IN 8051 MICROCONTROLLER 74-75
36 TRANSMIT A WORD SERIALLY 76-77
37 DATA READING AND SENDING SERIALLY 78-80
38 RECEIVING THE SERIAL DATA 81-82
TRANSMIT THE DATA SERIALLY RECEIVED FROM PORT-1 AND
39 PORT-2 83-86
CONTENTS
EXP.NO NAME OF THE EXPERIMENT PAGE.NO
INTERRUPTS IN 8051 MICROCONTROLLER 87-88
GENERASTION OF 50% DUTY CYCLE AND SERIAL DATA
40 TRANSMISSION 89-90
41 PERFORMING THE TASKS USING INTERRUPTS 91-94
INTERFACING OF VARIOUS DEVICES
42 LED CONTROL USING PUSH BUTTONS 95-96
43 LED CONTROL USING TIMERS AND INTERRUPTS 97-99
44 LCD INTERFACING USING 8051 MICROCONTROLLER 100-102
45 KEYBOARD INTERFACING USING 8051 MICROCONTROLLER 103-107
46 RELAY INTERFACING USING 8051 MICROCONTROLLER 108-109
47 OPTOCOUPLER INTERFACING USING 8051 MICROCONTYROLLER 110-111
48 STEPPER MOTOR FULL ROTATION USING 4-STEP SEQUENCE 112-113
49 STEPPER MOTOR TO ROTATE 72° IN 4-STEP SEQUENCE 114-115
50 STEPPER MOTOR ROTATION DIRECTION USING 8-STEP SEUENCE 116-118
51 STEPPER MOTOR FULL ROTATION USINFG 4-STEP WAVE DRIVING 119-120
52 DC MOTOR INTERFACING AND ROTATION DIRECTION CONTROL 121-122
53 DC MOTOR INTERFACING AND SPEED CONTROL 123-125
1

PROGRAM-1
ADDITION OF TWO 8-BIT NUMBERS
Aim: To perform addition of two 8-bit numbers with 8051 microcontroller and
keil µvision software.
EQUIPMENT REQUIRED:
1. keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load two RAM locations with two operands
2. Move the 1st operand to the accumulator
3. Add the accumulator content with the next location
4. Check the carry flag
5. If the carry is set indicate it by moving 1 in a location
6. Move the final result to a RAM location
PROGRAM:
ORG 0000H
MOV 40H,#11H
MOV 41H,#12H
MOV A,40H
ADD A,41H
JC NEXT
MOV 60H,A
SJMP OVER
NEXT:MOV 61H,#01H ; Indicates the carry is set
MOV 60H,A
OVER:NOP
END

INPUT:
2

OUTPUT:

INFERENCE:
The addition of two 8-bit is performed by giving the two operands in different
locations but the addition can also be performed with immediate data.
RESULT:
The addition of two 8 bit numbers is performed and the result is obtained.

Signature of the lecturer


3

PROGRAM-2
ADDITION OF TWO 16-BIT NUMBERS
AIM: To perform addition of two 16-bit numbers with 8051 microcontroller
and keil µvision software.
EQUIPMENT REQUIRED:
1. keil µvision software
2. 8051 development board
3. Universal programmer

ALGORITHM:
1. Load the R0 and R1 registers with 40H and 50H which will be used in the
indirect addressing mode.
2. Load the accumulator with the content of 40H
3. Add content of accumulator and content of 50H location. This performs
the lower byte addition
4. Move the result to 60H location.
5. Increment R0 and R1 registers so that they will be pointing towards the
next memory locations.
6. Again load the accumulator with 41H location pointed by R0
7. Add content of accumulator and 51H content pointed by R1 with carry
using ADDC instruction
8. If there is a carry the carry flag will set
9. Move the Higher byte result to 61H location

PROGRAM:
ORG 0000H
MOV R0, #40H
MOV R1, #50H
MOV A, @ R0
ADD A, @ R1
MOV 60H, A
INC R0
INC R1
MOV A, @ R0
ADDC A, @ R1
MOV 61H, A
END
4

INPUT:

OUTPUT:

INFERENCE:
The addition of two 16-bit is performed by giving the two 16 bit operands in
different locations but the addition can also be performed with immediate
data.

RESULT:
The addition of two 16-bit numbers is performed.

Signature of the lecturer


5

PROGRAM-3
ADDITION OF TWO 32-BIT NUMBERS
AIM: To perform addition of two 32-bit numbers with 8051 microcontroller
and Keil µvision software.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the first 32 bit number in the location starting from 40H
2. Load the second 32 bit number in the location starting from 50H
3. Load the R0 with 40H to act as a pointer
4. Load the R1 with 50H to act as a pointer
5. Load the R2 with 60h to store the result
6. Move the data pointed by R0 into the accumulator
7. Add the (A) and the data pointed by R1
8. Since R2 cannot be used in indirect addressing push the contents of R1
on to the stack and load r1 with (R2)
9. Store the result pointed by the R2
10.Repeat the same for the remaining 3 bytes until R3 becomes 0
11.If there is carry load R5 with 1 otherwise with 0
12.The final result will be in the location starting from 60H
PROGRAM:
ORG 0000H
MOV 40H,#68H ; giving immediate data of first 32 bit data
MOV 41H,#24H
MOV 42H,#57H
MOV 43H,#13H

MOV 50H,#31H ; giving immediate data of first 32 bit data


MOV 51H,#15H
MOV 52H,#00H
MOV 53H,#11H
MOV R0,#40H ;load the R0 with 40h to function as pointer
MOV R1,#50H ;load the R1 with 50h to function as pointer
MOV R2,#60H
6

MOV R3,#4 ;load R3 with 4 to act as a counter


MOV R5,#0
UP: MOV A,@ R0
ADDC A,@ R1 ; add the contents of a and ((R1))
PUSH 1 ; push the contents of R1 to stack
MOV 20H,R2 ; load 20h with contents of R2
MOV R1,20H ; load R1 with content of 20h to point the destination
MOV @ R1,A ; moves the result to the destination
POP 1
INC R1
CLR A
INC R0
INC R2
DJNZ R3, UP ;repeats the same by 4 times
JNC NEXT
MOV R5, #01H ;if there is a carry load R5 with 1
NEXT: MOV R5, #00H ;if there is no carry load R5 with 0
END

INPUT:

OUTPUT:

INFERENCE:
The addition of two 32-bit is performed by giving the two 32 bit operands in
different locations but the addition can also be performed with immediate
data and in this we also have used “PUSH” and ”POP” instructions.

RESULT:
The addition of two 32-bit numbers is performed.
Signature of the lecturer
7

PROGRAM-4
16-BIT ADDITION WITH THE OPERANDS PRESENT IN THE ROM
AIM: To perform addition of two 16-bit numbers present in the ROM locations
with 8051 microcontroller and Keil µvision software.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Initially start the program from a different location in code memory
2. Store the byte data with a label (e.g.: NUM)
3. Now start the program from 0000H
4. Load DPTR with the starting address of the data stored in code memory
5. Load the accumulator with appropriate value to act as an offset to the
DPTR content
6. Load the content of accumulator with the data pointed by the (A+DPTR)
7. Similarly load the any other location with the second byte and with the
content of accumulator
8. Store the lower order result in 51H location
9. Repeat the same and store the higher order result in 50H location.
PROGRAM:
ORG 0F00H
NUM:DB 50H,50H,61H,30H ; num1-5050h, num2-6130h result=b180h
ORG 0000H
MOV DPTR,#NUM
CLR A
INC A ; lower order of num1
MOVC A,@ A+DPTR
MOV 60H,A
CLR A
INC A
INC A ; lower order of num2
INC A
MOVC A, @ A+DPTR
ADD A,60H
MOV 51H,A ; lower order result
CLR A
MOVC A,@ A+DPTR ; higher order of num1
8

MOV 65H,A
CLR A
INC A
INC A
MOVC A,@ A+DPTR ; higher order of num2
ADDC A,65H
MOV 50H,A
END

INPUT:

OUTPUT:

INFERENCE:
The usage of ROM locations has been understood and the addressing using
DPTR (indexed addressing) has been performed.

RESULT:
The addition of two 16-bit numbers stored in ROM location is performed.

Signature of the lecturer


9

PROGRAM-5
8-BIT SUBTRACTION
AIM: To perform subtraction of two 8-bit numbers
a) From larger number to smaller number
b) From smaller number to larger number
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the two 8 bit operands into the locations 40H and 41H
2. Load the larger into the accumulator
3. Subtract the content of accumulator with the smaller number in 40H
location.
4. Store the result in 60H location
5. To subtract from smaller to larger number, load the smaller number into
the accumulator
6. Subtract the larger number from the smaller number.
7. Now doing this carry flag will set so take the 2’s complement of the
result
8. Store the result in 62H location
9. Load the R0 with 1 to indicate negative sign.
PROGRAM:
ORG 0000H
MOV 40H,#87H
MOV 41H,#37H

MOV A,40H ; To subtract smaller no. from bigger no.


SUBB A,41H
MOV 60H,A
MOV A,41H ; To subtract bigger no. from smaller no.
SUBB A,40H
JC NEXT
SJMP OVER
NEXT: CPL A
ADD A,#01
10

MOV R0, #01 ; to indicate the no .is negative


MOV 62H,A
OVER: NOP
END
INPUT:

OUTPUT:

INDICATES NEGATIVE
SIGN WHEN R0 IS 1

RESULT WHEN RESULT WHEN


SUBTRACTED SUBTRACTED
FROM LARGER FROM SMALLER
NO. NO.

INFERENCE:
While performing subtraction from the smaller number it is understood that
we need to perform the 2’s compliment of the result and indicate with
negative sign

RESULT:
The subtraction of two 8-bit numbers is performed.

Signature of the lecturer


11

PROGRAM-6
16-BIT SUBTRACTION
AIM: To perform subtraction of two 16-bit numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the two 16 bit operands starting from the locations 40H and 50H
2. Initialize R2 as a counter by loading 2
3. Initialize R3 as the destination pointer
4. Use R0 and R1 registers to point the operands locations
5. Load the accumulator with the data pointed by R0
6. Subtract the data pointed by R1 from the accumulator
7. If there is no carry store the result in the location pointed by R3
8. If there is a carry take 2’s complement of the result and store the lower
byte result
9. Repeat the same for the higher byte of the data.
PROGRAM:
ORG 0000H
MOV R2,#02H
MOV R3,#60H
MOV 40H,#15H
MOV 41H,#50H
MOV 50H,#13H
MOV 51H,#65H
MOV R0,#40H
MOV R1,#50H
UP: MOV A,@ R0
SUBB A,@ R1
JC NEXT
PUSH 1
MOV 20H,R3
MOV R1,20H
MOV @ R1,A
POP 1
INC R3
12

INC R0
INC R1
DJNZ R2,UP
SJMP OVER
NEXT: CPL A
ADD A,#01H
PUSH 1
MOV 20H,R3
MOV R1,20H
MOV @ R1,A
POP 1
INC R3
INC R0
INC R1
MOV R5, #01H ; indicates negative sign
DJNZ R2, UP
OVER: NOP
END
INPUT:

OUTPUT:

INDICATES -VE
SIGN
INFERENCE:
While performing subtraction from the smaller number it is understood that
we need to perform the 2’s compliment of the result and indicate with
negative sign

RESULT:
The subtraction of two 16-bit numbers is performed.

Signature of the lecturer


13

PROGRAM-7
ADDITION OF TEN 8-BIT NUMBERS
AIM: To perform addition of 10 8-bit numbers stored in the locations starting
from 50H and display the result on PORT-1 and PORT-2.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load R0 with appropriate location(50H) where the 10 number data is
stored
2. Load R2 with 10 to use as a counter
3. Load R3 with 00H to initialize as a carry counter
4. Load the accumulator with the first content pointed by R0
5. Then increment R0 and add the data pointed by R0 to the content of
accumulator
6. If there is a carry increment R3
7. Repeat the same until the R2 becomes 0
8. Load the content of accumulator to P1
9. Load the content of accumulator to P2.
PROGRAM:
ORG 0000
MOV R0, #50H
MOV R2, #10
MOV R3, #00H
MOV A, @ R0
UP:INC R0
ADD A, @ R0
JC NEXT
MOV P1, A
DJNZ R2, UP
SJMP OVER
NEXT:MOV P1, A
INC R3
MOV P2, R3
DJNZ R2, UP
14

OVER: NOP
END
INPUT:

OUTPUT:

FINAL RESULT IS 127H


OR 295 IN DECIMAL

INFERENCE:
While performing this addition since we have already initialize the carry
counter so we need to use the “ADDC” instruction.

RESULT:
The addition of 10 8-bit numbers has been performed.

Signature of the lecturer


15

PROGRAM-8
TO FIND THE SQUARE OF THE NUMBERS
AIM: To perform addition of 10 8-bit numbers stored in the locations starting
from 50H and display the result on PORT-1 and PORT-2.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Start the program from different ROM location say 0F00H
2. Store the squares of the 0 to 10 numbers starting from that location
3. Now start the program from 0000H
4. Configure P1 as the input port
5. Increment the DPTR by the given input number
6. Move the square of that number to port 2
PROGRAM:
ORG 0F00H
SQUARE: DB 0,1,4,9,16,25,36,49,64,81,100
ORG 0000H
MOV P1, #0FFH
MOV DPTR, #SQUARE
MOV A, P1
MOVC A, @ A+DPTR
MOV P2, A
END

INPUT:
16

OUTPUT:

19H = (25)10

INFERENCE:
The square’s of the numbers between 0-10 has been saved in the code
memory and hence the square of the given number between 0-10 is chosen
from the entered value.

RESULT:
The square of a given number is performed using 8051 microcontroller.

Signature of the lecturer


17

PROGRAM-9
ADDITION OF TWO SIGNED NUMBERS
AIM: To perform addition of two signed numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load accumulator with the immediate data
2. Load the B-register with the second operand
3. Convert the content of accumulator and B-register to the signed form
by taking 2’s complement
4. Add the both values and again perform 2’s complement of the result
5. The carry flag is set to indicate -ve sign and result will be in accumulator.
PROGRAM:
ORG 0000H
MOV A, #04H
MOV B, #05H
ACALL SUB ; calls the sub program
MOV R1, A
CLR A
MOV A, B
ACALL SUB
ADD A, R1
CPL A ; converts the signed value to its decimal equivalent
INC A
SJMP OVER
SUB:CPL A ; converts the data into their signed form
INC A
RET
OVER: NOP
END
INPUT:

OUTPUT:
Cy = 1 indicate

-ve sign
18

INFERENCE:
In the addition of signed numbers the result should be 2’s complemented to
view the actual value and the carry flag indicates the negative sign.

RESULT:
The addition of two signed numbers are performed.

Signature of the lecturer


19

PROGRAM-10
ADDITION OF PACKED BCD NUMBERS
AIM: To perform addition of packed BCD numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the BCD numbers starting from the location 40H
2. Load R0 with 40H to point the data
3. Load R1 with 04H to act as a counter
4. Move the content of first location into the accumulator
5. Increment R0 and the content of accumulator to the next location
6. Use DA A instruction to convert the result to packed BCD form
7. Repeat the loop until the counter value R1 becomes zero.
PROGRAM:
ORG 0000H
MOV 40H, #04H
MOV 41H, #01H
MOV 42H, #02H
MOV 43H, #03H

MOV R0, #40H


MOV R1, #04H
MOV A, @ R0
UP:INC R0
ADD A, @ R0
DA A
DJNZ R1, UP
END

INPUT:
20

OUTPUT:
The result is 10
which is the sum of
given data in BCD

INFERENCE:
After adding the 4 BCD numbers the result should be converted to packed BCD
for which we need to use DA A instruction.

RESULT:
The addition of packed BCD numbers are performed.

Signature of the lecturer


21

PROGRAM-11
SETTING THE ODD BITS OF THE ACCUMULATOR
AIM: To set the odd bits of the accumulator using ORL instruction
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Clear the accumulator
2. Perform ‘OR’ operation with 55H and the accumulator
3. The odds bits of the accumulator will set.
PROGRAM:
ORG 0000H
MOV A, #00H
ORL A, #55H
END

OUTPUT:
7 6 5 4 3 2 1 0
0 1 0 1 0 1 0 1

INFERENCE:
To set the odd bits of the accumulator load the accumulator in 55H so that the
odd locations becomes 1.

RESULT:
The setting of odd bits of accumulator is performed.

Signature of the lecturer


22

PROGRAM-12
MASK THE LOWER NIBBLE OF ACCUMULATOR
AIM: To mask the lower nibble of the accumulator.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the accumulator with FFH
2. Perform ‘AND’ operation with F0H and the accumulator
3. The lower nibble will be masked.
PROGRAM:
ORG 0000H
MOV A, #0FFH
ANL A, #0F0H
END
OUTPUT:
7 6 5 4 3 2 1 0
1 1 1 1 0 0 0 0

INFERENCE:
To mask the lower nibble of the accumulator load the accumulator in F0H so
that the lower nibble becomes zero.

RESULT:
The masking of the lower nibble of the accumulator is performed.

Signature of the lecturer


23

PROGRAM-13
TO SEND ‘1’ ON P1.2 WHEN ‘0AAH’ IS RECEIVED ON P0
AIM: To send ‘1’ on P1.2 when ‘0AAH’ is received on P0
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Configure P0 as the input port
2. Give the data to P0
3. Compare the given data with 0AAH
4. If it is equal set P1.2 otherwise P1.2 is low
PROGRAM:
ORG 0000H
MOV P0, #0FFH ;configure P0 as input
MOV P1, #00H
MOV A, P0 ; send the data
CJNE A, #0AAH, NEXT ; compare with 0AAH
SETB P1.2 ; if it is equal set P1.2
NEXT:NOP
END

INPUT:

OUTPUT:
24

INFERENCE:
The received data from P0 is continuously compared to 0AAH and if it is equal
set P1.2. also execute the program in steps and given input after P0 is
configured as input.

RESULT:
The displaying of ‘1’ on P1.2 on reception of ‘0AAH’ on P0 has been performed

Signature of the lecturer


25

PROGRAM-14
TO FIND THE LARGEST OF THE GIVEN SET OF NUMBERS
AIM: To find the largest number of the given set of numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the 65H location with 00H which is considered as the largest no.
initially
2. Initialize a counter based on no. of numbers given
3. Move the first number into the accumulator
4. Compare the content of accumulator with 00H in 65H
5. If there is no carry, which means (A) is the largest so, move the content
of accumulator to the 65H
6. If there is a carry leave the content of accumulator as it is.
7. Repeat the loop until all the numbers are compared and the counter
value becomes zero
8. The largest number will be in the 65H location.
PROGRAM:
MOV 65H, #00H ; initialize 65H with 0 which is considered as biggest
MOV R0, #40H ; load R0 with 40H where the data is present
MOV R1, #05 ; initialize R5 as counter for 5 numbers
UP: MOV A, @ R0
CJNE A, 65H, NEXT ; compare given number with 0
INC R0
DJNZ R1, UP
SJMP OVER
NEXT: JNC STEP_1
INC R0 ; if there is carry leave the content of accumulator as it is
DJNZ R1, UP
SJMP OVER
STEP_1: MOV 65H, A ; if there is no carry move the (65H) (A)
INC R0
DJNZ R1, UP
OVER: NOP ; the biggest number is in 65H
END
26

INPUT:

OUTPUT:

Largest number is
0BH and present in
65H location

INFERENCE:
In finding the largest of given set of numbers, first “0” is considered as the
largest and then it is compared with other numbers and the largest of those is
replaced in the destination.

RESULT:
The identification of the largest number from the given set of numbers has
been performed.

Signature of the lecturer


27

PROGRAM-15
TO FIND THE SMALLEST OF THE GIVEN SET OF NUMBERS
AIM: To find the smallest number of the given set of numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the 70H location with 0FFH which is considered as the smallest no.
initially
2. Initialize a counter based on no. of numbers given
3. Move the first number into the accumulator
4. Compare the content of accumulator with 0FFH in 70H
5. If there is carry, which means (A) is the smallest so, move the content of
accumulator to the 70H
6. If there is no carry leave the content of accumulator as it is.
7. Repeat the loop until all the numbers are compared and the counter
value becomes zero
8. The smallest number will be in the 70H location.
PROGRAM:
MOV 70H, #0FFH ; initialize 70H with 0FFH which is considered as small
MOV R0, #40H ; load R0 with 40H where the data is present
MOV R1, #05 ; initialize R5 as counter for 5 numbers
UP: MOV A, @ R0
CJNE A, 70H, NEXT ; compare given number with 0FFH
INC R0
DJNZ R1, UP
SJMP OVER
NEXT: JC STEP_1
INC R0 ; if there is no carry leave the content of accumulator as it is
DJNZ R1, UP
SJMP OVER
STEP_1: MOV 70H, A ; if there is carry move the (70H) (A)
INC R0
DJNZ R1, UP
OVER: NOP ; the biggest number is in 70H
END
28

INPUT:

OUTPUT:

Smallest number is
03H and present in
70H location

INFERENCE:
In finding the smallest of given set of numbers, first “0FFH” is considered as the
smallest and then it is compared with other numbers and the smallest of those
is replaced in the destination.

RESULT:
The identification of the smallest number from the given set of numbers has
been performed.

Signature of the lecturer


29

PROGRAM-16
MULTIPLICATION OF 8-BIT NUMBERS
AIM: To perform the multiplication of two 8-bit numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the accumulator with an immediate data
2. Load ‘B’ register with an immediate data
3. Multiply (A)*(B) using “MUL AB “ instruction
4. The lower byte is in accumulator
5. The higher byte is in the ‘B’ register
6. Move the result to the appropriate location.
PROGRAM:
ORG 0000H
MOV A, #40H
MOV B, #50H
MUL AB
MOV 50H, B ; higher byte
MOV 51H, A ; lower byte
END

INPUT:

OUTPUT:

Result is 1400H
which means
(5120)10
30

INFERENCE:
In the multiplication of two 8-bit numbers after the “MUL AB” instruction the
lower byte of the result is in accumulator and the higher byte is in the ‘B’
register

RESULT:
The multiplication of two 8-bit numbers has been performed

Signature of the lecturer


31

PROGRAM-17
LCM OF GIVEN SET OF NUMBERS
AIM: To write an ALP to find the LCM of the given set of numbers
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the numbers into locations for which the LCM is needed to find
out.
2. Load the register R0 with the starting location of the data to act as a
pointer
3. Load the R1 with no. of given numbers to find the LCM
4. Load the first location number into the accumulator and R4 register
5. Load the ‘B’ register with the data pointed by R0 (initially the R0 points
the 40H location so the (B) (40H)
6. Divide the content of accumulator and the content of ‘B’ register
7. If the remainder is equals to ‘0’, then the ‘B’ register is loaded with next
number and again the loop is repeated
8. If the remainder is not equals to ‘0’ then the content of ‘B’ register is
doubled and also the counter R1 and the pointer R0 are started from the
beginning
9. For one particular value of ‘A’ register all the numbers will be divisible
10.This number is the LCM of the given numbers
PROGRAM:
ORG 0000H
MOV 40H, #03
MOV 41H, #05
MOV 42H, #09
MOV R0, #40H
MOV A, 40H ; moves (A) (40H)
MOV R4, A

UP: MOV R1, #03


DV: MOV B, @ R0 ; moves (A) ((R0))
32

DIV AB ; divides A and B


MOV R2, B ; ‘B’ is the remainder moved to R2
CJNE R2, #00, STEP_1
INC R0 ;if R2 = 0
CLR A
MOV A, R4
DJNZ R1, DV
SJMP DOWN
STEP_1:MOV R3,40H ; if R2 is not equal to 0
CLR A
MOV A, R4
ADD A, R3
MOV R4, A
SJMP UP
DOWN: MOV 50H, A ; result is in 50H
END

INPUT:
INPUT IS

3, 4, 9

OUTPUT:

‘2D’ is the LCM of the given


numbers which is (45)10

INFERENCE:
While performing of the LCM of given numbers, any one number is chosen and
checked whether it is divisible with other numbers or not. If the number is not
divisible this number is double and again checked for the divisibility and for
one value all the numbers will be divisible and it is LCM of the numbers given.
RESULT:
The LCM of the given set of numbers has been performed.

Signature of the lecturer


33

PROGRAM-18
BLOCK TRANSFER OF DATA
AIM: To perform the block transfer of the given data starting from the
locations 30H to 39H to 40H to 49H .
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load R0 with 30H to acts as source pointer
2. Load R1 with 40H to act as a destination pointer
3. Load the accumulator with the content pointed by R0
4. Move the content of accumulator to the destination location pointed by
R1
5. Repeat the loop until the counter value becomes zero.
PROGRAM:
ORG 0000H
MOV R0, #30H
MOV R1, # 40H
MOV R2, #0AH ; counter
UP: MOV A, @ R0
MOV @ R1, A
INC R0
INC R1
DJNZ R2, UP
END

INPUT:
34

OUTPUT:

INFERENCE:
In performing the block transfer the data from one location is moved to other
location and no. of numbers transferred should be specified in the counter.

RESULT:
The block transfer given set of numbers has been performed.

Signature of the lecturer


35

PROGRAM-19
EXCHANGE OF DATA
AIM: To perform the exchange of the given data starting from the locations
30H to 39H to 40H to 49H.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load R0 with 30H to acts as source pointer
2. Load R1 with 40H to act as a destination pointer
3. Initialize the counter with by moving the appropriate value into register
(R2 =10)
4. Load the accumulator with the content pointed by R0
5. Exchange the content of accumulator to the destination location pointed
by R1
6. Move the content of accumulator into the location pointed by R0.
7. Repeat the loop until the counter value becomes zero.
PROGRAM:
ORG 0000H
MOV R0, #30H
MOV R1, # 40H
MOV R2, #0AH ; counter to exchange 10 numbers

UP:MOV A, @ R0
XCH A, @ R1
MOV @ R0, A
INC R0
INC R1
DJNZ R2, UP
END

INPUT:
36

OUTPUT:

INFERENCE:
In performing the block exchange, the data in one location is exchanged with
other locations and no. of numbers transferred should be specified in the
counter.

RESULT:
The block exchange given set of numbers has been performed.

Signature of the lecturer


37

PROGRAM-20
VERIFICATION OF NUMBERS READ FROM PORT-1
AIM: To check the 8-bit number read from PORT-1 is
a) Even or odd
b) Parity or not
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the accumulator with the input data entered from PORT-1
2. Store the content of accumulator in some other location
3. Load ‘B’ register with number ‘2’
4. Divide content of accumulator with ‘B’ register
5. If remainder is ‘0’ number is even and if remainder is not ‘0’ the number
is odd
6. Store the even or odd status of the number in some location (60H in this
case)
7. If result is even the content of ‘60H’ is ‘1’ and if it is odd the content of
‘60H’ is ‘0F’
8. To find the parity is even or odd parity load the accumulator with 50H
location (where the entered value is stored)
9. Load R2 with ‘08H’ since it has to check 8-bits of the entered number
10.Now with rotate through carry instruction shift the content of
accumulator into carry flag
11.If carry flag is set increment the ‘62H’ value which is the counter. Repeat
the loop until the bit counter R2 becomes zero
12.Now divide the content of ‘62H’ with ‘2’ , if the remainder is ‘0’ then it is
even parity otherwise odd parity, specified at ‘62H’ location.
PROGRAM:
ORG 0000H
MOV A, P1
MOV 50H, A
MOV B, #02H
DIV AB
MOV A, B
CJNE A, #00H, NEXT
38

MOV 60H, #01H ; INDICATES EVEN


SJMP PARITY
NEXT: MOV 60H, #0FH ; INDICATES ODD

//TO FIND EVEN OR ODD PARITY

PARITY:MOV A, 50H
MOV R2, #08H
MOV 62H, #00H
AGAIN: RRC A
JNC DOWN
INC 62H
DJNZ R2, AGAIN
SJMP OVER
DOWN: DJNZ R2, AGAIN
OVER: MOV A, 62H
MOV B, #02H

DIV AB
MOV A, B
CJNE A, #00H, DOWN_1
MOV 62H, #01H ; INDICATES EVEN PARITY
SJMP LAST
DOWN_1: MOV 62H, #0FH ; INDICATES ODD PARITY

LAST: NOP
END

INPUT:

Even input Odd input


which is ‘6’ which is ‘7’
39

OUTPUT:

Even number Even parity Odd number Odd parity


indicated by ‘1’ indicated by ‘1’ indicated by ‘0F’ indicated by ‘0F’

INFERENCE:
It is observed that in checking for even or odd, we divide the entered number
by ‘2’ if the remainder is ‘0’ then it is even otherwise the number is odd.
In determining the parity whether it is even or odd parity, count the number of
bits in that number and divide the count by ‘2’ if the remainder is ‘0’ then it is
even parity otherwise it is odd parity.
RESULT:
The verification for even or odd number and even or odd parity

Signature of the lecturer


40

PROGRAM-21
PACKED BCD TO UNPACKED BCD
AIM: To convert packed BCD into unpacked BCD.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the register R0 with data location to act as a pointer
2. Load the accumulator with the data pointed by R0
3. Mask the lower nibble
4. Swap the higher nibble to lower nibble and store it in 50H
5. Then, swap the lower nibble and store it in 51H
6. The higher byte of the unpacked BCD is moved to 50H and the lower
byte is moved to 51H location
PROGRAM:
ORG 0000H
MOV R0, #40H
MOV A, @ R0
ANL A, #0F0H ; upper nibble masked
SWAP A
MOV 50H, A
MOV A, @ R0
ANL A, #0FH ; lower nibble masked
MOV 51H, A

INPUT:

3 2
0 0 1 1 0 0 1 0 40H

OUTPUT: 03
0 0 0 0 0 0 1 1 50H

0 0 0 0 0 0 1 0 51H
02
41

INFERENCE:
In conversion of Packed BCD to unpacked the number is divided into separate
numbers and then save them into different locations
RESULT:
The conversion of packed BCD to unpacked BCD has been performed.

Signature of the lecturer


42

PROGRAM-22
PACKED BCD TO ASCII
AIM: To convert packed BCD into ASCII
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the register R0 with data location to act as a pointer
2. Load the accumulator with the data pointed by R0
3. Mask the lower nibble
4. Swap the higher nibble to lower nibble and store it in 50H
5. Then, swap the lower nibble and store it in 51H
6. The higher byte of the unpacked BCD is moved to 50H and the lower
byte is moved to 51H location
7. Now perform ‘OR’ operation with ‘3’ for both the results and the store
the ASCII code in a location
PROGRAM:
ORG 0000H
MOV R0, #40H
MOV A, @ R0
ANL A, #0F0H ; upper nibble masked
SWAP A
MOV 50H, A
MOV A, @ R0
ANL A, #0FH ; lower nibble masked
MOV 51H, A
MOV A, #03
SWAP A ; moves ‘3’ to higher nibble
ORL A, 50H
MOV 50H, A ; ‘or’ operation with ‘3’
CLR A
MOV A, #03
SWAP A
ORL A, 51H
MOV 51H, A
END
43

INPUT:

OUTPUT:

INFERENCE:
In conversion of Packed BCD to ASCII the number is divided into separate
numbers and then perform ‘OR’ operation with ‘3’ to get the ASCII value
RESULT:
The conversion of packed BCD to ASCII has been performed.

Signature of the lecturer


44

PROGRAM-23
ASCII TO PACKED BCD
AIM: To convert ASCII to packed BCD
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the register R0 with input location address to use R0 as pointer
2. Load the accumulator with the data pointed by R0
3. Mask the upper nibble i.e. ‘3’ in ASCII
4. Store the result in some location
5. Repeat the same process with other ASCII number and store it in some
location
6. Now swap the higher nibble of the assumed packed BCD from its lower
nibble which is obtained after masking
7. Now perform logical ‘OR’ operation with both the results and store the
packed BCD in a location.
PROGRAM:
ORG 0000H
MOV R0, #60H

MOV A, @ R0
ANL A, #0FH ; mask the upper nibble
SWAP A
MOV 50H, A

INC R0
MOV A, @ R0
ANL A, #0FH
ORL A, 50H
MOV 62H, A

END
45

INPUT:
Entered two
ASCII numbers 34
and 35

OUTPUT:
The result is
packed BCD
which is 45

INFERENCE:
In conversion ASCII to packed BCD the upper nibble of the number should be
masked which is ‘3’ in ASCII code and then the numbers should be combined.
RESULT:
The conversion of ASCII to packed BCD has been performed.

Signature of the lecturer


46

PROGRAM-24
8-BIT BINARY NUMBER TO BCD AND ASCII
AIM: To convert 8-bit number into BCD and ASCII
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the register with the input data location to use R0 as pointer
2. Load the data pointed by R0 into the accumulator
3. Use ‘DA A’ instruction to convert binary data into BCD
4. Store the BCD value in a location (i.e. 62H)
5. Now to convert into ASCII mask the upper nibble
6. Load the accumulator with ‘30’ and exchange the lower nibble of the
masked number with 30H lower nibble, store the ASCII code at 66H
location
7. Now mask the lower nibble and swap the result of its upper and lower
nibble and repeat step-6
8. The ASCII of higher nibble is stored in 65H location
PROGRAM:
ORG 0000H
MOV R0, #60H
MOV A, @ R0
DA A ; converts binary data to BCD
MOV 62H, A ; BCD value

ANL A, #0FH
MOV 50H, A
MOV R1, #50H
MOV A, #30H
XCHD A, @ R1 ; upper nibble masked and 30+data
MOV 66H, A

MOV A, 62H
ANL A, #0F0H ; lower nibble masked
SWAP A
MOV 50H, A
MOV R1, #50H
47

MOV A, #30H
XCHD A, @ R1
MOV 65H, A
END

INPUT:
Entered binary
number

OUTPUT:

BCD value of 35 ASCII value of


33 and 35

INFERENCE:
In conversion of 8-bit binary to BCD and ASCII the BCD conversion is done by
using ‘DA A’ instruction, whereas the ASCII is converted from BCD by
separating the digits and performing ‘OR’ operation with ‘3’.
RESULT:
The conversion of 8-bit number into BCD and ASCII has been performed.

Signature of the lecturer


48

PROGRAM-25
BCD TO BINARY
AIM: To convert BCD number into binary number
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the register R0 with the input data location address, to use as a
pointer
2. Load the accumulator with data pointed by R0
3. Load the same content in to a location say 30H
4. Separate the lower and upper nibble of the data
5. Add the lower and upper nibble and result will be in accumulator
6. Convert the content of accumulator into BCD
7. Compare this value with the eneterd value which is in 30H
8. If it is equal that the binary result is in 60H
9. If it is not equal, then add ‘9’ to the added lower and higher nibble and
then store this result in 50H and then convert into BCD
10.Again compare it with the data in 30H if it is equal the content of 50H is
the binary equivalent
11.If it is not equal the same process is repeated again until it is equal.
PROGRAM:
ORG 0000H
MOV R0, #40H
MOV A, @ R0
MOV 30H, @ R0
ANL A, #0FH
MOV 50H, A ; lower nibble
MOV A, @ R0
ANL A, #0F0H ; higher nibble
SWAP A
ADD A, 50H

UP: ACALL BCD


CJNE A, 30H, NEXT ; upto 0 to 9 hexa is same as decimal
SJMP OVER
49

NEXT: MOV A, 50H ; add's o the separated no.s


ADD A, #09H
MOV 50H, A
SJMP UP

BCD: MOV B, #0AH ; converts the binary to BCD


DIV AB
SWAP A
MOV 60H, B
ORL A, 60H
RET
OVER: NOP
END

INPUT:
BCD eneterd value
which is ‘10’

OUTPUT:
Hexadecimal value of BCD ‘10’ is
‘0AH’ which can be represented as
binary ‘0000 1010’

INFERENCE:
In conversion of BCD to binary all the digits forming a number are added and
converted into BCD and then compared with the entered BCD value, if it is not
equal ‘9’ is added to the added result and then converted to BCD and then it is
compared with the entered value and if it is equal the result after adding ‘9’ is
the binary representation of entered value.
RESULT:
The conversion of BCD to binary has been performed.

Signature of the lecturer


50

PROGRAM-26
GENERATION OF 50% DUTY CYCLE

AIM: To generate 50% duty cycle square wave on pin P1.3


EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the register R2 and R3 with ‘0FFH’
2. Compliment the output of P1.3
3. Call the sub program for a delay
4. Again repeat from the start
5. In the delay program the content of R2 is decrement to zero and then
again it is loaded with ‘0FFh’ until the R3 becomes zero.
PROGRAM:
ORG 0000H
UP: MOV R2, #0FFH
MOV R3, #0FFH
CPL P1.3
ACALL DELAY
SJMP UP
LOAD: MOV R2, #0FFH
DELAY: DJNZ R2, DELAY
DJNZ R3, LOAD
RET
END

OUTPUT:

DELAY
51

SIMULATION:

INFERENCE:
To create a 50% duty cycle compliment the output every after a specified delay
which is always same.

RESULT:
The generation of 50% duty cycle on P1.3 has been performed.

Signature of the lecturer


52

PROGRAM-27
MAKING LED’S GLOW ALTERNATIVELY
AIM: To make alternate led’s connected to PORT-1 ON and OFF with a specific
delay.
EQUIPMENT REQUIRED:
1. Keil µvision software
1. 8051 development board
2. Universal programmer
ALGORITHM:
1. Load the accumulator with ‘0AAH’
2. Load the PORT-1 with content of accumulator and call delay subroutine
3. Now complement the content of accumulator
4. Load the PORT-1 with content of accumulator and again call delay
subroutine
5. For the delay program load R1 and R2 with ‘255 ‘
6. In the delay subroutine decrement R1 for 255 times and then load the
R1 again with ‘255’ until the R2 becomes ‘0’
7. Return to the main program.
PROGRAM:
ORG 0000H
AGAIN:MOV R2, #0FFH
MOV R1, #0FFH
MOV A, #0AAH
MOV P0, A
ACALL DELAY
CPL A
MOV P0, A
ACALL DELAY
SJMP AGAIN
UP: MOV R1, #0FFH
DELAY:DJNZ R1, DELAY
DJNZ R2, UP
RET

END
53

OUTPUT:

SIMULATION:

INFERENCE:
To make led’s alternate first send ‘0AAH’ to PORT-1 and then complement it
after some delay to alternate the led’s

RESULT:
The alternating of led’s connected to PORT-1 has been performed.

Signature of the lecturer


54

TIMERS AND COUNTERS IN 8051 MICROCONTROLLER


Many of the microcontroller applications requires
counting of external events and creating time
delays. Both these counting and time delays can
be created using loops but for a precise counting
and time delays we use inbuilt timers and
counters of 8051 microcontroller.

The 8051 microcontroller has two 16-bit timers


that are timer-1 and timer-0 each timer can be
used as a 8-bit or a 16-bit timer the lower byte of
timers is TLX (x = 0 or 1) and THX (x = 0 or 1)

To use the timers and counter 8051 ahs two special registers TMOD(Timer
mode) and TCON(Timer control)

TIMER MODE (TMOD):

TMOD is an 8-bit register used for selecting


timer or counter and mode of timers. Lower
4-bits are used for control operation of
timer 0 or counter0, and remaining 4-bits
are used for control operation of timer1 or
counter1.This register is present in SFR
register, the address for SFR register is 89H.

Gate: If the gate bit is set to ‘0’, then we can start and stop the “software”
timer in the same way. If the gate is set to ‘1’, then we can perform hardware
timer.

C/T: If the C/T bit is ‘1’, then it is acting as a counter mode, and similarly when
set C/T bit is ‘0’ it is acting as a timer mode.

Mode select bits: The M1 and M0 are mode select bits, which are used to
select the timer operations. There are four modes to operate the timers.

MODES OF TIMERS:
55

Mode 0: This is a 13-bit mode that means the timer operation completes with
“8192” pulses.

Mode 1: This is a16-bit mode, which means the timer operation completes
with maximum clock pulses that “65535”

Mode 2: This mode is an 8-bit auto feature, which means the timer operation
completes with only “256” clock pulses and again reloaded automatically the
TH1 acts as reloader for the TL1 ones it rolls over to 00H

Mode 3: This mode is a split-timer mode, which means the loading values in T0
and automatically starts the T1.

TIMER CONTROL (TCON):


TCON is another register used to control operations of counter and timers in
microcontrollers. It is an 8-bit register wherein four upper bits are responsible
for timers and counters and lower bits are responsible for interrupts.

TFx (x = 0 or 1): The TF stands for ‘timer’ flag bit. Whenever calculating the
time-delay in timer, the THx and TLx reaches to the maximum value that is
“FFFF” and rolls over to “0000h” then the TF flag will set, Whenever the TF=1,
then clear the flag bit and stop the timer

TRx (x = 0 or 1): The TRx stands for timer run bit. The timer starting can be
through software instruction or through hardware method.

TIME DELAY CALCULATIONS FOR 8051 MICROCONTROLLER:


The 8051 microcontroller works with 11.0592 MHz frequency.

Frequency 11.0592MHz=12 clock pulses

1 clock pulse =11.0592MHz/12

F =0.921 MHz

Time delay=1/F

T=1/0.92MHz

T=1.080506 us (for ‘1’ cycle)


56

1000us=1MS

1000ms=1sec

PROCEDURE TO CALCULATE THE DELAY PROGRAM:


1.) First we have to load the TMOD register value for ‘Timer0’ and ‘Timer1’in
different modes. For example, if we want to operate timer1 in mode1 it must
be configured as “TMOD=0x10”.

2.) Whenever we operate the timer in mode 1, timer takes the maximum
pulses of 65535. Then the calculated time-delay pulses must be subtracted
from the maximum pulses, and afterwards converted to hexadecimal value.
This value has to be loaded in timer1 higher bit and lower bits.

Example: 500us time delay

500us/1.080806us

461pulses

P=65535-461

P=65074

65074 converted by hexa decimal =FE32

TH1=0xFE

TL1=0x32

3.) Start the timer1 “TR1=1”

4.) Monitor the flag bit “while(TF1==1)”

5.) Clear the flag bit “TF1=0”

6.) Cleat the timer “TR1=0”


57

PROGRAM-28
GENERATION OF SQUARE WAVE WITH A TIME PERIOD OF 1-SEC
AIM: To generate a square wave of time period 1 second using timers.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD register with 10H which means we are using timer-1 in
mode-1 i.e. 16-bit timer
2. Load register R1 with ‘0AH’ to call the same delay 10 times
3. Compliment the output of P1.3
4. Call the delay sub program
5. Again compliment the P1.3 output and this loop repeats
6. In the delay program load TL1 with ‘0FBH’ and TH1 with ‘4BH’ for 50ms
7. Start the timer by setting TR1 and monitor TF1 flag
8. Once the TF1 is set repeat the same delay for 20 times to get 1sec delay.
PROGRAM:
ORG 0000H
MOV TMOD, #10H
AGAIN: MOV R1, #0AH
CPL P1.3
ACALL DELAY
MOV R1, #0AH
CPL P1.3
ACALL DELAY
SJMP AGAIN

DELAY: MOV TL1, #0FDH


MOV TH1, #4BH
SETB TR1
HERE: JNB TF1, HERE
CLR TR1
CLR TF1
DJNZ R1, DELAY
RET
END
58

OUTPUT:

0.5SEC
SIMULATION:

0.5SEC

1 SEC

CALCULATIONS:
Time period = 50 ms
Now the machine cycles required are: ( (50*10-3)/ (1.085*10-6)) = (46083)10 =
(B403H)16.
Now subtract this from FFFFH and add ‘1’: FFFFH-B403+1 = 4BFDH
Then the value of TH1 = 4BH
TL1 = 0FDH

INFERENCE:
To generate a square wave of 0.5-sec load the timer for a specific delay say
50ms and repeat the same for 10 times to get 1-sec delay.

RESULT:
The generation of square wave with 1-sec delay has been performed.

Signature of the lecturer


59

PROGRAM-29
GENERATION OF 10kHz SQUARE WAVE IN MODE-0
AIM: To generate a 10kHz square wave with 50% duty cycle in mode-0 using
timer-0
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD register with ‘00H’ to operate in mode-0
2. Load the TH0 with ‘FEH’
3. Load the TH0 with ‘12H’
4. Compliment P2.5 and call the delay sub program
5. And after the delay again repeat the same loop
6. In the delay sub program start the timer and monitor the TF0 flag
7. Once the TF0 is set return to the main program.
PROGRAM:
ORG 0000H
MOV TMOD, #00H ; operated in mode-0
UP: MOV TH0, #0FEH
MOV TL0, #12H
CPL P2.5
ACALL DELAY
SJMP UP

DELAY: SETB TR0


HERE: JNB TF0, HERE
CLR TR0
CLR TF0
RET
END

OUTPUT:

0.1 ms
60

SIMULATION:

10 Khz

CALCULATIONS:
Frequency = 10kHz
Time period = (1/(10*103)) == (1*10-4)
Time period is halved = 5*10-5
Now the machine cycles required are: ((5*10-5)/(1.085*10-6)) = (46)10 = (2EH)16
Now subtract this from 1FFFH and add ‘1’: 1FFFH-2EH +1 = 1FD2H
The timer should be loaded with a different value that is
1 F D 1
1 111 1 110 1 0010
F E 12
The value of TH0 = 0FEH
TL0 = 012H

INFERENCE:
To generate a square wave of 10kHz using timer 0 in mode-0 first calculate the
TL0 and TH0 values and then start the timer, for which the time period is
halved and then TL0 and TH1 values are entered. and for every delay the
output complimented.

RESULT:
The generation of square wave of 10kHz frequency in mode-0 using timer-0
has been performed.

Signature of the lecturer


61

PROGRAM-30
GENERATION OF 10kHz SQUARE WAVE IN MODE-1
AIM: To generate a 10kHz square wave with 50% duty cycle in mode-1 using
timer-1
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD register with ‘10H’ to operate in mode-1
2. Load the TH1 with ‘0D1H’
3. Load the TH1 with ‘0FFH’
4. Compliment P2.5 and call the delay sub program
5. And after the delay again repeat the same loop
6. In the delay sub program start the timer and monitor the TF1 flag
7. Once the TF1 is set return to the main program.
PROGRAM:
ORG 0000H
MOV TMOD, #10H
UP: MOV TL1, #0D2H
MOV TH1, #0FFH
CPL P2.5
ACALL DELAY
SJMP UP

DELAY: SETB TR1


HERE: JNB TF1, HERE
CLR TR1
CLR TF1
RET
END

OUTPUT:

0.1 ms
62

SIMULATION:

10 Khz

CALCULATIONS:
Frequency = 10kHz
Time period = (1/(10*103)) == (1*10-4)
Time period is halved = 5*10-5
Now the machine cycles required are: ((5*10-5)/(1.085*10-6)) = (46)10 = (2EH)16
Now subtract this from FFFFH and add ‘1’: FFFFH-2EH +1 = FFD2H
The value of TH0 = 0FFH
TL0 = 0D2H

INFERENCE:
To generate a square wave of 10kHz using timer 1 in mode-1 first calculate the
TL1 and TH1 values and then start the timer, for which the time period is
halved and then TL1 and TH1 values are entered. and for every delay the
output is complemented

RESULT:
The generation of square wave of 10kHz frequency in mode-1 using timer-1
has been performed.

Signature of the lecturer


63

PROGRAM-31
GENERATION OF 66% DUTY CYCLE
AIM: To generate a 66% duty cycle at P1.5 by operating timer-0 in mode-1
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD register with 01H since the Timer-0 is operated in
mode-1
2. Compliment the output of P1.5 and call DELAY_1 sub program
3. After the DELAY_1 sub program set the P1.5 and call DELAY_2 sub
program
4. After the DELAY_2 sub program again compliment the P1.5 and call
DELAY_1 sub program and this loop repeats
5. The total time period is 1.65ms in which Delay_1 is 66% and the
DELAY_2 is 44%
PROGRAM:
ORG 0000H
MOV TMOD, #01H
UP: CPL P1.5
ACALL DELAY_1
SETB P1.5
ACALL DELAY_2
SJMP UP
DELAY_1: MOV TL0, #0AH
MOV TH0, #0FDH
SETB TR0
HERE: JNB TF0, HERE
CLR TR0
CLR TF0
RET
DELAY_2: MOV TL0, #70H
MOV TH0, #0FCH
SETB TR0
STAY: JNB TF0, STAY
CLR TR0
CLR TF0
64

RET
END

OUTPUT:

0.99ms 0.66ms

SIMULATION:

66% 44%

CALCULATIONS:
a.) 66% delay:
Time period = ((66*1.65*10-3)/100) = 0.99ms
Now the machine cycles required are: ((0.99*10-3)/(1.085*10-6)) = (912)10 =
(390H)16
Now subtract this from FFFFH and add ‘1’: FFFFH-390H +1 = FC70H
The value of TH0 = 0FCH
TL0 = 70H
b.) 44% delay:
Time period = ((44*1.65*10-3)/100) = 0.66ms
Now the machine cycles required are: ((0.66*10-3)/(1.085*10-6)) = (608)10 =
(260H)16
Now subtract this from FFFFH and add ‘1’: FFFFH-260H +1 = FDA0H
The value of TH0 = 0FDH
TL0 = 0A0H
INFERENCE:
To generate a 66% duty cycle the ON time period should be 66% of the total
time period and the OFF period is 44% of the total time period, so we need two
delay sub programs to achieve this.
RESULT:
The generation of 66% duty cycle signal has been performed.
Signature of the lecturer
65

PROGRAM-32
GENERATION OF A PULSE OF 2-SECONDS
AIM: To generate a pulse of duration 2 seconds by operating timer-1 in mode1
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD register with ‘10H’ to operate timer-1 in mode-1
2. Load the registers R1, R2, R3 with ‘0AH’
3. Load the registers R4 with ‘02H’
4. Compliment the output of P1.3 and call the delay sub program
5. After the delay the 2-sec pulse will be generated.
6. In the delay program the delay is for 1ms but it is repeated for
2000times to get 2 seconds i.e. R1*R2*R3*R4 = 10*10*10*2= 2000
PROGRAM:
ORG 0000H
MOV TMOD, #10H
MOV R1, #0AH
MOV R2, #0AH
MOV R3, #0AH
MOV R4, #02H
CPL P1.3
ACALL DELAY
MOV R1, #0AH
MOV R2, #0AH
MOV R3, #0AH
MOV R4, #02H
CPL P1.3
ACALL DELAY
SJMP OVER
UP_3: MOV R3, #0AH
UP_2: MOV R2, #0AH
UP_1: MOV R1, #0AH
DELAY: MOV TL1, #67H
MOV TH1, #0FCH
SETB TR1
66

HERE: JNB TF1, HERE


CLR TR1
CLR TF1
DJNZ R1, DELAY
DJNZ R2, UP_1
DJNZ R3, UP_2
DJNZ R4, UP_3

RET
OVER: NOP
END

OUTPUT:

2SEC

SIMULATION:

2SEC

CALCULATIONS:
Time period = 1ms
Now the machine cycles required are: ((1*10-3)/(1.085*10-6)) = (921)10 =
(399H)16
Now subtract this from FFFFH and add ‘1’: FFFFH - 399H +1 = FC67H
The value of TH0 = 0FCH
TL0 = 67H
INFERENCE:
To generate a 2 second pulse, we have generated 1ms delay and the repeated
the same delay for 2000 times to get 2 second’s delay
RESULT:
The generation of 2 second pulse on P1.3 has been performed.

Signature of the lecturer


67

PROGRAM-33
TOGGLE THE BIT OF PIN P2.6
AIM: To toggle the pin of P2.6 with a delay of 100ms using timer-1 operated in
mode-1.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD register with ‘10H’ to operate in mode-1
2. Load the register R1 with ‘2’ to repeat the 50ms delay for 2 times to get
100ms delay
3. Compliment the output of P2.6 and call the delay sub program
4. After the delay again compliment the output and again call the same
delay and repeat the loop
5. In the delay program load the TH1 with ‘4BH’ and TL1 with ‘FEH’ and
start the timer
6. Once the TF1 flag is set clear the TF1 and TR1 and return to the main
program.
PROGRAM:
ORG 0000H
MOV TMOD, #10H
AGAIN: MOV R1, #02H
CPL P2.6
ACALL DELAY
MOV R1, #02H
CPL P2.6
ACALL DELAY
SJMP AGAIN
DELAY: MOV TL1, #0FCH
MOV TH1, #4BH
SETB TR1
HERE: JNB TF1, HERE
CLR TR1
CLR TF1
DJNZ R1, DELAY
RET
END
68

OUTPUT:

100ms

SIMULATION:

100ms

CALCULATIONS:
Time period = 50ms
Now the machine cycles required are: ((50*10-3)/(1.085*10-6)) = (46083)10 =
(B404H)16
Now subtract this from FFFFH and add ‘1’: FFFFH-B404H +1 = 4BFCH
The value of TH0 = 4BH
TL0 = 0FCH
INFERENCE:
To toggle the P2.6 with a delay of 100ms first we need generate a delay of
50ms and repeat 2 times to get 100ms delay

RESULT:
The toggling of P2.6 with a delay of 100ms has been performed.

Signature of the lecturer


69

PROGRAM-34
PULSE COUNTER
AIM: To count the no. of pulses received on P3.5 for a duration of 1 second by
operating timer-0 in mode-1.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Load the TMOD with 51H to operate timer-0 as timer in mode-1 and
timer-1 as counter.
2. Load the registers R1, R2, R3 with ‘0AH’ to generate 1 sec delay by
repeating 1ms delay by 1000 times
3. Start the counter-1 and call the delay sub program
4. Give the pulses from P3.5 which the T1 pin
5. The no .of pulses received in 1 sec is counted by the counter.
PROGRAM:
ORG 0000H
MOV TMOD, #51H
CLR P3.5
MOV R1, #0AH
MOV R2, #0AH
MOV R3, #0AH
SETB TR1
ACALL DELAY
SJMP DOWN
UP_2: MOV R2, #0AH
UP_1: MOV R1, #0AH
DELAY: MOV TL0, #67H
MOV TH0, #0FCH
SETB TR0
HERE: JNB TF0, HERE
CLR TR0
CLR TF0
DJNZ R1, DELAY
DJNZ R2, UP_1
DJNZ R3, UP_2
RET
DOWN: MOV A, TL1
70

MOV P1, A
MOV A, TH1
MOV P2, A
CLR TR1
CLR TF1
END
INPUT:

P3.5 as input pin


and 3 pulses are
given

OUTPUT:

3 pulses are
counted by the
counter in 1-sec

CALCULATIONS:
Time period = 1ms
Now the machine cycles required are: ((1*10-3)/(1.085*10-6)) = (921)10 =
(399H)16
Now subtract this from FFFFH and add ‘1’: FFFFH - 399H +1 = FC67H
The value of TH0 = 0FCH
TL0 = 67H
INFERENCE:
To count the no. of pulses first call a certain delay after starting the counter so
that the counter can read the pulses during that delay

RESULT:
The counting of the no. of pulses received on P3.5 for a duration of 1 second
has been performed.

Signature of the lecturer


71

PROGRAM-35
DISPLAYING 0-99 ON 7-SEGMENT DISPLAY
AIM: To display 0-99 numbers on the 7-segment display
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
ALGORITHM:
1. Start the program from a 0100h code memory and save the seven
segment codes from 0-9
2. Now start the program from 0000h
3. Load the TMOD register with 10H to act as a timer so that each number
displays after a certain delay (1 sec in this case).
4. Clear the accumulator and load the content of R1 which is initially ‘0’
5. Load the DPTR with the starting address of seven segment codes
6. Load the first seven segment code into the accumulator by indexed
addressing mode
7. Move the content of accumulator to PORT-1
8. Increment the R1 and then load the accumulator with (R1) so that
accumulator gets the next 7-segment code
9. Repeat the same for 10 times for number ‘0’ – ‘9’
10.Then increment the R6 and move its content to the accumulator
11.The accumulator gets the 7-segment code of next location, for higher
digit if its initially zero then it becomes ‘1’
12.The program again starts displaying the lower digit from 0-9 and then
increment its higher digit
13.Once it display’s the numbers from 0-99 it again begins from the start.

PROGRAM:
ORG 0100H
SEVEN_SEG: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH ;seven seg code
ORG 0000H
MOV TMOD, #10H
AGAIN: MOV P1, #00H
MOV P2, #3FH
MOV R4, #64H ; 100
MOV R5, #0AH ; 10
MOV R1, #00H
MOV R6, #00H
72

UP_3: MOV R3, #0AH


UP_2: MOV R2, #0AH
MOV DPTR, #SEVEN_SEG
MOV A, #00H
UP_1: MOV A, R1
MOVC A, @ A+DPTR
MOV P1, A
ACALL DELAY
ACALL DELAY
INC R1
DJNZ R2, UP_1
MOV P1, #00H
MOV R1, #00H
SJMP NEXT
NEXT: INC R6
MOV A, R6
MOVC A, @ A+DPTR
MOV P2, A
DJNZ R3,UP_2

MOV P2,#00H
SJMP AGAIN
REPEAT: MOV R4, #64H
DELAY: MOV TL1, #67H
MOV TH1, #0FCH
SETB TR1
HERE: JNB TF1, HERE
CLR TR1
CLR TF1
DJNZ R4, DELAY
DJNZ R5, REPEAT
MOV R4, #64H
MOV R5, #0AH
RET
END
73

OUTPUT:

INFERENCE:
To display 0-99 on seven segment display for we need to store the 7-segment
codes for different number (E.g. 3FH – 0, 06H – 1) then first we need display
the lower digit from 0-9 and after ‘9’ we need to increment the higher digit by
‘1’ and again display the lower digits and repeat this loop

RESULT:
The displaying of numbers from 0-99 on 7-segment display has been
performed.

Signature of the lecturer


74

SERIAL COMMUNICATION IN 8051 MICROCONTROLLER


Microcontrollers need to communicate with external devices such as sensors,
computers and so on to collect data for processing. Data communication is
generally done by means of two methods – Parallel and Serial mode. In parallel
mode data bits are transferred faster using more data pins. But when comes to
a Microcontroller, we cannot afford to dedicate many pins for data transfer.
UART or Serial communication in 8051 microcontroller will allow the controller
to send and receive data’s just by using two pins

Serial Communication uses only two data pins TX and RX to establish


communication between Microcontroller and external devices. In this mode of
communication data is transferred one bit at a time.

SCON REGISTER:

SCON Register

It a bit addressable register used to set the mode in which serial


communication takes place in the controller. The above figure shows the
configuration of the SCON register. Here is the list of functions of each bit.

Serial Mode of 8051

SM0, SM1: Serial Mode control Bits


SM2: Multiprocessor mode control bit, logic 1 enables Multi processor mode
and 0 for normal mode.
REN: Enables Serial reception. If set, it enables the reception otherwise the
reception is disabled.
TB8: It is the 9th bit of the data that is to be transmitted.
RB8: It is used in modes 2 and 3, it is the 9th bit received by the
microcontroller.
TI: It is known as Transmit Interrupt flag which is set by hardware to indicate
the end of a transmission. It has to be cleared by the software.
75

RI: It is known as Receive Interrupt flag which is set by hardware to indicate


the end of a reception. It has to be cleared by the software.

BAUD RATE:
It is defined as number of bits transmitted or received per second and usually
expressed in Bits per second bps. For mode 0 and mode 2 the baud rate is
determined by means of 1/12, 1/32 or 1/64 of crystal frequency whereas for
mode 1 and 3 it is determined by means of timer 1.

SBUF REGISTER:
It is a 8 bit register that holds the data needed to be transmitted or the data
that is received recently. The serial port of 8051 is full duplex so the
microcontroller can transmit and receive data using the register
simultaneously.
76

PROGRAM-36
TRANSMIT A WORD SERIALLY
AIM: To transmit a word “INDIA” serially with a baud rate of 9600
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0F00H to store the text at the location
2. After that start the program from 0000H
3. Load the SCON register with 50H to operate in serial mode-1
4. Load the TMOD with 20H to set the baud rate in timer mode-2
5. Load the ‘TH1’ with ‘-3’ or ‘FDH’ to set a baud rate of 9600
6. Start the timer
7. Load the R2 with ‘6’ to indicate the six characters in the text
8. Load the ‘DPTR’ with the starting address of the text
9. Move the character into the accumulator
10.Load the ‘SBUF’ register with content of accumulator
11.Monitor the ‘TI’ flag, if it is set increment DPTR and load the next
character into ‘SBUF’ by clearing ‘TI’
12.Repeat loop to continuously display the text continuously
PROGRAM:
ORG 0F00H
MY_COUNTRY: DB "INDIA " ; text is stored in code memory
ORG 0000H
MOV R1, #0FFH
MOV R2, #0FFH
MOV SCON, #50H ; serial communication in MODE-1
MOV TMOD, #20H ; timer-1 in MODE-2
MOV TH1, #-3 ; 9600 BAUD RATE
SETB TR1
REPEAT:MOV R2, #06H ; no. of characters in the text
MOV DPTR, #MY_COUNTRY
AGAIN: CLR A
MOVC A, @ A+DPTR ; load the character byte
ACALL SEND
INC DPTR
CLR A
DJNZ R2, AGAIN
ACALL DELAY
77

SJMP REPEAT
SEND: CLR TI
MOV SBUF, A ; start the serial transmission
HERE: JNB TI, HERE
RET

UP_1: MOV R2, #0FFH ; delay program


DELAY: DJNZ R2, DELAY
DJNZ R1, UP_1
MOV R1, #0FFH
MOV R2, #0FFH
RET
END

OUTPUT:

INFERENCE:
To transmit data serially first the mode of the SCON register should be selected
which is usually mode-1, then set the baud rate and load the SBUF register
with the character code and monitor ‘TI’ flag if it is set the character byte is
transmitted, repeat the same for no. of characters in a word.
RESULT:
The transmission of a word through 8051 serial port has been performed.

Signature of the lecturer


78

PROGRAM-37
DATA READING AND SENDING SERIALLY
AIM: To display the data received from P1 and transmit it serially to a display .
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:

1. Load the text “READY” in the code memory


2. Load the CMNDS location with the commands to clear the lcd
3. Start the main program from 0000H
4. Load the R3 register with ‘07H’ to indicate no. of characters in the text
5. Load the R4 register with ‘02H’ to execute the commands
6. Load the SCON register with 50H to operate in mode-1
7. Load the TMOD register with 20H to operate in mode-2 and set the baud
rate to 9600.
8. Now first display the “READY” text by loading the characters one by one
into SBUF and monitor ‘TI’ flag before sending the next character
9. After displaying ready clear the serial lcd by executing ‘CMNDS’
functions
10.Now read the data from PORT-1 and load it into the accumulator
11.Compare the entered value with the stored codes in the code memory
12.If they are equal then transmit them serially by calling ‘SEND’
subprogram.

PROGRAM:

ORG 0F00H
MY_STATUS: DB " READY " ; display text
CMNDS: DB 254D,1D ; serial lcd commands
SERIAL_LCD: DB '1','2','3' ; stored no.s to be displayed when read from P1
ORG 0000H
MOV R1, #0FFH
MOV R2, #0FFH
MOV R3, #07H ; ' READY ' uses 7 characters including space
MOV R4, #02 ; two command codes execute counter
MOV SCON, #50H ; serial communication in MODE-1
MOV TMOD, #20H
MOV TH1, #-3 ;9600 baud rate
SETB TR1
79

MOV DPTR, #MY_STATUS ; display ready


AGAIN: CLR A
MOVC A, @ A+DPTR
ACALL SEND
INC DPTR
CLR A
DJNZ R3, AGAIN
SJMP PORT_1
SEND: CLR TI ; send subprogram
ACALL DELAY
MOV SBUF, A
HERE: JNB TI,HERE
RET
UP_1: MOV R2, #0FFH
DELAY:DJNZ R2, DELAY
DJNZ R1, UP_1
MOV R1,#0FFH
MOV R2,#0FFH
RET

PORT_1:ACALL DELAY ; lcd commands to clear the display


MOV DPTR, #CMNDS
B1: CLR A
MOVC A, @A+DPTR
ACALL SEND
INC DPTR
DJNZ R4,B1
LOOP: MOV DPTR, #SERIAL_LCD ; compares the input data with stored 1,2,3
MOV A, P1
CJNE A, #01H, N2 ;if the entered no. is equal transmit serially
CLR A
MOVC A, @ A+DPTR
ACALL SEND
N2: CJNE A, #02H, N3
CLR A
INC A
MOVC A,@ A+DPTR ;if the entered no. is equal transmit serially
ACALL SEND
N3: CJNE A,#03H,OVER
CLR A
INC A
80

INC A
MOVC A,@ A+DPTR ;if the entered no. is equal transmit serially
ACALL SEND
OVER: NOP
END

OUTPUT:

Entered value
‘1’ in binary

Entered value
Entered value
‘3’ in binary
‘2’ in binary

INFERENCE:
To transmit data serially first the mode of the SCON register should be selected
which is usually mode-1, then set the baud rate and load the SBUF register
with the character code and monitor ‘TI’ flag if it is set the character byte is
transmitted, repeat the same for no. of characters in a word. After displaying
the text read the data from PORT-1 and again transmit it serially.

RESULT:
The transmission of a word through 8051 serial port and also displaying the
characters read from PORT-1 has been performed

Signature of the lecturer


81

PROGRAM-38
RECEIVING THE SERIAL DATA
AIM: To receive the data serially and display it on the display device.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:

1. Load the TMOD register with ‘20H’ to set the baud rate to 9600
2. Receive the data serially
3. Monitor ‘RI’ flag to indicate the reception of the byte
4. Mov the data from the ‘SBUF’ to a specific loaction
5. Transmit the data again serially with the same baud rate
6. If the ‘$’ symbol is received end of transmission and reception

PROGRAM:

ORG 0000H
MOV TMOD, #20H ; set the baud rate to 9600
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
HERE: JNB RI, HERE ; if data received transmit again the same
MOV A, SBUF
MOV P1, A
CLR RI
MOV SBUF, A
STAY: JNB TI, STAY
CLR TI
CJNE A, #'$', HERE ; if there is ‘$’ sign end the transmission amd
END ; reception

OUTPUT:

Character code
of ‘H’
82

Character
code of ‘E’

INFERENCE:
For serial reception receive the data through the ‘RX’ of 8051 and monitor the
‘RI’ if a byte of data is received, save it in a location and send to PORT-1 and
also transmit serially.

RESULT:
The serial reception of data and also displaying the data has been performed

Signature of the lecturer


83

PROGRAM-39
TRANSMIT THE DATA SERIALLY RECEIVED FROM PORT-1 AND PORT-2
AIM: To transmit the data serially received from PORT-1 and PORT-2
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Load the text “READY” in the code memory
2. Load the CMNDS location with the commands to clear the lcd
3. Start the main program from 0000H
4. Load the R3 register with ‘07H’ to indicate no. of characters in the text
5. Load the R4 register with ‘02H’ to execute the commands
6. Load the SCON register with 50H to operate in mode-1
7. Load the TMOD register with 20H to operate in mode-2 and set the baud
rate to 9600.
8. Now first display the “READY” text by loading the characters one by one
into SBUF and monitor ‘TI’ flag before sending the next character
9. After displaying ready clear the serial lcd by executing ‘CMNDS’
functions
10.Now read the data from PORT-1 and load it into the accumulator
11.Compare the entered value with the stored codes in the code memory
12.If they are equal then transmit them serially by calling ‘SEND’
subprogram.
13. Now clear the LCD and display the data on the PORT-2

PROGRAM:

ORG 0F00H
MY_STATUS1: DB " PORT1=" ; display text
MY_STATUS2: DB " PORT2="
CMNDS: DB 254D,1D ; serial lcd commands
SERIAL_LCD: DB '1','2','3' ; stored no.s to be displayed when read from P1
ORG 0000H
MOV R1,#0FFH
MOV R2,#0FFH
MOV R3,#07H ; ' POTR1 ' uses 7 characters including space
MOV R4,#02 ; two command codes execute counter
MOV R5,#07H ; ' PORT2 ' uses 7 characters including space
MOV R6,#02
MOV SCON,#50H ; serial communication in MODE-1
84

MOV TMOD,#20H
MOV TH1,#-3 ;9600 baud rate
SETB TR1

MOV DPTR,#MY_STATUS1 ; display ready


AGAIN1:CLR A
MOVC A,@ A+DPTR
ACALL SEND
INC DPTR
CLR A
DJNZ R3,AGAIN1
SJMP PORT_1
SEND: CLR TI ; send subprogram
ACALL DELAY
MOV SBUF, A
HERE: JNB TI, HERE
RET

UP_1: MOV R2,#0FFH


DELAY: DJNZ R2,DELAY
DJNZ R1,UP_1
MOV R1,#0FFH
MOV R2,#0FFH
RET
PORT_1: MOV DPTR, #SERIAL_LCD ; compares the input data with stored data
MOV A, P1
CJNE A, #01H, N2 ;If the eneterd no. is equal transmit serially
CLR A
MOVC A, @ A+DPTR
ACALL SEND

N2: CJNE A, #02H, N3


CLR A
INC A
MOVC A,@ A+DPTR ;if the eneterd no. is equal transmit serially
ACALL SEND

N3: CJNE A,#03H,PORT_2


CLR A
INC A
INC A
MOVC A,@ A+DPTR ;if the eneterd no. is equal transmit serially
ACALL SEND
85

SJMP PORT_2 ; PORT-2 display reading and displaying


PORT_2: LCALL DELAY ; lcd commands to clear the display
LCALL DELAY
LCALL DELAY
MOV DPTR, # CMNDS
C1: CLR A
MOVC A, @ A+DPTR
ACALL SEND
INC DPTR
DJNZ R6, C1
MOV DPTR,# MY_STATUS2 ; display ready
AGAIN2: CLR A
MOVC A, @ A+DPTR
LCALL SEND
INC DPTR
CLR A

DJNZ R5, AGAIN2


SJMP DISP

DISP: MOV DPTR, #SERIAL_LCD ; compares the input data with stored
MOV A, P2
CJNE A, #01H, J2 ;if the eneterd no. is equal transmit serially
CLR A
MOVC A, @ A+DPTR
LCALL SEND

J2: CJNE A, #02H, J3


CLR A
INC A
MOVC A,@ A+DPTR ;if the eneterd no. is equal transmit serially
LCALL SEND

J3: CJNE A,#03H,J4


CLR A
INC A
INC A
MOVC A,@ A+DPTR ;if the eneterd no. is equal transmit serially
LCALL SEND

J4: MOV R6,#02


MOV DPTR,# CMNDS
C3: CLR A
86

MOVC A, @ A+DPTR
LCALL SEND
INC DPTR
DJNZ R6,C3

OVER: NOP
END

OUTPUT:

Here the PORT-1 data is


displayed which given in
binary at PORT-1 and its
decimal value ‘2’ is
displayed.

Here the PORT-2 data is


displayed which given in
binary at PORT-2 and its
decimal value ‘3’ is
displayed.

INFERENCE:
To transmit data serially first the mode of the SCON register should be selected
which is usually mode-1, then set the baud rate and load the SBUF register
with the character code and monitor ‘TI’ flag if it is set the character byte is
transmitted, repeat the same for no. of characters in a word. After displaying
the text read the data from PORT-1 and again transmit it serially. After that
read the text from PORT-2 and display it

RESULT:
The transmission of data received from PORT-1 and PORT-2 has been displayed
successfully.

Signature of the lecturer


87

INTERRUPTS IN 8051 MICROCONTROLLER


The most powerful and important features are interrupts in 8051
microcontroller. In most of the real-time processes, to handle certain
conditions properly, the actual task must be halt for some time – it takes
required action – and then must return to the main task. For executing such
type of programs, interrupts are necessary. It entirely differs from the polling
method where the processor must check sequentially each device and ask
whether the service is required or not while consuming more processor time

Interrupts in 8051 microcontroller are


more desirable to reduce the regular
status checking of the interfaced
devices or inbuilt devices. Interrupt is
an event that temporarily suspends the
main program, passes the control to a
special code section, executes the
event-related function and resumes the
main program flow where it had left off.

Interrupts are of different types like software and hardware, maskable and
non-maskable, fixed and vector interrupts, and so on. Interrupt Service Routine
(ISR) comes into the picture when interrupt occurs, and then tells the
processor to take appropriate action for the interrupt, and after ISR execution,
the controller jumps into the main program.

Types of Interrupts in 8051 Microcontroller

The 8051 microcontroller can recognize five different events that cause the
main program to interrupt from the normal execution. These five sources of
interrupts in 8051are:

1. Timer 0 overflow interrupt- TF0

2. Timer 1 overflow interrupt- TF1

3. External hardware interrupt- INT0

4. External hardware interrupt- INT1

5. Serial communication interrupt- RI/TI

The Timer and Serial interrupts are internally generated by the microcontroller,
whereas the external interrupts are generated by additional interfacing
devices or switches that are externally connected to the microcontroller. These
external interrupts can be edge triggered or level triggered. When an interrupt
occurs, the microcontroller executes the interrupt service routine so that
88

memory location corresponds to the interrupt that enables it. The Interrupt
corresponding to the memory location is given in the interrupt vector table
below.

Interrupt Enable (IE) Register: This register is responsible for enabling and
disabling the interrupt. It is a bit addressable register in which EA must be set
to one for enabling interrupts. The corresponding bit in this register enables
particular interrupt like timer, external and serial inputs. In the below IE
register, bit corresponding to 1 activates the interrupt and 0 disables the
interrupt.
89

PROGRAM-40
GENERATING 50% DUTY CYCLE AND ALSO SERIAL DATA TRANSMISSION
USING INTERRUPTS
AIM: To perform the following tasks using interrupts
1. Generate a 50% duty cycle on P1.3
2. To read the data from PORT-2 and transmit it serially
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Load the ‘IE’ register with ‘82H’ for enabling and interrupt execution for
timer-0
2. The ISR address of Timer-0 id ‘000BH’
3. Load the TMOD with ‘21H’ for both timer-0 and timer-1
4. start the timer TR1 for 9600 baud rate
5. compliment the P1.3 output and start the timer-0
6. If the ‘TF0’ flag is set the program control transfer to the ISR address of
that interrupt
7. Then it clears the ‘TF0’ and again starts the timer by complimenting the
input
8. By the time this happens the data is read from PORT-2 and transmitted
serially.
PROGRAM:
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP SQUARE_ISR
MAIN: MOV IE, #82H ; loading appropriate interrupts used value
MOV P2, #0FFH ; configuring P2 as input
SQUARE: MOV TMOD, #21H ; timer1 mode-2, timer0 mode-0
MOV TH1, #-3
SETB TR1
CPL P1.3
MOV TL0,#00H ; timer for 71ms
MOV TH0,#00H
SETB TR0
90

AGAIN: MOV A, P2
MOV SBUF, A
HERE: JNB TI, HERE
CLR T1
SJMP AGAIN
ORG 0500H
SQUARE_ISR:CLR TR0 ; ISR for squae wave
CLR TF0
MOV TL0, #00H
MOV TH0, #00H
CPL P1.3
SETB TR0
RETI
END

OUTPUT:

Displaying a bracket

INFERENCE:
To use interrupts the ‘IE’ register should be loaded with appropriate value so
that if any of the interrupts set the program control moves to its corresponding
ISR.
RESULT:
The generation of square wave and serial data transmission using interrupts
has been performed.
Signature of the lecturer
91

PROGRAM-41
PERFORMING THE TASKS USING INTERRUPTS
AIM: To perform the following tasks using interrupts
1. To send “MICROCONTROLLER” word serially
2. Generate a square wave of ON period 1085µsec and OFF period of
108.5µsec
3. Read the data from PORT-1 and send it to PORT-2
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Load the text to be displayed in the code memory
2. Load the ‘IE’ register with ‘92H’ to monitor the serial and timer-0
interrupts
3. Load the TMOD with ‘21H’ for timer-1 in mode-2 and timer-0 in mode-1
4. Load the ‘SCON’ with ‘50H’ for the serial communication
5. Start the timer-1 for a baud rate of 9600
6. Load ‘R1’ with ‘17’ or ‘11H’ to display the 17 characters in the text
7. Load the DPTR with address of the text
8. Load the character pointed by the DPTR into the accumulator
9. Load the ‘SBUF’ register with content of accumulator and serial data
transmission begins
10.By the time compliment the P1.3 output and start the timer-0
11.Now continuously read the data from port-1 and sent to port-2
12.Now if the ‘TI’ flag sets the program control transfers to serial ISR
13.There it clears the ‘TI’ increment the DPTR with next character and again
transmit it serially
14.By the time if the ‘TF0’ sets the program control transfer to square ISR
15.Here it clears the ‘TR0’ and ‘TF0’ and checks the status of R2 if it is ‘1’
then it load the ‘TH0’ and ‘TL0’ with a value which causes a delay of
108.5µs and makes R2 to ‘0’ so next time we will have delay of 1085µs
16.The same loop is repeated, if the character is completely displayed then
again it is displayed from beginning
92

PROGRAM:
ORG 0F00H
TEXT: DB 'M','I','C','R','O','C','O','N','T','R','O','L','L','E','R'
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP SQUARE_ISR
ORG 0023H
LJMP SERIAL_ISR
ORG 0600H
MAIN: MOV IE, #92H
MOV TMOD,#21H
MOV R2,#00H
MOV SCON,#50H
MOV P1,#0FFH
MOV TH1, #-3
SETB TR1
MOV R1,#11H ;17 CHARACTERS
MOV DPTR, #TEXT
MOVC A, @ A+DPTR
MOV SBUF,A

CPL P3.4
MOV TL0, #18H ; 1085 MICROSECONDS DELAY
MOV TH1, #0FCH

MOV R2,#01H
SETB TR0
AGAIN: MOV A, P1
MOV P2,A
SJMP AGAIN
ORG 0400H
SERIAL_ISR: CLR TI
DJNZ R1, NEXT_1
MOV DPTR,#TEXT
CLR A
MOVC A, @ A+DPTR
MOV R1, #11H
ACALL DELAY
MOV SBUF,A
93

SJMP OVER
NEXT_1: INC DPTR
CLR A
MOVC A, @ A+DPTR
ACALL DELAY
MOV SBUF,A
SJMP OVER
DELAY: MOV R4,#0FFH
RELOAD: MOV R5,#0FFH
HERE: DJNZ R5,HERE
DJNZ R4,RELOAD
RET
OVER: RETI
ORG 0B00H
SQUARE_ISR: CLR TF0
CLR TR0
MOV A, R2
CJNE A, #01H, DELAY_2
MOV TL0, #9CH ;108.5 MICROSEC
MOV TH0, #0FFH
CPL P3.4
MOV R2,#00H
SETB TR0
RETI
DELAY_2: MOV TL0, #18H
MOV TH0, #0FCH ;1085 MICROSEC

CPL P3.4
MOV R2,#01H
SETB TR0
RETI
END
94

OUTPUT:

Square wave with 1085 and 108.5 microsec

Sending data to PORT-2


from PORT-1

Displaying
“MICROCONTROLLER” on
display device.

INFERENCE:
To use interrupts the ‘IE’ register should be loaded with appropriate value so
that if any of the interrupts set the program control moves to its corresponding
ISR.
RESULT:
The generation of square wave with different duty cycle, serial transmission,
reading data has been performed using interrupts.

Signature of the lecturer


95

PROGRAM-42
LED CONTROL USING PUSH BUTTONS
AIM: To control two led’s connected to P2.0 and P2.3 using two push buttons
connected to P1.0 and P1.3
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Load the bit state of P1.0 into the carry flag
2. Jump if no carry to ‘NEXT’ label and clear P2.0
3. If there is carry set the P2.0
4. Now load the bit state of P1.3 into the carry flag
5. Jump if no carry to ‘NEXT_1’ label and clear P2.3
6. If there is carry set the P2.3
7. Repeat the same from the beginning to check the status
PROGRAM:
ORG 0000H
MOV P2,#00
MOV P1,#00H
AGAIN: MOV R2,#0FFH
MOV R3,#0FFH
ACALL DELAY
MOV C, P1.0 ; read the button-1 state
JNC NEXT
SETB P2.0 ; if carry set P2.0
CLR C
PORT2: MOV C, P1.3
JNC NEXT1
SETB P2.3
CLR C
SJMP OVER
NEXT: CLR C
MOV P2.0, C ; no carry clear P2.0
SJMP PORT2
NEXT1: CLR C
96

MOV P2.3,C
OVER: SJMP AGAIN
UP: MOV R2,#0FFH
DELAY: DJNZ R2,DELAY
DJNZ R3,UP
RET
END

OUTPUT:

Button-1 is pressed Button-1 is pressed


led-1 glows led-1 glows

INFERENCE:
To interface the led’s with push button read the push button state at set the
particular bit to glow either LED.
RESULT:
The LED control with push buttons has been performed.

Signature of the lecturer


97

PROGRAM-43
LED CONTROL USING TIMERS AND INTERRUPTS
AIM: To control two led’s connected to P2.0 and P2.3 simultaneously without
any delay when two push buttons pressed together using timers and
interrupts.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the main program from 0000H and the jump to any other location
(0800H in this case).
2. Make the outputs of P2.0 and P2.3 as low
3. Read the button state of P2.0 by sending the bit state to carry flag if it is
‘1’ then set the pin high and start the timer
4. If the ‘TF0’ sets the program control goes to the ISR which is very
minimum time and reads the second button state before the first one is
released
5. Then checks the carry bit if it is ‘1’ set the P2.3 otherwise resets the P2.3
and returns to the main program and this loop continues
PROGRAM:
ORG 0000H
LJMP MAIN
ORG 000BH ;timer-0 interrupt ISR
LJMP ISR
ORG 0800H
MAIN:MOV P2, #00H
MOV P1, #00H
MOV TMOD, #01H
MOV IE, #82H ; interrupt for timer-0
AGAIN:MOV C, P1.0
JNC NEXT
SETB P2.0
MOV TL0, #0FDH ; delay values
MOV TH0, #0FFH
SETB TR0
CLR C
98

SJMP AGAIN
NEXT: MOV P2.0, C
CLR C
MOV TL0, #0FDH ; delay values
MOV TH0, #0FFH
SETB TR0
SJMP AGAIN

ORG 0F00H
ISR: CLR TR1
CLR TF1
MOV C ,P1.3
JNC NEXT1
SETB P2.3

CLR C
SJMP OVER
NEXT1: MOV P2.3, C
CLR C
OVER: RETI
END

OUTPUT:

Two ports connected to


single button such tat two
buttons pressed together
99

INFERENCE:
To interface the led’s with push button using timers and interrupts
continuously read any of the button state and also after reading run a small
time delay and use interrupts to immediately read the next button state befoe
the button is released
RESULT:
The LED control with push buttons and using timers and interrupts has been
performed.

Signature of the lecturer


100

PROGRAM-44
LCD INTERFACING USING 8051 MICROCONTROLLER
AIM: To display the text ‘ANAKAPALLI’ on a 16 x 2 LCD display using 8051
microcontroller.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from the different location in the code memory to
store the data text to be displayed
2. Also store the commands for lcd commands which should be executed in
an order
3. Now star the main program from 0000H
4. Load the DPTR with the commands address
5. The last byte of data in the ‘CMNDS’ and ‘DATA1’ is ‘0’
6. So if ‘0’ is received then jump to the label specified
7. If not zero call the ‘CMDWRITE’ subprogram
8. Here the command register is selected by clearing P2.0 and write pin
P2.1 is active low which is also activated by pulling low
9. Enable pin connected to P2.2 has been set and reset immediately for a
negative edge trigger
10.If the value in the location pointed by DPTR is 0
11.Then it jumps to ‘DATA_SEND’ and the character is send to accumulator
and again checked for zero
12.If it is not zero calls the ‘DATAWRITE’ label and selects the data register
by setting P2.0 and also write mode by clearing P2.1
13.Enable pin is positive edge triggered by clearing and resetting P2.2
14.A delay is called before displaying a character to wait for the LCD to
ready
15.The same loop is repeated until all characters are displayed .
PROGRAM:
ORG 0200H
DATA1: DB 'A','N','A','K','A','P','A','L','L','I',0
CMNDS: DB 38H, 0EH, 01H, 06H, 0
ORG 0000H
MOV DPTR, #CMNDS
101

B1: CLR A
MOVC A, @ A+DPTR
JZ DATA_SEND
ACALL CMDWRITE
INC DPTR
SJMP B1
DATA_SEND: MOV DPTR, #DATA1
B2: CLR A
MOVC A, @ A+DPTR
JZ OVER
ACALL DATAWRITE
INC DPTR
SJMP B2
OVER: SJMP OVER
CMDWRITE: ACALL DELAY
MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET

DATAWRITE: ACALL DELAY


MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DELAY: MOV R3, #0FFH
UP: MOV R2, #0FFH
AGAIN: DJNZ R2, AGAIN
DJNZ R3,UP
RET
END
102

OUTPUT:

INFERENCE:
To interface an LCD the data and command registers should be loaded properly
and also the ‘R/S’, ‘R/W’, Enable should be controlled properly
RESULT:
The lcd interfacing with 8051 to display a text has been performed.

Signature of the lecturer


103

PROGRAM-45
KEYBOARD INTERFACING USING 8051 MICROCONTROLLER
AIM: To interface a 4 x 4 keyboard with 8051 microcontroller and display the
character for every key press.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0F00H and store the character code arranged in
the pattern of rows each low contain four characters.
2. Also store the commands for LCD
3. Now start the program from 0000H
4. Here all the rows are connected to P1.0 to P1.3 all the columns are
connected to P2.0 to P2.3
5. First we need to check the button release state which may be pressed
previously
6. Initially all the column bits are set an the row bits are reset
7. Now to check the button release compare the column value with ‘0FH’ if
it is equal button is released
8. If it is not equal button is not released again check the button state unitl
it is released
9. When the button is released then read the next button state by
comparing it column port-2 with ‘0F’ if it is not equal button is pressed, if
not equal button is not pressed
10.Now, if button is pressed confirm the button press by waiting for a delay
which is called debouncing delay
11.Now, if the button state is High then it is pressed load the column bits
arrangement P2 in a location say 50H
12.Now read the row bit for which load the row bits with one ‘0’ as every
pin by making remaining pins ‘1’ repeat the same for all four pins and
the compare the P2 state with ‘0FH’
13.When particular row is made ‘0’ the column values also becomes not
equal to ‘0FH’
14.Now this row address is loaded into DPTR and the particular button
could be detected by shifting right the previously stored column value in
50H
104

15.The right shift is performed until the first zero and the count that how
many times carry flag is set is stored in accumulator
16.This value is added to the DPTR’s address to identify the exact key and
then the keycode is loaded to accumulator
17.The code is displayed on the LCD.
PROGRAM:
ORG 0F00H
ROW0: DB '0','1','2','3'
ROW1: DB '4','5','6','7'
ROW2: DB '8','9','A','B'
ROW3: DB 'C','D','E','F'
CMNDS: DB 38H, 0FH, 06H, 0
ORG 0000H
START: MOV P1,#00H
MOV P2,#0FH
MOV TMOD, #10H
CLR A
UP: MOV A,P2
CJNE A, #0FH, UP ; CHECKING PREVIOUS BUTTON RELEASE
SJMP NEXT_1
NEXT_1:MOV A,P2
CJNE A, #0FH, PRESS
SJMP NEXT_1 ; DETECTING KEYPRESS
PRESS: ACALL DELAY
CJNE A, #0FH, PRESS_1
SJMP NEXT_1
PRESS_1:MOV 50H, A ; STORING COLUMN VALUE
MOV P2,#0FH
MOV P1,#0EH
MOV A ,P2
CJNE A, #0FH, N1 ; NOT EQUAL MEANS BUTTON PRESSED
SJMP PRESS_2
N1:MOV DPTR, #ROW0
SJMP COLNO
PRESS_2:MOV P2,#0FH
MOV P1,#0DH
MOV A ,P2
CJNE A,#0FH,N2 ; NOT EQUAL MEANS BUTTON PRESSED
105

SJMP PRESS_3
N2: MOV DPTR, #ROW1
SJMP COLNO
PRESS_3: MOV P2,#0FH
MOV P1,#0BH
MOV A ,P2
CJNE A, #0FH ,N3 ;NOT EQUAL MEANS BUTTON PRESSED
SJMP PRESS_4
N3: MOV DPTR, #ROW2
SJMP COLNO
PRESS_4:MOV P2,#0FH
MOV P1,#07H
MOV A ,P2
CJNE A, #0FH, N4 ;NOT EQUAL MEANS BUTTON PRESSED
SJMP PRESS_1
N4: MOV DPTR, #ROW3
SJMP COLNO
COLNO: MOV R2,#00H ;COLUMN COUNTER
MOV A,50H
LOOP: CLR C
RRC A
JNC NUM
INC R2
SJMP LOOP
NUM: MOV A,R2
MOVC A, @ A+DPTR
MOV 70H,A ;KEY CODE
SJMP DISP
DELAY:MOV TL1,#00H
MOV TH1,#00H
SETB TR1
HERE:JNB TF1, HERE
CLR TF1

CLR TR1
RET

DISP: MOV DPTR, #CMNDS


B1: CLR A
MOVC A, @ A+DPTR
106

JZ LOOP_1
ACALL CMDWRITE ; P2.5=RS, P2.6=R/W, P2.7=E
INC DPTR
SJMP B1
LOOP_1:MOV A,70H
ACALL DATAWRITE
LJMP START
CMDWRITE: ACALL DELAY
MOV P3,A
CLR P2.5
CLR P2.6
SETB P2.7
CLR P2.7
RET

DATAWRITE: ACALL DELAY


MOV P3,A
SETB P2.5
CLR P2.6
SETB P2.7
CLR P2.7
RET
END
OUTPUT:

Button-0 is pressed All Button are


pressed
107

INFERENCE:
To interface a keyboard first columns must be checked with ‘0FH’ if it is not
equal then confirm the press and check the row bit by consequently making
any of the pin ‘0’ while remaining row pins made High, and the right shift the
columns value until the first ‘0’ and the count of 1’s is added to row address to
get the keycode
RESULT:
The keyboard interfacing with 8051 and displaying a character has been
performed.

Signature of the lecturer


108

PROGRAM-46
RELAY INTERFACING USING 8051 MICROCONTROLLER
AIM: To interface relay with 8051 microcontroller to control a high volage
lamp
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0000H
2. First compliment the output of P2.5
3. Call the delay
4. Repeat the loop indifintely
PROGRAM:
ORG 0000H
AGAIN: CPL P2.5
ACALL DELAY
ACALL DELAY
ACALL DELAY
SJMP AGAIN
DELAY: MOV R2,#0FFH
UP: MOV R3,#0FFH
HERE: DJNZ R3,HERE
DJNZ R2,UP
RET
END

OUTPUT:

DELAY
109

SIMULATION:

Relay is not connecting Relay is connecting


lamp to GND lamp to GND

INFERENCE:
Here the relay is interfaced using ULN2003 IC which provides enough current
to the relay Coil which cannot be provided by the microcontroller.
RESULT:
The relay interfacing with 8051 has been performed.

Signature of the lecturer


110

PROGRAM-47
OPTOCOUPLER INTERFACING USING 8051 MICROCONTROLLER
AIM: To interface optocoupler with 8051 microcontroller to control a high
volage lamp
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0000H
2. First compliment the output of P2.5
3. Call the delay
4. Repeat the loop indifintely
PROGRAM:
ORG 0000H
AGAIN: CPL P2.5
ACALL DELAY
ACALL DELAY
ACALL DELAY
SJMP AGAIN
DELAY: MOV R2,#0FFH
UP: MOV R3,#0FFH
HERE: DJNZ R3,HERE
DJNZ R2,UP
RET
END

OUTPUT:

DELAY
111

SIMULATION:

Lamp turned
ON

INFERENCE:
Here the optocoupler is interfaced with 8051 microcontroller to provide
adequate current which cannot be provided by 8051
RESULT:
The optocoupler interfacing with 8051 has been performed.

Signature of the lecturer


112

PROGRAM-48
STEPPER MOTOR INTERFACING USING 8051 MICROCONTROLLER USING
4-STEP SEQUENCING
AIM: To interface stepper motor with 8051 using 4-step sequencing.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0000H
2. Load the accumulator with 99H
3. Load the PORT-2 with content of accumulator
4. Right shift the bits of the accumulator
5. Load the content of accumulator to PORT-2
6. Call the delay
7. Repeat the loop continuously
PROGRAM:
ORG 0000H
MOV A, #99H
AGAIN: MOV P2, A
ACALL DELAY
RR A
SJMP AGAIN
DELAY: MOV R2, #0FFH
UP: MOV R3, #0FFH
HERE: DJNZ R3, HERE
DJNZ R2, UP
RET
END
OUTPUT:

Right shifted
113

SIMULATION:

Rotating clockwise and


ULN2003 is used as a
driver

INFERENCE:
Here the stepper motor is interfaced with 8051 using ULN2003 IC which
provides enough current to the motor, which cannot be provided by the
microcontroller, and it is running in 4 step sequence where the 1001 is right
shifted and the loop is repeated
RESULT:
The stepper motor interfacing with 8051 in 4-step sequence has been
performed.

Signature of the lecturer


114

PROGRAM-49
STEPPER MOTOR INTERFACING USING 8051 MICROCONTROLLER USING
4-STEP SEQUENCING TO ROTATE 72◦
AIM: To interface stepper motor with 8051 using 4-step sequencing to rotate
72 degrees
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Load the accumulator with 99H
2. Load the ‘R2’ register with 36 since the each step is 2 degrees then 36
steps gives 72 degrees
3. Move the content of accumulator to the P2
4. Call the delay sub program
5. Right shift the bits of accumulator for 36 times
6. Repeat until ‘R2’ becomes ’0’
PROGRAM:
ORG 0000H
MOV A, #99H ; Load 99H to accumulator to get next step while
MOV R2,#36 ;Load R2 with 36 to make 72⁰step angle
AGAIN: MOV P2,A ;Load port-2 with accumulator content
ACALL DELAY
RR A ; rotate right accumulator
DJNZ R2,AGAIN ; decrement R2 until it becomes ‘0’
SJMP OVER
DELAY: MOV R2,#0FFH ; delay program
UP: MOV R3,#0FFH
HERE: DJNZ R3,HERE
DJNZ R2,UP
RET
OVER: NOP
END
OUTPUT:
115

INFERENCE:
To get a step angle of 72 degrees repeat the 4-step sequence for 36 times so
that we get a rotation of 72 degree
RESULT:
The rotation of a stepper motor to 72 degrees using 4-step sequence has been
performed.

Signature of the lecturer


116

PROGRAM-50
TO RUN THE STEPPER MOTOR IN CLOCKWISE AND ANTI-CLOCKWISE
DIRECTION USING 8-STEP SEQUENCE
AIM: To write an assembly language program to run the stepper motor in
clockwise and anticlockwise direction using 8-step sequence and also by using
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from the code memory ‘0F00H’
2. Load the code memory with 8- bit sequence values
3. Start the main program from 0000H
4. Load the ‘R2’ with ‘7’ to point the seven location from the beginning
5. Load ‘DPTR’ with the starting address of the code memory
6. Load the carry flag with status of the P1.5
7. If the carry flag is set rotate in anticlockwise
8. If there is no carry then rotate in clock wise
9. Repeat loop by checking the status of P1.5
PROGRAM:
ORG 0F00H
SEQ: DB 09H, 08H, 0CH, 04H, 06H, 02H, 03H, 01H
ORG 0000H
AGAIN: CLR A
MOV R2,#07H ;R2 initialised as counter to read the code memory
MOV DPTR,#0F00H ;Load DPTR with ‘0F00h’
MOV C,P1.5 ;Load port1.5 content to carry
JC NEXT ; If there is carry go to next instruction
UP: MOVC A, @ A+DPTR
MOV P2,A ;Load port2 with accumulator content
ACALL DELAY ;Call delay program
INC DPTR ;Increment DPTR content to next location
DJNZ R2,UP ;Decrement R2 until it is ‘0’ and go to ‘UP’ label
SJMP AGAIN
NEXT: MOV R5,#07H ; R5 initialised as counter to read the code memory
UP_1: MOV A,R5 ;Load accumulator with R5 CONTENT
MOV DPTR,#0F00H
117

MOVC A, @ A+DPTR
MOV P2,A ;Load accumulator content to port-2
ACALL DELAY
DJNZ R5,UP_1 ;Decrement R5 until it is ‘0’ and go to label ‘up’
SJMP AGAIN
DELAY:MOV R3,#0FFH ;DELAY PROGRAM
UP_2: MOV R4,#0FFH
HERE: DJNZ R4,HERE
DJNZ R3,UP_2
RET
END

OUTPUT:

When switch is closed


motor rotates
clockwise

When switch is
open motor rotates
anti-clockwise
118

INFERENCE:
With this program we can easily rotate the stepper in clockwise and in anti-
clockwise by ON and OFF of a switch by changing the content of DPTR either
from start or last
RESULT:
The stepper motor direction has been controlled and also used 8-step
sequence

Signature of the lecturer


119

PROGRAM-51
STEPPER MOTOR INTERFACING USING 8051 MICROCONTROLLER USING
4-STEP WAVE DRIVE SEQUENCING
AIM: To interface stepper motor with 8051 using 4-step sequencing.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0F00H
2. Load the 4-step wave drive sequence values in the code memory
3. Load the ‘R2’ register with ‘4’ to point four values
4. Load the ‘DPTR’ with the starting address of the 4-step wave driving
values
5. Load the content of accumulator to P2
6. Increment ‘DPTR’ until the R2 becomes ‘0’
7. Ones ‘R2’ becomes ‘0’ load it again and also load the ‘DPTR’ from the
starting address.
PROGRAM:
ORG 0F00H
SEQ: DB 00H, 01H, 02H, 03H
ORG 0000H
REPEAT:MOV R2,#04H
MOV DPTR, #SEQ
CLR A
MOV A, @ A+DPTR
AGAIN: MOV P2, A
ACALL DELAY
INC DPTR
DJNZ R2,AGAIN
SJMP REPEAT
DELAY: MOV R2, #0FFH
UP: MOV R3, #0FFH
HERE: DJNZ R3, HERE
DJNZ R2, UP
RET
END
120

OUTPUT:

Stepper motor
rotating

INFERENCE:
Here the stepper motor is interfaced with 8051 using ULN2003 IC which
provides enough current to the motor, which cannot be provided by the
microcontroller, and it is running in 4 step wave driving sequence where the
1001 is right shifted and the loop is repeated
RESULT:
The stepper motor interfacing with 8051 in 4-step wave driving sequence has
been performed.

Signature of the lecturer


121

PROGRAM-52
DC MOTOR INTERFACING WITH 8051 AND ALSO CONTROL ITS DIRECTION
AIM: To interface DC motor with 8051and also control its direction.
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Load the bit status of P2.0 into the carry flag
2. If the carry flag is set then set the P1.0 which is the enable pin of L293d
3. Now to control the rotation direction read the bit status of P2.5
4. If the carry is set then rotate in the clockwise direction
5. If the carry flag is not set rotate in anticlockwise direction by changing
the set and reset of P1.1 and P1.2
6. Repeat the loop by checking the control bits status P2.0 and P2.5 again
PROGRAM:
ORG 0000H
READ: CLR C
MOV C, P2.0 ; ON and OFF switch
JC ROTATE
CLR P1.0 ;ENABLE OF L293D
SJMP READ
ROTATE: CLR C
SETB P1.0
MOV C , P2.5
JC CLKWISE
SETB P1.1
CLR P1.2
SJMP READ
CLKWISE: CLR C
CLR P1.1
SETB P1.2
SJMP READ
END
122

OUTPUT:

Motor rotates in
anticlockwise direction
when the P2.5 is low

Motor rotates in
clockwise direction when
the P2.5 is low

INFERENCE:
To control the DC motor we have used L293D which is a dual H-bridge motor
driver and to control the direction just reverse the bit states of the P1.2 and
P1.2
RESULT:
The interfacing of the DC motor and also controlling its direction has been
performed.

Signature of the lecturer


123

PROGRAM-53
DC MOTOR INTERFACING WITH 8051 AND ALSO CONTROL ITS SPEED
AIM: To interface DC motor with 8051and also control speed of the motor
EQUIPMENT REQUIRED:
1. Keil µvision software
2. 8051 development board
3. Universal programmer
4. Proteus
ALGORITHM:
1. Start the program from 0000H
2. Here P2.0 and P2.1 are the control bits to set the motor speed
3. First load the bit status of P2.0 if the carry flag is set then move to ‘NEXT’
label
4. If there is no carry check the bit status of P2.2 if it is ‘0’ then rotate with
full speed 100% with P1.0 enable pin High
5. If the P2.2 is ‘1’ then jump to ‘seventyfive’ label and call the delay for
three times and compliment the P1.0 and again call the delay ones
6. At the next label if P2.0 is ‘1’ and P2.1 is ‘0’ then execute 50% duty cycle
by calling delay two times and compliment the output and again call the
delay for two times
7. If the P2.0 is ‘1’ and P2.2 is ‘1’ then rotate at 25% by setting the P1.0 and
call the delay one time and again compliment P1.0 and call the delay 3
times
8. Repeat to check the status of the control bits P2.0 and P2.2 and repeat
the loop
PROGRAM:
ORG 0000H
SETB P1.0
SETB P1.1
CLR P1.2
CLR P2.0
CLR P2.2
UP: MOV C,P2.0
JC NEXT
MOV C,P2.2
JC SEVENTYFIVE
SETB P1.0 ; NO CARRY 100%
SJMP UP
124

SEVENTYFIVE: SETB P1.0


ACALL DELAY
ACALL DELAY
ACALL DELAY
CLR P1.0
ACALL DELAY
SJMP UP
NEXT: MOV C ,P2.2
JC TWENTYFIVE
SETB P1.0
ACALL DELAY
ACALL DELAY
CLR P1.0
ACALL DELAY
ACALL DELAY
SJMP UP

TWENTYFIVE: SETB P1.0


ACALL DELAY
CLR P1.0
ACALL DELAY
CLR P1.0
ACALL DELAY
ACALL DELAY
ACALL DELAY
SJMP UP

DELAY: MOV R2, #0FFH


UP_1: MOV R3,#0FFH
HERE: DJNZ R3,HERE
DJNZ R2,UP_1
RET
END
125

OUTPUT:

100% 75%

50% 25%

INFERENCE:
To control the DC motor we have used L293D which is a dual H-bridge motor
driver and to control the speed of the DC motor we use two control pins for
which push buttons are connected that is P2.0 and P2.2
RESULT:
The interfacing of the DC motor and also controlling its speed has been
performed.

Signature of the lecturer

You might also like