If 8-bit PWM is an issue, it's possible to use the Mega's 16-bit timers: 1, 3, 4 and 5 each with 3 channels A, B and C, to generate PWM for up to 12 motor outputs with 11-bit resolution at 490Hz:
// Output 490Hz dual slope PWM at 11-bit resolution on digital pins:
// On the Arduino Mega there are 4 16 bit timers: 1, 3, 4 & 5, each with three outputs: A, B & C
// Store the addresses of the PWM timer compare (duty-cycle) registers
volatile uint16_t* pOCRxxReg[] = { &OCR1A, &OCR1B, &OCR1C, &OCR3A, &OCR3B, &OCR3C, &OCR4A, &OCR4B, &OCR4C, &OCR5A, &OCR5B, &OCR5C };
void setup() {
uint8_t pins[] = { 11, 12, 13, 5, 2, 3, 6, 7, 8, 46, 45, 44 }; // List the timers' Arduino pins
for (uint8_t i = 0; i < 12; i++) // Iterate through each pin
{
pinMode(pins[i], OUTPUT); // Set the pin to an output
}
// Initialise timers 1, 3, 4 and 5 for phase and frequency correct PWM
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1C1); // Enable the PWM outputs OC1A, OC1B and OC1C on digital pins 11, 12 and 13
TCCR1B = _BV(WGM13) | _BV(CS11); // Set phase and frequency correct PWM and prescaler of 8 on timer 1
TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1); // Enable the PWM output OC3A, OC3B and OC3C on digital pins 5, 2 and 3
TCCR3B = _BV(WGM33) | _BV(CS31); // Set phase and frequency correct PWM and prescaler of 8 on timer 3
TCCR4A = _BV(COM4A1) | _BV(COM4B1) | _BV(COM4C1); // Enable the PWM outputs OC4A, OC4B and OC4C on digital pins 6, 7 and 8
TCCR4B = _BV(WGM43) | _BV(CS41); // Set phase and frequency correct PWM and prescaler of 8 on timer 4
TCCR5A = _BV(COM5A1) | _BV(COM5B1) | _BV(COM5C1); // Enable the PWM outputs OC5A, OC5B and OC5C on digital pins 46, 45 and 44
TCCR5B = _BV(WGM53) | _BV(CS51); // Set phase and frequency correct PWM and prescaler of 8 on timer 5
ICR1 = 2040; // Set the timer 1 frequency to 490Hz
ICR3 = 2040; // Set the timer 3 frequency to 490Hz
ICR4 = 2040; // Set the timer 4 frequency to 490Hz
ICR5 = 2040; // Set the timer 5 frequency to 490Hz
}
void loop()
{
for (uint8_t i = 0; i < 12; i++) // Iterate through the 12 motors
{
*(pOCRxxReg[i]) = 1200; // Set the motors to low throttle
delay(1000); // Wait for 1 second
}
for (uint8_t i = 0; i < 12; i++) // Iterate through the 12 motors
{
*(pOCRxxReg[i]) = 1500; // Set motors to half throttle
delay(1000); // Wait for 1 second
}
}