In the documentation, it shows that the higher frequency that can be reached with an Arduino UNO for and 8bits PWM is using timer0 with the pre-scaler set at 1. That gives a Fs of 62500 Hz, but why is that so?
I understand the Prescaler is dividing the frequency, but why it does start at 62500 Hz and timer1 and timer2 have as maximum 31372 Hz.
1 Answer 1
Timers 1 and 2 are configured by the Arduino core library to run the so called "phase correct PWM" mode. In this mode, the timers count forward from 0 to 255, and then they count backwards to zero. Timer 0 is configured by the same library in "fast PWM" mode, where it counts from 0 to 255 and then overflows back to zero in a single timer cycle.
You can, if you want, configure timers 1 and 2 in fast PWM mode. For doing this, you will have to access their I/O registers directly. C.f. the datasheet of the relevant microcontroller for details.
A couple of links:
- the datasheet of the ATmega328P microcontroller describes all the possible configurations of the timers (sections 15, 16 and 18 for timer 0, 1 and 2, respectively)
- the source code of the Arduino init() function to see how Arduino core configures them.
-
Where is that function called? is it in the bootloader?Luis Ramon Ramirez Rodriguez– Luis Ramon Ramirez Rodriguez2016年05月28日 20:08:04 +00:00Commented May 28, 2016 at 20:08
-
1@LuisRamonRamirezRodriguez: No,
init()
is called bymain()
.Edgar Bonet– Edgar Bonet2016年05月28日 20:28:03 +00:00Commented May 28, 2016 at 20:28