I am trying to get a nice sounding varying PWM to DAC, and intend to use a Sallen Key as part of an active low pass filter.

I set the Fast PWM at a max of 255 (WGM1[0-3] = 5) and set/clear OC1A upon comparing with OCR1A (1<< COM1A1)

I also update the value of OCR1A in 64 distinct steps, changing those steps around 15'000 times a second to create a specific sinusoidal shape.

As the chip works at 16 Mega Hertz, does this mean that one cycle of PWM is 16'000'000/256= 62.5 Kilo Hertz? If not, what is its resolution

Thank you!

  • 2
    As you are directly poking into the hardware I/O registers, you have to read the datasheet. Look for the prescaler, and the bits CS1[0-2]. – Edgar Bonet Sep 23 '20 at 9:45
  • 1
    Assuming you have no prescaler, you'd indeed get around 62.5kHz. The 15kHz update rate for the sinusoidal is a bit odd, as it's 1/4.167 of the PWM update frequency. I'd probably use the timers interrupt to call a function that updates OC1A every 4th call. – Gerben Sep 23 '20 at 12:39
  • Thank you both for your answers! I read the datasheet but I guess without prescalers it's indeed 62.5kHz. The update will vary considerably as I'm trying to do the 12 tones of the scale. If I were to update OC1A every 4th PWM I'm not sure I'd be able to do a perfect period. – B7th Sep 24 '20 at 4:20

With the help of the comments, I decided to use variable PWM max values to match absolutely with the frequency. This resulted in a crispier sound that actually doesn't need the Sallen Key anymore.

For example, a low C natural, or 261.6Hz, updated 32 times for 8 full cycles of PWM give us, for a 16MHz chip, an period and amplitude of 238.

This means that having a prescaler of 8 (CS01), a period (OCR0A) AND a max potential duty cycle (ICR1) of 238, and going through 32 iterations of the sound sought for, I got the sound that I wanted and none of the wave groove that comes with a pulse duty cycle cut short.

Sure, it means less granularity when it comes to a Bb with a max duty cycle of 133, however it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.