PWM again…

What to do if you favourite microcontroller doesn’t have enough hardware PWM pins? Software driven PWM of course.

There many absolutely dumb ways to do it, and I’ve probably tested all of these. They can be classified as “brute force”. Very cpu intensive, starving the rest of your code.

A better way of doing it is to only do any work if there is anything to do. But you already know all of this of course. Calculate when to toggle the lines and let a timer do all of the work. Code can be found on my blog, which would be right here.

I started a discussion on about this. Initially I had trouble with _delay_ms(), which I only inserted for debugging purposes. And all of this was also affected by different optimization levels (-O3 vs. -Os). The execution time of the ISR changes by a factor of 3. As it turns out most (if not all) of my problems were caused by the ISR starving the rest of the code.

So here’s how NOT to do PWM if you have to be conscious of cpu time:

This piece of code is looping again and again, constantly checking if the data lines should be high or low. Suffice to say that this takes quite a lot of time. And if you don’t keep the ISR running all the time the duty cycle goes down. In my case this caused the LEDs to go dim.

Now to the “all new” and improved version. Use at your own risk ;-)

This piece of code uses a hardware timer to trigger an interrupt, which calculates when it should be invoked again and changes the state of the data lines. Basically it only does work when necessary. Total execution time for this one is just 100µs. The crude and dumb ISR used up more than 60% of cpu time, the improved version is happy with just 8%.

The full code (including a timer0 driven system ticker + time/delay function) is here. It’s tailored for an ATtiny 2313.

I’ll have to test if this approach is easily applied to a row multiplexed application, probably not.

This entry was posted in Electronics. and tagged , , , . Bookmark the permalink.

19 Responses to PWM again…

  1. Pingback: Weekly Shared Items – 25. February, 2011 | TOXIN LABS - weblog of a german design student from wuerzburg

  2. Pingback: Optimizing code for PWM efficiency | You've been blogged!

  3. Rando says:

    Cool post! Hey what’s that tiny 20 pin AVR dev board you’re using???

  4. robert says:


    It’s from ‘tinkerlog’:

    I replaced the male headers with female ones and upgraded it with an ATtiny4313 (which I only could find on ebay back then).

  5. Pingback: Optimizing code for PWM efficiency « Black Hat Security

  6. Lauri says:

    How do you measure CPU utilization on an AVR chip or Arduino?

    • robert says:

      In this case I use my oscilloscope. At the beginning of a function I set a pin to high and at the end back to low again (using direct port manipulation, not “digitalWrite()”). The resulting waveform is all that is needed. It is not 100% accurate as it only covers the time spent inside a function and not pro/epilogues as well. Running a simulator works as well and is more accurate, but I like using my scope ;-)

  7. Lauri says:

    Good idea :)

  8. Pingback: hackaholicballa - Optimizing code for PWM efficiency

  9. Greeeg says:

    Is that a 1.9mm led matrix you are using? Where did you buy it from?

    I got 10 of them awhile back but they weren’t diffused, IMO diffused matrices look alot better.

    cool project.

  10. robert says:

    I think I got them from SeeedStudio quite a while ago. If it’s still available, I don’t know.

  11. Bouba says:


    Nice job! I’ve been trying myself to do this kind of job. I have a working implementation that drives 9 rgb led strips (so 27 leds). It’s based on BAM (bit angle modulation which is I think the same thing that you’ve done. Do you experiment any blinking issue when changing the brightness value? Because I do :-( I havent find how to solve the issue for the moment, but I will certainly try with your implementation!


    • robert says:

      Interesting read, seems different though. The effect may be the same.

      If everything runs fine, there’s no blinking as long as I use double buffering and only update the ‘live’ data once the PWM cycle has fully completed. Flipping the buffers is done in the ISR at the start of a new cycle and only if the main code has set a flag that things have changed.

      The code excerpt posted above is _not_ the latest version I use. You can find it in the git repo (2313 folder). For the real hardware I used an ATtiny24 and threw away most of the stuff anyway. Just one channel and no bubble-sort anymore…

  12. Daniel says:

    You would probably be interested in my pwm code. It sorts a list of PWM pins in the main loop (not in an ISR) and iterates through the list in the compare interrupt. The overflow interrupt resets the loop. The code could be improved as it seems to be more sensitive to missed interrupts (e.g. handling the UART in an interrupt) than necessary.

    while (pp->width pin;
    OCR0A = pp->width;

    • Jack says:

      HTML killed your code snippet; can you please repost it with tags around it? thanks

  13. robert says:

    You also merge identical states… sneaky. Too complicated for my ever shrinking little brain.

  14. Pingback: Optimizing code for PWM efficiency | House of Mods

  15. rf says:

    the idea looks nice, but maybe is it possible to do the the math with the 32bit (!!) ints in the workloop and only shift out the data via spi and reload the OCR1A in the ISR?

    the code is nice! i will spend some time on it.. thanks for sharing!

  16. robert says:

    Yes, that would be much better. The ISR is running for almost too long right now. It must finish between two ticks of its associated timer. I’d need a prescaler of 2048, which doesn’t exist.

    Anyway, the real hardware doesn’t use this PWM code anymore. It dims all of the leds globally using the enable line of the driver chip. Problem is that for real individual control all of the pwm values must be sorted. Running something like bubble-sort works, but takes too much time for smooth transitions. At least when I write the code ;-)

    And I only started this PWM madness, because some time ago I bought a very large number of a certain type of led driver which doesn’t do PWM on its own. But at least they were cheap. Monetarily…

Comments are closed.