Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 05, 2016, 09:26:50 09:26


Login with username, password and session length


Pages: [1]
Print
Author Topic: PICBASIC PIC16F767 RGB Led Controller  (Read 7819 times)
0 Members and 1 Guest are viewing this topic.
PilotPTK
V.I.P
Junior Member
*****
Offline Offline

Posts: 40

Thank You
-Given: 5
-Receive: 23


« on: August 12, 2009, 05:43:02 05:43 »

Greeting Sonsivri,

This is a PICBASIC-PRO Program that I wrote to control my pool lights.  I have hundreds of RED, White and BLUE LED's around the edge of the pool, and this controller - through three N-CHannel MOSFET's handles the 'blending' of the three colors in different patterns.

I use a 20Mhz Crystal, but you could easily use anything from about 4-20Mhz.  All three PWM Channels are used on the F767 for blending the colors.  True 10-Bit PWM Control is implemented (rather than the 0-255 scheme that PICBASIC natively uses).

Interrupts are used which keep the fading of the colors VERY constant.

The entire fade can be shifted through 8 levels of brightness.

Here's the code!
Code:
'PIC16F767 Device Controlling Red/White/Blue LED's
'Written July 2, 2009
'Copyright (c) PilotPTK
'U1 Main LED Driver


DEFINE OSC 20
ADCON1 = %10001101 'Port A0,A1 Analog, VREF+ = VDD, Right Justified Result
CCP1CON = %00001100 'CCP Setup (PWM Mode)
CCP2CON = %00001100 'CCP Setup (PWM Mode)
CCP3CON = %00001100 'CCP Setup (PWM Mode)
ON INTERRUPT GOTO IntHandle
INTCON = %10100000 'Enable Global Interrupts and Timer Interrupts
OPTION_REG = %00000111 '(PreScaller = 256) (64mS = 1250 Counts... = 4.88 Timer Interrupts)

'CCPs to OUTPUT
TRISB.5 = 0
TRISC.1 = 0
TRISC.2 = 0

ButOnOff VAR PORTC.0
ButMode VAR PORTA.2
ButBright VAR PORTA.3
PhotoIN VAR PORTA.0
Photo5VRef VAR PORTA.1
X VAR WORD

TRISB.1 = 0
AUTOLed VAR PORTB.1
TRISB.2 = 0
ONLed VAR PORTB.2
TRISC.6 = 0
DebugSer VAR PORTC.6

TRISC.5 = 0
MOSI VAR PORTC.5 'Master Out Slave In (OUTPUT)
TRISB.0 = 1
MISO VAR PORTB.0 'Master In Slave Out (INPUT)
TRISC.3 = 0
SERDATA VAR PORTC.3

DutyR VAR WORD '0-64 Multiplied by Brightness
RupDown VAR BIT '0 = UP, 1 = DOWN
DutyW VAR WORD '0-64 Multiplied by Brightness
WupDown VAR BIT
DutyB VAR WORD '0-64 Multiplied by Brightness
BupDown VAR BIT
RWB VAR BYTE '1=R,2=W,3=B (Which are We Working On Right Now)
Brightness VAR WORD '1=*1, 2=*2, 3=*3, 4=*4, 5=*8, 6=*12, 7=*16
ONOff VAR Byte '1=Off, 2=On, 3=Auto
MODE VAR Byte
'Modes:
'1) All On
'2) Fade From Bottom Up ON and then Top Down OFF
'3) Rotate Through One Color, then the next
'4) Dim and Brigten All
'5) Flashing (All)
'6) Chasing
'7) Random Flashing
IntOver VAR Byte
IntCount VAR Byte
RndNum VAR WORD
JunkVar VAR WORD

'Set All LED's Off and Set PWM's to 0 Duty Cycle
PORTB.1 = 0
PORTB.2 = 0
HPWM 1,0,5000
HPWM 2,0,5000
HPWM 3,0,5000
T2CON = %00000101
PR2 = 255
CCP1CON.4 = 0
CCP1CON.5 = 0
CCPR1L = %00000000
CCP2CON.4 = 0
CCP2CON.5 = 0
CCPR2L = %00000000
CCP3CON.4 = 0
CCP3CON.5 = 0
CCPR3L = %00000000

DutyR = 0
DutyW = 0
DutyB = 0
Brightness = 1
ONOff = 1
MODE = 1 'Set Starting Mode to 1
IntOver = 0
IntCount = 4
RupDown = 0
WupDown = 0
BupDown = 0
RWB = 1
MOSI = 1

'-----------------------------------------------------------------------------------
MainLoop: IF ButOnOff = 1 THEN 'Step OnOff From 1 to 3
ONOff = ONOff + 1
IF ONOff > 3 THEN
ONOff = 1
ENDIF
PAUSE 400
ENDIF

