Many AVR devices have nice PWM timer modes, which I found out about using after generating servo pulses the harder way. I've seen servo control projects where PWM wasn't available controlling 8 or more servos, implementing a list of servo set positions and toggling the output based on those set positions to make each pulse the correct width, all controlled by one timer.
Now I'm thinking about motor control. If I was doing PWM control of < 3 motors on an AVR with 3 PWM timers it would be easy. What about more motors? Can the following approach work?
- use byte array to hold fan states
- fill array with initial states, i.e. 0 = off, 4 = full speed for a four-speed system
- Decide on frequency of PWM to suit motors
- Use timer to produce interrupts giving required granularity of control (e.g. 100Hz PWM with four speed levels requires 400Hz, four interrupts per cycle)
- all motors start in off state
- on each interrupt, increment a static interrupt count. When it reaches the number of levels, i.e. 4, reset it to 0. When the count is 0 set each motor with a non-zero value in the array on, when the count exceeds the value for a motor in the array turn it off.
Is there a better way? I'd really like constructive criticism or to hear how others have done it.