View previous topic :: View next topic |
Author |
Message |
tmsenthil03
Joined: 27 Feb 2011 Posts: 4 Location: chennai
|
RGB LED control |
Posted: Sun Feb 27, 2011 8:17 am |
|
|
hai..
I am doing the project of multiple colors generate using RGB LED with soft color changing. Right now I only have PIC16F877A controller. It have only 2 PWM. I need 3 PWM How to I generate? Please help me.
M.Senthilkumar |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Feb 28, 2011 8:55 am |
|
|
I did this exact same thing with a 16F84A about a 3 years ago.
It can be done without any hardware pwm.
You can generate a pwm signal with counters
based on that signal you can produce 2 more that are offset by 120 degrees....
I failed to backup the code and my pc crashed (died).... so I can't give it to you and I didn't try to recode it.
But it can be done easily and its pretty short code... _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Feb 28, 2011 10:53 am |
|
|
Hi,
Another possibility is to use an LED module that has a built in PWM controller, like the ShiftBrite. I recently did a project using these modules and found them very easy to work with. They can be 'cascaded', and you only need to send them digital data corresponding to color and intensity to make them work.
John |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Feb 28, 2011 1:16 pm |
|
|
There are also some LEDs that have an integrated controller.
They look like an ordinary LED but constantly cycle through all color paterns....
You only need to feed it power and GND.
The software solution I mentioned is pretty simple... I think all it took was a 2 nested loops and a few counter variables...give it a try. You might learn something...
g _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Feb 28, 2011 2:04 pm |
|
|
Code: |
int setduty=0; //defines duty cycle
bool direction=0; //direction of duty cycle
while(1)
{
freq=0; //frequency period counter
counter1=0; //duty cycle counter
output_high(PIN1); //set pin X high
while(freq<255) //frequency is fixed to 255 loop cycles.
{
if(counter1>=setduty1) //when the desired duty cycle ends, drive pin low.
{
output_low(PIN1); //On time has expired, drive pin low
}
freq++; //controls the frequency period
counter++; //controls the pin ON state
}
if(direction==0); //count up till 255
setduty++;
if(direction==1); //count down till 255
setduty--;
if(setduty>=255) // if maximum duty cycle is reached, change directions.
direction!=direction;
} |
I have not tested the above.... think of it more as pseudo code...
I wrote it in wordpad at the office..... it "should" fade a LED from off to full bright and then back to off (continuously).
You can use this as a base... incorporating 2 more pwm I/O should not be that difficult from here....
I recommend you try this out and get one LED fading on and off first.
I do not guarantee this code to work but you should get the idea...
By adding 2 more lines you could drive another pin in the exact opposite direction of the first one... when you turn "PIN1" on turn "PIN2" off....
then you will have 2 pwm 180 degrees off... in other words one LED getting brighter while the other is proportionally fading. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|