IF ButMode = 1 THEN 'Step MODE From 1 TO 7
DutyR = 0
DutyW = 0
DutyB = 0
RupDown = 0
WupDown = 0
BupDown = 0
RWB = 1
MODE = MODE + 1
IF MODE > 7 THEN
MODE = 1
ENDIF
PAUSE 400
ENDIF

IF ButBright = 1 THEN 'Step Brightness
SELECT CASE Brightness
CASE 1
Brightness = 2
CASE 2
Brightness = 3
CASE 3
Brightness = 4
CASE 4
Brightness = 6
CASE 6
Brightness = 8
CASE 8
Brightness = 12
CASE 12
Brightness = 16
CASE 16
Brightness = 1
END SELECT
PAUSE 400
ENDIF

IF ONOff = 1 THEN
ONLed = 0
AUTOLed = 0
ENDIF
IF ONOff = 2 THEN
ONLed = 1
AUTOLed = 0
ENDIF
IF ONOff = 3 THEN
ONLed = 0
AUTOLed = 1
ENDIF

GoTo MainLoop
'-----------------------------------------------------------------------------------

'-----------------------------------------------------------------------------------
'Set PWM SubRoutines (Send From 0 to 1023 (10 bits) as DutyR)
DISABLE
PWMRed: JunkVar = DutyR * Brightness
IF JunkVar = 1024 THEN
JunkVar = 1023
Endif
CCP1CON.4 = JunkVar.0
CCP1CON.5 = JunkVar.1
CCPR1L = JunkVar >> 2
Return

PWMWhite: JunkVar = DutyW * Brightness
IF JunkVar = 1024 THEN
JunkVar = 1023
Endif
CCP2CON.4 = JunkVar.0
CCP2CON.5 = JunkVar.1
CCPR2L = JunkVar >> 2
Return

PWMBlue: JunkVar = DutyB * Brightness
IF JunkVar = 1024 THEN
JunkVar = 1023
Endif
CCP3CON.4 = JunkVar.0
CCP3CON.5 = JunkVar.1
CCPR3L = JunkVar >> 2
Return

ENABLE
'-----------------------------------------------------------------------------------

'-----------------------------------------------------------------------------------
'The Interrupt Handler
DISABLE
IntHandle:
IF IntOver < IntCount THEN 'Multiply Timer by 5 For 64mS Interrupts
IntOver = IntOver + 1
GoTo ExitInt
ENDIF
IF IntOver > 4 THEN
IntOver = 0
ENDIF

IF OnOff = 1 THEN TurnOFF
'Code Here For TurnOff if PhotoCell not Dark Enough.....
SELECT CASE MODE
CASE 1
GoTo Mode1
Case 2
GoTo Mode2
Case 3
GoTo Mode3
Case 4
GoTo Mode4
Case 5
GoTo Mode5
Case 6
GoTo Mode6
Case 7
GoTo Mode7
END SELECT

Mode1:
IntCount = 4
DutyR = 64
DutyW = 64
DutyB = 64
GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

Mode2:
IntCount = 4
IF RupDown = 0 Then 'We're Moving Up
IF DutyW = 64 AND DutyR < 64 Then
DutyR = DutyR + 1
GoTo Mode2Fin1
ENDIF
IF DutyB = 64 AND DutyW < 64 Then
DutyW = DutyW + 1
GoTo Mode2Fin1
ENDIF
IF DutyB < 64 Then
DutyB = DutyB + 1
ENDIF
Mode2Fin1: IF DutyR = 64 THEN
RupDown = 1
ENDIF
ENDIF
IF RupDown = 1 Then 'We're Moving Down
If DutyW = 0 AND DutyB > 0 Then
DutyB = DutyB - 1
GoTo Mode2Fin2
ENDIF
If DutyR = 0 AND DutyW > 0 Then
DutyW = DutyW - 1
GoTo Mode2Fin2
ENDIF
If DutyR > 0 Then
DutyR = DutyR - 1
ENDIF
Mode2Fin2: IF DutyB = 0 THEN
RupDown = 0
ENDIF
ENDIF
GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

Mode3: 'Red Up/Down, then White Up/Down, then Blue Up/Down > Red..
IntCount = 4
SELECT CASE RWB
CASE 1 'Red
SELECT CASE RupDown
CASE 0 'Moving Up
DutyR = DutyR + 1
IF DutyR = 64 THEN
RupDown = 1
ENDIF
CASE 1 'Moving Down
DutyR = DutyR - 1
IF DutyR = 0 THEN
RupDown = 0
RWB = 2 'Switch To White
ENDIF
END SELECT
CASE 2 'White
SELECT CASE WupDown
CASE 0
DutyW = DutyW + 1
IF DutyW = 64 THEN
WupDown = 1
ENDIF
CASE 1
DutyW = DutyW - 1
IF DutyW = 0 THEN
WupDown = 0
RWB = 3 'Switch To Blue
ENDIF
END SELECT
CASE 3 'Blue
SELECT CASE BupDown
CASE 0
DutyB = DutyB + 1
IF DutyB = 64 THEN
BupDown = 1
ENDIF
CASE 1
DutyB = DutyB - 1
IF DutyB = 0 THEN
BupDown = 0
RWB = 1 'Switch To Red
ENDIF
END SELECT
END SELECT
GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

