delay for LED

Thread Starter

vead

Joined Nov 24, 2011
629
I want to write assembly program for delay
turn on LED
wait 40 ms
turn off LED
8051 operating from 12MHZ crystal

I know I need to use loop , DJNZ condition I don't know how to make loop ?
how to decide what is value of resistor used in djnz condition
 

Arm_n_Legs

Joined Mar 7, 2007
186
MOV R1,#0FFH​
D1: MOV R2,#0FFH
D2: DJNZ R2,D2
DJNZ R1,D1​

The values loaded into R1 and R2 will determine the duration of the delay.
 

Thread Starter

vead

Joined Nov 24, 2011
629
how I decide what will the value of R1 and R2
give idea I want to calculate delay for 40ms
 

tubeguy

Joined Nov 3, 2012
1,157
The original 8051 uses 12 clocks for 1 machine cycle.
So,with a 12MHZ crystal, 1 machine cycle = 1us.
The DJNZ and MOV instructions would each require 2 cycles or 2us to execute.

A convenient value to load in the inner loop could be #0FAH.
If you need to be extremely accurate then you must include the time required to execute the MOV instructions in the calculation. To get fine control of delays you can add NOP instructions which require only 1 cycle.
 
Last edited:
Top