8051 CH3 950217

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 72

Chapter 3 Jump, Loop, and Call Instructions

Objective
8051 control transfer
control transferconditional jump, unconditional jump, call subroutine. 8051 Jump 8051 Call 8051 Jump 8051 Call Loop
2

Application of Jump & Call



ON/OFF


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

Section 3.1 Loop and Jump Instructions

Conditional/Unconditional Jump Instructions


The conditional jump is a jump in which control is transferred to the target location if it meets the required condition.
DJNZ, JZ, JNC... Example 3-13-5

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

Looping in the 8051


Repeating a sequence of instructions a certain number of times is called a loop.
Activity works several times. It need some control transfer instructions. 8051 jump instructions
Do Activity First if exists Test the condition later Initialization
Label

Activity

DJNZ, JZ, JNC Example 3-13-5

Jump condition is true

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

DEC R2, Test

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 R2,#10 A,#03 R2,AGAIN


R5,A

;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

Example 3-3 (1/2)


Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC 700 times. MOV R3,#10 Solution: NEXT The following code shows how to R2 and R3 for the count. MOV R2,#70 70010 70 AGAIN
Inner loop: R2=70 Outer loop: R3=10
DJNZ R2 AGAIN

use

DJNZ R3 NEXT
13

Example 3-3 (2/2)


MOV MOV NEXT: MOV AGAIN:CPL DJNZ DJNZ A,#55H ;A=55H R3,#10 ;R3=10,the outer loop count R2,#70 ;R2=70,the inner loop count A ;complement A register R2,AGAIN;repeat 70 times(inner loop) R3,NEXT MOV R3,#10
NEXT
MOV R2,#70 AGAIN DJNZ R2 AGAIN DJNZ R3 NEXT
14

JZ
Jump if A = zero JZ target
MOV A,R5 JZ NEXT MOV R5,#55H NEXT: ...
MOV A, R5 Jump if A0

Test Not Jump if A 0 MOV R5,#55H

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

Test Not Jump if A =0 MOV R5,#55H

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:

MOV R5,#55H NEXT

17

JNC
Jump if no carryif CY=0 JNC target
MOV ADD JNC INC NEXT: ...
NEXT ADD A, #01H Jump if CY=0

A,#0FFH A,#01H NEXT R5

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

Table 3-1: 8051 Conditional Jump Instructions


Instruction JZJump Zero JNZJump no zero DJNZ Rn,target CJNE A,byte,target CJNE reg,#data,target JCJump carry JNCJump no carry JBJump bit JNBJump no bit Action Jump if A=0 Jump if A0 Decrement and jump if byte0 Compare A with byte and jump if not equal

(Abyte)
Compare reg. with #data and jump if

not equal (byte #data)


Jump if CY=1 Jump if CY=0 Jump if bit=1 Jump if bit=0

JBCjump bit clear bit

Jump if bit=1 and clear bit

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 Target CPL A

LJMP is used to jump to any address location within the 64K byte code space of the 8051.
Bits 23 16 15 opcode = 02

A010 Target: MOV R0,A

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

1152 Target: MOV R0,A

FFFF

Assembling: PC=1122, target=1152 relative = target-PC=1152-1122=30H Running: PC=1122, relative=30 target=PC+relative=1122+30=1152H

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

SJMP to Itself Using $ Sign


Sometimes, there is nothing to do but we want to keep the microcontroller busy. You can use
Line PC 17 0015 18 OO17 SJMP $ Opcode 80FE Mnemonic Operand HERE: SJMP HERE END

We can use the following:

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)

Real target address = PC + relative address


Ex1: PC=1001H, relative address=40H target address=1001H+40H=1041H Ex2: PC=1001H, relative address=FFFEH (-210) target address=1001H+FFFEH=0FFFH

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

Other Conditional Jumps


The usage of these instructions
See Appendix A.1, Table A-1page 523, Tables 10 11 in Appendix Hpage 612616

All conditional jumps are short jumps.