Mode4:
IntCount = 4
SELECT CASE RupDown
CASE 0 'We're Moving Up
DutyR = DutyR + 1
DutyW = DutyR
DutyB = DutyR
IF DutyR = 64 Then
RupDown = 1
ENDIF
CASE 1
DutyR = DutyR - 1
DutyW = DutyR
DutyB = DutyR
IF DutyR = 0 Then
RupDown = 0
ENDIF
END SELECT
GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

Mode5:
IntCount = 15
SELECT CASE RupDown
CASE 0 'Turn On
DutyR = 64
DutyW = 64
DutyB = 64
RupDown = 1
CASE 1
DutyR = 0
DutyW = 0
DutyB = 0
RupDown = 0
END SELECT
GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

Mode6:
IntCount = 15
IF DutyR = 0 AND DutyW = 0 AND DutyB = 0 Then
DutyR = 64
ENDIF
IF DutyR = 64 THEN
DutyW = 64
DutyR = 0
GoTo Mode6Exit
ENDIF
IF DutyW = 64 THEN
DutyB = 64
DutyW = 0
GoTo Mode6Exit
ENDIF
IF DutyB = 64 THEN
DutyR = 64
DutyB = 0
ENDIF
Mode6Exit: GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

Mode7:
IntCount = 15
RANDOM RndNum
DutyR.0 = RndNum.0
DutyR.1 = RndNum.1
DutyR.2 = RndNum.2
DutyR.3 = RndNum.3
DutyR.4 = RndNum.4
DutyR.5 = RndNum.5

DutyW.0 = RndNum.5
DutyW.1 = RndNum.6
DutyW.2 = RndNum.7
DutyW.3 = RndNum.8
DutyW.4 = RndNum.9
DutyW.5 = RndNum.10

DutyB.0 = RndNum.10
DutyB.1 = RndNum.11
DutyB.2 = RndNum.12
DutyB.3 = RndNum.13
DutyB.4 = RndNum.14
DutyB.5 = RndNum.15
GoSub PWMRed
GoSub PWMWhite
GoSub PWMBlue
GoTo ExitInt

TurnOff:
DutyR = 0
DutyW = 0
DutyB = 0
GoSub PWMRed
GOSub PWMWhite
GOSub PWMBlue
GoTo ExitInt

ExitInt: INTCON.2 = 0
RESUME
ENABLE
'-----------------------------------------------------------------------------------
Logged
PilotPTK
V.I.P
Junior Member
*****
Offline Offline

Posts: 40

Thank You
-Given: 5
-Receive: 23


« Reply #1 on: August 13, 2009, 09:42:29 21:42 »

If I have some time this weekend, I will try to remember to take a short video of it in action.  It really is quite beautiful - I get a LOT of compliments on it.

PPtk
Logged
Faros
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 125
-Receive: 178


« Reply #2 on: August 15, 2009, 02:41:33 02:41 »

Thank you for sharing this effort, It is nice to know that some of Sonsivri members are rich enough to have private pools  Grin… just kidding, I have a nice Villa on the Mediterranean  Wink

I would like if you can post the circuit schematics and also a .hex file where we can simulate the circuit in action using Protues, and my be we can have a little chat over MOSFET driving, controller location …etc. 
Logged
nileshhande
Junior Member
**
Offline Offline

Posts: 70

Thank You
-Given: 22
-Receive: 5


« Reply #3 on: August 17, 2009, 06:59:19 18:59 »

Hi, Have been hunting for this kind of project.. Could you pls post the circuit schematic... Thanks..
Logged
selim
Guest
« Reply #4 on: August 17, 2009, 08:09:11 20:09 »

Could you please post the  schematic...

Thanks
Logged
charudatt
Active Member
***
Offline Offline

Posts: 110

Thank You
-Given: 47
-Receive: 69


« Reply #5 on: September 25, 2009, 09:28:09 09:28 »

do you have any code example for driving a TLC5940 IC using PICBASIC Pro.

Posted on: September 25, 2009, 09:19:04 09:19 - Automerged

http://www.picbasic.co.uk/forum/showthread.php?t=10564

I think this should also be interesting to try out.

regards
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC