Timer/Counter in Embedded System

In this article we are going to discuss about
1. What is Timer/Counter in embedded system.
2. Difference between timer and counter.
3. How to generate delay using timer
4. Applications of timer and counter.


1. Introduction :

Timer/counter are part of micro-controller internal hardware. Timer counter are same as per hardware point of view. Timer is used to generate delay and counter is for counting external events. There is small line between timer and counter which differentiate them. Timer/Counter module has internal counter register, which plays an important role.

When we want to count events, we connect external event (e.g. event : rising edge or falling edge) source to input pin of timer/counter module. When external event occurs the value of counter increments. Value of counter represents no. of external events. 





e.g. A IR sensor is connected to wheel of vehicle, to calculate speed to vehicle. For 1 rotation of wheel sensor gives 100 pulses. So in this case counter will come in picture and let us assume counter is configured to calculate events for rising edge. counter will calculate no. of input pulses.

Fig. 1 Counter Mechanism

When we want to generate time delays, we connect external oscillator to input pin of timer/counter module. At each tick (rising edge or falling edge) of clock (external oscillator), the value of counter increments and content of counter register represents time elapsed. As we know the frequency of oscillator we can calculate time elapsed from.

e.g. A external clock (5 KHz) is connected to micro-controller. In 1 sec it will gives 5000 pulses. Consider timer module is configured to count rising events. We want a delay of 5 seconds. So when counter reaches to 25000 count, a time of 5 seconds is elapsed i.e. a delay of 5 second is achieved

Fig. 2 Timer Mechanism


Difference between timer and Counter: 

External oscillator or event generator are input to timer/counter module and in case of timer or counter internal register value is incremented in response to external event. Now in case of counter when event occurs counter is incremented and in case of timer external oscillator is external event source and counter increments in response to event occurs. The difference is in case of timer we know when event is going to occur but in case of counter we don't. Otherwise both functionality is almost same and works on same principle to count event.



2. More About Timers


2.1 What is 8 bit/16 bit timer :


In micro-controller you will see 8 bit timer, 16 bit timer or 32 bit timer module. Now what is significance of them. In case of 8 bit timer, timer will count clock pulses maximum 255 and in case of 16 bit counter, timer will count maximum 65535 clock pulses. If clock of 1 KHz is connected to micro-controller, using 8 bit timer maximum delay can be achieved is 255 ms and using and using 16 bit timer 65.355 seconds.

How this calculation is done? 
Clock frequency is 1KHz and 1/1KHz=0.001 seconds=1ms
i.e. Period of 1 clock pulse is 1ms.
Therefore 255 pulses = 255ms 
and 65535 pulses =65.355 seconds


2.2 Prescaler of clock :


Prescaler is used to achieve a big delay,clock is multiplied  using prescaler value.  How? Let us consider above scenario with 8 bit timer and 1KHz frequency. Maximum delay that can be achieved is 255 ms. 

Now consider prescaler with value of 1/2.
New clock = 1 KHz/2 = 500 Hz.
So period of 1 tick equals to 2 ms (1/500Hz) and max delay achieved is 510 ms (2ms*255 pulses).
Now to achieve big delay increase prescaler value from available values.

fig. 3 Clock Prescaler


2.3 How to configure timer module in micro-controller


In micro-controller you will find timer module. Every timer module has counter register, whose count increments to rising edge of clock, and there is one more register,compare register, who hold the particular count value, that count value is for particular delay that user wants. So counter register will be continuously compared with compare register and when both matches you will get desired delay.  

e.g. Consider you are 8 bit timer module with frequency of 40Hz. Required delay is 100ms.
In this case Compare register will be loaded with value of 4.
As frequency is 40Hz, therefore 1pulse = 0.025 sec =25ms.

When counter register count value 4 then 100ms is delay is achieved and Flag register updated to indicate compare is done and match occurred also if interrupt is enabled on match then interrupt will get generated.


Fig. 4 Timer Working





1 comments:

Very well explained.
Thank you

Reply

Post a Comment