Showing posts with label PWM - Software. Show all posts
Showing posts with label PWM - Software. Show all posts

Wednesday, February 3, 2021

Multi-channel software PWM library for AVR microcontrollers | ATmega328P

What you do when you run out of PWM pins on hardware? You make software PWM of course. This library is based on "AVR136: Low-Jitter Multi-Channel Software PWM" application note. It supports up to 10 PWM channels (more can be added) and it's suitable for led dimming, DC motor control and RGB led controller.

Multi-channel software PWM library for AVR microcontrollers | ATmega328P

Since this method is already explained in the AVR136 app note I won't go in to too many details. So the basic principles behind software PWM are this. A timer interrupt is set to trigger every 256 system clocks. For 8 bit timers the interrupt is on overflow and for 16 bit timers is on compare match. A 16 bit timer has the advantage that the base frequency can be modified. On every interrupt a variable "softcount" is incremented from 0 to 255 and each time is compared against each PWM channel. At the beginning of the cycle the pins are set high and when "softcount" equals to a channel's set value then the specific pin is set low. On 8MHz CPU the ISR takes between 4 and 10us to execute the code depending on how many channels and on how many ports there are. The size of the "softcount" variable dictates the PWM resolution and it is set to 8 bits.