Hi guys!
Becoming a big 'ol topic this...
jonw0224, good to hear you are interested. About the 'bit mask' thing, you've hit the nail right on the head. Its exactly what I've done in all 3 idea's I've had. The 'start bit pattern' determines which LED's start the period as 'off' and is used to keep track of which ones we have 'on' every time the ISR is called. I've tried to think of a better way, but I think this is easy and efficient.
ECSD, good to hear your input. I think I mentioned in my original post, that this was my first idea. For a 4-bit PWM, the 'ISR' has a 4-bit counter, along with the PWM values for each LED (also 4-bits). Every interrupt form a timer, decrements all of these counters, when a 'PWM' counter reaches 0, its corresponding pin is turned off. When the 'counter' reaches 0, we simply reload everything and start again, a new PWM period. Although, the reason I abandonned this at the time was the amount of processor time it takes up. For 100Hz this would only need :100*16*(ISR length, say, 8 instructions) = 12800 instructions per second.
Example:
8-bit PWM, so an 8-bit counter is used, 0-255. The ISR must be called 255 times per PWM period. So for a frequency of 100Hz, thats 25500 interrupts per second. Now, this leaves us plenty of time to do 'other things', with a 4Mhz PIC, we have a million instructions per second to play with. BUT.....how big is our interrupt routine? say 8 instructions max. Thats 8*255*100 instructions per second = 204000. Just over 20% of the total time the PIC is running. When I did this, I noticed a slight flicker, so I had to up the frequency to 180-200Hz, plus I think my ISR was about 17 instructions. This left my PIC with about 10% left to spare. So, its almost like having a PIC running at 4Mhz/10 = 400Khz, still useful, but I'm obsessed with efficiency
That said......I am indeed going to do this, but without it being in an interrupt routine. I've decided to just dedicate a small 8-pin PIC to do all the PWM work in its 'main code' with a UART in an interrupt to change PWM values. That way, I don't need the PIC to do anything else, it can run at 4Khz, and here in the UK I can get a 8-pin PIC for 90p. (0.9GBP). Its just easier, and cheaper.
Anyway, I will explain my code fully once its finished. It has to be said, it
really is complicated. The code is straight forward, but the idea behind it took me a week to think up. I've got it down to 400 instructions, including the ISR, and 'without' any optimisations (I usually write a rough code, then add subroutines and little tricks to shrink it down to about 40%). It has 4 interupts per PWM period. With the ISR being 8 instrucitons. At 100Hz, thats 100*4*8 = 3200. Which, on a 4MHz PIC is about 0.32% of the total time used for PWM generation. It uses a single 'timer interrupt' which can be either Timer0, Timer1, or Timer2 depending on which routine you use. The real drawback with it is 'set up'.
For most PWM algorithms you people have described, you simply load in your PWM values, and start the interrupts. Making changing colours and fading very easy. But my code pushes all the complexity into the 'set up' routine, so every time you change a PWM value, you must re-do this, which is 350(ish) instructions. However, if you want a single colour most of the time, that changes every-so-often, like a Temperature indicator for your PC, its great, your PIC can do loads of other things whilst running this in the background. Personally, I'm planning on using it for an LCD backlight, that changes colour depending on user settings and other factors (an MSN message makes it flash green, CPU temp high makes it RED, and I've got a little fading routine that just makes my PC look cool
).
Anyway, I'm ranting, as I said, I've deciding to buckle, and spend about 1USD on a micro dedicated to this task. Allowing it to run fast, change colours quickly, and fade, as well as store patterns and control the current for each LED (PWM, ADC feedback to obtain a 'pure' white compensating for temperature variations and resistor tolerences). This will use everything ECSD, jonw0224, atferrari, MirekCz have said. And 'hopefully' when I get time, I'll set up a website, with all the algorithms on there (I've got 3 main algorithms for PWM generation, only 2 are any good though).
Thanks again guys.
BuriedCode.