I'm a beginner but have mastered RS232, LED displays, and switch
de-bouncing on PIC's (84's) so I'm getting the hang of it.
Could someone give me a simple (if possible) explanation of the what is
the "pre-scaler" and maybe an practical example (not code unless that
helps) with and without it's use?
Thanks very much in advance.
|Scott Horton <MITVMA.MIT.EDU> wrote: PICLIST
> Could someone give me a simple (if possible) explanation of the what
> is the "pre-scaler"
What follows is a slightly-edited version of a message I posted here
a year or so ago. I hope it helps.
The RTCC (or "TMR0", if you prefer; I'm going to refer to it as
"RTCC" in this message) has four components: The clock input
(either from the RTCC pin or from the internal instruction clock),
the RTCC prescaler, the RTCC register, and the RTCC interrupt flags
(GIE, T0IE, and T0IF).
I'm assuming that you're using the internal instruction clock as
your RTCC input, and that your PIC is running at 4 MHz. Since
there's an internal divide-by-4 between the oscillator frequency and
the instruction clock, this means that instruction clocks occur at a
1 MHz rate.
The RTCC prescaler is set (via 4 bits in the OPTION register) to
divide-by-1, -2, -4, -8, -16, -32, -64, -128, or -256. The RTCC
input clock (1 MHz in your case) is passed to the prescaler, whose
divided-down output is then fed to the RTCC register.
In your case, if the RTCC prescaler is set to divide-by-4, for
instance, the prescaler will send a 250 KHz clock to the RTCC
The RTCC register can be preloaded with any 8-bit value that you like.
Each clock pulse from the prescaler increments the contents of the
RTCC register. When the value in the RTCC register rolls over from
0xFF to 0x00, the T0IF flag is set (the RTCC register continues to be
incremented on every pulse from the prescaler, though).
If the GIE and T0IE flags are set when the T0IF flag is set, an
interrupt is generated, the GIE bit is automatically cleared (to
temporarily prevent further interrupts while your interrupt routine is
executing), and the PIC jumps to the "interrupt vector" at location
0x04. Your interrupt-service routine at that location should check
the T0IF flag to determine the source of the interrupt, then must
clear the T0IF flag to prevent the PIC from immediately jumping back
to the interrupt routine as soon as interrupts are re-enabled.
At this point in the interrupt routine, you can re-load the RTCC with
any appropriate value.
When you're finished handling the interrupt, your code should execute
a RETFIE instruction, which will automatically set the GIE bit to
re-enable interrupts and return to your main program.
=== Andrew Warren - ix.netcom.comfastfwd
=== Fast Forward Engineering, Vista, California
Scott Horton wrote:
> I'm a beginner but have mastered RS232, LED displays, and switch
> de-bouncing on PIC's (84's) so I'm getting the hang of it.
> Could someone give me a simple (if possible) explanation of the what is
> the "pre-scaler" and maybe an practical example (not code unless that
> helps) with and without it's use?
> Thanks very much in advance.
> Scott Horton
If you use a one byte prescaler on portpin a,4 you can accept up to 255
pulses before the processor is interupted. You can then preload the
timer/prescaler with a constant and let it increment from there till
rollover thus generating an interupt only after n externally generated
events. Or so I gather. Tony M.
|On Wed, 21 May 1997 08:07:14 -0600 Scott Horton
<BRIDGE.BST.BLS.COM> writes: Scott.Horton1
>Could someone give me a simple (if possible) explanation of the what
>the "pre-scaler" and maybe an practical example (not code unless that
>helps) with and without it's use?
The "prescaler" is part of the TIMER0 module present on almost all PIC
chips (on 16C5x's it's called the RTCC, but its logic is the same). The
purpose of the prescaler is primarily to slow down the counting of the
timer register by dividing the input frequency by a constant. Since the
timer register is only 8 bits, it can be difficult for the program to
keep up with the count of a high input frequency before it rolls over and
data is lost.
The prescaler is not tightly coupled to the rest of the PIC logic. It is
a simple 8-bit ripple counter (made of 8 toggle flip-flops where the 1-0
transition of one clocks the input of the next one). Settings in the
option register control a multiplexer that connects one of the flip-flop
outputs to the timer's internal input. There are 9 possible settings,
bypass, 2, 4, 8, 16, 32, 64, 128, and 256. (In the bypass mode, the
prescaler extends the period of the WDT instead.)
The ripple-counter design of the prescaler makes it impractical for the
PIC logic to read the prescaler value into the CPU since all the bits do
not change at the same time. Thus there is no way to directly read the
count in the prescaler. There are ways to deduce the value in the
prescaler but not directly. On the other hand, since the prescaler need
not be synchronized with the PIC clocks, it will operate at up to 50 MHz
regardless of the PIC clock rate, even while the PIC is in sleep mode.
The output frequency from the prescaler must be compatible with the timer
register, i.e. it must be less than the PIC crystal frequency divided by
4, or the main timer register will mis-count.
The only instruction that affects the count in the prescaler is writing
to the TMR0 register, which will set it to 0. Therefore, this should be
avoided if long-term accuracy is required and the prescale count is not
known at the time of the write. (One of the old PIC application notes, a
digital clock, contains this mistake. The clock will run slower than
expected because the prescaler is cleared inadvertently). An accurate
count of the number of timer counts accumulated over a long time can be
maintianed either by using the timer0 interrupt (it is OK to reset the
timer during the ISR since the value in the prescaler was 0+latency time
at the start of the interrupt, thus there is no random error but the
possibilty of introducing a constant error into the process must be
accounted for), or by reading the timer0 value, subtracting the value
that was last read, and adding the difference to a register that
accumulates the number of time ticks that have occurred since the process
started. As long as the accumulator is updated before the timer0 has
counted more than 256 times, the accumulated count will be correct, and
writing to the timer (thus corrupting the prescaler) is not necessary.
More... (looser matching)
- Last day of these posts
- In 1997
, 1998 only
- New search...