0% found this document useful (0 votes)
30 views

Introduction to Timers

The document provides an overview of timers and counters in microcontrollers, specifically focusing on the Atmega328P, which features three timers: Timer0, Timer1, and Timer2. It explains the basic functionality of timers, including modes of operation such as Normal, CTC, Fast PWM, and Phase Correct PWM, as well as the use of prescalars to adjust clock frequencies. Additionally, it discusses the role of timers in Arduino programming, highlighting their importance in functions like delay and PWM control.

Uploaded by

Aksh Vashist
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Introduction to Timers

The document provides an overview of timers and counters in microcontrollers, specifically focusing on the Atmega328P, which features three timers: Timer0, Timer1, and Timer2. It explains the basic functionality of timers, including modes of operation such as Normal, CTC, Fast PWM, and Phase Correct PWM, as well as the use of prescalars to adjust clock frequencies. Additionally, it discusses the role of timers in Arduino programming, highlighting their importance in functions like delay and PWM control.

Uploaded by

Aksh Vashist
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to Timers /Counter

Figure 1 shows a simple timer similar to those often included on-chip within a microcontroller.
You could build something similar from a couple of 74HC161 counters or a programmable
logic device. 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.
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

TIMER is used as time delay generator/calculate delay.

While COUNTER –

• Count External Events.

• External input from input pin to count the number of events on register.

Ex: The number of people passing through an entrance, the number of wheel rotations, other event
that can be converted to pulses.

Timers

As an Arduino programmer, for sure you have used timers and interrupts without even knowing.
That’s because all the low-level hardware stuff is hidden by the Arduino functions which are already
premade. A lot of Arduino functions uses timers, for example the time functions: delay, millis, micros
and delay Microseconds. They all use the Arduino timers in the background. Other functions such as
the PWM analogWrite also uses timers. The same for the tone and the noTone functions and even
the Servo library. So, what is this timer?

A timer is a piece of hardware builtin the Arduino controller and depending on the model, it could
have different amount of timers. For example, the Arduino UNO has 3 timers, Timer0, Tmer1 and
Timer2. This is like a clock, and can be used to measure time events. The timer can be programmed
by some special registers so is like programming a clock.
• A Functional block diagram of an eight bit unit is shown in Figure First, there are a
couple of registers used to program the operation of the block.
• These are TCCRnA and TCCRnB (Timer Counter Control Registers A and B) where n
is the timer/counter number (0, 1 or 2 here, although other microcontrollers in the series
may have more).
• These bits are usually “set and forget”, that is, they are set/cleared for a given use and
not touched again unless the unit needs to be reprogrammed for a different use. More
detail on these will be presented momentarily.
• There are also two registers that are used with software interrupts, TIFRn and TIMSKn
(Timer Interrupt Flag Register and Timer Interrupt MaSK register) that aren’t shown
here. We will examine these in the section on software interrupts.
• The other key registers are TCNTx (Timer CouNT) along with OCRnA.

• Its basic operation the unit increments the TCNTn register with each tick of the system
clock.
• Eventually, this register will reach its maximum value and overflow, resulting in zero,
effectively resetting the register and the count continues up from there.
• This maximum value is 255 for an eight bit unit (i.e., 11111111 binary) and 65535 for
a 16 bit unit.
• Optionally, the unit may be programmed to inspect the values contained in the OCRn
registers and compare them to the current contents of TCNTn to see if they match.
• Both the overflow and the compare match can be used trigger some action, e.g., a
waveform level change or software interrupt.
• The compare match section feeds a pulse waveform generator that in turn feeds an
output pin.

Timer is an 8/16 bit register that keeps on increasing its value, so one of the basic conditions is the
situation when timer register OVERFLOWS i.e. it has counted up to its maximum value (255 for 8 BIT
timers) and rolled back to 0. In this situation timer can issue an interrupt and you must write an
Interrupt Service Routine (ISR) to handle the event. There are three different timers available in
Atmega328 and all the timers work in almost same way. They are TIMER0 (8 Bit), TIMER1 (16 Bit)
and
TIMER2 (8Bit).

TIMER0 and TIMER1 can be used as a counter but TIMER2 can not be used as counter.

TIMER0 (8Bit) can be load with


2^8 -1=256-1=255 =FFH

TIMER1 (16Bit) can be load with


2^16 -1=65536-1=65535=FFFFH

TIMER2 (8Bit) can be load with


2^8- 1=256-1=255= FFH

Prescalar of CPU Clock


The Prescalar is a mechanism for generating clock for timer by CPU clock.
Atmega has clocks of several frequencies such as 1 MHz, 8 MHz, 12 MHz, 16 MHz (max). The
Prescalar is used to divide this clock frequency and produce a clock for TIMER.

The Prescalar can be set to produce the following types of clocks:

No Clock(Timer stop)
No prescaling (clock frequency = CPU frequency)
FCPU/8
FCPU/64
FCPU/256
FCPU/1024
External clock

TIMER MODE

Timers are usually used in one of the following modes:

Normal

CTC

Fast PWM

Phase correct PWM

Normal Mode

A timer running in normal mode will count up to its maximum value. When it reaches this maximum
value, it issues an Overflow interrupt and resets the value of the timer to its original value.

In the above case, you can see that the time period is 256 times the time period of the clock. 255
clock cycles are required to attain the maximum value and one clock cycle to clear the timer value.

So, ftimer = fclock / 256

This mode has its limitations. We are confined to a very small set of values of frequency for the
timer. This limitation is overcome by the compare mode.

CTC Mode (Clear Timer on Compare Match)

Compare mode makes use of a register known as the Output Compare Register which stores a value
of our choice. The timer continuously compares its current value with the value on the register and
when the two values match, the timer resets itself to 0.

In that case, the output pin will remain high for one time period of the timer and will remain low for
another time period.
Fast PWM mode

The Fast PWM mode is based on single-slope operation. In single slope operation, the register
TCNTn counts from bottom value to maximum value and its value resets to zero. The counting starts
again from bottom.

Phase Correct PWM Mode

This mode is very similar to the Fast PWM mode except that whenever the value of the timer
reaches its maximum value then instead of clearing the value of the timer it simply starts counting
down.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy