Pages: [1]   Go Down
Author Topic: Variable duty cycle freq gen  (Read 449 times)
0 Members and 1 Guest are viewing this topic.
Columbus, Ohio USA
Offline Offline
Full Member
***
Karma: 3
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Joe
Logged

Pages: [1]   Go Up
Jump to: