# Understanding PI16F877 Timing

Status
Not open for further replies.

#### dramrattan

##### New Member
Hi,

I'm trying to understand how to arrive at the timing values stated in the lecture unit attached. On page 6, there is a snippet which states that the delay should be 1ms however i think it should be 1us since it is a 4MHz clock. Here is the code:

delay_1ms movlw 0xF9 ; 0xF9 = 249
nop
btfss STATUS,Z
goto usec4
return

The value 249 is used because it needs to go through 256 cycles in order for one instruction to take place right?...The 249 + 2 (cycles for call) + 1 (cycle for loading of W) + 4 (cycles for loop) = 256 cycles

Therefore, if all this is correct, could someone help me understand (which is also on page 6):

This routine can be expanded through the use of a variable to allow times in multiples of 1 ms.
For example
; routine is passes the amount of ms to delay in W
; it uses an additional register variable msec_count
delay_ms movwf msec_count
msecloop movlw 0xF8 ; 0xF8 = 248
call usec4 ; 248 * 4 + 2 (call) = 994
nop
nop
decfsz msec_count,F
goto msecloop
return

How come decrement could be used in the second snippet and not the first?...The 248 * 4 + 2 = 994 is what is confusing me i think...because it is using that to generate the multiples of 1ms delays right?...but i don't understand how they arrive at that.

Thanks for any help.

#### Attachments

• 886.2 KB Views: 245

#### testerplus

##### New Member
(Tags CODE would be nice)

Hi,
There is no decrement instructions for WREG. You can decrease WREG by adding -1 (or 0xFF) only.

call usec4 ; 248 * 4 + 2 (call) = 994
Here is an error: 248*4 + 2(call) + 2(return) = 996

decfsz msec_count,F
...
How come decrement could be used in the second snippet and not the first?
Instruction decfsz decrements a register (it can not to work with an accumulator WREG).

#### Pommie

##### Well-Known Member
Here is an error: 248*4 + 2(call) + 2(return) = 996
There is still an error as the last pass through usec4 takes 1 cycle less. So, it's 248*4-1 + 2(call) + 2(return) = 995.

Also, another reason to use addlw 255 is that decrement doesn't set the zero flag.

Mike.

Status
Not open for further replies.