Increasing the PWM frequency beyond 62Khz

I understand that it is possible to get the Atmega chip that the Arduino duemilanove uses to pulse a PWM pin faster than 62Khz, IF you're willing to give up some granularity in the pulse width?

So, if you're willing to go in 128 or 64 or even 32 steps, you could get the PWM pin to go at 125 or 250 Khz?

Would someone care to post some example code, if this is possible, of how you'd go about doing this in the Arduino dev framework?

In case you're wondering why I want to go faster than 62Khz at the expense of pulse width steps, I've built a piggy-back board that uses a PWM pin to drive a buck converter as part of a charge controller for a solar panel+battery. The size of the inductor goes as the inverse of the PWM frequency, so if I can double or quadruple the frequency, I can halve or fourth the inductor and free up space for other stuff.

And for a power management module, I can give up some pulse width granularity.

The Arduino PWM frequency is around 500Hz so you would need a huge inductor unless you seek other means.

PWM output is through the three AtMega timers (assuming Arduino Duemllianove with an AtMega328 mcu) and each timer can support two output pins. As an alternative to PWM, the timers can be configured for simple pin toggle at any frequency from 0Hz to F_CPU/2. So on a Duemillanove this would give you a frequency range of 0Hz to 8MHz.

The timer counter value is determined by the following equation:

TimerDivisor=F_CPU/Prescaler/(DesiredFrequency*2)

Timers 0 and 2 are 8-bit timers and timer1 is a 16-bit timer. That is the "TimerDivisor" must be in the range of 0-255 for timer0/timer2 and 0-65535 for timer1. Timer0 is used for millis so you may want to leave this timer as is and choose either timer1 or timer2.

The following example will configure timer2 for a 250kHz output signal on digital pin 11.

#define F_CTC 250000l
#define PRESCALER 1

#define T2DIV (F_CPU/PRESCALER/(F_CTC*2))

void Setup()
{
  OCR2A = T2DIV; // counter compare value
  TCCR2B = _BV(CS20);  // prescaler = 1 (F_CPU MHz)
  TCCR2A = _BV(COM2A0) | _BV(WGM21); // toggle OC2A, CTC mode
}

T2DIV for above is 32. If you change OCR2A, frequency will change as follows:

OutputFrequency = F_CPU/Prescaler/(T2DIV*2)

So for T2DIV = 33 you get a frequency of 242kHz and for T2DIV=31 you get 258kHz.

This is the same frequency I was looking for in my application which is similar in the sense that I am driving a mosfet at High frequencies. How do you change the duty cycle on this 250kHz wave?

This is the same frequency I was looking for in my application which is similar in the sense that I am driving a mosfet at High frequencies. How do you change the duty cycle on this 250kHz wave?

No one has answered, and I'm off to bed, so will check later, but I believe OCR2B is still available to give a PWM style output. You could write into that using analogWrite. The value must be less than T2DIV, which is 64.

HTH
GB