8051 CH3 950217
8051 CH3 950217
8051 CH3 950217
Objective
8051 control transfer
control transferconditional jump, unconditional jump, call subroutine. 8051 Jump 8051 Call 8051 Jump 8051 Call Loop
2
DO262Hz 3816ms RE294Hz 3401ms
DO 262, RE 294, ME 330, FA 349, SO 392, LA 440, SI 494, DO 522
3
Sections
3.1 Loop and jump instructions 3.2 Call instructions 3.3 Time delay for various 8051 chips
The unconditional jump is a jump in which control is transferred unconditionally to the target location. There are two unconditional jumps
LJMPLong Jump SJMPShort Jump Example 3-63-7
Activity
Actiontest condition
Not Jump condition is false
7
DJNZ1/2
Decrement and jump if not zero DJNZ Rn, target
MOV CLR HERE: INC DJNZ R2,#02H A A R2,HERE
MOV R2,#02H CLR A HERE
INC A
Jump if R20
Rn is one register of R0 - R7. Target is a label. A labelHERE is the ROM address of the following instructionINC A. Activity: twice for R22,1,0 A=2 INC, DJNZ: twice; jump once
Not Jump if R2 = 0
DJNZ2/2
Direct addressing mode DJNZ direct, target
MOV CLR HERE: INC DJNZ 30H,#02 ;X=the value in RAM 30H A ;A=0 A ;increase A by 1 30H,HERE;Decrement X and ;Jump to HERE if X0
Direct means directly access the RAM with address. See Appendix A-1page 534
Example 3-1
Write a program to (a) clear ACC, then (b) add 3 to the accumulator ten times. Solution: ;This program adds value 3 to the ACC ten times MOV MOV AGAIN: ADD DJNZ
MOV
;A=0, clear ACC ;load counter R2=10 ;add 03 to ACC ;repeat until R2=0(10 times) ;save A in R5
10
Example 3-2
What is the maximum number of times that the loop in Example 3-1 can be repeated? Solution:
Since R2 holds the count and R2 is an 8-bit register, the loop can be repeated a maximum of 256 times by setting R2=0. Thus, R20HFFH, FEH, ..., 2, 1, 0total 256 times of ADD DJNZ.
11
Nested Loop
A single loop is repeated 256 times in maximum. If we want to repeat an action more times than 256, we use a loop inside a loop. This is called nested loop. outer loop For Example:
The inner loop is 256 The outer loop is 2 Total 256*2=512 times
inner loop
activity
12
use
DJNZ R3 NEXT
13
JZ
Jump if A = zero JZ target
MOV A,R5 JZ NEXT MOV R5,#55H NEXT: ...
MOV A, R5 Jump if A0
NEXT
This instruction examines the content of the ACC and jumps if ACC has value 0.
15
JNZ
Jump if A is not zero JNZ target
MOV A,R5 JNZ NEXT MOV R5,#55H NEXT: ...
MOV A, R5
Jump if A0
NEXT
This instruction examines the contents of the ACC and jumps if ACC is not 0.
16
Example 3-4
Write a program to determine if R5 contains the value 0. If so, put 55H in it.
Solution:
MOV A,R5 JNZ NEXT MOV R5,#55H ...
Jump if A0
MOV A, R5
Test
Not Jump if A =0
NEXT:
17
JNC
Jump if no carryif CY=0 JNC target
MOV ADD JNC INC NEXT: ...
NEXT ADD A, #01H Jump if CY=0
Test
Not Jump if CY 0
INC R5
CY is PSW. This instruction examines the CY flag, and if it is zero it will jump to the target address.
18
Example 3-5
Find the sum of the values 79H, F5H, and E2H. Put the sum in registers R0 (low byte) and R5 (high byte). Solution: A CY MOV A,#0 ;clear A(A=0) MOV R5,A ;clear R5(R5=0) R5 R0 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(R5=0) JNC N_2 ;jump if CY=0 INC R5 ;if CY=1, increment R5 N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1(R5=1) JNC OVER ;jump if CY=0 INC R5 ;CY=1, increment 5 OVER:MOV R0,A ;Now R0=A=50H,and R5=02
19
(Abyte)
Compare reg. with #data and jump if
20
Long JumpLJMP
ROM address
A 3-byte instruction
The first byte is the opcode The next two bytes are the target address (real address)
0000
1120 1123
LJMP is used to jump to any address location within the 64K byte code space of the 8051.
Bits 23 16 15 opcode = 02
FFFF
Target=A010 opcode=02A010 8 7 0
target address
21
LJMP
Jump to a new address LJMP 16-bit-target-addr.
Line Addr. 17 0015 18 OO18 Opcode 020015 Mnemonic Operand HERE: LJMP HERE END
The opcode of LJMP is 02. When executing Line 17, jump to the target address 0015H. The 8051 Assembler can transfer the label HERE to the value 0015H.
22
Short JumpSJMP
A 2-byte instruction
The first byte is the opcode. The second byte is the relative address. The address is referred to as a relative address since the target address is relative to the PC. It is a signed number displacement
Bits 15 opcode = 80 8 7 0 relative address
ROM address 0000
Forward jump
Opcode 8030H
1120
SJMP Target
1122
CPL
FFFF
23
SJMP
Jump to a new address SJMP 8-bit-relative-address
Line Addr. 17 0015 18 OO17 Opcode 80FE Mnemonic Operand HERE: SJMP HERE END
Assembling The target label HERE has the value 0015H. Backward jump PC=0017H. Relative address = Target address-PC =0015H-0017H=FFFEH Running Target address = PC + Relative address =0017H+FFFEH (The CARRY is dropped) = 0015H
24
25
Relative Address
The target address must be within -128 to +127 bytes of the PC from the program counter.
Forward jump: 0 ~ 127 (0 ~ 7FH) Backward jump: -1 ~ -128 (FFH ~ 80H)
26
AJMP
Absolute jumpAJMP AJMP 11-bit-target-address
The target address must be within 2K bytes of program memory (from 0000H to 07FFH). The opcode of AJMP are 01H, 21H,,E1H (page 616)
Bits 15
13 12 target opcode
8 7 target address
00001
27
28
29
Example 3-7
Verify the calculation of backward jumps in Example 3-6. Solution: The target address < PC jump backward JNC AGAIN (50F2H) Assembling Opcode=50; the target address=AGAIN=0007H; PC=0015H The relative address the target addressPC00070015-14=FFF2H Running The target address 0015H + FFF2H = 0007H
Assembling SJMP HERE (80FEH) Opcode=80; the target address=HERE=0015H; PC=0017H The relative address the target addressPC00150017-2=FFFEH Running The target address=0017H+FFFEH=0015H
31
32
CALL
Another control transfer instruction is the CALL instruction, which is used to call a subroutine. Subroutines are often used to perform tasks that need to be performed frequently. This make a program more structured in addition to saving memory space. In the 8051 there are two instructions for call
LCALLlong callExamples 3-83-10 ACALLabsolute callExamples 3-113-12
33
34
35
Long CallLCALL
A 3-byte instruction
The first byte is the opcode. The next two bytes are the target address.
LCALL is used to jump to any address location within the 64K byte code space of the 8051.
Bits 23
16 15 opcode = 12
8 7 target address
36
LCALL
Jump to a new address LCALL 16-bit-target-addr.
Line Addr. 04 0004 05 0007 11 12 0300 0300 Opcode 120300 74AA
target address
return address
Mnemonic Operand LCALL DELAY MOV A,#0AAH ... ORG 300H MOV R5,#0FFH ... RET
subroutine DELAY
37
7DFF
DELAY:
15
0304 22 The opcode of LCALL is 12. The target address is 0300. The return address is 0007
ROM addr.
0000
ROM addr.
DELAY
0300 MOV R5,#0FFH; 0004 LCALL DELAY; 0007 MOV A,#0AAH; 0009 return address
0304
RET;
38
Example 3-8
Write a program to toggle all the bits of port 1 by sending to it the values 55H and AAH continuously. Put a time delay in between each issuing of data to port 1. This program will be used to test the ports of the 8051 in the next chapter. Solution:
ORG 0 BACK: MOV A,#55H ;load A with 55H MOV P1,A ;send 55H to port 1 LCALL DELAY ;time delay MOV A,#0AAH ;load A with AA (in hex) MOV P1,A ;send AAH to port 1 LCALL DELAY SJMP BACK ;keep doing this indefinitely ;this is the delay subroutine ORG 300H ;put time delay at address 300H DELAY:MOV R5,#OFFH ;R5=255(FF in hex), the counter AGAIN:DJNZ R5,AGAIN ;stay here until R5 becomes 0 RET ;return to caller (when R5=0) END ;end of asm file
39
delay subroutine 300H R5,#OFFH ;R5=255 R5,AGAIN ;stay here ;return to caller ;end of asm file
40
0A 09 08 00 07
SP = 09
41
42
43
0A
09 08 00 0B
PCH PCL
0A
09 08
99
00 0B
R4
PCH PCL
0A
09 08
99
00 0B
R4
PCH PCL
SP=09
SP=0A
SP=0B
44
After POP 4 0B 0A 09 08 00 0B
PCH PCL
After RET 0B 0A 09 08
SP=07
45
SP=0A
SP=09
Absolute CallACALL
a 2-byte instruction
The target address must be within 2K bytes of program memory. Using ACALL can save memory space than using LCALL. The opcode of ACALL are 11H, 31H,,F1H (page 616)
Bits 15
13 12 target opcode
8 7 target address
10001
46
ACALL
Jump to a new address ACALL 11-bit-target-address
Line PC 04 0004 05 0006 11 12 0300 0300 Opcode 7100 74 AA Mnemonic Operand ACALL DELAY MOV A,#0AAH ...
ORG 300H 7DFF DELAY: MOV R5,#0FFH ... 15 0304 22 RET The opcode of ACALL is 10001B (5 bits). The target address is 0300H=0000 0011 0000 0000B(11 bits). The machine code is 0111 0001 0000 0000 B=7100H
47
Example 3-11
A developer is using the Atmel AT89C1051 microcontroller chip for a product. This chip has only 1K bytes of on-chip flash ROM. Which of the instructions LCALL and ACALL is most useful in programming this chip?
Solution:
The ACALL instruction is more useful since it is a 2-byte instruction. It saves one byte each time the call instruction is used.
48
Example 3-12
Rewrite Example 3-8 as efficiently as you can. Solution: ORG 0 MOV A,#55H ;A=01010101B=55H BACK: MOV P1,A ;put reg A to port 1 ACALL DELAY ;time delay CPL A ;A=10101010B=0AAH SJMP BACK ;indefinitely loop ;this is the delay subroutine DELAY: MOV R5,#OFFH ;R5=255, the counter AGAIN: DJNZ R5,AGAIN ;jump if R5 becomes 0 RET ;return to caller END ;end of asm file
49
50
Time Delay
We have written a delay subroutine in Ex3-8. How to calculate exact delays How to generate various time delay
51
Machine Cycle1/2
For the CPU to execute an instruction takes a certain number of clock cycles. In the 8051 family, these clock cycles are referred to as machine cycles.
ExRET needs 2 machine cycles
The 8051 has an on-chip oscillator which generates machine cycles. The 8051 requires an external clocka quartz crystal oscillatorto run the on-chip oscillator.
52
Machine Cycle2/2
The relationship between two oscillators
The length of the machine cycle is 12 of the oscillator period. The frequency of the machine cycle is 1/12 of the crystal frequency.
The frequency of the external crystal can be vary from 4 MHz to 30MHz. Very often the 11.0592MHz crystal oscillator is used to make the 8051-based system compatible with the serial port of the IBM PCSee Chapter 10.
53
machine cycle
30pF
GND external oscillator
machine cycle
54
Example 3-13
The following shows crystal frequency for three different 8051based systems. Find the period of the machine cycle in each case. (a) 11.0592 MHz (b) 16 MHz (c) 20 MHz Solution: (a) 11.0592MHz/12 = 921.6 KHz machine cycle is 1/921.6 KHz = 1.085 ms (microsecond) (b) oscillator period = 1/16 MHz = 0.0625 ms machine cycle (MC) = 0.0625 ms 12 = 0.75 ms (c) 20 MHz/12 = 1.66 MHz MC = 1/1.66 MHz = 0.60 ms
55
Example 3-14
For an 8051 system of 11.0592 MHz, find how long it takes to execute each of the following instructions. (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2, target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Solution: The machine cycle for a system of 11.0952 MHz is 1.085 ms. Table A-1 shows machine cycles for each instructions. Instruction Machine cycles Time to execute (a) MOV R3,#55 1 11.085 ms = 1.085 ms (b) DEC R3 1 11.085 ms = 1.085 ms (c) DJNZ R2,target 2 21.085 ms = 2.17 ms (d) LJMP 2 21.085 ms = 2.17 ms (e) SJMP 2 21.085 ms = 2.17 ms (f) NOP 1 11.085 ms = 1.085 ms (g) MUL AB 4 41.085 ms = 4.34 ms
56
Example 3-15
Find the size of the delay in the following program, if the crystal frequency is 11.0592 MHz. MOV A,#55H AGAIN: MOV P1,A ACALL DELAY CPL A SJMP AGAIN ;Time delay Machine Cycle DELAY: MOV R3,#200 1 HERE: DJNZ R3,HERE 2 RET 2 Solution: Table A-1 The time delay is [(200 2)+1+2] 1.085 ms = 437.255 ms.
57
Delay Calculation
Two way to get a large delay is
to use NOPExample 3-16 to use a loop inside a loopnested loopExample 3-17
Very often we calculate the time delay based on the instructions inside the loop and ignore the clock cycles associated with the instructions outside the loop.
58
Example 3-16
Find the time delay for the following subroutine, assuming a crystal frequency of 11.0592 MHz. Machine Cycle DELAY: MOV R3,#250 1 HERE: NOP NOP NOP NOP DJNZ R3,HERE RET 1 1 1 1 2 2
Solution: the HERE loop the two instructions outside the loop { [250 (1+1+1+1+2)] + 3 } 1.085 ms = (1500+3) 1.085 ms = 1630.755 ms.
59
Example 3-17
For a machine cycle of 1.085 ms, find the time delay in the following subroutine. DELAY: Machine Cycle MOV R2,#200 1 AGAIN: MOV R3,#250 1 HERE: NOP 1 NOP 1 DJNZ R3,HERE 2 DJNZ R2,AGAIN 2 RET 2 Solution: the HERE loop = 250 (1+1+2)=1000 MCs the AGAIN loop = 200(1000+1+2) =200600 MCs = 200600 1.085 ms = 217651 ms the whole program = 200603 MCs = 217654.255 ms
60
Advances in both IC technology and CPU design The number of clock periods per machine cycle varies among the different versions of the 8051 ICs.
Table 3-2
Chip/Maker AT89C51 Atmel P89C54X2 Philips DS5000 Dallas Semi DS89C420/30/40/50 Dallas Semi Clocks per Machine Cycle 12 6 4 1
61
Example 3-18
From Table 3-2, find the period of the machine cycle (MC) in each case if XTAL=11.0592 MHz, and discuss the impact on performance. (a) AT89C51 (b) P89C54X2 (c) DS5000 (d) DS89C4x0 Solution: (a) 11.0592MHz/12 = 921.6 KHz MC is 1/921.6 KHz = 1.085 ms (microsecond) = 1085 ns (b) 11.0592MHz/6 =1.8432 MHz, MC is 1/1.8432MHz = 542 ns (c) 11.0592MHz/4 =2.7648 MHz, MC is 1/2.7648MHz = 360 ns (d) 11.0592MHz/1 =11.0592 MHz, MC is 1/11.0592MHz =90 ns Approximately 9 to 10 times performance boost for the DS89C4x0 over AT89C51. Why not 12 times?
62
DS89C4x0
2 1
DJNZ
LJMP SJMP NOP MUL AB
2
2 2 1 4
4
3 3 1 9
63
Example 3-19
For an AT8051 and DS89C4x0 system of 11.0592 MHz, find how long it takes to execute each of the following instructions. (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2, target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Solution: The MC for a system of 11.0952 MHz is shown in Example 3-18. Table 3-3 shows machine cycles for each instructions. Instruction (a) MOV R3,#55 (b) DEC R3 (c) DJNZ (d) LJMP (e) SJMP (f) NOP (g) MUL AB AT89C51 11085ns=1085ns 11085ns=1085ns 21085ns=2170ns 21085ns=2170ns 21085ns=2170ns 11085ns=1085ns 41085ns=4340ns DS89C4x0 290ns=180ns 190ns= 90ns 490ns=360ns 390ns=270ns 390ns=270ns 190ns= 90ns 990ns=810ns
64
Example 3-20
Find the time delay for the following subroutine if it run on a DS89C420 chip, assuming a crystal frequency of 11.0592 MHz. DS89C420 Machine Cycle DELAY: MOV R3,#250 HERE: NOP NOP NOP NOP DJNZ R3,HERE RET 1 1 1 1 4
Solution: The time delay inside the HERE loop is [250 (1+1+1+1+4)] 90 ns = 2000 90 ns = 180 ms Compare AT89C51 with DS89C420: 1627 ms/180 ms = 9
65
66
67
Example 3-22(1/2)
Write a program to toggle all the bits of P1 every 200ms for DS89C4x0. Assume that the crystal frequency is 11.0592 MHz. Solution: MOV A,#55H AGAIN: MOV P1,A ACALL DELAY CPL A SJMP AGAIN DELAY: MOV R5,#9 2 HERE1: MOV R4,#242 2 HERE2: MOV R3,#255 2 HERE3: DJNZ R3,HERE3 4 DJNZ R4,HERE2 4 DJNZ R5,HERE1 4 RET
68
69
Timers
The use of the instruction in generating time delay is not the most reliable method.
For example, interrupt!
To get more accurate time delay we use timer as described in Chapter 9. To get an accurate time delay for a given 8051 microcontroller, we must use an oscilloscope to measure the exact time delay.
70
72