Wednesday, December 7, 2011

Microchip PIC – Build in timer modules

Timer is like a counter which increments with the time. It goes in a loop form 0 to MAX. So what is the difference between a counter and a timer? Both components count events. The distinction actually comes from how this result is used. For example, we could count the number of times that a pushbutton is pressed by connecting it to the input of a Timer/Counter module.

MAIN COMPONENTS OF A TIMER/COUNTER MODULE

 

clip_image002

First, a source is required. This could either be a synchronous clock provided by an oscillator or an event that is not so periodic such as a pushbutton connected to the Timerx Clock Input pin (TxCKI). Next, the current value will need to be stored somewhere. On the mid-range PIC microcontroller, this will be an 8 to 16-bit register. Finally, each of the three timer/counters has at least one scaler. These scalers (or we can call them frequency dividers) may come before (pre-scaler) or after (post-scaler) the timer/counter value register.



TIMER0

Timer0 is an 8-bit Timer/Counter module with the following features:
· 8-bit timer / counter, readable and writable, with 8-bit pre-scalar, mostly used as a timer.
· Internal or external clock with edge selectability on external clock.
· Internal clock is based on instruction clock rather than the oscillator directly.
· Interrupt on overflow

TIMER0 block diagram

clip_image004
To configure the Timer0 module the OPTION_REG Special Function Register (SFR) is used.
clip_image006

Simplified diagram of TIMER0 module

clip_image008

Identify different modules in the TIMER0

T0SE (Timer0 Source Edge Select bit)

clip_image010
· 1 = TMR0 register increments on high-to-low transition on T0CKI pin.
· 0 = TMR0 register increments on low-to-high transition on T0CKI pin.
clip_image012

Timer0 Clock Source Select bit

· 1 = Signal present on T0CKI (Timer0 Clock Input) pin used as Timer0 source
· 0 = Internal instruction cycle clock used as source
· Internal instruction cycle = (Microcontroller oscillator Frequency)/4
clip_image014

PSA: Prescaler Assignment bit

· 1 = Prescaler is assigned to the Watchdog Timer module.
· 0 = Prescaler is assigned to Timer0 module.
clip_image015
The prescaler will determine how many source edges will increment the TMR0 register value by 1. The Timer0 prescaler on the mid-range microcontrollers can be configured to increment the value in TMR0 from a 1:1 ratio to selected source edges or up to a ratio of 1:256.
clip_image017

How it works (overview)

clip_image018
Let us start from the end, at the end we can see a TMR0 register (which is a counter) which connects with an 8-bit data bus. This register can be written as well as read. This fulfills one requirement. This register counts through 0 to 255. When it is overloaded, it passes an interrupt and restarts the counter.
Before that, there is a “SYNC 2 Cycles” component. Microprocessor looks for interrupts at the end of the instruction; we know that instruction clock runs in lesser frequency so if we put an interrupt we need it to be undertaken by the processor immediately. Therefore, we have synchronized it.
Before that, there is a multiplexer, which will decide whether to take the direct source or scaled output. From here, our path divides into two. First, we will go towards the normal input then to scaled input. Going through this line, we meet another mux, which will decide whether to use Timer0 Clock Input or Internal instruction cycle clock, as source.
Let us move towards the prescaler this has three choices to work with, it can work with the source (‘Timer0 Clock Input’ or ‘internal instruction cycle clock’) or watchdog timer. Getting them as inputs, it scales them down accordingly.

Timer Resolution and range

Resolution basically refers to number of different values that can be stored in the timer. If the resistor is 8-bits, we can store 256 different values. We cannot change the resolution. Range refers to the number of clock pulses we can go through between two overload interrupts. Here if we go with normal input it is 256 but if we use prescaler to scale down by 1:256 it becomes 256x256. So our range is 256 – 65536. When we go for lesser frequencies, we loose some intermediates.

Counter as a frequency divider (How prescaler work)

We talked about this in earlier lesson there we used a comparator and reset to get frequencies other than powers of two. There we did not get a symmetric wave as the output.
clip_image020
We will get an excitation for very minor. That time equal to the time that it takes comparator signal to reach counter, soon it will be zero again and output will be 0. We can use a sync reset that will synchronize with clock and we will get the pulse as one for bit larger time (still it is smaller than one clock pulse).
Solution: Connect the output to a D flip-flop as the clock and give Q bar as input for D flip-flop.

TIMER1

clip_image022
· A 16-bit timer addressable via two 8-bit registers (TMR1L and TMR1H).
· Timer mode supports only internal clock, based on instruction clock.
· Can be operated as a 16 bit Counter – counting on an external clock input.
· Counter has a RESET input, often via one of the two CCP modules.
· Counter can operate either on synchronous or asynchronous mode.
· In synchronous mode, counter operation is synchronized with internal instruction clock.
clip_image024

TIMER2

clip_image026
· 8-bit timer with a pre-scalar and a post-scalar.
· Timer2 register (TM2) is readable and writable.
· Is also used as the time-base for PWM and CCP modules via the PR2 register (period generator).
clip_image028

Content

Email:

Syndicators/Readers:

FaceBook: