On Wed, 21 May 1997 08:07:14 -0600 Scott Horton writes: >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? 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. -Mike