Searching \ for '[PIC] Timer Tribulations' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/time.htm?key=time
Search entire site for: 'Timer Tribulations'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Timer Tribulations'
2001\02\20@220246 by James

flavicon
face
I am programming an interrupt service for 16F84A which provides a time base in units of seconds.  My project is to make . . . yet another clock, but this "time" using a 4.000 MHZ xtal, which is not the most natural choice, but offers some
challenge, maybe more than I thought.  Have not yet produced accurate time :-(   and don't know why.  Off by about 1 to 2 percent.

Me thinking is thus:

(1) XTAL is 4.000MHZ

(2) Instruction cycle is therefore 4.000/4 = 1.000 MHZ

(3) Need to divide 1.000 MHZ by 250, then by 250 again, then by 16 to get 1.000 second cycle rate.  1000000/(250*250*16) = 1 cycle/second

(4) Must "preload" TMR0 with literal 6 so it will have a cyclic period of 250 (256-6).  But wait, must compensate by loading TMR0 with 6+2 = 8 because the timer increment is latent for
2 instruction cycles after a write to TMR0 (see data sheet)

(5) In the same isr, set up a register "clocked" that decrements upon every TMR0 overflow.  And every time that register underflows to zero, it is to be preloaded with 249 to give it a period of 250.

(6) The final divide by 16 is done by picking off the desired bit of another register "clocked_2" that is decremented each time "clocked" underflows.  The "picked off" bit is written to port in the "main" loop to wink an LED (not shown
below)

(7) The timer prescaler was not used, it offers no help in this case.  Therefore the prescaler was tossed (assigned) to the dog.

Here is my humble and most unworthy interrupt routine:
;****************************************************************
isr:  ORG     h'0004'   ;interrupt service routine invoked each time TMR0 overflows 255

movlw     d'8'             ;preload TMR0 with 8 (8-2 dead cycles=6?)

movwf     TMR0        ;write preload to timer (want cycle of 250)

decfsz     clocked, 1      ;decrement clocked_2 and preload clocked each time TMR0=zero

goto cont      ;skipped on clocked=0

movlw     d'249'
movwf     clocked               ;preload clocked with 249 (250 cyclic)

decf     clocked_2, 1            ;decrement clocked_2

cont: bcf intcon,TOIF      ;clear Timer Overflow Interrupt Flag TOIF

retfie
;***************************************************************

Seems to me that this should work, and it does run, but not accurately.  What might I be missing?  Maybe someone can point me to some .asm code example that shows writing to the timer, including the necessary 2 instruction cycle
compensation.

THX!
James

--
http://www.piclist.com hint: To leave the PICList
spam_OUTpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu


2001\02\20@222022 by Tony Nixon

flavicon
picon face
James wrote:
>
> I am programming an interrupt service for 16F84A which provides a time base in units of seconds.  My project is to make . . . yet another clock, but this "time" using a 4.000 MHZ xtal, which is not the most natural choice, but offers some
> challenge, maybe more than I thought.  Have not yet produced accurate time :-(   and don't know why.  Off by about 1 to 2 percent.

Instead of adjusting the TMR0 value, which can cause large long term
effects, try adjusting another value further down the time data chain.

For example, if on TMR0 overflow, you increment some registers, and when
these registers equal a set value, the seconds register increments and
so forth.

Try adjusting the comparison value for these registers, so that the
value is lower if the time is slow, or raise the value if the time is
fast.

You will find that the clock will drift one way or the other depending
on temperature, so using the mains as a frequency source is much more
accurate.


--
Best regards

Tony

mICro's
http://www.picnpoke.com
.....salesKILLspamspam@spam@picnpoke.com

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestspamKILLspammitvma.mit.edu


2001\02\21@072554 by Bob Ammerman

picon face
Instead of 'preloading' with a 8, try adding 8 to TMR0 each interrupt.

This should do it.

This accounts for the fact that TMR0 might _not_ be zero when you get into
the interrupt routine. In fact it is highly unlikely that it will be!

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

{Original Message removed}

2001\02\21@080400 by promero

flavicon
face
part 0 44 bytes
his is a multi-part message in MIME format.
part 1 546 bytes content-type:text/plain; charset=us-ascii (decoded 7bit)

> Instead of 'preloading' with a 8, try adding 8 to TMR0 each interrupt.

>
>This should do it.
>
>This accounts for the fact that TMR0 might _not_ be zero when you get
into
>the interrupt routine. In fact it is highly unlikely that it will be!

This is totally true.

You also have to consider that loading (and preloading) "clocked" with a
value of 249 will result in 249 decrements to reach zero (instead of the
250 you are looking for) so try using 250 as the pre-loaded value.


part 2 422 bytes content-type:text/x-vcard; charset=us-ascii;
(decoded quoted-printable)

begin:vcard n:Romero Plaza;Pável Ernesto
tel;cell:5489528
tel;fax:6-7444829
tel;home:6-7464233
tel;work:6-7444829
x-mozilla-html:TRUE
url:http://www.insitel.com.co
org:Insitel Ltda.;Research & Development
adr:;;Calle 21 # 16 - 46 Piso 7;Armenia;Quindío;;Colombia
version:2.1
email;internet:.....promeroKILLspamspam.....insitel.com.co
title:Hardware Engineer
fn:Pável Ernesto Romero Plaza
end:vcard


part 3 154 bytes
--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...