Set PWM prescaler to 1

Hello forum,

I read this article http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM but there is still something unclear for me.

For controlling a BLDC motor, i have to control the phases with PWM and commutate 2000 times per second. The standard PWM duty cycle of 490Hz is far too slow. My goal is to set the prescaler of all 3 timers to 1 to reach 62500Hz duty cycle (@16Mhz clock).

Is it possible to do this for all 3 timers(ATmega328p), even the for 16-bit timer1? Please tell me the exact commandos!

Greetings from Austria!

Yes, the clock prescale bits are at the low end of TCCR0B, TCCR1B and TCCR2B
if I remember right - see the datasheet.

If you change the prescale of timer0 then you will find millis(), micros() and delay()
will then misbheave and the timer0 interrupt handler will run much more often, using
up CPU time. You might want to abandon using these and disable the timer0 interrupt

  TIMSK0 = 0x00 ; // no timer ints

I think that would do it (not tested yet)

TIMSK0 = 0x00 ; // no timer ints

//clear
int prescaler = 0x07;
TCCR0B &= ~prescaler;
TCCR1B &= ~prescaler;
TCCR2B &= ~prescaler;

//set
prescaler = 1;
TCCR0B |= prescaler;
TCCR1B |= prescaler;
TCCR2B |= prescaler;

does timer0 affect the serial communication as well?

Greetings,
Peter

peterpoetzi2:
does timer0 affect the serial communication as well?

My understanding is no, hardware serial communication is not dependent on timers. However I believe SoftwareSerial depends on a hardware timer for transmitting characters.

FYI hardware serial does use interrupts (Arduino interrupts. - Page 1).

If you want to generate PWM on several pins you can use one timer to generate the PWM signal on one pin
and register a pin-change interrupt on that pin. The handler of the pin change interrupt can then copy
the PWM pin state to whichever other pins you want to PWM (using direct port manipulation for speed).

Thus you only need one counter but have the overhead of the pin change handler running all the time.
It limits the narrowest PWM pulses to a minimum of a few us though.