timer counter
timer counter
INTRODUCTION:
• Counter/timer hardware is a crucial component of most embedded
systems. In some cases a timer is needed to measure elapsed time;
in others we want to count or time some external events. Here's a
primer on the hardware.
Figure 1 The timer shown consists of a loadable 8-bit count register, an input clock signal,
and an output signal. Software loads the count register with an initial value between 0x00
and 0xFF. Each subsequent transition of the input clock signal increments that value.
When the 8-bit count overflows, the output signal is asserted. The output signal may
thereby trigger an interrupt at the processor or set a bit that the processor can read.
To restart the timer, software reloads the count register with the same or a different initial
value. If a counter is an up counter, it counts up from the initial value toward 0xFF. A down
counter counts down, toward 0x00.A typical counter will have some means to start the
counter running once it is loaded, usually by setting a bit in a control register.
This is not shown in the figure. A real counter would generally also provide a way for the
processor to read the current value of the count register at any time, over the data bus.
THEORY OF OPERATION:
The clock source (from the internal clock or an external source) sends pulses to
the prescaler which divides the pulses by a determined amount. This input is
sent to the control circuit which increments the TCNTn register.
When the register hits its TOP value it resets to 0 and sends a TOVn (timer
overflow) signal which could be used to trigger an interrupt.
Unfortunately, the AVR timer does process time in hours, minutes or seconds
(what we are use to). The prescaler.
Timer/Counter Registers
• TCNT0 – Timer/Counter Register (8-bit) (The actual timer value is stored here.)
• OCR0A – Output Compare Register A
• OCR0B – Output Compare Register B
• TCCR0A/B – Timer/Counter Control Registers(Prescaler is configured here.)
• TIMSK0 – Timer/Counter Interrupt Mask Register (To enable/disable timer
interrupts. )
• TOV interrupt Compare A&B interrupts
• TIFR0 – Timer/Counter Interrupt Flag Register (Indicates a pending timer
interrupt.)
When the prescaler receives a pulse from a clock cycle and passes it onto the Control
Logic. The Control Logic increments the TCNTn register by 1. When TCNTn hits the
TOP (0xFF in the 8 bit timers and 0xFFFF in the 16 bit timer) it overflows to 0 and
sets the TOVn bit in the TIFR register. Useful for generating interrupts every N time
units .Set TCNT0 to an initial value (255 – N) .
The problem with Normal Mode is that it is very hard to use for an exact interval,
because of this Normal Mode is only useful if you need a none-specific time
interval (say you don't care if it happens every 1 or 2 ms as long as it happens at
the same time each time) its nice and easy option. Because, of this limitation
many programmers choose to use CTC mode for their timers.
• CTC Mode :
CTC stands for "Clear Timer on Compare" and it does the following. When the
prescaler receives a pulse from a clock cycle and passes it onto the Control
Logic. The Control Logic increments the TCNTn register by 1. The TCNTn register is
compared to the OCRn register, when a compare match occurs the TOVn bit is set
in the TIFR register.
• OCF0A interrupt flag set when TCNT0 reset to 0
• Pin OC0A can be made to toggle when counter resets
• Generate output waveform
• Fast PWM Mode
Timer increments
Wraps around at 0xFF (3) or OCR0A (7)
Start again at 0
Pin OC0x generates waveform
Set (reset) when timer=0
Reset (set) when timer=OCR0x
Due to the high frequency of this mode is best used for DAC, fading LEDs,
rectification and Power regulation.
Clock select and timer frequency
• Different clock sources can be independently selected for
each timer.
• To calculate the timer frequency (e.g. 2Hz using timer1) you
need .
• CPU frequency: 16 MHz for Arduino UNO.
• Maximum timer counter value: 256 for 8bit timer, 65536 for
16bit.
• A prescaler value: either 256 or 1024.
• Divide CPU frequency with a prescaler (16000000 / 256 =
62500).
• Divide result through the desired frequency (62500 / 2Hz =
31250).
• Verify the result against the maximum timer counter value
(31250 < 65536 success). If fail, choose bigger prescaler
TIMER0 (8BIT PWM):
Unlike the ATmega8 the ATmega168/328's Timer0 does have a OCR0 register therefore it
is capable of running in normal and CTC mode.
TOV0 can generate a Timer Overflow interrupt. In order to activate the timer0 interrupts
you need to SET(1) the TOIE0 bit within the TIMSK register.
• Software:
ATmega168/328 Code:
// this code sets up a timer0 for 1ms @ 16Mhz clock cycle
// in order to function as a time delay at the begining of the main loop
// using no interrupts
#include <avr/io.h>
int main(void)
{
while (1)
{
// Set the Timer Mode to CTC
TCCR0A |= (1 << WGM01);
// Set the value that you want to count to
OCR0A = 0xF9;
// start the timer
TCCR0B |= (1 << CS01) | (1 << CS00);
// set prescaler to 64 and start the timer
while ( (TIFR0 & (1 << TOV0) ) > 0) // wait for the overflow event
{
}
• In normal mode TOV1 can generate a Overflow interrupt. In order to activate the timer1
overflow interrupts you need to SET(1) the TOIE1 bit within the TIMSK1 register.
In CTC (mode 4) mode OCIF1A can generate an interrupt when it detects a compare match. In
order to activate the timer1 CTC interrupt SET(1) the OCF1A bit within the TIMSK1 register.
•
• In CTC (mode 12) mode TICIE1 can generate an interrupt when it detects a compare match. In
order to activate the timer1 CTC interrupt SET(1) the TICIE1 bit within the TIMSK1 register.
•
•
TIMER2 (8BIT PWM):
• Timer/Counter2 is the preferred timer among programmers
for short time delays because, its prescaler has the greatest
number of options . It can run in Normal mode or CTC
modes.