Arduino Uno same frequency on ALL PWM pins

2.5k views Asked by At

I'm controlling a BLDC motor with an inverter/driver IC doing the switching work. I need to provide 6 PWM signals for the driver all at the same frequency. The exact frequency doesn't really matter, as long as its between 5kHz to 10kHz and all the PWM pins are the same.

I'm currently using an Arduino Uno, I'm not sure how to program each PWM pin (3, 5, 6, 9, 10, 11) for all the same frequency as the timers(0, 1 and 2) for particular pins have different clock speeds.

  • Pins 5 and 6: controlled by timer0, base frequency 62500Hz
  • Pins 9 and 10: controlled by timer1, base frequency 31250Hz
  • Pins 11 and 3: controlled by timer2, base frequency 31250Hz

timer1 and timer2 will be okay and can be scaled down by the prescaler value 8 (7812.5Hz). Not sure how to get timer0 to the same frequency?

http://playground.arduino.cc/Main/TimerPWMCheatsheet

I'm sure a work around is relatively simple. Is it possible to divide the frequency for timer0 by a number which is not a prescaler value, say 40?

Thanks in advance.

Pat.

1

There are 1 answers

2
UncleO On

You haven't included the chip number for your motor controller, but I'm pretty sure you won't be able to achieve what you want with six different timers. The issue is that the PWM on the different pins all have to be synchronized, not just toggled on and off for the right amount of time.

Instead, you should use a single timer and toggle all the outputs at the correct synchronization. This page has the start of an example in the section "Bit-banging Pulse Width Modulation". You would have to modify it to toggle more pins.

That example is probably still not good enough. Instead, you should be using an interrupt service routine to toggle the pins. That way, the PWM runs more independently and allows the loop function to do more.

Your routine will be attached to an output compare interrupt on a timer, and you will have to keep the values of the various PWM outputs in some volatile variables. The output compare register will be set to wake up for the next toggle. When the routine runs, it will perform the toggle and set the output compare register for the toggle. The details of the timing and synchronization will depend on the datasheet for your controller.

The loop function can read inputs and adjust the volatile variables to change the motor speed.

There are also motor drivers that do all this for you. All you have to do is provide a direction and speed, and the chip creates the 6 PWMs for you.