16F RGB LED Example

RGB LED's are a true amazement to watch. I don't have access to
any
RGB LED controllers, so I developed my own little program that
will randomly cycle the intensity of each primary color.
RGB color is additive, that is, equal intensities of Red + Green +
Blue will produce white (not black). Here's an example of additive
color;

Having no experience with RGB LED's, I was really in
the deep end, and most of the forums online suggested using MAX6696
LED controllers and such, but I cant get hold of them.
The LED's can be controlled by PWM,
Pulse Wave
Modulation, and a
PIC micro has the capability to drive a single
RGB LED without a driver, you could control more than one, but you
would have to be sure that the PIC does not draw too much current. I
used the
ULN2003 to drive several in parallel, as the
ULN2003 can handle up to 500mA continuous.
Unfortunately, ISIS does not provide a simulation for RGB LED's, so
a brief explanation will have to do. For this example, one RGB LED
will be driven by a
PIC micro.
The program will make the RGB LED Cycle through
every primary
color randomly, and it will make sure that the chosen
color can cycle in the direction that was
chosen
randomly (i.e. wont 're-cycle' red down of red is
already at
minimum).
Dim R_Delay as Word
Dim G_Delay as Word
Dim B_Delay as Word
Dim Temp as Word
Dim R_Loops as Word
Dim G_Loops as Word
Dim B_Loops as Word
Dim R_Max As Word
Dim G_Max As Word
Dim B_Max As Word
Dim TimeScale as Word
Dim Max_Time as Word
Dim Last as Word
Dim Percent as Word
DIM RND1 as Word
Dim RND2 as Word
Dim DTemp as Dword
Symbol B = PORTA.1
Symbol G = PORTA.2
Symbol R = PORTA.3
DEVICE = 16F877A
XTAL = 4
;--------------------------------------------------------
;--------------------------------------------------------
CONFIG XT_OSC , WDT_OFF , PWRTE_ON , BODEN_OFF , LVP_OFF , CPD_OFF, CP_OFF , DEBUG_OFF
ALL_DIGITAL = TRUE
SEED $0345
TRISB = %11111111
TRISA = %00000000
PORTA = $FF
R_Max = 250 ' Adjust so that the proper color spectrum is shown
G_Max = 102 '
B_Max = 100 '
TimeScale = 3500 ' Controls the rate in which the colors change
' (Note: Max Brightness control will modify time!)
R_Loops = TimeScale / R_Max ' The timescale provides the number of loops
G_Loops = TimeScale / G_Max ' for each display of a new color and to
B_Loops = TimeScale / B_Max ' ensure best performance, each individual color
' loop is modified to fit into the timescale selected
R_Delay = R_Max ' Set values to known amounts
G_Delay = G_Max
B_Delay = B_Max
If R_Max > G_Max and R_Max > B_Max Then Max_Time = R_Max ' Also ensures maximum
If G_Max > R_Max and G_Max > B_Max Then Max_Time = G_Max ' refresh rate depending
If B_Max > R_Max and B_Max > G_Max Then Max_Time = B_Max ' on user settings
Main:
RND1 = Random ' Produce a random number
Select RND1 ' Depending on the value produced
Case 0 To 10000 ' will control the color flow
Goto Red_Up
Case 9999 To 20000
Goto Red_Down
Case 19999 To 30000
Goto Green_Up
Case 29999 To 40000
Goto Green_Down
Case 39999 To 50000
Goto Blue_Up
Case 49999 To 60000
Goto Blue_Down
EndSelect
Goto Main ' If the value was >60000 then get a new one
Red_Up: ' Increase the amount of red
If R_Delay = R_Max Then Goto Main
For R_Delay = 0 To R_Max
For Temp = 0 To R_Loops
R = 1
Delayus R_Delay
R = 0
Delayus Max_Time - R_Delay
G = 1
Delayus G_Delay
G = 0
Delayus Max_Time - G_Delay
B = 1
Delayus B_Delay
B = 0
Delayus Max_Time - B_Delay
Next
Next
R_Delay = R_Max
Goto Main
Red_Down: ' Decrease the amount of red
If R_Delay = 0 Then Goto Main
For R_Delay = R_Max To 0 Step -1
For Temp = 0 To R_Loops
R = 1
Delayus R_Delay
R = 0
Delayus Max_Time - R_Delay
G = 1
Delayus G_Delay
G = 0
Delayus Max_Time - G_Delay
B = 1
Delayus B_Delay
B = 0
Delayus Max_Time - B_Delay
Next
Next
R_Delay = 0
Goto Main
Green_Up: ' Increase the amount of green
If G_Delay = G_Max Then Goto Main
For G_Delay = 0 To G_Max
For Temp = 0 To G_Loops
R = 1
Delayus R_Delay
R = 0
Delayus Max_Time - R_Delay
G = 1
Delayus G_Delay
G = 0
Delayus Max_Time - G_Delay
B = 1
Delayus B_Delay
B = 0
Delayus Max_Time - B_Delay
Next
Next
G_Delay = G_Max
Goto Main
Green_Down: ' Decrease the amount of green
If G_Delay = 0 Then Goto Main
For G_Delay = G_Max To 0 Step -1
For Temp = 0 To G_Loops
R = 1
Delayus R_Delay
R = 0
Delayus Max_Time - R_Delay
G = 1
Delayus G_Delay
G = 0
Delayus Max_Time - G_Delay
B = 1
Delayus B_Delay
B = 0
Delayus Max_Time - B_Delay
Next
Next
G_Delay = 0
Goto Main
Blue_Up: ' Increase the amount of blue
If B_Delay = B_Max Then Goto Main
For B_Delay = 0 To B_Max
For Temp = 0 To B_Loops
R = 1
Delayus R_Delay
R = 0
Delayus Max_Time - R_Delay
G = 1
Delayus G_Delay
G = 0
Delayus Max_Time - G_Delay
B = 1
Delayus B_Delay
B = 0
Delayus Max_Time - B_Delay
Next
Next
B_Delay = B_Max
Goto Main
Blue_Down: ' Decrease the amount of blue
If B_Delay = 0 Then Goto Main
For B_Delay = B_Max To 0 Step -1
For Temp = 0 To B_Loops
R = 1
Delayus R_Delay
R = 0
Delayus Max_Time - R_Delay
G = 1
Delayus G_Delay
G = 0
Delayus Max_Time - G_Delay
B = 1
Delayus B_Delay
B = 0
Delayus Max_Time - B_Delay
Next
Next
B_Delay = 0
Goto Main
Where you can get
the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
| |  | Motors |
| |  | Thermometers |
| |  | Code Snippets |
|  | 18F PIC Examples |
|  | Handy Tips |