0% found this document useful (0 votes)
69 views

Programming Using 8051

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Programming Using 8051

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

PROGRAMMING USING 8051 MPMC

DATA TRANSFER EXAMPLES

MOV R0, #99H ;load R0 with 99H


MOV R1, #85H ;load R1 with 85H

MOV 00, #99H ;RAM location 00H has 99H


MOV 01, #85H ;RAM location 01H has 85H

SETB PSW.4 ;select bank 2


ADD 2 8-BIT NUMBERS
MOV A, #33H ;load 33H into A
MOV R2, #22H ;load 22H into R2
ADD A, R2 ;add R2 to Accumulator (A = A + R2)

OR

MOV A, #33H ;load one operand ;into A (A=25H)


ADD A, #22H ;add the second operand 22H to A
ADD THE VALUE 3 TO THE ACC TEN TIMES
MOV A,#0 ;A=0, clear ACC
MOV R2,#10 ;load counter R2=10
AGAIN: ADD A,#03 ;add 03 to ACC
DJNZ R2,AGAIN ;repeat until R2=0,10 times
MOV R5,A ;save A in R5
WRITE A PROGRAM TO
(A) LOAD THE ACCUMULATOR WITH THE VALUE 55H, AND
(B) COMPLEMENT THE ACC 700 TIMES

MOV A,#55H ;A=55H


MOV R3,#10 ;R3=10, outer loop count
NEXT: MOV R2,#70 ;R2=70, inner loop count
AGAIN: CPL A ;complement A register
DJNZ R2,AGAIN ;repeat it 70 times
DJNZ R3,NEXT
DETERMINE IF R5 CONTAINS THE VALUE 0.
IF SO, PUT 55H IN IT.

MOV A,R5 ;copy R5 to A


JNZ NEXT ;jump if A is not zero

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

SETB P1.2 ;make P1.2 an input


MOV A,#45H ;A=45H
AGAIN: JNB P1.2,AGAIN ; get out when P1.2=1
MOV P0,A ;issue A to P0
SETB P2.3 ;make P2.3 high
CLR P2.3 ;make P2.3 low for H-to-L
EXAMPLE
Create a square wave of 50% duty cycle on bit 0 of port 1. The 50% duty cycle means that the “on”
and “off ” state (or the high and low portion of the pulse) have the same length. Therefore, we toggle
P1.0 with a time delay in between each state.

HERE: SETB P1.0 ;set to high bit 0 of port 1


LCALL DELAY ;call the delay subroutine
CLR P1.0 ;P1.0=0
LCALL DELAY
SJMP HERE ;keep doing it

Another way to write the above program is:


HERE: CPL P1.0 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
SJMP HERE ;keep doing it
Port 0 is configured first as an input port by writing 1s to it, and then data is received from
that port and sent to P1
MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 an i/p port by writing it all 1s
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it
Port 1 is configured first as an input port by writing 1s to it, then data is received from that
port and saved in R7 and R5
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 an input port by writing it all 1s
MOV A,P1 ;get data from P1
MOV R7,A ;save it to in reg R7
ACALL DELAY ;wait
MOV A,P1 ;another data from P1
MOV R5,A ;save it to in reg R5
EXAMPLE
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.
Solution:
HERE: JNB P2.3,HERE ;keep monitoring for high
SETB P1.5 ;set bit P1.5=1
CLR P1.5 ;make high-to-low
SJMP HERE ;keep repeating
EXAMPLE
A switch is connected to pin P1.7. Write a program to check the status of SW and perform
the following:(a) If SW=0, send letter “N” to P2 (b) If SW=1, send letter “Y” to P2
SETB P1.7 ;make P1.7 an input
AGAIN: JB P1.2,OVER ;jump if P1.7=1
MOV P2,#‟N‟ ;SW=0, issue „N‟ to P2
SJMP AGAIN ;keep monitoring
OVER: MOV P2,#“Y” ;SW=1, issue “Y” to P2
SJMP AGAIN ;keep monitoring
EXAMPLE
A switch is connected to pin P1.7. Write a program to check the status of SW and perform the
following: (a) If SW=0, send letter “N‟ to P2 (b) If SW=1, send letter „Y‟ to P2. Use the carry
flag to check the switch status.
;make P1.7 an input
AGAIN: MOV C,P1.7 ;read SW status into CF
JC OVER ;jump if SW=1
MOV P2,#‟N‟ ;SW=0, issue “N” to P2
SJMP AGAIN ;keep monitoring
OVER: MOV P2,#‟Y‟ ;SW=1, issue “Y‟ to P2
SJMP AGAIN ;keep monitoring
EG..
A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status
of the switch and send it to the LED
SETB P1.7 ;make P1.7 an input
AGAIN: MOV C,P1.7 ;read SW status into CF send SW status to LED
MOV P2.7,C
SJMP AGAIN ;keep repeating
EG..
Write code to send 55H to ports P1 and P2, using (a) their names (b) their addresses
(a) MOV A,#55H ;A=55H
MOV P1,A ;P1=55H
MOV P2,A ;P2=55H