They have a 1-byte relative address. The 8051 Assembler changes the target label into the relative offset to PC and save the offset in the instructions. The target address cannot be more than -128 to +127 bytes away from the program counter.

28

Example 3-6 (1)


Using the following list file, verify the jump forward address calculation. Line PC Opcode Mnemonic Operand 01 0000 ORG OOOO 02 0000 7800 MOV R0,#0 03 0002 7455 MOV A,#55H 04 0004 6003 JZ NEXT 05 0006 08 INC R0 06 0007 04 AGAIN: INC A 07 0008 04 INC A 08 0009 2477 NEXT: ADD A,#77H 09 000B 5005 JNC OVER 10 000D E4 CLR A 11 000E F8 MOV R0,A 12 OOOF F9 MOV R1,A 13 OO1O FA MOV R2,A 14 OO11 FB MOV R3,A 15 OO12 2B OVER: ADD A,R3 16 OO13 50F2 JNC AGAIN 17 0015 80FE HERE: SJMP HERE 18 OO17 END

29

Example 3-6 (2)


Solution: The target address > PC jump forward
JZ NEXT (6003H) Assembling Opcode=60; the target address=NEXT=0009H; PC=0006H The relative address the target addressPC000900060003 Running The target address=0006H+0003H=0009H JNC OVER (5005H) Assembling Opcode=50; the target address=OVER=0012H; PC=000DH The relative address the target addressPC0012000D0005 Running The target address=000DH+0005H=0012H
30

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

Section 3.2 Call Instructions

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

Figure 3-1. 8051 Assembly Main Program That Calls Subroutines


;MAIN program calling subroutines ORG 0 MAIN: LCALL SUBR_1 LCALL SUBR_2 LCALL SUBR_3 HERE: SJMP HERE ;end of MAIN SUBR_1: .... .... RET ;end of subroutine 1 SUBR_2: .... .... RET ;end of subroutine 2 SUBR_3: .... .... RET ; end of subroutine 3 END ;end of the asm file

34

The Flow of Control Involving a Procedure

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

LCALL and Memory Addresses

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

Example 3-9 (1/2)


Analyze the stack contents after the execution of the first LCALL in the following. Solution: (a)
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 0000 0000 0002 0004 0007 0009 000B 000E 0010 0010 0300 O300 O300 O302 O304 O305 ORG 0 7455 BACK: MOV A,#55H F590 MOV P1,A 120300 LCALL DELAY 74AA MOV A,#0AAH F590 MOV P1,A 120300 LCALL DELAY 80F0 SJMP BACK ;this is the ORG DELAY: 7DFF MOV DDFE AGAIN: DJNZ 22 RET END ;load ;send ;time ;load ;send A with 55H to delay A with AAH to 55H port1 AAH port1

;keep doing this

delay subroutine 300H R5,#OFFH ;R5=255 R5,AGAIN ;stay here ;return to caller ;end of asm file

40

Example 3-9 (2/2)


Solution: (b) When the first LCALL is executed, the address of the instruction MOV A,#0AAH is saved on the stack. Notice that the low byte goes first and the high byte is last. The last Instruction of the called subroutine must be a RET instruction which directs the CPU to POP the top bytes of the stack into the PC and resume executing at address 07. The diagram shows the stack frame after the first LCALL.

0A 09 08 00 07

SP = 09

41

The Process of Calling a Subroutine


After execution of the called subroutine, the 8051 must know where to come back to. The process of calling a subroutine
A subroutine is called by CALL instructions. The 8051 pushes the PC onto the stack. The 8051 copies the target address to the PC. The 8051 fetches instructions from the new location. When the instruction RET is fetched, the subroutine ends. The 8051 pops the return address from the stack. The 8051 copies the return address to the PC. The 8051 fetches instructions from the new location.

42

Example 3-10 (1/3)


