I want to generate a complex timer code on an ATmega328P for controlling a vibration pump. The timer code (similar to PWM but more complex) has the following properties: a repeating on-pulse with a length from 0.5 ms to 10 ms ("pulse width"), with a pause between successive on-pulses which can vary in the range 10 ms to 190 ms ("pause"). The aim is to be able to set the pulse width and pause through variables in the program, and the Arduino will then generate the required waveform. Normal timing functions of the arduino should not be interferred with.
Does somebody know if it is possible to do this using Timer1 or Timer2? Ideally I would like to use Timer2 on Arduino pin 3 (OC2B), but if necessary I could use Timer1 on pin 10 (OC1B).
I am speculating it might be possible with Timer2 in non-PWM mode and maybe toggle OC2A/B on compare match. Is there any way of doing a compare match on both OC2A and OC2B for the same waveform output, setting pulse width from OC2A and pause from OC2B?
Essentially all I need to do is have a counter to count from BOTTOM up to TOP1 for the on-pulse width, which starts a second counter to count from BOTTOM up to TOP2 for pause, which then restarts the first timer.
It is not so critical whether the timing for the pause is counted from the beginning of the on-pulse or the end of the on-pulse (or even from a fixed point 10ms after the start of the on-pulse). More critical is that I can get the on-pulse fixed in the manner outlined (not as a proportion of the total period, as in PWM), and that I can set the pause time from about 10ms to about 190ms.
I have Ken Sherriff's excellent article (Secrets of Arduino PWM) but that does not cover non-PWM waveforms. I also have the 328 datasheet of course, but that is pretty tough going. Can someone kindly set me on the right track?
Many thanks.