[03] Loop Delay

18

description

Loop Delay

Transcript of [03] Loop Delay

Page 1: [03] Loop Delay
Page 2: [03] Loop Delay

Loop and Jump Instructions

Repeating a sequence of instructions a certainnumber of times is called a loop.

The loop action is performed by the instructionDJNZ reg,label

In this instruction, the register is decremented; if it isnot zero, it jumps to the target address referred to bythe label.

Prior to the start of the loop the register is loadedwith the counter for the number of repetitions.

Page 3: [03] Loop Delay

Example

Write a program to clear the Acc, then add 3 to theaccumulator ten times.

MOV A,#0MOV R2,#10

AGAIN: ADD A,#03DJNZ R2,AGAINMOV R5,A

Page 4: [03] Loop Delay

Other Conditional Jumps

Instruction ActionJZ Jump if A = 0JNZ Jump if A ≠ 0DJNZ Decrement and jump if A ≠ 0CJNE A,byte Jump if A ≠ byteCJNE reg,#data Jump if byte ≠ #dataJC Jump if CY = 1JNC Jump if CY = 0JB Jump if bit = 1JNB Jump if bit = 0JBC Jump if bit = 1 and clear bit

Page 5: [03] Loop Delay

Unconditional Jump Instructions

All conditional jumps are short jumps, meaning that the address ofthe target must be within -128 and +127 bytes of the contents of theprogram counter (PC).

Unconditional jump instructions are: LJMP (Long jump) – 3 byte instruction

SJMP (Short jump) – 2 byte instruction

Page 6: [03] Loop Delay

CALL instructions

CALL instruction is used to call a subroutine LCALL (long call) – 3 byte instruction ACALL (absolute call) – 2 byte instruction

When a subroutine is called, control is transferred to thatsubroutine.

After finishing execution of the subroutine, the instructionRET (return) transfers control back to the caller.

Page 7: [03] Loop Delay

Time Delay Generation and Calculation

For the CPU to execute an instruction takes a certainnumber of clock cycles.

In the 8051 family, these clock cycles are referred toas machine cycles.

We can calculate a time delay using the availablelist of instructions and their machine cycles.

In the 8051, the length of the machine cycledepends on the frequency of the crystal oscillatorconnected to the 8051 system.

Page 8: [03] Loop Delay

Time Delay Generation and Calculation(cont’d)

The frequency of the crystal connected to the 8051 family can varyfrom 4MHz to 30MHz.

In the 8051, one machine cycle lasts 12 oscillator periods. Therefore, to calculate the machine cycle, we take 1/12 of the

crystal frequency and then take the inverse.

Page 9: [03] Loop Delay

Example

The following shows crystal frequency for three different 8051-basedsystems. Find the period of the machine cycle in each case.(a) 11.0592MHz(b) 16MHz(c) 20MHz

Page 10: [03] Loop Delay

Solution

1/11.0592MHz = period per oscillationMachine cycle = 12x= 1.085μs

1/16MHz = period per oscillationMachine cycle = 12x= 0.75μs

1/20MHz = period per oscillationMachine cycle = 12x= 0.6μs

Page 11: [03] Loop Delay

Question

For an 8051 system of 11.0592MHz, find how long it takes to executeeach of the following instructions:(a) MOV R3,#55(b) DEC R3(c) DJNZ R2,target

Page 12: [03] Loop Delay

Solution

(a) MOV R3,#55 1x1.085μs(b) DEC R3 1x1.085μs(c) DJNZ R2,target 2x1.085μs

Page 13: [03] Loop Delay

Delay Calculation

A delay subroutine consists of two parts:(a) setting a counter(b) a loop

Most of the time delay is performed by the body of the loop. Very often we calculate the time delay based on the instructions

inside the loop and ignore the clock cycles associated with theinstructions outside the loop.

Largest value a register can hold is 255; therefore, one way toincrease the delay is to use the NOP command.

NOP, which stands for “No Operation” simply wastes time.

Page 14: [03] Loop Delay

Example

Find the size of the delay in the following program, if the crystalfrequency is 12MHz.

MOV A,#55HAGAIN: MOV P1,A

ACALL DELAYCPL ASJMP AGAIN

DELAY: MOV R3,#200HERE: DJNZ R3,HERE

RET What does the above program do?

Page 15: [03] Loop Delay

Solution

Crystal CycleDELAY: MOV R3,#200 12HERE: DJNZ R3,HERE 24

RET 12

Therefore, we have a delay of [(200X24)+12+24]x0.083μs = 402μs

Page 16: [03] Loop Delay

Loop inside a loop delay

Another way to get a large delay is to use a loop inside a loop,which is also called a nested loop. E.g.

Crystal CycleDELAY:MOV R3,#250 12HERE: NOP 12

NOP 12NOP 12NOP 12DJNZ R3,HERE 24RET 24

Time Delay=[250.(12+12+12+12+24)]x0.083μs + (12+24)x0.083μs = μs

Page 17: [03] Loop Delay

Question

For a machine cycle of 1μs, find the time delay of the followingsubroutine.

DELAY:MOV R2,#200

AGAIN: MOV R3,#250HERE: NOP

NOPDJNZ R3,HEREDJNZ R2,AGAINRET

Page 18: [03] Loop Delay

Solution

HERE Loop: 250x1μs = 250 μs AGAIN Loop: Repeats the HERE loop 200 times i.e.

200x250μs = 200ms