Delay doesn't seem to work - Help!

Status
Not open for further replies.

bsodmike

New Member
Code:
Delay   movlw	d'39'		; load this to the timer

Loop	btfss	_intcon,2	; if tmr0 overflow is not set
	goto	Loop		; loop, else skip

	bcf	_intcon,2	; clear the timer interrupt flag

	decfsz	w,w		; loop until the wreg is zero
	goto 	Loop		; it isn't zero so loop

	return			; zero, so we return

Not sure why this doesn't seem to work. My thinking is as follows:

Clock speed of 4000000Hz/4 = 1000000hz

This 1/4th value is further divided by my pre-scaler of choice /256: 1000000hz/256 = 3906.25hz

3906.25 pulses take 1 second - so to count 10ms delay we need to count when this has tripped over 39 times. To prove this 1/3906.25 = 2.56x10-4 * 39 = 9.98ms.

Right or wrong? Is there anything else needed to get the TMR0 overflow to work?

Thanks,

Mike
 
bsodmike said:
Any ideas?

My main idea is 'why bother'.

Why not simply use the delay code generator on the PICList to generate delay code for you? - it's far more versatile!.

Are you sure you are setting everything up correctly?, you've only posted a small piece of the code, so there's no indication of how it's set up at all.
 
I move to RPO and have set this:

movlw b'10100000'
movwf _intcon

however by using this delay:

Delay clrf _tmr0
tloop movlw d'39'
subwf _tmr0,w
btfss _status,0
goto tloop
return

I have got it to work - but it alters tmr0. I could also count 10000 lines of code (big nested loop...) and it'll work...

I will settle for the way I've posted above...thanks

Mike
 
Code:
; Delay = 0.01 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.01 seconds = 10000 cycles
; Error = 0 %

	cblock
	cnt1
	cnt1_1
	endc

Delay
			;9993 cycles
	movlw	0xCE
	movwf	cnt1
	movlw	0x08
	movwf	cnt1_1
Delay_0
	decfsz	cnt1, f
	goto	$+2
	decfsz	cnt1_1, f
	goto	Delay_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

This is what the generator gives me. CE = 206 and 8, so wouldn't this mean 206 * 8 ??? How is it 9993 ?!?!
 
bsodmike said:
This is what the generator gives me. CE = 206 and 8, so wouldn't this mean 206 * 8 ??? How is it 9993 ?!?!

It's not as simple as that, the inner loop (cnt1) loops round to 255 after it reaches zero.

Don't worry about it, just use it!.
 
I took a quick glance at your code. You can't use the ff. sequence because the W register is not a regular file register.

Code:
   decfsz   w,w      ; loop until the wreg is zero 
   goto    Loop      ; it isn't zero so loop

It will compile without error but because the value of W is defined as "0", the register that will be accessed will be the INDF register. You have to use a real file register instead of W.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…