The ATmega328p microcontroller, widely used in the popular Arduino platform, features three timers: Timer 0, Timer 1, and Timer 2. These timers serve timing and counting purposes and are used for PWM signal generation, counting external events, sampling, motor control,delay generation etc. In this blog post, we will compare the features and formulas of these timers to understand their differences and when to use them.
Timer 0
Timer 0 is an 8-bit timer, which means it has a range of 0 to 255. It is the smaller of the three timers and can be used for basic tasks such as delay generation, tone generation, and event counting and tasks such as pulse width modulation (PWM), phase correct PWM, and event counting.
CTC Mode
The CTC mode frequency is given by,
\(F_{OC0x}=\frac{F_{clk}}{2 N (1+OCR0x)}\)
and the compare value is,
Where N is Prescaler (1, 8, 64, 256, or 1024) which is set in the TCCR0B register and OCR0x(x=A or B) is the value set in the Output Compare Register A or B. The \(F_{clk}\) is the clock frequency of the microcontroller’s internal peripheral control clock.
Fast PWM mode
The Fast PWM frequency for the output can be calculated by the following equation:
\(F_{OC0x}=\frac{F_{clk}}{256 N }\)
The N variable represents the prescale factor (1, 8, 64, 256, or 1024), where \(F_{OC1x}\) is the frequency of the fast PWM wave at pin OC1A or OC1B.
Phase Shift PWM
The formula for calculating the frequency of the Phase correct PWM is given by,
\(F_{OC0x}=\frac{F_{clk}}{510N }\)
The N variable represents
the prescale factor (1, 8, 64, 256, or 1024), where \(F_{OC0x}\) is the
frequency of the fast PWM wave at pin OC0A or OC0B.
Timer 1
Timer 1 is a 16-bit timer, which means it has a range of 0 to 65,535. It is the largest of the three timers and can be used for complex tasks such as pulse width modulation (PWM), phase correct PWM, and event counting.
CTC Mode
The formula to calculate the delay time for Timer1 is as follows:
The CTC mode frequency is given by,
\(F_{OC1x}=\frac{F_{clk}}{2 N (1+OCR1x)}\)
and the compare value is,
Where \(F_{OC1x}\) is the frequency of the CTC wave, N is Prescaler(1, 8, 64, 256, or 1024) which is set in the TCCR1B register, and OCR0x(x=A or B) is the value set in the Output Compare Register A or B. Again, the \(F_{clk}\) is the clock frequency of the microcontroller’s internal peripheral control clock.
Fast PWM mode
The Fast PWM frequency for the output can be calculated by the following equation:
\(F_{OC1x}=\frac{F_{clk}}{N(1+TOP) }\)
The N variable represents the prescale factor (1, 8, 64, 256, or 1024), where \(F_{OC1x}\) is the frequency of the fast PWM wave at pin OC1A or OC1B.
Phase Shift PWM and Frequency & Phase Correct PWM
The formula for calculating the frequency of the Phase correct PWM is given by,
\(F_{OC1x}=\frac{F_{clk}}{2N TOP }\)
The N variable represents the prescale factor (1, 8, 64, 256, or 1024), where \(F_{OC1x}\) is the frequency of the fast PWM wave at pin OC1A or OC1B.Timer 2
Timer 2 is an 8-bit timer, similar to Timer0, but with some additional features. It has a range of 0 to 255 and can be used for basic tasks such as delay generation, tone generation, and event counting.
CTC Mode
The formula to calculate the delay time for Timer1 is as follows:
The timer 2 CTC mode frequency is given by,
\(F_{OC2x}=\frac{F_{clk}}{2 N (1+OCR2x)}\)
and the compare value is,
Where N is Prescaler (1, 8, 32, 64, 128, 256, or 1024) which is set in the TCCR1B register and OCR2x(x=A or B) is the value set in the Output Compare Register A or B. Again, the \(F_{clk}\) is the clock frequency of the microcontroller’s internal peripheral control clock.
Fast PWM mode
The Fast PWM frequency for the output can be calculated by the following equation:
\(F_{OC2x}=\frac{F_{clk}}{256 N}\)
The N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024), where \(F_{OC1x}\) is the frequency of the fast PWM wave at pin OC1A or OC1B.
Phase Shift PWM
The formula for calculating the frequency of the Phase correct PWM is given by,
\(F_{OC1x}=\frac{F_{clk}}{2N TOP }\)
The N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024), where \(F_{OC1x}\) is the frequency of the fast PWM wave at pin OC1A or OC1B.- The formula for calculating the CTC mode frequency in all the three timers are the same
- The compare value or the count value formula in the CTC mode of all the three timers are the same
- The Fast PWM mode frequency formula for Timer 0 and Timer 2 are the same which is fw=fclk/256N while the Fast PWM mode frequency formula for Timer 1 depends on the TOP value which is given by, fw=fclk/(N(1+TOP))
- The Phase Correct mode frequency formula for Timer 0 and Timer 2 are the same which is fw=fclk/510N while the Fast PWM mode frequency formula for Timer 1 depends on the TOP value which is given by, fw=fclk/(2N(TOP))
- The Timer 0 and Timer 1 can have pre-scalar value N of (1, 8, 64, 256, or 1024) while the Timer 2 has more choice for pre-scalar value of (1, 8, 32, 64, 128, 256, or 1024). Thus more frequency can be chosen with Timer 2.
Conclusion
In conclusion, each of the three timers in the ATmega328p microcontroller serves a different purpose and has its own capabilities. Timer0 is a basic 8-bit timer suitable for simple tasks, Timer1 is a 16-bit timer suitable for complex tasks, and Timer2 is an 8-bit timer with additional features. The formulas used to calculate the delay time for each timer are similar, but the values used in the formulas differ based on the specific timer being used. By understanding these differences, you can choose the right timer for your specific project needs.
References:
1. AVR CTC mode
2. AVR Fast PWM
