Skip to main content

Posts

Featured

BY  MARIOS ANDREOPOULOS  —  FEBRUARY 20, 2014 Fast PWM on ATmega328, up to 8MHz sacrificing duty cycle resolution to get higher frequency A couple of days earlier, a friend asked me how he could get fast PWM from an Atmel ATmega328 microcontroller —fast as in over 62.5KHz. Surpisingly I couldn't find a working code example, despite the fact there are many articles and forum posts about this. Of course one may always refer to the uC's datasheet, but the code part isn't always straightforward. So, without delay here is a sample code you can load to your Arduino Uno or directly to an AVR, which will give you a 250KHz, 6 bit resolution PWM on pin 3 (ATmega pin 5) and a 8MHz, 1 bit resolution —thus only 50% duty cycle— on pin 5 (ATmega pin 11). The duty cycle of the 250KHz PWM is rolling. // A sketch that creates an 8MHz, 50% duty cycle PWM and a 250KHz, // 6bit resolution PWM with varying duty cycle (changes every 5μs // or about every period. #include ...

Latest Posts