Go Down

Topic: Variable duty cycle freq gen (Read 718 times) previous topic - next topic


I want to produce a square wave output on the OC2A pin, variable in frequency from about 30 KHz to 100 KHz. The CTC mode of Timer 2 works pretty well for this, producing a 50% duty cycle square wave.

But now I'd like to make the duty cycle user-settable--is there a way to do this? Looking at the descriptions of the PWM modes, e.g. Fast PWM, it looks like the base frequency has to be the 16MHz clock divided by the prescale*256, and no frequencies in between--is that right? As for the CTC mode, the only way I can think of is to catch each compare/match interrupt and set the TCNT2 value alternately, but that wouldn't work in my case because there's another timer interrupt  going (Timer 1) which would interfere.


Go Up