8051 creating delays question

Discussion in 'Embedded Systems and Microcontrollers' started by ra1ph, Dec 28, 2011.

  1. ra1ph

    Thread Starter Active Member

    Jan 5, 2010
    31
    0
    Hi,
    Could someone take a look at my answer and tell me if it is right?

    The question is to modify the following piece of code so the the first delay takes 101us and the second takes 340us?

    LJMP BEGIN
    ORG 0200H
    BEGIN:
    SETB P1.6
    MOV R0,#26H
    LOOP1:
    DJNZ R0,LOOP1
    CLR P1.6
    MOV R0,#26H
    LOOP2:
    DJNZ R0,LOOP2
    JMP BEGIN
    END

    My answer is:
    ORG 0200H
    BEGIN:
    SETB P1.6
    MOV R0,#49
    LOOP1:
    DJNZ R0,LOOP1
    NOP
    CLR P1.6
    MOV R0,#168
    LOOP2:
    DJNZ R0,LOOP2
    JMP BEGIN
    END

    I have a simulator but I am unsure exactly where I should put the break-points and if this is meant to be a 50% duty cycle square wave which leads me on to the next question in this assignment.

    Thanks
     
  2. Zazoo

    Member

    Jul 27, 2011
    114
    43
    If you are using the on-chip 12MHz oscillator, each machine-cycle requires 1μs (12 clock pulses per cycle)

    For the first delay I get a machine cycle count of:
    1+1+49*2+1 = 101
    (for SETB, MOV, DJNZ loop, and NOP)

    For the second delay I get a machine cycle count of:
    1+1+168*2+2 = 340
    (for CLR, MOV, DJNZ loop, and (S)JMP)

    Looks good to me.

    Duty cycle is actually about 23% for this square wave (101/(101+340)).
    A 50% duty cycle would have equal on-off times.
     
    ra1ph likes this.
  3. ra1ph

    Thread Starter Active Member

    Jan 5, 2010
    31
    0
    Thanks for this.

    I should have been more clearer regarding the 50% duty cycle square wave. It was in relation to the original question. Anyhow using your confirmation of my answer I was able to determine for sure that the delay times were 78us and 80us and is indeed not a square wave of 50% duty cycle.

    Thanks Again
     
  4. Arm_n_Legs

    Active Member

    Mar 7, 2007
    183
    10
    You can confirm the timing by using a simulator such as the Keil uVision3 (or 4). See attached for answer.
     
    ra1ph likes this.
Loading...