Newsgroups: comp.robotics
Path: brunix!news.Brown.EDU!agate!howland.reston.ans.net!cs.utexas.edu!convex!cnn.exu.ericsson.se!s08a04!exujet
From: exujet@exu.ericsson.se (Jerry Ethridge)
Subject: Re: Parallax PIC RTCC Option Register
Message-ID: <1994Feb23.230755.12489@exu.ericsson.se>
Sender: news@exu.ericsson.se
Nntp-Posting-Host: s08a04.exu.ericsson.se
Reply-To: exujet@exu.ericsson.se
Organization: Ericsson Network Systems, Inc.
References: <i7q8Hc2w165w@sfrsa.com>
Date: Wed, 23 Feb 1994 23:07:55 GMT
X-Disclaimer: This article was posted by a user at Ericsson.
              Any opinions expressed are strictly those of the
              user and not necessarily those of Ericsson.
Lines: 79

In article i7q8Hc2w165w@sfrsa.com, bsmall@sfrsa.com (bsmall) writes:
>Can any one out there please tell me how to write to
>the Option Register in the Parallax Assembly Language.
>I am also looking for example programs of timing loops
>using the RTCC to count instruction cycles. I believe
>I need to write to the Option Register to set bit 5
>to 0 so that the RTCC counts instruction cycles instead
>of external events.
>

Sorry I cannot help you with Parallax's language because I write 
code in the PIC native language. However you are right, bit 5 of 
the OPTION register needs to be zero in order for the RTCC register 
to increment from the instruction clock. I wanted to point out a
very important problem with using the RTCC with the prescaler. The 
problem manefests itself if you want to execute code at a very 
accuarate periodic rate.

Example: you want to execute 600 lines of code every 1ms

With a 4Mhz crystal the instruction clock would be 1MHz.
That means each instruction executes in 1usec. By setting the 
prescaler to 1:8, and loading the RTCC with 131 decimal, the 
RTCC will reach zero in 1msec (1usec x 8 x (256-131) = 1msec).
Since it takes 1000 clock cycles for the RTCC to reach a value
of zero, you can safely execute 600 lines of code and go into 
a loop that waits until the RTCC has reached zero, then reload
RTCC with 131 and jump to your periodic routine.

Here's the problem. When you reload the RTCC with 131, the 
prescaler register is cleared to zero. That means that the 
few instructions that it took to reset the value of the RTCC
to 131 that was registered in the prescaler are lost each time 
the RTCC is written. You might say lets just find out how many
clock cycles are lost each time and adjust either the RTCC
value or crystal to obtain exactly 1msec. However, if your
code has decisions that might make it execute 600 lines one
cycle and 625 lines another cycle then you will enter your
loop that checks for the RTCC turning to zero in different
phases. Lets just say that it would be a very difficult and
tedious analysis process that would require you to load the 
RTCC with different values dependent on the branching 
possibilities of your program.

Every one I talked to suggested I use a seperate clock chip
to do my timing if it needed to be accurate but I came up with
a solution that circumvents the RTCC/prescaler problem. I 
figured that if the prescaler is reset each time the RTCC is
written to with a result of lost clock cycles, then never 
reset the RTCC. Instead, pick your crystal frequency and
prescaler values such that the RTCC does not need to be 
reloaded:

RTCC(256) x prescaler x 1000(1msec) x 4(instruction clock)=
crystal freq

256 x 4 x 1000 x 4 = 4.096 MHz is one example

This will allow 1024 instructions before RTCC value reaches 
zero.

This combination gives exactly 1msec resolution on your code
with an error that only depends on the accuracy of your crystal.
For 5msec resloution, a counter could be used to tell you when
5msec is up, then branch to your code.

Looking back, I got a little long winded, but one must take
care in order to get accurate timing from the PIC microcontrollers.

Regards,
Jerry E.

===============================================================================
Jerry Ethridge		     Richardson, Tx.                 |
ERICSSON NETWORK SYSTEMS                                    /O\
exujet@exu.ericsson.se                            \_______[|(.)|]_______/
                                                    o   ++   O   ++   o
===============================================================================

