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.

TMR0 Timing Code Problem

Status
Not open for further replies.

Suraj143

Active Member
Hello all,My clock time base is using TMR0.

The problem is for every 12 hours the time is varying about 3 minutes.

Here is the code.Any method to accurate?
Code:
;PS=1:128,TMR0=39
;39X128 	= 4992US+8uS
		= 5000uS

ISR_Enter	movwf   W_Temp           	;save W register contents
		movf	STATUS,W        	;move status register into W register
		movwf	S_Temp			;+1=5		
		movlw	100h-d'39'		;+1 =6		
		nop				;+1 =7
		movwf	TMR0			;+1 =8 + 39*128 = 5,000
		bcf	INTCON,T0IF		;reset int flag
		decfsz	ISR_Count,F
		goto	Split_Digits
		movlw	.200
		movwf	ISR_Count
			;
;===============================================
;Update RTC on every 1 second period
;===============================================
			;
Update_Time	incf	Time_Seconds,F		;generate 60 seconds
 
The problem is caused by the prescaler getting reset whenever you write to TMR0. Are you able to use timer 1 and the CCP module for this?

Mike.
 
Hi thanks for the replies. I can use TMR2 & TMR0.But I can’t use TMR1. Is the above code is varying time?

Any method to overcome?

I need 5000us (5ms) interrupts.
 
When you load TMR0 you clear it's prescaler causing jitter. Freerunning the timer and just clearing the interrupt flag will give you accurate timing. TMR2 is much more flexible, what crystal are you running?
 
That Roman Blacks code is too long.Thats why I didn't use that.I need a simple method.

I'm using 4Mhz.
 
Last edited:
If you put 0x25 in T2CON and 249 in PR2 then you will get a timer 2 interrupt every 5mS and it will be very accurate. This sets prescaler=4, postscaler=5 and period = 250. 250*4*5=5000.

Mike.
 
If you put 0x25 in T2CON and 249 in PR2 then you will get a timer 2 interrupt every 5mS and it will be very accurate. This sets prescaler=4, postscaler=5 and period = 250. 250*4*5=5000.

Mike.

Wow thats very nice I'm going to use this method in my clock.

I think TMR2 is more accurate for a clock.

Thanks for the help mike.
 
Hi Mike can you tell is this code ok.

I need 4096uS interrupts for seven segment multiplex & a perfect 1 second timing to run the clock.

Code:
		movlw	.16		;subtract 16*256 from accumulator
		subwf	AccHi,F
		skpnc
		goto	DoneTimer
		decfsz	AccUp,F
		goto	DoneTimer

;only gets here if 1 second has passed

		movlw	low(.1000000)	;add 1 million to the accumulator
		addwf	AccLo,f
		movlw	high(.1000000)
		skpnc
		addlw	1
		addwf	AccHi,f
		skpnc
		incf	AccUp,F
		movlw	upper(.1000000)
		addwf	AccUp,F

;time update starts from here

Do_Minutes	incf	Minutes,F
		movf	Minutes,W
		xorlw	.60
		btfss	STATUS,Z
		goto	DoneTimer
		clrf	Minutes
		;
Do_Hours	incf	Hours,F
		movf	Hours,W
		xorlw	.24
		btfss	STATUS,Z
		goto	DoneTimer
		clrf	Hours
		;
DoneTimer	bcf	INTCON,TMR0IF
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top