Timers And Interrupts On The ATMEGA168 Microcontroller
There are three timers available to you on the ATmega168. There is one 16-bit timer, called Counter 1 and two 8-bit timers called Counter 0 and Counter 2. The 16-bit timer can count up from 0 to 65,535 (Which in Binary is 11111111 11111111). The 8-bit timers can only count up from 0 to 255 (Which in Binary is 11111111).
How to Setup the 8-bit Timers Counter 0 and Counter 2
There are seven registers to each 8-bit timer that you will need to look at. The table below shows both 8-bit Counters and there related registers with descriptions of what each register stands for. Each register handles different aspects of the timer. Below we will go through what they are called and what each register is used for. As you will notice, Counter 0 and Counter 2 registers look very similar. The difference being whether it contains a 0 or a 2. This allows you to easly identify which register belongs to which Counter.
8-bit Timer Registers
Counter 0
Description
Counter 2
TCCR0A
Timer/Counter Control Register A
TCCR2A
TCCR0B
Timer/Counter Control Register B
TCCR2B
TIMSK0
Timer/Counter Interrupt Mask Register
TIMSK2
TIFR0
Timer/Counter Interrupt Flag Register
TIFR2
TCNT0
Timer/Counter Register (The Timer)
TCNT2
OCR0A
Output Compare Register A
OCR2A
OCR0B
Output Compare Register B
OCR2B
8-bit Timer Register Tables For Counter 0 & Counter 2
TCCR0A
COM0A1
COM0A0
COM0B1
COM0B0
-
-
WGM01
WGM00
TCCR2A
COM2A1
COM2A0
COM2B1
COM2B0
-
-
WGM21
WGM20
Read/Write
R/W
R/W
R/W
R/W
R
R
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores Compare Output settings and 2 of the 3 Waveform Generation Mode settings.
TCCR0B
FOC0A
FOC0B
-
-
WGM02
CS02
CS01
CS00
TCCR2B
FOC2A
FOC2B
-
-
WGM22
CS22
CS21
CS20
Read/Write
W
W
R
R
R/W
R/W
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores A and B Force Output Compare bits, 1 of the 3 Waveform Generation Mode settings, and the Clock Speed Selection settings.
TIMSK0
-
-
-
-
-
OCIE0B
OCIE0A
TOIE0
TIMSK2
-
-
-
-
-
OCIE2B
OCIE2A
TOIE2
Read/Write
R
R
R
R
R
R/W
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores the Output Compare Match Interrupt Enable A and B bits and Timer Overflow Interrupt Enable bit
TIFR0
-
-
-
-
-
OCF0B
OCF0A
TOV0
TIFR2
-
-
-
-
-
OCF2B
OCF2A
TOV2
Read/Write
R
R
R
R
R
R/W
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores the Output Compare Match Flags A and B and Timer Overflow Interrupt flag
Looking at registers TCCR0A and TCCR0B, we will start by setting bits, WGM02, WGM01 and WGM00 for Counter 0 and WGM22, WGM21 and WGM20 for Counter 2. These bits are used for setting what is called the Waveform Generation Mode (WGM). The table below shows you each mode that you can set your timer for and which bits you need to set to get the required mode. (Normal is set by default)
(PWM Type explaned here http://appusajeev.wordpress.com/2010/09/30/pwm-in-avr/)
Waveform Generation Mode (WGM):
Mode
WGM02
WGM01
WGM00
Description ( Counter 0 )
TOP
Mode
WGM22
WGM21
WGM20
Description ( Counter 2 )
TOP
0
0
0
0
Normal
0XFF
1
0
0
1
Phase Correct Pulse Width Modulation (PWM)
0XFF
2
0
1
0
Clear Timer on Compare (CTC)
OCRA
3
0
1
1
Fast Pulse Width Modulation (PWM)
0XFF
4
1
0
0
Reserved
-
5
1
0
1
Phase Correct Pulse Width Modulation (PWM)
OCRA
6
1
1
0
Reserved
-
7
1
1
1
Fast Pulse Width Modulation (PWM)
OCRA
Now that you have picked the WGM mode you want (Normal, CTC, Phase Correct PWM, or Fast PWM), you will need to set the Compare Output Mode using one of the three sets of tables below. By default, it is set to normal port operation.
Compare Output Mode, Using non-PWM Mode (Normal / CTC)
COM0A1
COM0A0
Description ( Counter 0 )
COM2A1
COM2A0
Description ( Counter 2 )
0
0
Normal port operation, OC0A / OC2A disconnected.
0
1
Toggle OC0A / OC2A on Compare Match
1
0
Clear OC0A / OC2A on Compare Match
1
1
Set OC0A / OC2A on Compare Match
COM0B1
COM0B0
Description ( Counter 0 )
COM2B1
COM2B0
Description ( Counter 2 )
0
0
Normal port operation, OC0B / OC2B disconnected.
0
1
Toggle OC0B / OC2B on Compare Match
1
0
Clear OC0B / OC2B on Compare Match
1
1
Set OC0B / OC2B on Compare Match
Compare Output Mode, Using Fast PWM Pulse Width Modulation Mode
COM0A1
COM0A0
Description ( Counter 0 )
COM2A1
COM2A0
Description ( Counter 2 )
0
0
Normal port operation, OC0A / OC2A disconnected.
0
1
WGM02 / WGM22 = 0: Normal Port Operation, OC0A / OC2A Disconnected. WGM02 / WGM22 = 1: Toggle OC0A / OC2A on Compare Match.
1
0
Clear OC0A / OC2A on Compare MatchSet OC0A / OC2A at BOTTOM (non-inverting mode)
1
1
Set OC0A / OC2A on Compare MatchClear OC0A / OC2A at BOTTOM (inverting mode)
COM0B1
COM0B0
Description ( Counter 0 )
COM2B1
COM2B0
Description ( Counter 2 )
0
0
Normal port operation, OC0B / OC2B disconnected.
0
1
Reserved
1
0
Clear OC0B / OC2B on Compare MatchSet OC0B / OC2B at BOTTOM (non-inverting mode)
1
1
Set OC0B / OC2B on Compare MatchClear OC0B / OC2B at BOTTOM (inverting mode)
Compare Output Mode, Using Phase Correct PWM Mode
COM0A1
COM0A0
Description ( Counter 0 )
COM2A1
COM2A0
Description ( Counter 2 )
0
0
Normal port operation, OC0A / OC2A disconnected.
0
1
WGM22 = 0: Normal Port Operation, OC0A / OC2A Disconnected. WGM22 = 1: Toggle OC0A / OC2A on Compare Match.
1
0
Clear OC0A / OC2A on Compare Match when up-counting. Set OC0A / OC2A on Compare Match when down-counting
1
1
Set OC0A / OC2A on Compare Match when up-counting. Clear OC0A / OC2A on Compare Match when down-counting.
COM0B1
COM0B0
Description ( Counter 0 )
COM2B1
COM2B0
Description ( Counter 2 )
0
0
Normal port operation, OC0B / OC2B disconnected.
0
1
Reserved
1
0
Clear OC0B / OC2B on Compare Match when up-counting. Set OC0B / OC2B on Compare Match when down-counting
1
1
Set OC0B / OC2B on Compare Match when up-counting. Clear OC0B / OC2B on Compare Match when down-counting.
Note: When not using normal mode, you will need to set the Data Direction Register (DDR) bit corresponding to the Counters output pin you want to use.
Counter 0 in register TCCR0A
Counter 2 in register TCCR2A
COM0A1 & COM0A0
COM0B1 & COM0B0
COM2A1 & COM2A0
COM2B1 & COM2B0
OC0A is PD6 (pin 6 on Port D)
OC0B is PD5 (pin 5 on Port D)
OC2A is PB3 (pin 3 on Port B)
OC2B is PD3 (pin 3 on Port D)
Selecting the Speed at Which The Timer Runs At
By default the ATmega168 runs at 8MHz with the system clock. This means that the timer can be updated, up to 8,000,000 times per second. By setting bits CS02, CS01 and CS00 (that are in register TCCR0B for Counter 0), or setting bits CS22, CS21 and CS20 (that are in register TCCR2B for Counter 2), using the table below you can set the clock from off, which is the default setting, to the full 8mHz or slow down this rate with a prescaler.
Clock Speed Selection
CS02
CS01
CS00
Description ( Counter 0 )
CS22
CS21
CS20
Description ( Counter 2 )
0
0
0
No clock source (Timer/Counter stopped)
0
0
1
Clock (No prescaling)
0
1
0
Clock/8 (From prescaler)
0
1
1
Clock/64 (From prescaler)
1
0
0
Clock/256 (From prescaler)
1
0
1
Clock/1024 (From prescaler)
1
1
0
External clock source on T0 pin. Clock on falling edge.
1
1
1
External clock source on T0 pin. Clock on rising edge.
Optional Bit Settings
FOC0A / FOC0B Force Output Compare A and B (that are in register TCCR0B Counter 0)
FOC2A / FOC2B Force Output Compare A and B (that are in register TCCR2B Counter 2)
The FOC (FOC0A, FOC0B, FOC2A, FOC2B) bits are only active when the Waveform Generation Mode (WGM) is set to a non-PWM setting. However, when operating in PWM mode this bit must be set to zero when TCCR0B / TCCR2B is written to ensure compatibility with future devices. When the FOC bit is set to one, an immediate Compare Match is forced on the Waveform Generation unit. The FOC output is changed according to the settings of the Output Compare Match bits. The FOC bit is implemented as a strobe. Therefore the value present in the Output Compare Match bits determines the effect of the forced compare. A FOC strobe will not generate any interrupt, nor will it clear the timer in Clear Timer on Compare (CTC) mode using Output Compare Register as TOP. The FOC bit is always read as zero.
OCIE0A / OCIE2A Timer/Counter Output Compare Match Interrupt Enable A
When the OCIE0A / OCIE2A bit is set to one the Timer/Counter Compare Match Interrupt A is enabled.
Once enabled, an interrupt will execute when a Compare Match in Timer/Counter occurs.
(When the OCF0A / OCF2A bit is set in the Timer/Counter Interrupt Flag Register TIFR0 / TIFR2)
OCIE0B / OCIE2B Timer/Counter Output Compare Match Interrupt Enable B
When the OCIE0B / OCIE2B bit is set to one, the Timer/Counter Compare Match Interrupt B is enabled.
Once enabled, an interrupt will execute when a Compare Match in Timer/Counter occurs.
(When the OCF0B / OCF2B bit is set in the Timer/Counter Interrupt Flag Register TIFR0 / TIFR2)
TOIE0 / TOIE2 Timer/Counter Overflow Interrupt Enable
When the TOIE0 / TOIE2 bit is set to one, the Timer/Counter Overflow interrupt is enabled.
Once enabled, an interrupt will execute when an overflow in Timer/Counter occurs.
(When the TOV0 / TOV2 bit is set in the Timer/Counter Interrupt Flag Register TIFR0 / TIFR2)
Interrupt Flag Register (Register TIFR0 / TIFR2)
OCF0A / OCF2A Timer/Counter Output Compare Match Flag A
The OCF0A / OCF2A bit is set when a Compare Match occurs between the Timer/Counter and the data in
OCR0A / OCR2A (Output Compare Register A). OCF0A / OCF2A is cleared by hardware when executing the corresponding interrupt. OCF0A / OCF2A can also be cleared by writing a logic one to the flag.
OCF0B / OCF2B Timer/Counter Output Compare Match Flag B
The OCF0B / OCF2B bit is set when a Compare Match occurs between the Timer/Counter and the data in
OCR0B / OCR2B (Output Compare Register B). OCF0B / OCF2B is cleared by hardware when executing the corresponding interrupt. OCF0B / OCF2B can also be cleared by writing a logic one to the flag.
TOV0 / TOV2 Timer/Counter Overflow Flag
The TOV0 / TOV2 bit is set when an overflow occurs in the Timer/Counter. TOV0 / TOV2 is cleared by hardware when executing the corresponding interrupt. TOV0 / TOV2 can also be cleared by writing a logic one to the flag.
Output Compare Registers
Counter 0 has two Output Compare Registers, A (OCR0A) and B (OCR0B).
Counter 2 has two Output Compare Registers, A (OCR2A) and B (OCR2B).
In each of these registers you set the valve you would like the Counter to generate an interrupt at.
How to Setup the 16-bit Timer Counter 1
There are thirteen registers to the 16-bit timer that you will need to look at. The table below shows the 16-bit Counter and its related registers with descriptions of what each register stands for. Each register handles different aspects of the timer. Below we will go through what they are called and what each register is used for. As you will notice, Counter 1 looks similar to the 8-bit timer registers.
16-bit Timer Registers
Counter 1
Description
TCCR1A
Timer/Counter Control Register A
TCCR1B
Timer/Counter Control Register B
TCCR1C
Timer/Counter Control Register C
ICR1H
Input Capture Register 1 High
ICR1L
Input Capture Register 1 Low
TIMSK1
Timer/Counter Interrupt Mask Register
TIFR1
Timer/Counter Interrupt Flag Register
TCNT1H
Timer/Counter Register High (The Timer - High)
TCNT1L
Timer/Counter Register Low (The Timer - Low)
OCR1AH
Output Compare Register A High
OCR1AL
Output Compare Register A Low
OCR1BH
Output Compare Register B High
OCR1BL
Output Compare Register B Low
16-bit Timer Register Tables For Counter 1
TCCR1A
COM1A1
COM1A0
COM1B1
COM1B0
-
-
WGM11
WGM10
Read/Write
R/W
R/W
R/W
R/W
R
R
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores Compare Output settings and 2 of the 4 Waveform Generation Mode settings.
TCCR1B
ICNC1
ICES1
-
WGM13
WGM12
CS12
CS11
CS10
Read/Write
W
W
R
R/W
R/W
R/W
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores the Input Capture Noise Canceller and Input Capture Edge Select bits, 2 of the 4 Waveform Generation Mode settings, and the Clock Speed Selection settings.
TCCR1C
FOC1A
FOC1B
-
-
-
-
-
-
Read/Write
W
W
R
R
R
R
R
R
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores A and B Force Output Compare bits.
TIMSK1
-
-
ICIE1
-
-
OCIE1B
OCIE1A
TOIE1
Read/Write
R
R
R/W
R
R
R/W
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores the Input Capture Interrupt Enable bit, the Output Compare Match Interrupt Enable A and B bits and the Timer Overflow Interrupt Enable bit
TIFR1
-
-
ICF1
-
-
OCF1B
OCF1A
TOV1
Read/Write
R
R
R/W
R
R
R/W
R/W
R/W
Initial Value
0
0
0
0
0
0
0
0
Bit
7
6
5
4
3
2
1
0
Stores the Input Capture Flag, the Output Compare Match Flags A and B and the Timer Overflow Interrupt flag
Looking at registers TCCR1A and TCCR1B, we will start by setting bits, WGM10, WGM11, WGM12, and WGM13. These bits are used for setting what is called the Waveform Generation Mode (WGM). The table below shows you each mode that you can set your 16-bit timer for and which bits you need to set to get the required mode. (Normal is set by default)
(PWM Type explaned here http://appusajeev.wordpress.com/2010/09/30/pwm-in-avr/)
Pulse Width Modulation (PWM), Phase and Frequency Correct
ICR1
9
1
0
0
1
Pulse Width Modulation (PWM), Phase and Frequency Correct
OCR1A
10
1
0
1
0
Pulse Width Modulation (PWM), Phase Correct
ICR1
11
1
0
1
1
Pulse Width Modulation (PWM), Phase Correct
OCR1A
12
1
1
0
0
Clear Timer on Compare (CTC)
ICR1
13
1
1
0
1
(Reserved)
-
14
1
1
1
0
Fast Pulse Width Modulation (PWM)
ICR1
15
1
1
1
1
Fast Pulse Width Modulation (PWM)
OCR1A
Now that you have picked the WGM mode you want, you will need to set the Compare Output Mode using one of the three sets of tables below. By default, it is set to normal port operation.
Compare Output Mode, non-PWM
COM1A1/COM1B1
COM1A0/COM1B0
Description
0
0
Normal port operation, OC1A/OC1B disconnected.
0
1
Toggle OC1A / OC1B on Compare Match.
1
0
Clear OC1A / OC1B on Compare Match (Set output to low level).
1
1
Set OC1A / OC1B on Compare Match (Set output to high level).
Compare Output Mode, Fast PWM
COM1A1/COM1B1
COM1A0/COM1B0
Description
0
0
Normal port operation, OC1A/OC1B disconnected.
0
1
WGM13:0 = 14 or 15: Toggle OC1A on Compare Match, OC1B disconnected (normal port operation). For all other WGM1 settings, normal port operation, OC1A/OC1B disconnected.
1
0
Clear OC1A/OC1B on Compare Match,set OC1A/OC1B at BOTTOM (non-inverting mode)
1
1
Set OC1A/OC1B on Compare Match,clear OC1A/OC1B at BOTTOM (inverting mode)
Compare Output Mode, Phase Correct and Phase and Frequency Correct PWM
COM1A1/COM1B1
COM1A0/COM1B0
Description
0
0
Normal port operation, OC1A/OC1B disconnected.
0
1
WGM13:0 = 9 or 11: Toggle OC1A on Compare Match, OC1B disconnected (normal port operation). For all other WGM1 settings, normal port operation, OC1A/OC1B disconnected.
1
0
Clear OC1A/OC1B on Compare Match when up counting. Set OC1A/OC1B on Compare Match when down counting.
1
1
Set OC1A/OC1B on Compare Match when up counting. Clear OC1A/OC1B on Compare Match when down counting.
Note: When not using normal mode, you will need to set the Data Direction Register (DDR) bit corresponding to the Counters output pin you want to use.
Counter 1 in register TCCR1A
COM1A1 & COM1A0
COM1B1 & COM1B0
OC1A is PB1 (pin 1 on Port B)
OC1B is PD2 (pin 2 on Port B)
Selecting the Speed at Which The Timer Runs At
By default the ATmega168 runs at 8MHz with the system clock. This means that the timer can be updated, up to 8,000,000 times per second. By setting bits CS12, CS11 and CS10 that are in register TCCR1B, using the table below you can set the clock from off, which is the default setting, to the full 8mHz or slow down this rate with a prescaler.
Clock Speed Selection
CS12
CS11
CS10
Description ( Counter 1 )
0
0
0
No clock source (Timer/Counter stopped)
0
0
1
Clock (No prescaling)
0
1
0
Clock/8 (From prescaler)
0
1
1
Clock/64 (From prescaler)
1
0
0
Clock/256 (From prescaler)
1
0
1
Clock/1024 (From prescaler)
1
1
0
External clock source on T0 pin. Clock on falling edge.
1
1
1
External clock source on T0 pin. Clock on rising edge.
Optional Bit Settings That are In Register TCCR1B
ICNC1: Input Capture Noise Canceller (Bit 7 of register TCCR1B)
Setting this bit to one activates the Input Capture Noise Canceller. When the noise canceller is activated, the input from the Input Capture pin (ICP1 - PB0 of Port B) is filtered. The filter function requires four successive equal valued samples of the ICP1 pin for changing its output. The Input Capture is therefore delayed by four Oscillator cycles.
ICES1: Input Capture Edge Select (Bit 6 of register TCCR1B)
This bit selects which edge on the Input Capture pin (ICP1 - PB0 of Port B) that is used to trigger a capture event. When the ICES1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is written to one, a rising (positive) edge will trigger the capture. When a capture is triggered according to the ICES1 setting, the counter value is copied into the Input Capture Register (ICR1). The event will also set the Input Capture Flag (ICF1), and this can be used to cause an Input Capture Interrupt, if enabled. When the ICR1 is used as the TOP value, if set Waveform Generation Mode (mode 8, 10, 12, 14) bits located in the TCCR1A and the TCCR1B Register), the ICP1 is disconnected and consequently the Input Capture function is disabled.
FOC1A / FOC1B Force Output Compare A and B (that are in register TCCR1C)
The FOC1A / FOC1B bits are only active when the Waveform Generation Mode (WGM) is set to a non-PWM setting. When writing a logical one to the FOC1A / FOC1B bit, an immediate compare match is forced on the Waveform Generation unit. The OC1A / OC1B output is changed according to its COM1A1 / COM1B1 bits setting. The FOC1A / FOC1B bits are implemented as strobes. Therefore it is the value present in the COM1A1 / COM1B1 bits that determine the effect of the forced compare. A FOC1A/FOC1B strobe will not generate any interrupt nor will it clear the timer in Clear Timer on Compare match (CTC) mode using OCR1A as TOP.
Timer/Counter Interrupts (Register TIMSK1)
ICIE1 Timer/Counter Input Capture Interrupt Enable
When the ICIE1 bit is written to one the Timer/Counter Input Capture interrupt is enabled. Once enabled, an interrupt will execute when the ICF1 Flag, located in TIFR1, is set.
OCIE1A Timer/Counter Output Compare Match Interrupt Enable A
When the OCIE1A bit is set to one the Timer/Counter Compare Match Interrupt A is enabled.
Once enabled, an interrupt will execute when a Compare Match in Timer/Counter occurs.
(When the OCF1A bit is set in the Timer/Counter Interrupt Flag Register TIFR1)
OCIE1B Timer/Counter Output Compare Match Interrupt Enable B
When the OCIE1B bit is set to one, the Timer/Counter Compare Match Interrupt B is enabled.
Once enabled, an interrupt will execute when a Compare Match in Timer/Counter occurs.
(When the OCF1B bit is set in the Timer/Counter Interrupt Flag Register TIFR1)
TOIE1 Timer/Counter Overflow Interrupt Enable
When the TOIE1 bit is set to one, the Timer/Counter Overflow interrupt is enabled.
Once enabled, an interrupt will execute when an overflow in Timer/Counter occurs.
(When the TOV1 bit is set in the Timer/Counter Interrupt Flag Register TIFR1)
Interrupt Flag Register (Register TIFR1)
ICF1 Timer/Counter Input Capture Flag
The ICF1 bit set when a capture event occurs on the ICP1 pin (PB0 of Port B). When the Input Capture Register
(ICR1) is set by the Waveform Generation Mode (mode 8, 10, 12, 14) to be used as the TOP value, the ICF1 flag is set when the counter reaches the TOP value. ICF1 is automatically cleared when the Input Capture Interrupt Vector is executed. ICF1 can also be cleared by writing a logic one to the flag.
OCF1A Timer/Counter Output Compare Match Flag A
The OCF1A bit is set when a Compare Match occurs between the Timer/Counter and the data in
OCR1AH & OCR1AL (Output Compare Register A). OCF1A is cleared by hardware when executing the corresponding interrupt. OCF1A can also be cleared by writing a logic one to the flag.
OCF1B Timer/Counter Output Compare Match Flag B
The OCF1B bit is set when a Compare Match occurs between the Timer/Counter and the data in
OCR1BH & OCR1BL (Output Compare Register B). OCF1B is cleared by hardware when executing the corresponding interrupt. OCF1B can also be cleared by writing a logic one to the flag.
TOV1 Timer/Counter Overflow Flag
The TOV1 bit is set when an overflow occurs in the Timer/Counter. TOV1 is cleared by hardware when executing the corresponding interrupt. TOV1 can also be cleared by writing a logic one to the flag.
Now For The Software End Of Things
The ISR(vector_name) Macro ISR stands for Interrupt Service Routine
Vector name
Old vector name
Description
ADC_vect
SIG_ADC
ADC Conversion Complete
ANALOG_COMP_vect
SIG_COMPARATOR
Analog Comparator
EE_READY_vect
SIG_EEPROM_READY
EEPROM Ready
INT0_vect
SIG_INTERRUPT0
External Interrupt 0
INT1_vect
SIG_INTERRUPT1
External Interrupt Request 1
PCINT0_vect
SIG_PIN_CHANGE0
Pin Change Interrupt Request 0
PCINT1_vect
SIG_PIN_CHANGE1
Pin Change Interrupt Request 1
PCINT2_vect
SIG_PIN_CHANGE2
Pin Change Interrupt Request 2
SPI_STC_vect
SIG_SPI
Serial Transfer Complete
SPM_READY_vect
SIG_SPM_READY
Store Program Memory Read
TIMER0_COMPA_vect
SIG_OUTPUT_COMPARE0A
Timer/Counter0 Compare Match A
TIMER0_COMPB_vect
SIG_OUTPUT_COMPARE0B
Timer/Counter0 Compare Match B
TIMER0_OVF_vect
SIG_OVERFLOW0
Timer/Counter0 Overflow
TIMER1_CAPT_vect
SIG_INPUT_CAPTURE1
Timer/Counter Capture Event
TIMER1_COMPA_vect
SIG_OUTPUT_COMPARE1A
Timer/Counter1 Compare Match A
TIMER1_COMPB_vect
SIG_OUTPUT_COMPARE1B
Timer/Counter1 Compare Match B
TIMER1_OVF_vect
SIG_OVERFLOW1
Timer/Counter1 Overflow
TIMER2_COMPA_vect
SIG_OUTPUT_COMPARE2A
Timer/Counter2 Compare Match A
TIMER2_COMPB_vect
SIG_OUTPUT_COMPARE2B
Timer/Counter2 Compare Match A
TIMER2_OVF_vect
SIG_OVERFLOW2
Timer/Counter2 Overflow
TWI_vect
SIG_2WIRE_SERIAL
2-wire Serial Interface
USART_RX_vect
SIG_USART_RECV, SIG_UART_RECV
USART, Rx Complete
USART_TX_vect
SIG_USART_TRANS, SIG_UART_TRANS
USART, Tx Complete
USART_UDRE_vect
SIG_USART_DATA, SIG_UART_DATA
USART Data Register Empty
WDT_vect
SIG_WDT, SIG_WATCHDOG_TIMEOUT
Watchdog Timeout Interrupt
Timer Counting Speed Table
How fast should the timer count, in relation to the clock speed of the microcontroller (Typically 8 MHz / 20 MHz)
Values for an 8 MHz clock
Prescaler
Duration of one timer tick
Frequency (timer ticks per second)
Time until overflow 16-bit
Time until overflow 8-bit
CLOCK_DIV_1
0.125 us ( 1 / 8000000 sec)
8000.0000 kHz (8000000 / 1 Hz)
8.192 ms
0.032ms
CLOCK_DIV_8
1.000 us ( 8 / 8000000 sec)
1000.0000 kHz (8000000 / 8 Hz)
65.536 ms
0.256ms
CLOCK_DIV_64
8.000 us ( 64 / 8000000 sec)
125.0000 kHz (8000000 / 64 Hz)
524.288 ms
2.048ms
CLOCK_DIV_256
32.000 us ( 256 / 8000000 sec)
31.2500 kHz (8000000 / 256 Hz)
2097.152 ms
8.192ms
CLOCK_DIV_1024
128.000 us (1024 / 8000000 sec)
7.8125 kHz (8000000 / 1024 Hz)
8388.608 ms
32.768ms
Values for a 20 MHz clock
Prescaler
Duration of one timer tick
Frequency (timer ticks per second)
Time until overflow 16-bit
Time until overflow 8-bit
CLOCK_DIV_1
0.05 us ( 1 / 20000000 sec)
20000.00000 kHz (20000000 / 1 Hz)
3.2768 ms
0.0128ms
CLOCK_DIV_8
0.40 us ( 8 / 20000000 sec)
2500.00000 kHz (20000000 / 8 Hz)
26.2144 ms
0.1024ms
CLOCK_DIV_64
3.20 us ( 64 / 20000000 sec)
312.50000 kHz (20000000 / 64 Hz)
209.7152 ms
0.8192ms
CLOCK_DIV_256
12.80 us ( 256 / 20000000 sec)
78.12500 kHz (20000000 / 256 Hz)
838.8608 ms
3.2768ms
CLOCK_DIV_1024
51.20 us (1024 / 20000000 sec)
19.53125 kHz (20000000 / 1024 Hz)
3355.4432 ms
13.1072ms
8-bit Timer Example - Hardware Controlled
8-bit Timer Example - Software Controlled
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void){//***************************************** Setup 8-bit Timer 0 *****************************************//
TCCR0A = 0b00000010;// |COM0A1|COM0A0|COM0B1|COM0B0|0|0|WGM01|WGM00|
// COMA and COMB are set to normal, OC0A and OC0B are disconnected respectively
// WGM is set to CTC - Clear Timer on Compare (of OCR0A) Mode#2 = 010
TCCR0B = 0b00000000;// |FOC0A|FOC0B|0|0|WGM02|CS02|CS01|CS00|
// FOC set to default - WGM02 set to 0 - Clock set to off
TIMSK0 = 0b00000110;// |0|0|0|0|0|OCIE0B|OCIE0A|TOIE0|
// Timer Output Compare Match Interrupt A and B are enabled
TIFR0 = 0b00000000;// |0|0|0|0|0|OCFOB|OCF0A|TOV0|
// Flags are set to 0 - Default settings
// IE If running at 1 mhz (1000000 ticks per second), and selection set to 1024.
// Duration of one timer tick - 1024/1000000 sec = 0.001024
OCR0A = 244;// Set to trip every 0.249856 sec (244 * 0.001024) Which is about 0.25sec, 1/4 of a second.
OCR0B = 122;// Set to trip every 0.124928 sec (122 * 0.001024) Which is about 0.125sec, 1/8 of a second.
// We need to set Port B's Data Direction Register for pin PB0 for output.
DDRB = DDRB | 0b00000001;// Leave all the other bits alone, just set bit 0 for output
sei();// Enables the global registers.
TCCR0B = 0b00000101;// |FOC0A|FOC0B|0|0|WGM02|CS02|CS01|CS00|
// Turn On Clock and set speed selection to Clock/1024
while(1){}}
ISR#TIMER0_COMPA_vect#
{
PORTB = PORTB & 0b11111110;// Turn off pin PB0 on Port B, leave all other bits alone.
}
ISR#TIMER0_COMPB_vect){
PORTB = PORTB | 0b00000001;// Turn on pin PB0 on Port B, leave all other bits alone.
}