epv
Joined: 23 Feb 2008 Posts: 8
|
|
Posted: Wed Apr 09, 2008 1:00 pm |
|
|
Here is what you do.
Set up a fast 8 bit timer, and set it to interrupt on overflow. Now your interrupt routine will get called on every rollover of the timer. This is one "tick" of your pwm clock. In this routine, increment an 8 bit variable, which will wrap around to zero every 255 "ticks." (you can manually reset before that, if you want a faster loop and less resolution)
Now, declare three global variables to store the duty cycle of each of Red, Green, and Blue. In the interrupt routine, on every tick #0, you turn on the output pin assigned to each color. On every successive tick, you compare the current tick number to the duty cycle set for each color, and if it's been reached, you turn off that pin.
So, if you have R=50, G=100, and B=150, all three will be turned on at tick 0, R will turn off at tick 50 and remain off until the next tick 0. G will turn off at tick 100, etc...
Now, since changing the duty cycles mid-cycle will cause ugly glitches, you should set up four more global variables - one each for R, G, and B, which will be the "user accessible" ones you will actually assign values to in your main program, and a fourth to use as a flag to notify the interrupt routine that you've changed their values.
Then, in the interrupt routine, at every tick 0, you check to see whether the lag variable is set, and if so, assign the working R, G, and B variables from the user-accessible ones. This way, the duty cycle only changes at the beginning of the cycle, avoiding glitches.
Make sure to run the counter as fast as you can, (ie, no prescaling) and run at a high CPU frequency, or else you will get noticeable flicker.
Note that by doing it in software like this, you can do more than just 3 channels without too much penalty in terms of speed. Just turn on all your channels at the beginning of a cycle and then turn them off at the right times as the counter increments.
I bet this isn't the kind of "help and software" you were looking for, but knowing how it's actually done is probably more useful than someone handing you a hex file. |
|