delay for LED

Discussion in 'Embedded Systems and Microcontrollers' started by vead, Oct 4, 2013.

  1. vead

    Thread Starter Well-Known Member

    Nov 24, 2011
    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
  2. Arm_n_Legs

    Active Member

    Mar 7, 2007
    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.
  3. vead

    Thread Starter Well-Known Member

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

    Well-Known Member

    Nov 3, 2012
    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: Oct 5, 2013