Programming Using 8051
Programming Using 8051
OR
MOV R5,#55H
NEXT: ...
FIND THE SUM OF THE VALUES 79H, F5H,
E2H. PUT THE SUM IN REGISTERS R0 (LOW
BYTE) AND R5 (HIGH BYTE).
MOV A,#0 ;A=0
MOV R5,A ;clear R5
ADD A,#79H ;A=0+79H=79H
JNC N_1 ;if CY=0, add next number
INC R5 ;if CY=1, increment R5
N_1: ADD A,#0F5H ;A=79+F5=6E and CY=1
JNC N_2 ;jump if CY=0
INC R5 ;if CY=1,increment R5 (R5=1)
N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment R5
OVER: MOV R0, A ;now R0=50H, and R5=02
IO PORT PROGRAMMING
The following code will continuously send out to port 0 the alternating value 55H and
AAH
BACK: MOV A,#55H
MOV P0,A
ACALL DELAY
MOV A,#0AAH
MOV P0,A
ACALL DELAY
SJMP BACK
WRITE A PROGRAM TO PERFORM THE
FOLLOWING:
(a) Keep monitoring the P1.2 bit until it becomes high
(b) When P1.2 becomes high, write value 45H to port 0
(c) Send a high-to-low (H-to-L) pulse to P2.3
ORG 200H
MYDATA:DB “CET”
ORG 300H
XSQR_TABLE:
DB 0,1,4,9,16,25,36,49,64,81
END
EG.
Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it
means that the oven is hot. Monitor the bit continuously. Whenever it goes high, send a
high-to-low pulse to port P1.5 to turn on a buzzer.
SJMP HERE
OVER: MOV MYDATA,#‟1‟
SJMP HERE END
EG.
Assume that the on-chip ROM has a message. Write a program to copy it from code space into the upper memory
space starting at address 80H. Also, as you place a byte in upper RAM, give a copy to P0.
ORG 0
MOV DPTR,#MYDATA
MOV R1,#80H ;access the upper memory
B1: CLR A
MOVC A,@A+DPTR ;copy from code ROM
MOV @R1,A ;store in upper memory
MOV P0,A ;give a copy to P0
JZ EXIT ;exit if last byte
INC DPTR ;increment DPTR
INC R1 ;increment R1
SJMP B1 ;repeat until last byte
EXIT: SJMP EXIT ;stay here when finished
;---------------
ORG 300H
MYDATA: DB “Microcontroller 8051”, 0
END
EG.
Assume that RAM locations 40 – 44H have the following values. Write a program to find the sum of the
values. At the end of the program, register A should contain the low byte and R7 the high byte.
40 = (7D)
41 = (EB)
42 = (C5)
43 = (5B)
44 = (30)
MOV R0,#40H ;load pointer
MOV R2,#5 ;load counter
CLR A ;A=0
MOV R7,A ;clear R7→ hold the carry
AGAIN: ADD A,@R0 ;add the byte ptr to by R0
JNC NEXT ;if CY=0 don‟t add carry
INC R7 ;keep track of carry
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is zero
EG..
Write a program to add two 16-bit numbers. Place the sum in R7 and R6; R6 should have
the lower byte.
CLR C ;make CY=0
MOV A, #0E7H ;load the low byte now A=E7H
ADD A, #8DH ;add the low byte
MOV R6, A ;save the low byte sum in R6
MOV A, #3CH ;load the high byte
ADDC A, #3BH ;add with the carry
MOV R7, A ;save the high byte sum
EG..
Assume that 5 BCD data items are stored in RAM locations starting at 40H, as shown below. Write a program to find the sum of all the
numbers. The result must be in BCD.
40=(71)
41=(11)
42=(65)
43=(59)
44=(37)
Multiplication
MOV A,#25H ;load 25H to reg. A
MOV B,#65H ;load 65H to reg. B
MUL AB ;25H * 65H = E99 where B = OEH and A = 99H
If T = 75 then A = 75
If T < 75 then R1 = T If T > 75 then R2 = T