Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers

Notices


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 14th June 2004, 05:01 AM   (permalink (permalink))
Default Delay doesn't seem to work - Help!

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
__________________
"Ambition is the last refuge of failure..." --Oscar Wilde
"Success is not final, failure is not fatal: it is the courage to continue that counts." -- Winston Churchill
bsodmike is offline  
Reply With Quote
Old 14th June 2004, 06:01 AM   (permalink (permalink))
Default

Any ideas?
__________________
"Ambition is the last refuge of failure..." --Oscar Wilde
"Success is not final, failure is not fatal: it is the courage to continue that counts." -- Winston Churchill
bsodmike is offline  
Reply With Quote
Old 14th June 2004, 06:17 AM   (permalink (permalink))
Default

Quote:
Originally Posted by bsodmike
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply With Quote
Old 14th June 2004, 06:20 AM   (permalink (permalink))
Default

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
__________________
"Ambition is the last refuge of failure..." --Oscar Wilde
"Success is not final, failure is not fatal: it is the courage to continue that counts." -- Winston Churchill
bsodmike is offline  
Reply With Quote
Old 14th June 2004, 06:30 AM   (permalink (permalink))
Default

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 ?!?!
__________________
"Ambition is the last refuge of failure..." --Oscar Wilde
"Success is not final, failure is not fatal: it is the courage to continue that counts." -- Winston Churchill
bsodmike is offline  
Reply With Quote
Old 14th June 2004, 08:17 AM   (permalink (permalink))
Default

Quote:
Originally Posted by bsodmike
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!.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply With Quote
Old 14th June 2004, 09:47 AM   (permalink (permalink))
Default

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.
__________________
"Having to do with Motion Control"
motion is offline  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 02:57 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.