Microcontroller (1) Lab Manual: Prepared By: Eng: Mohsen Ali AL-awami Supervisered By: DR: 2010-2011
Microcontroller (1) Lab Manual: Prepared By: Eng: Mohsen Ali AL-awami Supervisered By: DR: 2010-2011
BIOMEDICAL DEPARTMENT
Prepared By:
Supervisered By:
2010-2011
1
LAB Expeirment (2)
Main Topics:
Jump ,Loop and Call instructions
Assembely Arthimatics and Logic Operations
Learning Objectives/Tasks:
Upon Completion this experiment ,you will be able to :
The loop is one of the most widely used actions is performed by the instraction
2
Example 1:
Write a program to
Solution:
Example 2:
What is maximum number of times that the loop in last example can be repeated?
Solution:
Since the holds the count and R2 ia an 8-bites register, it can be hold maximum of FFH
Example 3:
Write a program to
Solution:
Other Conditional Jumps
Conditional jumps for the 8051 are summerized in the next table:
JC (jump if carry =1 )
instraction Action
JZ Jump if a=0
JNZ Jump if a not= 0
DJNZ Decrement and Jump if a not=0
CJNE A,BYTE Jump if a not= byte
CJNE Jump if a not= #data
REG,#DATA
JC Jump if carry=1
JNC Jump if carry=0
JB Jump if bit=1
4
JNB Jump if bit=0
JBC Jump if bit=1 and clear bit
Example 4:
Write a program to determine if R5 contains the value 0 .if so, put 55H in it.
Solution:
Mov R5,#55H
NEXT: …………………………
It need to be noted that there is also ‘’JC lable ’’ instruction .in the jc
instruction, if cy=1 it jumps to the target address.
Example 5:
Find the sum of the values 79H,F5H and E2H.put the sum in the registers R0(low byte)and R5(high
byte).
5
Solution:
Call instructions
6
Call instruction is used to call subroutine
Subroutines are often used to perform tasks
that need to be performed frequently
This makes a program more structured in
addition to saving memory space
LCALL (long call)
o 3-byte instruction
First byte is the opcode
Second and third bytes are used for address of target
subroutine
– Subroutine is located anywhere within 64K byte address space
ACALL (absolute call)
o 2-byte instruction
11 bits are used for address within 2K-byte range
Example 6:
ORG 0
BACK: MOV A,#55H ;load A with 55H
MOV P1,A ;send 55H to port 1 7
LCALL DELAY ;time delay
MOV A,#0AAH ;load A with AA (in hex)
MOV P1,A ;send AAH to port 1
001 0000 ORG 0
002 0000 7455 BACK: MOV A,#55H ;load A with 55H
003 0002 F590 MOV P1,A ;send 55H to p1
004 0004 120300 LCALL DELAY ;time delay
005 0007 74AA MOV A,#0AAH ;load A with AAH
006 0009 F590 MOV P1,A ;send AAH to p1
007 000B 120300 LCALL DELAY
008 000E 80F0 SJMP BACK ;keep doing this
009 0010
010 0010 ;-------this is the delay subroutine------
011 0300 ORG 300H
012 0300 DELAY:
013 0300 7DFF MOV R5,#0FFH ;R5=255
014 0302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here
015 0304 22 RET ;return to caller
016 0305 END ;end of asm file
8
Stack fram after the first LCALL
08
0A Low byte goes first then high byte
09 00
08 07
SP (stack pointer) = 09
Write a program to add two 16-bit numbers. Place the sum in R7 and
R6; R6 should have the lower byte.
Solution:
10
CLR C ;make CY=0
MOV A, #0E7H ;load the low byte now A=E7H
ADD A, #8DH ;add the low byte
o The binary representation of the digits 0 to 9 is called BCD (Binary
Coded Decimal)
Unpacked BCD
In unpacked BCD, the lower 4 bits of the number represent
the BCD number, and the rest of the bits are 0 .
Ex. 00001001 and 00000101 are unpacked BCD for 9 and 5.
Packed BCD
In packed BCD, a single byte has two BCD number in it, one in the lower
4 bits, and one in the upper 4 bits .
Ex. 0101 1001 is packed BCD for 59H.
o Adding two BCD numbers must give a BCD result.
Example 2:
MOV A, #17H
ADD A, #28H
The result above should have been 17 + 28 = 45 (0100 0101).
To correct this problem, the programmer must add 6 (0110) to the
low digit: 3F + 06 = 45H.
Example 3 :
11
MOV A,#47H ;A=47H first BCD operand
MOV B,#25H ;B=25H second BCD operand
ADD A,B ;hex(binary) addition(A=6CH)
DA A ;adjust for BCD addition
(A=72H)
The “DA” instruction works only on A. In other word, while the source
can be an operand of any addressing mode, the destination must be in
register A in order for DA to work.
Example 4:
CLR C
MOV A,#4C ;load A with value 4CH
SUBB A,#6EH ;subtract 6E from A 12
JNC NEXT ;if CY=0 jump to NEXT
CPL A ;if CY=1, take 1’s complement
INC A ;and increment to get 2’s comp
SUBB when CY = 1
This instruction is used for multi-byte numbers and
will take care of the borrow of the lower operand .
Example 5:
CLR C
MOV A,#62H ;A=62H
SUBB A,#96H ;62H-96H=CCH with CY=1
MOV R7,A ;save the result
MOV A,#27H ;A=27H
SUBB A,#12H ;27H-12H-1=14H
MOV R6,A ;save the result
Solution:
We have 2762H - 1296H = 14CCH.
D7 D6 D5 D4 D3 D2 D1 D0
-- --
Sign Magnitude
(Overflow Problem)
(2's Complement)
Example 1:
Example 2:
04H 0 0 0 0 0 1 0 0
68H 0 1 1 0 1 0 0 0
=----- --------- ---------
6CH 0 1 1 0 1 1 0 0
(3- XOR)
XRL destination, source ;dest = dest XOR
source
This instruction will perform XOR operation on the two operands and
place the result in the destination
16
The destination is normally the accumulator
The source operand can be a register, in memory, or immediate .
Example 3:
54H 0 1 0 1 0 1 0 0
78H 0 1 1 1 1 0 0 0
= ------ -------- ----------
2CH 0 0 1 0 1 1 0 0
4- Compare Instruction
Example 4:
17
destination ≥ source CY = 0
destination < source CY = 1
RR A ;rotate right A
In rotate right
The 8 bits of the accumulator are rotated right one bit, and
Bit D0 exits from the LSB and enters into MSB, D7
MSB LSB
MOV A,#36H ;A = 0011 0110
RR A ;A = 0001 1011
RR A ;A = 1000 1101
RR A ;A = 1100 0110
RR A ;A = 0110 0011
t’)
RL A ;rotate left A
In rotate left
The 8 bits of the accumulator are rotate left one bit, and
Bit D7 exits from the MSB and enters into LSB, D0
MSB LSB
18
Rotating through Carry
CY
MSB LSB
CLR C ;make CY = 0
MOV A,#26H ;A = 0010 0110
RRC A ;A = 0001 0011 CY = 0
RRC A ;A = 0000 1001 CY = 1
RRC A ;A = 1000 0100 CY = 1
RLC A ;rotate left through carry
In RLC A
Bits are shifted from right to left.
They exit the MSB and enter the carry flag,
and the carry flag enters the LSB.
CY MSB LSB