Analyze the stack for the first LCALL instruction in the following program.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 0000 0000 0002 0004 0006 0008 000B 000D 000F 0012 0014 0300 O300 O302 O304 O306 O308 030A 030C 030E 0310 0311 ORG 0 7455 BACK: MOV A,#55H ;load A with 55H F590 MOV P1,A ;send 55H to port1 7C99 MOV R4,#99H 7D67 MOV R5,#67H 120300 LCALL DELAY ;time delay 74AA MOV A,#0AAH ;Load A with AA F590 MOV P1,A ;send AAH to port 1 120300 LCALL DELAY 80EC SJMP BACK ;keep doing this ; this is the delay subroutine ORG 300H C004 DELAY:PUSH 4 ;PUSH RAM 04H C005 PUSH 5 ;PUSH RAM 05H 7CFF MOV R4,#0FFH;R4=FFH 7DFF NEXT: MOV R5,#0FFH;R5=255 DDFE AGAIN:DJNZ R5,AGAIN DCFA DJNZ R4,NEXT D005 POP 5 ;POP INTO RAM 05H D004 POP 4 ;POP INTO RAM 04H 22 RET ;return to caller END ;end of asm file

43

Example 3-10 (2/3)


Solution: The stack keeps track of where the CPU should return after completing the subroutine.For this reason, the number of PUSH and POP instructions must always match in any called subroutine.
After the first LCALL 0B After PUSH 4 0B After PUSH 5 0B 67
R5

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

Example 3-10 (3/3)


Solution: Also notice that for the PUSH and POP instructions we must specify the direct address of the register being pushed or popped. Here is the stack frame.
After the POP 5 0B 0A 09 08 99 00 0B
R4 PCH PCL

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

Section 3.3 Time Delay Generation and Calculation

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

The 8051 Oscillators


C2 30pF C1 XTAL1 The period of MC external clock XTAL2 internal oscillator

machine cycle

30pF
GND external oscillator

= 12 oscillator period The frequency of MC = 1/12 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

A delay subroutine consists of two parts


setting a counterinitialization a loop

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

Delay Calculation for Other Versions of 8051


Two factors for time delay
The crystal frequency The 8051 design

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

Delay Calculation for DS89C4x0


The number of MCs to execute an instructions varies among different 8051 versions. Reference web sites: www.maxim-ic.com or www.MicroDigitalEd.com
Instruction 8051
1 1

DS89C4x0
2 1

Table 3-3: Comparison of 8051 and DS89C4x0 Machine Cycles

MOV R3,#value DEC Rx

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

Example 3-21 (1/2)


Write a program to toggle all the bits of P1 every 200ms for AT89C51. 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,#2 1 HERE1: MOV R4,#180 1 HERE2: MOV R3,#255 1 HERE3: DJNZ R3,HERE3 2 DJNZ R4,HERE2 2 DJNZ R5,HERE1 2 RET 2

66

Example 3-21 (2/2)


We only count the time delay expends by DJNZ. The approximate value of time delay inside the HERE1 loop: the HERE1 loop = 2 180 255 2MC 1.085 ms = 199206 ms If you want to get an accurate time delay, you should count the delay of MOV in the HERE1/HERE2/HERE3 and AGAIN loop .

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

Example 3-22 (2/2)


We only count the time delay expends by DJNZ. The approximate value of time delay inside the HERE1 loop: the HERE1 loop = 9 242 255 4MC 90 ns = 199940 ns If you want to get an accurate time delay, you should count the delay of MOV in the HERE1/HERE2/HERE3 and AGAIN loop .

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

You are able to (1/2)


Code 8051 Assembly language instructions using loops Code 8051 Assembly language conditional jump instructions Explain conditions that determine each conditional jump instruction Code long jump instructions for unconditional jumps Code short jump instructions for unconditional short jumps
71

You are able to (2/2)


Calculate target addresses for jump instructions Code 8051 subroutines Describe precautions in using the stack in subroutines Discuss crystal frequency versus machine cycle Code 8051 programs to generate a time delay

72

You might also like