Wednesday, March 23, 2016

8051 Delay

The CPU takes a certain number of clock cycles to execute an instruction. These clock cycles are referred to as machine cycles. The length of the machine cycle depends on the frequency of the crystal oscillator connected to the 8051. The crystal oscillator, along with on-chip circuitry, provides the clock source for the 8051 CPU. The frequency of the crystal connected to the 8051 family can vary from 4 MHz to 30 MHz, depending on the chip rating and manufacturer. Very often the 11.0592 MHz crystal oscillator is used to make the 8051 -based system compatible with the serial port of the IBM PC. In the original 8051, one machine cycle lasts 12 oscillator periods.

Period of Machine Cycle = 11.0592 MHz / 12 = 921.6 KHz
Machine Cycle = 1/921.6 KHz = 1.085 µs (Microseond)
Minimum time taken to execute an instruction is 1.085 µs

Simple Delay Program:

ORG 0H
MOV A, #55H ; load 55H in to A
MAIN :MOV P1, A ; copy content of A in to port 1
ACALL DELAY; call time delay sub routine
CPL A ; complement A
SJMP MAIN ; call main again

; Time Delay subroutine
ORG 100H
DELAY :MOV R3, #200 ; load 200 in to R3
HERE : DJNZ R3, HERE ; stay here until R3 becomes 0
RET ; return to main
END


The DELAY subroutine has 3 instructions in which MOV needs 1 machine cycle to execute and DJNZ and RET needs 2 machine cycles
Total number of machine cycle required is 1 + 200*2 + 2 = 403
Total time taken by DELAY sub routine is 403 * 1.085 µs = 437.255 µs
Delay can be increased using NOP instruction or using nested loop.

Program to toggle all the bits of P1 for every 200ms.

ORG 0H
MOV A, #55H ; load 55H in to A
MAIN : MOV P1, A ; copy content of A in to port 1
ACALL DELAY ; call time delay sub routine
CPL A ; complement A
SJMP MAIN ; call main again

; Time Delay subroutine
ORG 100H
DELAY :MOV R5, #2
HERE1 :MOV R4, #180
HERE2 :MOV R3, #255
HERE3 :DJNZ R3, HERE3
DJNZ R4, HERE2
DJNZ R5, HERE1
RET
END



Related topics:
8051 Loop   |   8051 IO Port Programming   |   8051 Timer Programming   |   8051 Counter   |   8051 Serial Port Programming   |   8051 Programming Timer Interrupt   |   8051 Programming Serial Interrupt   |   8051 Programming External Interrupt

List of topics: 8051

No comments:

Post a Comment