Delay program
Dr [Link]
🞂 For an 8051 microcomputer a single instruction cycle is
executed for every 12 clock cycles of the
processor clock.
🞂 Thus, for an 8051 clocked at 12MHz. the instruction
cycle time is one microsecond, as follows:
🞂 Instruction cycle time = 12 clock cycles/ 12 x 106
cycles/sec, or 1 μsec.
🞂 The shortest instructions will execute in one instruction
cycle(machine cycle), i.e. 1 μsec.
🞂 Other instructions may take two or more instruction cycle
times to execute 12 clock cycles
Instruction Timing
🞂 One “machine cycle” = 6 states (S1 - S6)
🞂 One state = 2 clock cycles
◦ One “machine cycle” = 12 clock cycles
🞂 Instructions take 1 - 4 cycles
◦ e.g. 1 cycle instructions: ADD, MOV, SETB, NOP
◦ e.g. 2 cycle instructions: JMP, JZ
◦ 4 cycle instructions: MUL, DIV
8051 Overview CSE 477 3
Instruction Timing
8051 Overview CSE 477 4
Find the size of the delay in following program, if the crystal
frequency is 11.0592MHz.
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;---time delay-------
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE
RET
Solution:
Machine cycle
DELAY: MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 2
Therefore, [(200x2)+1+2]x1.085μs = 436.255μs.
🞂 Find the size of the delay in following program, if the crystal
🞂 frequency is 11.0592MHz.
🞂 Machine Cycle
🞂 DELAY: MOV R3,#250 1
🞂 HERE: NOP 1
🞂 NOP 1
🞂 NOP 1
🞂 NOP 1
🞂 DJNZ R3,HERE 2
🞂 RET 2
🞂 Solution:
🞂 The time delay inside HERE loop is
🞂 [250(1+1+1+1+2)]x1.085μs = 1627.5μs.
🞂 Adding the two instructions outside loop we
🞂 have 1627.5μs + 3 x 1.085μs = 1630.755μs
🞂 Find the size of the delay in following program, if the crystal
🞂 frequency is 11.0592MHz.
🞂 Machine Cycle
🞂 DELAY: 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:
🞂 For HERE loop, we have (4x250)x1.085μs = 1085μs.
🞂 For AGAIN loop repeats HERE loop 200 times, so
🞂 we have 200x1085μs = 217000μs.
🞂 But “MOV R3,#250” and “DJNZ R2,AGAIN” at the
start and
🞂 end of the AGAIN loop add (3x200x1.805)=651μs.
🞂 As a result we have 217000+651=217651μs.
🞂 NOP takes 1 instruction cycle to execute
🞂 NOP takes 1 instruction cycle to execute
🞂 DJNZ R7, LOOP_1_MILLI takes 2 instruction cycle to
execute
🞂 Total instruction cycles = 4
🞂 12MHz 8051
🞂 So, it takes 4 instruction cycles, or 4 microsecs, to execute
the loop. Thus, if we execute
🞂 the loop 250 times it will take a 1000 microsecs (250 x 4),
i.e. 1 millisecond, to complete
🞂 the loops.
Subroutine to generate 1msec
🞂 PUSH 07h ; save R7 to stack
🞂 MOV R7, #250d ; 250 decimal to R7 to count 250
loops
🞂 LOOP_1_MILLI: ; loops 250 times
🞂 NOP ; inserted NOPs to cause delay
🞂 NOP ;
🞂 DJNZ R7, LOOP_1_MILLI ; decrement R7, if not zero
loop back
🞂 POP 07h ; restore R7 to original value
🞂 RET ; return from subroutine
🞂 Write a routine to generate a delay of 1msec using
11.0592MHz clock for 8051.
🞂 Nxnumber of machine cyclesx1.085μsec
=1000μsec=1msec
Dely to generate for 1 second using 12
Mhz clock
🞂 This subroutine calls the ONE_MILLI_SUB subroutine
and is structured so that the
🞂 ONE_MILLI_SUB subroutine is called exactly 1000
times, thus causing a total delay
🞂 of 1000 milli. seconds, i.e. ONE second
🞂 PUSH 07h ; save R7 to stack
🞂 MOV R7, #250d ; 250 decimal to R7 to count 250 loops
🞂 LOOP_SEC: ; Calls 4 one millisec. delays, 250 times
🞂 LCALL ONE_MILLI_SUB ; call subroutine to delay 1 millisecond
🞂 LCALL ONE_MILLI_SUB ; call subroutine to delay 1 millisecond
🞂 LCALL ONE_MILLI_SUB ; call subroutine to delay 1 millisecond
🞂 LCALL ONE_MILLI_SUB ; call subroutine to delay 1 millisecond
🞂 DJNZ R7, LOOP_SEC ; decrement R7, if not zero loop back
🞂 POP 07h ; restore R7 to original value
🞂 RET ; return from subroutine
🞂 Write a program to generate a delay of 2 seconds using
11.0595MHz clock for 8051.
🞂 Write a program to generate a waveform of 1 KHz at
port 2.1.
example of delay
mov a,#0aah Delay2:
Back1:mov p0,a mov r6,#0ffh
lcall delay1 back1: mov r7,#0ffh ;1cycle
cpl a Here: djnz r7,here ;2cycle
sjmp back1 djnz r6,back1;2cycle
Delay1:mov r0,#0ffh;1cycle
Here: djnz r0,here ;2cycle ret ;2cycle
end
ret ;2cycle
end Delay=1+(1+255*2+2)*255+2
=130818 machine cycle
Delay=1+255*2+2=513 cycle
Long delay Example
GREEN_LED: equ P1.6
org ooh reset service
ljmp Main
org 100h
Main: clr GREEN_LED
main program
Again: acall Delay
cpl GREEN_LED
sjmp Again
Delay: mov R7, #02
Loop1: mov R6, #00h
Loop0: mov R5, #00h
subroutine
djnz R5, $
djnz R6, Loop0
djnz R7, Loop1
Timers
🞂 The 8051 has two timers/counters,
🞂 they can be used either as
🞂 Timers to generate a time delay or as
🞂 Event counters to count events happening
🞂 outside the microcontroller
🞂 Both Timer 0 and Timer 1 are 16 bits
🞂 wide
🞂 Since 8051 has an 8-bit architecture, each
🞂 16-bits timer is accessed as two separate
🞂 registers of low byte and high byte
🞂 Accessed as low byte and high byte
🞂 The low byte register is called TL0/TL1
🞂 and
🞂 The high byte register is called TH0/TH1
🞂 Accessed like any other register
🞂 MOV TL0,#4FH
🞂 MOV R5,TH0
Number of machine cycles required to generate a delay
of 1millisec for 11.059MHz osc
Number of machine cyclesX time required for one
machine cycle = 1msec
No of machine cycles = 1msec/1.085micrsec
= 921