Buriedcode
Full Member level 6
pwm+3ch+picbasic
Hi,
I suppose this is a fairly simple question, but...
I'm trying to write software for 3 PWM outputs on a PIC, with a frequency of roughly 100=200Hz. Its for controlling the brightness of LEDs, a red, green, and blue. (Don't have an RGB led, but I'm using tiny 0403 chips close together, its as good as).
I'm using the PIC16F628A which does have a hardware PWM, but alas, only one.
Could this somehow be 'multiplexed' at high speed for all three LED's?
My first code was simple. Set up a timer (Timer0) interupt, every time it 'pings'(rolls over) I decrement each of the PWM registers (containing 0-15, its only 4 bit so far) as well as a 'counter' register (filled with 15 at the start). When a PWM register reaches 0, its pin is pulled low. When the counter register reaches 0, all pins are made high, the counter is loaded with 15 again, and all PWM registers are reloaded.
Theres many prog's like this on the net, but honestly, I had this idea before I saw any of them
So far so good, it also means that adding up to 8 PWM outputs, won't drastically increase the time spent on PWM generation (just decrement another PWM register).
However, this is only 4-bit resolution. And despite the speed (4 Mhz osc, interrupt every 0.5ms or so) I can see a flicker when I turn my head quickly. I used an interupt because I really would like the PIC to do other things, with the PWM in the background, although, PWM generation is the main function of the software.
I suppose I could use Timer2 and set it to count to 127, instead of Timer0's 'set' 255. But I'm worried that my interrupt routine will eventually be too long, so the interrupt wants to go again while I'm in the middle of it (even if I turn interrupts off during the ISR). That would mean, any code in my 'main' code wouldn't get executed.
My second idea is a bit drastic. Setup a timer1, so it compares its value with a register and causes an interupt when they are the same. So, I only have to do 4 things: Switch of each PWM in turn (3) and then reload when the timer has reset.
Only 4 interupts fro every period of PWM, but putting the PWM values in order is tricky (two values might be the same).
I would like to have 8-bit resolution, 3-ch PWM running at a higher speed (100-150Hz?). Preferably with the ability to use the serial port. Does anyone have any 'fancy' methods for doing this? Or will I have to dedicate the entire PIC to generating these PWM's at a decent resolution/frequency? Ideally, I'm trying to make each LED randomly fade in and out to get various mixes of colours, with no set sequence.
All idea's welcome. And yes, I've scoured google for days and I've collected loads of info. Tomorrow I'll see just how quickly I can get an 8-bit version of my idea above to work.
Thanks.
BuriedCode
Hi,
I suppose this is a fairly simple question, but...
I'm trying to write software for 3 PWM outputs on a PIC, with a frequency of roughly 100=200Hz. Its for controlling the brightness of LEDs, a red, green, and blue. (Don't have an RGB led, but I'm using tiny 0403 chips close together, its as good as).
I'm using the PIC16F628A which does have a hardware PWM, but alas, only one.
Could this somehow be 'multiplexed' at high speed for all three LED's?
My first code was simple. Set up a timer (Timer0) interupt, every time it 'pings'(rolls over) I decrement each of the PWM registers (containing 0-15, its only 4 bit so far) as well as a 'counter' register (filled with 15 at the start). When a PWM register reaches 0, its pin is pulled low. When the counter register reaches 0, all pins are made high, the counter is loaded with 15 again, and all PWM registers are reloaded.
Theres many prog's like this on the net, but honestly, I had this idea before I saw any of them
So far so good, it also means that adding up to 8 PWM outputs, won't drastically increase the time spent on PWM generation (just decrement another PWM register).
However, this is only 4-bit resolution. And despite the speed (4 Mhz osc, interrupt every 0.5ms or so) I can see a flicker when I turn my head quickly. I used an interupt because I really would like the PIC to do other things, with the PWM in the background, although, PWM generation is the main function of the software.
I suppose I could use Timer2 and set it to count to 127, instead of Timer0's 'set' 255. But I'm worried that my interrupt routine will eventually be too long, so the interrupt wants to go again while I'm in the middle of it (even if I turn interrupts off during the ISR). That would mean, any code in my 'main' code wouldn't get executed.
My second idea is a bit drastic. Setup a timer1, so it compares its value with a register and causes an interupt when they are the same. So, I only have to do 4 things: Switch of each PWM in turn (3) and then reload when the timer has reset.
Only 4 interupts fro every period of PWM, but putting the PWM values in order is tricky (two values might be the same).
I would like to have 8-bit resolution, 3-ch PWM running at a higher speed (100-150Hz?). Preferably with the ability to use the serial port. Does anyone have any 'fancy' methods for doing this? Or will I have to dedicate the entire PIC to generating these PWM's at a decent resolution/frequency? Ideally, I'm trying to make each LED randomly fade in and out to get various mixes of colours, with no set sequence.
All idea's welcome. And yes, I've scoured google for days and I've collected loads of info. Tomorrow I'll see just how quickly I can get an 8-bit version of my idea above to work.
Thanks.
BuriedCode