Professional Documents
Culture Documents
8051
8051
8051
8051 PROGRAMMING
8 BIT ADDITION
AIM:
To write a program to add two 8-bit numbers using 8051 microcontroller.
ALGORITHM:
1. Clear Program Status Word.
2. Select Register bank by giving proper values to RS1 & RS0 of PSW.
3. Load accumulator A with any desired 8-bit data.
4. Load the register R 0 with the second 8- bit data.
5. Add these two 8-bit numbers.
6. Store the result.
7. Stop the program.
FLOW CHART:
START
Clear PSW
Select Register
Bank
Load A and R 0
with 8- bit datas
Add A & R 0
Store the sum
STOP
CLR
HEX
CODE
C3
4101
MOV
A, data1
74,data1
4103
ADDC
A, # data 2
24,data2
4105
MOV
90,45,00
4108
MOVX
DPTR, #
4500H
@ DPTR, A
SJMP
L1
80,FE
4109
L1
OUTPUT
MEMORY LOCATION
F0
COMMENTS
Clear CY Flag
DATA
4500
RESULT:
Thus the 8051 ALP for addition of two 8 bit numbers is executed.
EX. NO.
8 BIT SUBTRACTION
AIM:
To perform the subtraction of two 8 bit numbers and store the result in memory.
ALGORITHM:
a. Clear the carry flag.
b. Initialize the register for borrow.
c. Get the first operand into the accumulator.
d. Subtract the second operand from the accumulator.
e. If a borrow results increment the carry register.
f. Store the result in memory.
FLOWCHART:
START
CLEAR CARRY
FLAG
GET IST
OPERAND IN
ACCR
SUBTRACT THE
2ND OPERAND
FROM ACCR
N
IS CF=1
Y
INCREMENT
THE BORROW
REGISTER
STORE
RESULT IN
MEMORY
STOP
CLR
HEX
CODE
C3
4101
MOV
A, # data1
74, data1
4103
SUBB
A, # data2
94,data2
4105
MOV
DPTR, # 4500
90,45,00
4108
MOVX
@ DPTR, A
F0
SJMP
L1
80,FE
4109
L1
OUTPUT
MEMORY LOCATION
COMMENTS
Store data1 in
accumulator
Subtract data2 from
data1
Initialize memory
location
Store the difference
in memory location
Stop
DATA
Clear CY flag
4500
RESULT:
Thus the 8051 ALP for subtraction of two 8 bit numbers is executed.
4
EX. NO.
8051 PROGRAMMING
8 BIT MULTIPLICATION
AIM:
To perform multiplication of two 8 bit data and store the result in memory.
ALGORITHM:
a. Get the multiplier in the accumulator.
b. Get the multiplicand in the B register.
c. Multiply A with B.
d. Store the product in memory.
FLOWCHART:
START
GET
MULTIPLIER IN
ACCR
GET
MULTIPLICAND
IN B REG
MULTIPLY A
WITH B
STORE
RESULT IN
MEMORY
STOP
8 Bit Multiplication
ADDRESS LABEL
MNEMONIC
OPERAND
4100
MOV
A ,#data1
HEX
CODE
74, data1
4102
MOV
B, #data2
75,data2
Store data1 in
accumulator
Store data2 in B reg
4104
MUL
A,B
F5,F0
Multiply both
4106
MOV
90,45,00
4109
MOVX
DPTR, #
4500H
@ DPTR, A
401A
INC
DPTR
A3
Initialize memory
location
Store lower order
result
Go to next memory
location
410B
MOV
A,B
E5,F0
410D
MOV
@ DPTR, A
F0
SJMP
STOP
80,FE
Stop
410E
STOP
INPUT
MEMORY LOCATION
DATA
F0
OUTPUT
MEMORY LOCATION
4500
4502
4501
4503
COMMENTS
DATA
RESULT:
Thus the 8051 ALP for multiplication of two 8 bit numbers is executed.
EX. NO.
8051 PROGRAMMING
8 BIT DIVISION
AIM:
To perform the division of two 8 bit data and store the result in memory.
ALGORITHM:
1. Get the Dividend in the accumulator.
2. Get the Divisor in the B register.
3. Divide A by B.
4. Store the Quotient and Remainder in memory.
FLOWCHART:
START
GET DIVIDEND
IN ACCR
GET DIVISOR IN
B REG
DIVIDE A BY B
STORE
QUOTIENT &
REMAINDER
IN MEMORY
STOP
8 Bit Division
ADDRESS LABEL MNEMONIC
OPERAND
4100
MOV
A, # data1
HEX
CODE
74,data1
4102
MOV
B, # data2
75,data2
Store data1 in
accumulator
Store data2 in B reg
4104
DIV
A,B
84
Divide
4015
MOV
DPTR, # 4500H
90,45,00
4018
MOVX
@ DPTR, A
F0
Initialize memory
location
Store remainder
4109
INC
DPTR
A3
410A
MOV
A,B
E5,F0
410C
MOV
@ DPTR, A
F0
SJMP
STOP
80,FE
COMMENTS
Go to next memory
location
Store quotient
410D
STOP
INPUT
MEMORY LOCATION
4500
(dividend)
4501
(divisor)
DATA
OUTPUT
MEMORY LOCATION
4502
(remainder)
4503
(quotient)
RESULT:
Thus the 8051 ALP for division of two 8 bit numbers is executed.
8
Stop
DATA
EX. NO.
AIM:
To write an ALP to perform logical and bit manipulation operations using 8051
microcontroller.
APPARATUS REQUIRED:
8051 microcontroller kit
ALGORITHM:
1. Initialize content of accumulator as FFH
2. Set carry flag (cy = 1).
3. AND bit 7 of accumulator with cy and store PSW format.
4. OR bit 6 of PSW and store the PSW format.
5. Set bit 5 of SCON.
6. Clear bit 1 of SCON.
7. Move SCON.1 to carry register.
8. Stop the execution of program.
FLOWCHART:
START
PROGRAM TABLE
ADDRESS HEX
CODE
4100
90,45,00
LABEL
MNEMONICS
OPERAND
COMMENT
MOV
DPTR,#4500
Initialize
memory
location
Get the data in
accumulator
Set CY bit
4103
74,FF
MOV
A,#FF
4105
D3
SETB
4016
82,EF
ANL
C, ACC.7
4018
410A
E5,D0
F0
MOV
MOVX
A,DOH
@DPTR,A
410B
A3
INC
DPTR
Go to next location
410C
72,AA
ORL
C, IE.2
410E
C2,D6
CLR
PSW.6
4110
4112
E5,D0
F0
MOV
MOVX
A,DOH
@DPTR,A
4113
A3
INC
DPTR
Go to next location
4114
4116
D2,90
C2,99
SETB
CLR
SCON.5
SCON.1
4118
411A
E5,98
F0
MOV
MOVX
A,98H
@DPTR,A
411B
A3
INC
DPTR
Go to next location
411C
A2,99
MOV
C,SCON.1
411E
E5,D0
MOV
A,DOH
F0
4122
80,FE
L2
MOVX
@DPTR,A
SJMP
L2
10
Stop
MEMORY
BEFORE
AFTER
LOCATION
4500H (PSW)
CY
AC
FO
RS1
RS0 OV
EXECUTION EXECUTION
00H
88H
4501H (PSW)
CY
AC
FO
RS1
RS0 OV
40H
88H
4502H (SCON)
20H
4503H (PSW)
CY
09H
AC
FO
RS1
RS0 OV
FFH
RESULT:
Thus the bit manipulation operation is done in 8051 microcontroller.
11
EX. NO.
8051 PROGRAMMING
ASCENDING ORDER
AIM:
To sort the numbers in ascending order
ALGORITHM:
1. Get the count of the numbers which has to be sorted.
2. Get the numbers to be sorted.
3. Move the first value to the accumulator.
4. Compare the value in the accumulator with that in any register
5. Depending on the status of the flag, the values are swapped.
FLOWCHART:
START
12
PROGRAM TABLE
ADDRESS
4100
4103
4105
HEX
CODE
90,45,00
74,FF
D3
4016
82,EF
MOV R0,#ARR
4018
E5,D0
MOV R2,#04h
410A
F0
MOV R3,#00h
410B
A3
DEC R2
410C
72,AA
410E
C2,D6
MOV B,A
4110
E5,D0
INC R1
4112
F0
SUBB A,@R1
A3
JC L2
4114
4116
D2,90
MOV A,B
C2,99
XCH A,@R1
4118
E5,98
MOV @R0,A
411A
F0
MOV R3,#01h
411B
A3
4113
411C
LABEL
MNEMONICS
OPERAND
COMMENT
00h
70h
L0:
ORG
ARR EQU
MOV R1,#ARR
L1:
L2:
MOV A,@R0
INC R0
A2,99
DJNZ R2,L1
411E
4120
E5,D0
CJNE
F0
R3,#00,L0
4122
80,FE
SJMP $
END
Stop
RESULT:
Thus the numbers are sorted in ascending order using 8051 microcontroller.
13
EX. NO.
8051 PROGRAMMING
DESCENDING ORDER
AIM:
To sort the numbers in ascending order
ALGORITHM:
1. Get the count of the numbers which has to be sorted.
2. Get the numbers to be sorted.
3. Move the first value to the accumulator.
4. Compare the value in the accumulator with that in any register
5. Depending on the status of the flag, the values are swapped.
FLOWCHART:
START
14
PROGRAM TABLE
ADDRESS
4100
4103
4105
HEX
CODE
90,45,00
74,FF
D3
4016
82,EF
MOV R0,#ARR
4018
E5,D0
MOV R2,#04h
410A
F0
MOV R3,#00h
410B
A3
DEC R2
410C
72,AA
410E
C2,D6
MOV B,A
4110
E5,D0
INC R1
4112
F0
SUBB A,@R1
A3
JNC L2
4114
4116
D2,90
MOV A,B
C2,99
XCH A,@R1
4118
E5,98
MOV @R0,A
411A
F0
MOV R3,#01h
411B
A3
4113
411C
LABEL
MNEMONICS
OPERAND
COMMENT
00h
70h
L0:
ORG
ARR EQU
MOV R1,#ARR
L1:
L2:
MOV A,@R0
INC R0
A2,99
DJNZ R2,L1
411E
4120
E5,D0
CJNE
F0
R3,#00,L0
4122
80,FE
SJMP $
END
Stop
RESULT:
Thus the numbers are sorted in descending order using 8051 microcontroller.
15
EX. NO.
8051 PROGRAMMING
SMALLEST AND LARGEST NUMBER
AIM:
To find the smallest and largest number in an array.
ALGORITHM:
1. Get the numbers.
2. Move the first value to the accumulator.
3. Compare the value in the accumulator with that in any register
4. Thus the smallest number and largest number are stored in desired locations.
FLOWCHART:
START
16
PROGRAM TABLE
ADDRESS
4100
4103
HEX
CODE
90,45,00
74,FF
4105
D3
4016
82,EF
4018
E5,D0
410A
F0
MOV
DS,AX
410B
A3
MOV
CX,0000
410C
72,AA
MOV
CL,06
410E
C2,D6
LEA
BX,A
4110
E5,D0
MOV
4112
F0
MOV
AL,00
AH,BYTE
PTR[BX]
AL,BYTE
PTR[BX]
4113
4114
4116
A3
LABEL
MNEMONICS
START:
DATASEGMENT
A DB
B DB ?
DATA ENDS
CODE SEGMENT
ASSUME
DS:DATA,CS:CODE
MOV
L1:
D2,90
JNC
MOV
C2,99
4118
E5,98
411A
F0
CMP
CMP
OPERAND
COMMENT
5,2,5,6,4,3
AX,DATA
L2
AL,BYTE
PTR[BX]
AH,BYTE
PTR[BX]
JC
MOV
411B
411C
411E
4120
4122
A3
L2:
A2,99
L3:
E5,D0
F0
80,FE
L3
AH,BYTE
PTR[BX]
INC
BX
DEC
CMP
CL
JNZ
MOV
INT
CODE ENDS
END START
17
CL,00
Go to next location
L1
AH,4CH
21H
Stop
;OUTPUT:->
;-G CS: 0025
;
;AX=0206 BX=0006 CX=0000 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
;DS=0BF4 ES=0BE4 SS=0BF4 CS=0BF5 IP=0025 NV UP EI PL ZR NA PE NC
;0BF5:0025 B44C
MOV AH,4C
RESULT:
Thus the largest and smallest numbers are found using 8051 microcontroller.
18
EX.NO.
AIM:
To write an assembly language program in 8051 to rotate the motor at different speeds.
APPARATUS REQUIRED:
SL.NO
1.
2.
3.
4.
ITEM
Microprocessor kit
Power Supply
Stepper Motor Interface board
Stepper Motor
SPECIFICATION
8051
+5 V, dc,+12 V dc
-
QUANTITY
1
1
1
1
PROBLEM STATEMENT:
Write a code for achieving a specific angle of rotation in a given time and particular
number of rotations in a specific time.
THEORY:
A motor in which the rotor is able to assume only discrete stationary angular
position is a stepper motor. The rotary motion occurs in a stepwise manner from one
equilibrium position to the next. Two-phase scheme: Any two adjacent stator windings are
energized. There are two magnetic fields active in quadrature and none of the rotor pole faces
can be in direct alignment with the stator poles. A partial but symmetric alignment of the
rotor poles is of course possible.
ALGORITHM:
For running stepper motor clockwise and anticlockwise directions
(i)
(ii)
(iii)
(iv)
(v)
Repeat the above procedure both for backward and forward directions.
19
A1
A2
B1
B2
1
0
0
1
0
1
1
0
0
0
1
1
0
1
0
0
HEX
CODE
09 H
05 H
06 H
0A H
FLOWCHART:
START
INTIALIZE COUNTER FOR LOOK UP TABLE
GET THE FIRST DATA FROM THE ACCUMULATOR
MOVE DATA INTO THE ACCUMULATOR
DRIVE THE MOTOR
CIRCUITARY
DELAY
DECREMENT COUNTER
YES
IS B = 0 ?
NO
GET THE DATA FROM LOOK UP
TABLE
PROGRAM TABLE
PROGRAM
COMMENTS
OUT 0C0,AL
L1 : DEC DX
Introduce delay
JNZ L1
INC DI
LOOP LOOP1
JMP START
1200 : 09,05,06,0A
Array of datas
RESULT:
Thus the assembly language program for rotating stepper motor in both clockwise and
anticlockwise directions is written and verified.
21
EX.NO.
AIM :
To display the rolling message HELP US in the display.
APPARATUS REQUIRED:
8051 Microprocessor kit, Power supply, Interfacing board.
ALGORITHM :
Display of rolling message HELP US
1. Initialize the counter
2. Set 8279 for 8 digit character display, right entry
3. Set 8279 for clearing the display
4. Write the command to display
5. Load the character into accumulator and display it
6. Introduce the delay
7. Repeat from step 1.
0
0
0
0
1
D
0
D
DD
00- 8Bit character display left entry
01- 16Bit character display left entry
10- 8Bit character display right entry
11- 16Bit character display right entry
KKK- Key Board Mode
000-2Key lockout.
0
K
0
K
0
K
1
1
1
1
0
0
1
CD
1
CD
11
1
CD
0
CF
0
CA
0
1
0
0
1
0
AI
FLOWCHART:
23
SET UP
POINTER
INITIALIZE THE COUNTER
SET 8279 FOR 8-DIGIT CHARACTER
DISPLAY
SET 8279 FOR CLEARING THE
DISPLAY
WRITE THE COMMAND TO DISPLAY
LOAD THE CHARACTER INTO
ACCUMULATOR AND DISPLAY
DELAY
PROGRAM TABLE
PROGRAM
COMMENTS
Initialize array
MOV CX,000FH
MOV AL,10
OUT C2,AL
MOV AL,CC
OUT C2,AL
MOV AL,90
OUT C2,AL
L1 : MOV AL,[SI]
OUT C0,AL
CALL DELAY
Give delay
INC SI
LOOP L1
JMP START
Go to starting location
LOOP1 : DEC DX
JNZ LOOP1
RET
LOOK-UP TABLE:
1200
1204
98
FF
MEMORY
LOCATION
1200H
1201H
1202H
1203H
1204H
1205H
1206H
1207H
68
1C
d
1
0
0
1
1
0
0
1
7C
29
C8
FF
HEX DATA
f
0
0
0
0
1
0
1
1
98
68
7C
C8
FF
1C
29
FF
RESULT:
Thus the rolling message HELP US is displayed using 8279 interface kit.
25
EX. NO.
AIM:
To write ALP by interfacing 8255 with 8051 in mode 0, mode 1 and mode 2
APPARATUS REQUIRED:
8051 kit, 8255 interface kit.
ALGORITHM:
Mode 0
1. Initialize accumulator to hold control word
2. store control word in control word register
3. Read data port A.
4. Store data from port A in memory
5. Place contents in port B
Mode 1 & Mode 2
1. Initialize accumulator to hold control word (for port A)
2. Store control word in control word register
3. Initialize accumulator to hold control word (for port B)
4. Place contents in control word register.
5. Disable all maskable interrupts, enable RST 5.5
6. send interrupt mask for RST 6.5 & 7.5
7. Enable interrupt flag
8. Read data from port A, place contents in port B
26
FLOWCHART
Mode 0
Mode 1 & 2
START
START
Store control word in control register
Store control word in
control register
Input to be read from port A
Input to be read from port A
Disable all interrupts except RST
6.5
Store into accumulator
Store output to port B
Output written on port B
STOP
STOP
MODE 0
PROGRAM
COMMENTS
MOV AL,90H
OUT C6,AL
IN AL,C0
OUT C2,AL
HLT
Stop
MODE 1
PROGRAM
COMMENTS
MOV AL,0B0H
OUT C6,AL
MOV AL,09H
OUT C6,AL
MOV AL,13H
Interrupt generation
OUT 30,AL
MOV AL,0AH
Through 8259
OUT 32,AL
MOV AL,0FH
OUT 32,AL
MOV AL,00H
OUT 32,AL
STI
HLT
Stop
ISR:
Subroutine
IN AL,C0
OUT C2,AL
Send it to Port B
HLT
MODE 2
PROGRAM
Stop
MOV AL,0C0H
OUT C6,AL
MOV AL,09H
OUT C6,AL
MOV AL,13H
Interrupt generation
COMMENTS
OUT 30,AL
MOV AL,0AH
Through 8259
OUT 32,AL
MOV AL,0FH
OUT 32,AL
MOV AL,00H
OUT 32,AL
STI
HLT
Stop
ISR:
Subroutine
IN AL,C0
OUT C2,AL
Send it to Port B
HLT
Stop
BSR mode
Bit set/reset, applicable to PC only. One bit is S/R at a time. Control word:
D7
D6
D5
D4
D3
D2
D1
D0
0 (0=BSR)
B2
B1
B0
S/R (1=S,0=R)
28
Bit
B2
0
0
0
0
1
1
1
1
I/O mode
D7
D6
D5
D4
D3
D2
D1
D0
1 (1=I/O)
GA mode select
PA
PCU
GB mode select
PB
PCL
Mode 0
Input
Mode 1
Output
Mode 2
Output
Input
Input
Output
RESULT:
Thus the programs for interfacing 8255 with 8085 are executed & the output is
obtained for modes 0,1 & 2
29
EX.NO:
DATE :
AIM:
To write an ALP to generate a square wave of frequency --------, and also write an ALP
to transfer data serially from one kit to another.
Steps to determine the count:
Let the frequency of square wave to be generated be Fs KHz.
And the time period of the square wave be Ts Sec.
Oscillator Frequency = 11.0592MHz.
One machine cycle = 12 clock periods
Time taken to complete one machine cycle=12*(1/11.0592MHz)= 1.085microsec.
Y(dec) = (Ts/2)/(1.085microsec)
Count(dec) = 65536(dec) Y(dec)
= Count(hexa)
PROGRAM:
MOV TMOD,#10h
L1:
BACK:
SETB TR1
JNB TF1,BACK
CPL Px.x
CLR TR1
; stop timer
CLR TF1
SJMP L1
PROGRAM TO TRANSFER DATA SERIALLY:
Transmitter:
MOV TMOD,#20H
MOV TL1,#FDH
MOV TH1,#FFH
30
MOV SCON,#50H
SETB TR1
; Start timer1
MOV A,#06h
MOV SBUF,A
LOOP:
CLR TI
HERE:
SJMP HERE
Receiver:
MOV TMOD,#20H
MOV TL1,#FDH
MOV TH1,#FFH
MOV SCON,#50H
SETB TR1
LOOP:
JNB RI,LOOP
MOV A,SBUF
MOV DPTR,#4500H
MOVX @DPTR,A
CLR RI
HERE:
SJMP HERE
RESULT:
Thus the ALP for generation of a square wave of frequency --------, and also transfer of
data serially from one kit to another is also verified.
31
EX.NO.21
DATE :
KIT & PC
AIM:
To write an ALP to perform serial communication using 8051.
APPARATUS REQUIRED:
8051 Kit 2 numbers
Personal computer - 1
SERIAL COMMUNICATION
8051>H
HELP MENU
D
F10
Assembler
Disassembler
5. Test 8253
6. Exit
Select (1-6):
Suppose the user presses the key '1', following message is displayed if the internal data RAM
is OK.
Testing internal data RAM: Pass
After displaying the message, the menu is displayed once again waits for user to enter a key
EDITING MEMORY COMMAND:
8051>E
EDIT (R,B,M,P,D)D - EXTERNAL DATA RAM
Enter STA address = 0400
0400 =
0401 =
D5:66
0402 =
D3:77
0403 =
73:88
0404 =
6F:12
0405 =
CB:01
0406 =
0407 =
6F:03
0408 =
7B:04
0409 =
29:05
040A =
6F:06
040B =
73:07
040C =
FF:08
040D =
040E =
8051>E
EDIT (R,B,M,P,D)B - BITS
Enter STA address = 00
00 = 0:1
01= 0:1
02 = 0:0
03 = 0:1
03 = 1:
33
03 = 1:
02 = 0:
8051>E
EDIT (R,B,M,P,D)R- REGISTERS
ACC = 00:33
PSW = 00:44
DPH = 00:55
DPL = 00:00
DPL = 00:00
8051>E
EDIT (R,B,M,P,D)-P = PROGRAM CODE
8000 = FF:78
8001 = FF:10
8002 = FF:79
8003 = FF:20
8004 = FF:7A
8005 = FF: 12
8007 = FF : 00
8008 = FF : 03
8009 = FF : 0F
8051>E
EDIT (R,B,M,P,D)-M - INTERNAL RAM
0000 = 00 : 12
0001 = 00 : 34
0002 = 00 : 00
DISPLAY COMMAND
8051>D
EDIT (R,B,M,P,D)-EXTERNAL DATA RAM
Enter STA address = 0400
Enter END address = 040F
0500 55 66 77 88 12 01 02 03 04 05 06 07 08 09 04 D7
34
8051>C
BR_N0:1
RESULT:
Thus the assembly language program for serial communication was verified.
35
EX. NO.
8085 PROGRAMMING
SMALLEST AND LARGEST NUMBER
AIM:
To find the smallest and largest number in an array.
ALGORITHM:
1. Get the numbers.
2. Move the first value to the accumulator.
3. Compare the value in the accumulator with that in any register
4. Thus the smallest number and largest number are stored in desired locations.
FLOWCHART:
START
36
PROGRAM TABLE
ADDRESS
HEX
CODE
4100
90,45,00
4103
74,FF
4105
D3
4016
4018
410A
410B
82,EF
E5,D0
F0
A3
410C
72,AA
MOV B,M
410E
C2,D6
MOV M,A
4110
E5,D0
MOV A,B
4112
F0
4113
A3
LABEL
MNEMONICS
OPERAND
MVI C,09H
LXI H,4500H
MOV A,M
Next:
Skip:
INX H
CMP M
JC Skip / JNC Skip
JZ Skip
DCR C
JNZ Next
COMMENT
Move the value to the C
register
Move the address to the H
memory
Move the value from
memory to accumulator
Increment the H register
Compare the values
Jump on carry
Jump on zero
Move the value from
memory to the B register
Move the value from
memory to the accumulator
Move the value from
memory to the B register
Decrement the C value
Jump on non zero
4114
4116
D2,90
INX H
C2,99
MOV M,A
4122
80,FE
HLT
RESULT:
Thus the largest and smallest numbers are found using 8085.
37
Stop