Question

I need to code a delay for a 8051. I can do that but what I don't know is how to calculate the frecuency of my delay. For example, here is a delay I did:

DELAY: MOV R2, 0FFH
D2:    NOP
       NOP
       DJNZ R2, DELAY
       RET

But what I don't know is how many Hz of frequency this delay produces. Is there any way of calculating it?

Was it helpful?

Solution

You need to know how many cycles each instruction takes, and the speed of your processor.

Generically, if your processor is 1MHz, and NOP takes 2 cycles, the DJNZ take 3 cycles (making all of these up, no idea how many cycles these take on a 8051), then the first trip through the loop, from D2: would be 7 cycles (2 + 2 + 3 = 7), with a 1MHz processor, each cycle is 1 microsecond, so that would take 7 µs, rinse and repeat until you reach your desired delay.

Note, you probably don't want the DJNZ to jump to DELAY, but D2. Not familiar with the 8051 really, but that's just a guess.

Also don't forget to add up the MOV and RET instructions.

OTHER TIPS

a simple delay routine with comments for understanding

delay_1_ms:       ;calling this routine take 2 mc ;tmc=2
    MOV R7,#250   ;mov rn,#data take 1 mc(machine cycle);tmc=2+1
    DJNZ R7,$     ;djnz take 2mc for each time exicuted;tmc=3+(2*250)
    MOV R7,#247   ;mov rn,#data take 1 mc              ;tmc=503+1
    DJNZ R7,$     ;djnz take 2mc for each time exicuted.;tmc=504+(247*2)
    RET           ;ret takes 2mc so total machine cycle=998+2=1000mc

if one machine cycle for 12mhz crystal is 1micro sec.So this routine takes 1000*1microsec=1mili sec delay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top