Optimize PWM routin...
 
Notifications
Clear all

Optimize PWM routines

5 Posts
3 Users
0 Reactions
2,945 Views
Aurbo99
Posts: 2803
Topic starter
(@aurbo99)
Famed Member
Joined: 19 years ago
[#15164]

http://hackaday.com/2011/05/14/optimizing-code-for-pwm-efficiency/

Anyone having written PWM code, take a look at the link. Might offer additional insight into reducing CPU loads and efficiency.


4 Replies
Posts: 789
(@chilloutdocdoc)
Prominent Member
Joined: 18 years ago

Fairly trivial for blinking one LED. I have an app-note on my computer at home I'll link to about doing a PWM routine in C on an AVR. Very efficient, supports multiple channels.


Reply
Posts: 597
(@entropy)
Honorable Member
Joined: 19 years ago

I've used the Atmel softpwm approach - works well. I've also increased its resolution using sigma-delta modulation to dither the output - this allows higher than 8 bit dimming, but keeping the math in the inner interrupt loop 8 bits (much faster on AVR) - https://github.com/Entropy512/I2C_RGB


Reply
Posts: 789
(@chilloutdocdoc)
Prominent Member
Joined: 18 years ago
Entropy wrote:I've used the Atmel softpwm approach - works well. I've also increased its resolution using sigma-delta modulation to dither the output - this allows higher than 8 bit dimming, but keeping the math in the inner interrupt loop 8 bits (much faster on AVR) - https://github.com/Entropy512/I2C_RGB

I'm going to have to take a look at that, how many bits and led's are you doing through one AVR?


Reply
Posts: 597
(@entropy)
Honorable Member
Joined: 19 years ago

12-bit, 3 channels, but it should be scalable up to 8+ channels without much trouble. You might need to drop to 11 bits then.

Increasing bit depth is also not difficult, however beyond 12 bits you risk flicker. When you add bits to the sigma-delta code, it dithers the low-level 8-bit PWM. The worst-case scenario is a brightness of 1 - At that point, the sigma-delta modulator turns on the PWM at minimum brightness for one cycle, and then off for 15 PWM cycles. If you add a bit, that goes up to 1 on, 31 off, and at this point you WILL see flicker unless you can tighten up the main PWM ISR to get the PWM frequency up.

As you increase channel count, you risk running out of instruction cycles in the PWM ISR, so you may need to slow down the base PWM frequency. At that point you would have to decrease the bit depth of the sigma-delta dithering.

Unfortunately, due to the logarithmic nature of human vision, those low-brightness scenarios (and the minimum achievable brightness) are VERY important. At higher brightnesses, you could increase bit depth significantly without ill effects (a human won't notice much flicker when something dithers between, for example, a brightness of 10 and 11, while dithering 0/1 is very noticeable at low duty cycles.)


Reply
Share: