Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to calculate the delay?

Status
Not open for further replies.
how to calculate the delay for each instruction?

Code:
57:                      delay_us(90);
00002F    303B     MOVLW 0x3b
000030     0F7     MOVWF 0x77
000031     BF7     DECFSZ 0x77, F
000032    2831     GOTO 0x31
000033     000     NOP
000034     000     NOP


The above listing is taken from the MPLAB (compiled with CCS compiler).
I am using 8Mhz resonator.


Each instruction clock takes 4 clock cycles = 1 /(8 MHz / 4 ) = 0.5 us
GOTO takes 2 instruction clocks
DECFSZ + GOTO loop for 119 times (0x77 = 119) = (1 + 2) x 119 x 0.5 us = 178.5 us
MOVLW + MOVWF + NOP + NOP = 4 x 0.5us = 2 us
The total time spent = 178.5us + 2us = 180.5 us

Is the calculation correct?
I am expecting 90 us, the calculation give me 180.5 us.
 
Code:
57:                      delay_us(90);
00002F    303B     MOVLW 0x3b
000030     0F7     MOVWF 0x77
000031     BF7     DECFSZ 0x77, F
000032    2831     GOTO 0x31
000033     000     NOP
000034     000     NOP

MOVLW+MOVWF+NOP+NOP=4 cycles
DECFSZ (no skip)+GOTO=58*(1+2) cycles
DECFSZ (skip) = 2 cycles
Total=180 cycles

Since 1 cycles=0.5us
180cycles=90us

P.S. 0x3B=59
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top