Searching \ for 'Timing using RTCC/TMR0' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page:
Search entire site for: 'Timing using RTCC/TMR0'.

Truncated match.
PICList Thread
'Timing using RTCC/TMR0'
1997\09\14@182536 by Andy Shaw

Hi Folks,
I thought that I would pass this along just in case
anyone else finds it useful....

One of my favorite ways of producing timing loops
is to make use of the TMR0 free running timer.
Basically I sample the value of TMR0 at the start
of the period event and then simply subtract this
value from the current value of TMR0 to check on
the actual time. Obviously this requires that the
period to be measured falls within the range of
256 ticks of TMR0.

I use the PIC a lot to do
radio control stuff both decoding standard RC
signals and generating pulses for servos etc.
To do this you need to be able to both generate
and time pulses in the range 1-2ms. My usual
setup is to arrange for the TMR0 period to be just
over 2ms with a 4Mhz pic. (Actually the setup I use
gives a 1ms as a count of 125 in TMR0). To do this
I load the prescaler with the value 2 which gives a
divide by 8). With this setup the obvious way to
generate a 1ms period would be

       movf    TMR0, W ; Get current tick count
       movwf   start           ; save it
       movwf   start, W                ; Get tick count now
       subwf   TMR0, W ; Subtract start time
       addlw   -.125           ; Subtract 1ms count
       btfss   STATUS, Z       ; have we finished
       goto    loop            ; no so carry on waiting

However if you actually use this type of code to drive a servo
you will find that the servo 'twitches'. The reason for this is that
at the start of the loop the pre-scaler register may have any value
in it and so you will have an uncertainty of up to 1/125ms in the
actual period. The 'fix' for this simply clear the pre-scaler at the
same time as saving the start point by saving the TMR0 value
back to the timer...

       movf    TMR0, W ; Get current tick count
       movwf   TMR0            ; clear pre-scaler counter
       movwf   start           ; save start time

Anyway what a ramble this turned out to be!  Hope somebody
out there finds this of some use. It took me a long time to learn this
particular lesson!

BTW I know you can probably do this sort of timing stuff without
having to use TMR0. But I hate all of that instruction counting stuff!
also I find having a standard timer very useful, I use it to schedule
various tasks, time pulse lengths etc. It also makes it very easy
to change timer resolution and/or clock frequency!


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