Hi James, The only thing that I wasn't sure about was the lines: ;; subtract 1s from copy of counter movlw d'1000000'&0xff subwf t0,f btfss status,c decf t1,f This is an eight bit subtract of a 24 bit value, but you are not bringing the carry to the most significant byte ("t2"). I haven't done any kind of analysis on what this can do to you, but I'm sure every once in a while it will bite you. I would suggest something like: ;; subtract 1s from copy of counter movlw d'1000000'&0xff subwf t0,f btfsc status,c goto $ + 5 ;; If No Borrow, Subtract from the Middle Byte movlw d'1' subwf t1,f btfss status,c decf t2,f ;; "t1" was Zero upon Entry into Subtraction Routine, Decrement t2 myke ----- Original Message ----- From: "James Cameron" To: Sent: Wednesday, March 07, 2001 11:25 PM Subject: Re: [PIC]: Clock Tuning > On Wed, Mar 07, 2001 at 11:52:40AM -0500, Dan Michaels wrote: > > James Cameron wrote: > > >Looking for archived discussions or sample code to adjust for consistent > > >drift in my PIC 16F877's 4MHz crystal oscillator. > > ....... > > >The problem is that my clock is losing one second every three hours. It > > >is at least consistently doing this. It is well within specifications > > >for the crystal. > > Your error is ~1/3600, while xtal error is <= 50/1000000 = 1/20000, > > so the problem is no doubt in your code. 4 mhz is not an even power > > of 2, so your method of adjusting for timer overflows probably needs > > to be fixed. Look there. > > Ah, maybe I've got one of those off-by-one problems. I checked most > carefully ... TMR0 at 1:256, should give one T0IF every 65536uS exactly, > which I add into a 24-bit counter and then subtract one second. If the > result is positive, I keep the new counter value and activate my 1Hz > code path. TMR0 runs free, never reset. Does this logic seem sound? > > http://quozl.netrek.org/uptime/uptime.asm is the code. > http://quozl.netrek.org/uptime/ has the rest of the code. > > -- > James Cameron mailto:quozl@us.netrek.org http://quozl.netrek.org/ > > -- > http://www.piclist.com hint: To leave the PICList > mailto:piclist-unsubscribe-request@mitvma.mit.edu > > > -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads