0% found this document useful (0 votes)
8 views28 pages

timer counter

Counter/timer hardware is essential in embedded systems for measuring time and counting events, with timers often used for elapsed time and counters for event counting. The Atmega328P features three timer/counters, each with different bit sizes and functionalities, including Normal, CTC, and Fast PWM modes. The document also outlines the operation theory, timer registers, and programming examples for setting up timers in various modes.

Uploaded by

Aksh Vashist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views28 pages

timer counter

Counter/timer hardware is essential in embedded systems for measuring time and counting events, with timers often used for elapsed time and counters for event counting. The Atmega328P features three timer/counters, each with different bit sizes and functionalities, including Normal, CTC, and Fast PWM modes. The document also outlines the operation theory, timer registers, and programming examples for setting up timers in various modes.

Uploaded by

Aksh Vashist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction to Counter/Timers

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.

• The timers are the heart of automation. How we could take in an


input, perform mathematical functions on the data and, perform an
action. Timers give us an extra level of control by giving us not
only control over what happens but when it happens.

• Counter/timer hardware is a crucial component of most embedded


systems. In some cases, a timer measures elapsed time (counting
processor clock ticks). In others, we want to count or time external
events. The names counter and timer can be used interchangeably
when talking about the hardware. The difference in terminology has
more to do with how the hardware is used in a given application.
• A timer is a specialized type of clock which is used to
measure time intervals. A timer that counts from zero
upwards for measuring time elapsed is often called
a stopwatch. It is a device that counts down from a
specified time interval and used to generate a time delay,
for example, an hourglass is a timer.

• A counter is a device that stores (and sometimes displays)


the number of times a particular event or process
occurred, with respect to a clock signal. It is used to count
the events happening outside the microcontroller. In
electronics, counters can be implemented quite easily
using register-type circuits such as a flip-flop.
Difference between a Timer and a Counter
The Atmega328P has a total of
Three timer/counters named Timer/counter 0, Timer/counter 1, and Timer/counter 2.
The first and last of these are both 8-bit timer/counters and have a maximum value of
255, while Timer/Counter 1 is 16 bits and its maximum is 65,535

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.

OCRn = [ (clock_speed / Prescaler_value) * Desired_time_in_Seconds ] - 1


Now for the bad news OCRn has to be a whole number. If you end up with a
decimal number it means that your desired timer will not be exact. The other
thing is that your number has to be able to fit into the register. So 255 for an 8bit
timers and 65535 for the 16bit timer.

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.)

• ICR1 - Input Capture Register (only for 16 bit timer)


• Normal Mode:

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
{
}

TIFR0 |= (1 << TOV0);


// reset the overflow flag
TIMER1 (16BIT PWM):
• Timer/Counter1 is the big daddy of timers. It can run in Normal mode (0xFFFF) and 2 CTC modes.
The difference between the 2 CTC modes that mode 4 uses the OCR1A register for its compare
value and mode 12 uses the ICR1 register.

• 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.

• In normal mode TOV2 can generate a Overflow


interrupt. In order to activate the timer1 overflow
interrupts you need to SET(1) the TOIE1 bit within the
TIMSK2 register.
In CTC mode OCIF2 can generate an interrupt when it
detects a compare match. In order to activate the timer1
CTC interrupt SET(1) the OCF2 bit within the TIMSK register.

You might also like