05 AVR Timer-Counter AVR
05 AVR Timer-Counter AVR
AVR ATmega
INTERRUPTS
Interrupts
Definition of ‘Interrupt’
3
Interrupts
Program
time t
4
Interrupts
Interrupt
Program
time t
5
Interrupts
Interrupt
Program Program
time t
6
Interrupts in the ATmega
Many possible interrupt sources
One interrupt vector for each source
Interrupt vector table shown in datasheet
#include <avr/interrupt.h>
ISR(ADC_vect)
{
// user code here
}
EMPTY_INTERRUPT(vector_name)
- Defines an interrupt which does nothing.
Internal Interrupts
• ATmega328p has 20 internal interrupts.
• These internal interrupts are generated by the
internalperipherals of Microcontroller like Timer,
ADC, serial, eeprom etc.
• The internal interrupts are used for efficient
operation of the internal peripherals.
• They can be enabled through the registers of these
peripherals
External Interrupts
• Triggered by the INT pins or any of the PCINT pins. The
PinChange Interrupt Request 2 (PCI2) will trigger if any enabled
PCINT[23:16] pin toggles.
• The Pin Change Interrupt Request 1 (PCI1) will trigger if any
enabled PCINT[14:8] pin toggles.
• The Pin Change Interrupt Request 0 (PCI0) will trigger if any
enabled PCINT[7:0] pin toggles.The PCMSK2, PCMSK1 and
PCMSK0 Registers control which pins contribute to the
pinchange interrupts. Pin change interrupts on PCINT are
detected asynchronously.
• The External Interrupts can be triggered by a falling or rising
edge or a low level. This is set upas indicated in the
specification for the External Interrupt Control Register A
(EICRA
How to use interrupts
• Configure and enable the global interrupts. SREG I Bit
• Write functions called Interrupt Service Routine (ISR) to
handle interrupts. (defined outside the main function)
• Enable external interrupts locally in External Interrupt
Mask Register (EIMSK).
• Configure the interrupts for falling edge, rising edge, low
level or any logical change by using EICRA register.
• Enable the internal interrupts locally by writing 1 to
interrupt enable bit in the registersof the peripheral under
use. For example, ADC system consists of ADIE bit
inADCSRA register. ADIE bit is enabled to use ADC
interrupts
External Interrupts – eg. On INT0
Registers for External interrupt
• EICRA: External Interrupt Control Register A
• EIMSK: External Interrupt Mask Register
Timer/Counter
• Timer/Counter 0
• 8 bit
• Timer/Counter 1
• 16 bit
• Timer/Counter 2
• 8 bit
8-bit Timer/Counter0 with PWM
• Features
• Single Compare Unit Counter
• Clear Timer on Compare Match (Auto Reload)
• Phase Correct Pulse Width Modulator (PWM)
• Frequency Generator
• External Event Counter
• 10-bit Clock Prescaler (1, 8, 64, 256, 1024)
• Overflow and Compare Match Interrupt Sources (TOV0
and OCF0)
Setting Prescalar
How much prescalar is needed?
- Can the counter count for 1000ms?
- 16 bits, 65,536 is max value
- System clock period is 1/50kHz = 20 microseconds
- 65,536 * 20 microseconds = 1.31 seconds
- 1.31 sec > 1000ms, so no prescalar is needed
Setting Prescalar
How much prescalar is needed?
- Can the counter count for 1000ms?
- 16 bits, 65,536 is max value
- System clock period is 1/50kHz = 20 microseconds
- 65,536 * 20 microseconds = 1.31 seconds
- 1.31 sec > 1000ms, so no prescalar is needed
Timer0
Timer Registers
• The Timer/Counter (TCNT0) and Output Compare
Register (OCR0) are 8-bit registers.
• Interrupt request signals are all visible in the Timer
Interrupt Flag Register (TIFR).
• All interrupts are individually masked with the Timer
Interrupt Mask Register (TIMSK).
• TIFR and TIMSK are not shown in the figure since
these registers are shared by other timer units.
Registers
• The Timer/Counter can be clocked internally, via the
prescaler, or by an external clock source on the T0 pin.
• The Clock Select logic block controls which clock
source and edge the Timer/Counter uses to increment
(or decrement) its value.
• The Timer/Counter is inactive when no clock source is
selected.
• The output from the Clock Select logic is referred to as
the timer clock (clkT0)
Registers
• The double buffered Output Compare Register (OCR0)
is compared with the Timer/Counter value at all times
• The result of the compare can be used by the
waveform generator to generate a PWM or variable
frequency output on the Output Compare Pin (OC0)
• The compare match event will also set the Compare
Flag (OCF0) which can be used to generate an output
compare interrupt request
Registers used in
timers/counters
• TCCR0 – Timer/Counter Control Register
• TCNT0 – Timer/Counter Register
• OCR0 – Output Compare Register
• TIMSK – Timer/Counter Interrupt Mask Register
• TIFR – Timer/Counter Interrupt Flag Register
Mode Timer/Counter 0
• Normal
• CTC (Clear Timer on Compare Match)
• Fast PWM (Single Slope PWM)
• Phase Correct PWM (Double Slope PWM)
TCCR0 – Timer/Counter Control
Register
Bit 7 – FOC0: Force Output Compare
• The FOC0 bit is only active when the WGM00 bit specifies a
non-PWM mode
• However, for ensuring compatibility with future devices, this bit must
be set to zero when TCCR0 is written when operating in PWM mode.
• When writing a logical one to the FOC0 bit, an immediate
compare match is forced on the Waveform Generation unit.
• The OC0 output is changed according to its COM01:0 bits
setting. Note that the FOC0 bit is implemented as a strobe.
• Therefore it is the value present in the COM01:0 bits that determines
the effect of the forced compare.
• A FOC0 strobe will not generate any interrupt, nor will it clear
the timer in CTC mode using OCR0 as TOP
Bit 3, 6 – WGM0[1:0]: Waveform
Generation Mode
Bit 5:4 – COM01:0: Compare Match
Output Mode
• Pin OC0 , mode Fast PWM
• Pin OC0 , mode Phase Correct PWM
Bit 2:0 – CS02:0: Clock Select
• TimerClock = {Off, fc, fc/8, fc/64, fc/256, fc/1024}
TIMSK – Timer/Counter Interrupt
Mask Register
TIFR – Timer/Counter Interrupt Flag
Register
Normal Mode
• The simplest mode of operation is the normal mode
(WGM01:0 = 0).
• In this mode the counting direction is always up
(incrementing), and no counter clear is performed.
• The counter simply overruns when it passes its
maximum 8-bit value (TOP = 0xFF) and then restarts
from the bottom (0x00).
Normal Mode
• In normal operation the Timer/Counter Overflow
Flag (TOV0) will be set in the same timer clock cycle
as the TCNT0 becomes zero.
• The TOV0 Flag in this case behaves like a ninth bit,
except that it is only set, not cleared.
Clear Timer on Compare Match
(CTC) Mode
• In Clear Timer on Compare or CTC mode (WGM01:0
= 2), the OCR0 Register is used to manipulate the
counter resolution.
• In CTC mode the counter is cleared to zero when the
counter value (TCNT0) matches the OCR0
CTC Mode
• The OCR0 defines the top value for the counter,
hence also its resolution.
• This mode allows greater control of the compare
match output frequency
CTC Mode
• The timing diagram for the CTC mode is shown
below
• The counter value (TCNT0) increases until a
compare match occurs between TCNT0 and OCR0,
and then counter (TCNT0) is cleared
// in order to function as a time delay at the beginning of the main loop
// NOTE: 1ms = 0xF9 but we are going 0xFF, so we are actually creating a
// 1.024ms timer.
#include <avr/io.h>
int main(void)
{
while (1)
{
// start the timer
while ( (TIFR & (1 << TOV0) ) > 0) // wait for the overflow event
{
}