(b) Given address=80H; P2 address=A0H


MOV A,#55H ;A=55H
MOV 80H,A ;P1=55H
MOV 0A0H,A ;P2=55H
EG..
Show the code to push R5 and A onto the stack and then pop them back them into R2 and
B, where B = A and R2 = R5
PUSH 05 ;push R5 onto stack
PUSH 0E0H ;push register A onto stack
POP 0F0H ;pop top of stack into B now register B = register A
POP 02 ;pop top of stack into R2 now R2=R6
EG..
Write a program to copy the value 55H into RAM memory locations 40H to 41H using (a) direct
addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop
(a) MOV A,#55H ;load A with value 55H
MOV 40H,A ;copy A to RAM location 40H
MOV 41H, A ;copy A to RAM location 41H (b)
MOV A,#55H ;load A with value 55H

MOV R0,#40H ;load the pointer. R0=40H


MOV @R0, A ;copy A to RAM R0 points to 40H
INC R0 ;increment pointer. Now R0=41h
MOV @R0,A ;copy A to RAM R0 points to (c)

MOV A,#55H ;A=55H


MOV R0,#40H ;load pointer.R0=40H,
MOV R2,#02 ;load counter, R2=3
AGAIN: MOV @R0,A ;copy 55 to RAM R0 points to 40h
INC R0 ;increment R0 pointer
DJNZ R2,AGAIN ;loop until counter = zero
EG.
Write a program to clear 16 RAM locations starting at RAM address 60H
CLR A ;A=0
MOV R1,#60H ;load pointer. R1=60H
MOV R7,#16 ;load counter, R7=16
AGAIN: MOV @R1,A ;clear RAM R1 points to 60h
INC R1 ;increment R1 pointer
DJNZ R7,AGAIN ;loop until counter=zero
---
EG..
Write a program to copy a block of 10 bytes of data from 35H to 60H
MOV R0,#35H ;source pointer
MOV R1,#60H ;destination pointer
MOV R3,#10 ;counter
BACK: MOV A,@R0 ;get a byte from source
MOV @R1,A ;copy it to destination
INC R0 ;increment source pointer
INC R1 ;increment destination pointer
DJNZ R3,BACK ;keep doing for ten bytes
IN THIS PROGRAM, ASSUME THAT THE WORD “CET” IS BURNED INTO ROM
LOCATIONS STARTING AT 200H. AND THAT THE PROGRAM IS BURNED INTO ROM
LOCATIONS STARTING AT 0. ANALYZE HOW THE PROGRAM WORKS AND STATE
WHERE “CET” IS STORED AFTER THIS PROGRAM IS RUN.
ORG 0000H ;burn into ROM starting at 0

MOV DPTR,#200H ;DPTR=200H look-up table addr

CLR A ;clear A(A=0)

MOVC A,@A+DPTR ;get the char from code space

MOV R0,A ;save it in R0

INC DPTR ;DPTR=201 point to next char

CLR A ;clear A(A=0)

MOVC A,@A+DPTR ;get the next char

MOV R1,A ;save it in R1

INC DPTR ;DPTR=202 point to next char

CLR A ;clear A(A=0)

MOVC A,@A+DPTR ;get the next char

MOV R2,A ;save it in R2

Here: SJMP HERE ;stay here

;Data is burned into code space starting at 200H

ORG 200H

MYDATA:DB “CET”

END ;end of program


WRITE A PROGRAM TO GET THE X VALUE
FROM
ORG 0
P1 AND SEND X2 TO P2, CONTINUOUSLY
MOV DPTR,#300H ;LOAD TABLE ADDRESS
MOV A,#0FFH ;A=FF
MOV P1,A ;CONFIGURE P1 INPUT PORT
BACK:MOV A,P1 ;GET X
MOV A,@A+DPTR ;GET X SQAURE FROM TABLE
MOV P2,A ;ISSUE IT TO P2
SJMP BACK ;KEEP DOING IT

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.

OVEN_HOT BIT P2.3


BUZZER BIT P1.5
HERE: JNB OVEN_HOT,HERE ;keep monitoring
ACALL DELAY
CPL BUZZER ;sound the buzzer
ACALL DELAY
SJMP HERE
EG.
A switch is connected to pin P1.7 and an LED to pin P2.0. Write a program to get the
status of the switch and send it to the LED.
SW BIT P1.7 ;assign bit
LED BIT P2.0 ;assign bit
HERE: MOV C,SW ;get the bit from the port
MOV LED,C ;send the bit to the port
SJMP HERE ;repeat forever
EG.
A switch is connected to pin P1.7. Write a program to check the status of the switch and make the
following decision. (a) If SW = 0, send “0” to P2(b) If SW = 1, send “1“ to P2
SW EQU P1.7
MYDATA EQU P2

HERE: MOV C,SW


JC OVER
MOV MYDATA,#‟0‟

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)

MOV R0,#40H ;Load pointer


MOV R2,#5 ;Load counter CLR A ;A=0
MOV R7,A ;Clear R7
AGAIN: ADD A,@R0 ;add the byte pointer to by R0
DA A ;adjust for BCD
JNC NEXT ;if CY=0 don’t accumulate carry
INC R7 ;keep track of carries
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is 0
EG..
Show how the 8051 would represent -34H
1. 0011 0100 34H given in binary
2. 1100 1011 invert each bit
3. 1100 1100 add 1 (which is CC in hex)
Signed number representation of -34 in 2‟s complement is CCH

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

Replace MUL by DIV to obtain A/B


EG..
1‟s Complement
MOV A, #55H 1 1.085usec
CPL A ; 1 now A=AAH →0101 0101(55H) becomes 1010 1010(AAH)
EG..
Write a program to read the temperature and test it for the value 75. According to the test results,
place the temperature value into the registers indicated by the following.

If T = 75 then A = 75
If T < 75 then R1 = T If T > 75 then R2 = T

MOV P1,#0FFH ;make P1 an input port


MOV A,P1 ;read P1 port
CJNE A,#75,OVER ;jump if A is not 75
SJMP EXIT ;A=75, exit
OVER: JNC NEXT ;if CY=0 then A>75
MOV R1,A ;CY=1, A<75, save in R1
SJMP EXIT ; and exit
NEXT: MOV R2,A ;A>75, save it in R2
EXIT: ...
EG..
Assume that bit P2.2 is used to control an outdoor light and bit P2.5 a light inside a
building. Show how to turn on the outside light and turn off the inside one.
SETB C ;CY = 1
ORL C,P2.2 ;CY = P2.2 ORed w/ CY
MOV P2.2,C ;turn it on if not on
CLR C ;CY = 0
ANL C,P2.5 ;CY = P2.5 ANDed w/ CY
MOV P2.5,C ;turn it off if not off
EG..
Write a program that finds the number of 1s in a given byte.

MOV R1,#0 ;R1 keeps number of 1s


MOV R7,#8 ;counter, rotate 8 times
MOV A,#97H ;find number of 1s in 97H
AGAIN: RLC A ;rotate it thru CY
JNC NEXT ;check CY
INC R1 ;if CY=1, inc count
NEXT: DJNZ R7,AGAIN ;go thru 8 times
EG..
Assume that register A has packed BCD, write a program to convert packed BCD to two ASCII
numbers and place them in R2 and R6.
MOV A,#29H ;A=29H, packed BCD
MOV R2,A ;keep a copy of BCD data
ANL A,#0FH ;mask the upper nibble (A=09)
ORL A,#30H ;make it an ASCII, A=39H(„9‟)
MOV R6,A ;save it
MOV A,R2 ;A=29H, get the original data
ANL A,#0F0H ;mask the lower nibble
RR A ;rotate right RR A ;rotate right
RR A ;rotate right RR A ;rotate right
ORL A,#30H ;A=32H, ASCII char. ‟2‟
MOV R2,A ;save ASCII char in R2
END OF LECTURE

You might also like