| Swordfish Tutorial - Red Green Blue (RGB) LED |
|
|
| Saturday, 04 April 2009 09:40 | |
|
Red Green Blue (RGB) LED's are a marvel to watch in action, and I have got to say, they are worth the effort to play around with. The programming side of things does take a little thought, primarily because you need to control 3 different Pulse Width Modulated (PWM) signals to control the brightness of each LED (Red Green and Blue).
The above diagram utilises the ULN2003 to drive much higher current devices then what the PIC can handle so with this design, you could put a lot of RGB LED's in parallel for some really stunning lighting effects.
Green resistor = (5 - 3.5 - 0.9) / 0.020 = 30 ohm or greater Blue resistor = (5 - 3.5 - 0.9) / 0.020 = 30 ohm or greater
Device = 18F4550 Clock = 48 Config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HSPLL_HS Include "RandGen.bas" Dim TMR2IE As PIE1.1, // TMR2 interrupt enable TMR2IF As PIR1.1, // TMR2 overflow flag TMR2ON As T2CON.2, // Enables TMR2 to begin incrementing Signal_Pin As PORTB.0 // Signal output to frequency meter Dim Red_Pin As PORTC.0, Green_Pin As PORTC.1, Blue_Pin As PORTC.2, Red_Duty As Byte, Green_Duty As Byte, Blue_Duty As Byte, Red_DutyVal As Byte, Green_DutyVal As Byte, Blue_DutyVal As Byte, RandomVal As Byte Dim uS As Word, mS As Word Interrupt TMR2_Interrupt() High(Signal_Pin) Save(0) // Back up system variables If TMR2IF = 1 Then // Check if the interrupt was from TMR2 TMR2IF = 0 // Clear the TMR2 interrupt flag uS = uS + 50 If uS >= 1000 Then uS = uS - 1000 Inc(mS) EndIf Inc(Red_DutyVal) Inc(Green_DutyVal) Inc(Blue_DutyVal) If Red_DutyVal > Red_Duty or red_Duty = 0 Then Red_Pin = 0 Else Red_Pin = 1 EndIf If Green_DutyVal > Green_Duty or Green_Duty = 0 Then Green_Pin = 0 Else Green_Pin = 1 EndIf If Blue_DutyVal > Blue_Duty or Blue_Duty = 0 Then Blue_Pin = 0 Else Blue_Pin = 1 EndIf EndIf // Restore // Restore system variables Low(Signal_Pin) End Interrupt Private Sub TMR2_Initialize() TMR2ON = 0 // Disable TMR2 TMR2IE = 0 // Turn off TMR2 interrupts PR2 = 149 // TMR2 Period register PR2 T2CON = %00000001 // T2CON 0:1 = Prescale // 00 = Prescale is 1:1 // 01 = Prescale is 1:4 // 1x = Prescale is 1:16 // 3:6 = Postscale // 0000 = 1:1 postscale // 0001 = 1:2 postscale // 0010 = 1:3 postscale... // 1111 = 1:16 postscale TMR2 = 0 // Reset TMR2 Value TMR2IE = 1 // Enable TMR2 interrupts TMR2ON = 1 // Enable TMR2 to increment Enable(TMR2_Interrupt) End Sub // Start Of Program... Low(Red_Pin) Low(Green_Pin) Low(Blue_Pin) Red_Duty = 0 Green_Duty = 0 Blue_Duty = 0 Red_DutyVal = 0 Green_DutyVal = 0 Blue_DutyVal = 0 uS = 0 mS = 0 RandGen.Initialize(128) // Initialize the Random Number generator TMR2_Initialize // Setup and enable TMR2 While True // Create an infinite loop RandomVal = RandGen.Rand // Grab a random number from 0 to 255 Select RandomVal // Find out what colour to increase/decrease Case 0 To 42 While Red_duty < 255 mS = 0 Repeat Until mS = 19 inc(Red_Duty) Wend Case 43 To 84 While Red_duty > 0 mS = 0 Repeat Until mS = 19 dec(Red_Duty) Wend Case 84 To 127 While Green_duty < 255 mS = 0 Repeat Until mS = 19 inc(Green_Duty) Wend Case 128 To 170 While Green_duty > 0 mS = 0 Repeat Until mS = 19 dec(Green_Duty) Wend Case 170 To 212 While Blue_duty < 255 mS = 0 Repeat Until mS = 19 inc(Blue_Duty) Wend Case 212 To 255 While Blue_duty > 0 mS = 0 Repeat Until mS = 19 dec(Blue_Duty) Wend End Select Wend
Simulation of the program
|
|
| Last Updated ( Monday, 27 April 2009 10:53 ) |
Whos Online
- theoptimist
- Jon Chandler
Forum Activity
DROID! Just got it - hop Monday, 08 February 2010 14:07 [4 replies]
Math - mast Monday, 08 February 2010 03:21 [4 replies]
win 98 / swordfish - Graham Saturday, 06 February 2010 17:34 [1 reply]
iHID - rickeybell Friday, 05 February 2010 15:28 [4 replies]
Problem in ON INDEX GOTOL - Roshan Thursday, 04 February 2010 07:58 [3 replies]
not familar with uart or ? - Jon Chandler Thursday, 04 February 2010 04:25 [5 replies]
Will this fry my CPU? - Graham Thursday, 04 February 2010 03:40 [16 replies]





Thanks for going to the trouble of creating this tutorial...
Cheers Jon, sounds like a good rapid way to calibrate the...
Thanks Jon, should be working now - had some issues with ...
The video link appears to be broken by the way.
Nice work Graham! Love using a pot as the user interface...
The devices in the schematic above are actually user desi...
hello everyone,i am using a tlp 434 & rlp 434 rf module t...
I've been working on a better way to calibrate the servo ...