PDA

View Full Version : Newbie help. RGB Led on a PIC10F202



brianmorris
- 14th August 2011, 00:12
Hello all,

I have a piece of code for PBP that i guess i need help with and am hoping someone will be able to point out the issue. Its a simple RGB LED circuit that when you push the button, should flip to the next color in a subroutine then at the end of the colors, go back to the start. Posting my code below, it may be the incorrect way to do it. It just sits on 'Red' as its the value for ButtonCount. I can change that value to 1,2,3 & compile/program and get ir to display the correct color, but it will not switch colors. Any suggestions will be much appreciated. Thanks in advance.




Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 1

;Make sure LEDs are off
HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin = 1 THEN ButtonCount = ButtonCount + 1
GOTO SWITCHLOOP
END

SWITCHLOOP:
If ButtonCount = 1 THEN GOSUB RedProgram
If ButtonCount = 2 THEN GOSUB GreenProgram
If ButtonCount = 3 THEN GOSUB BlueProgram
If ButtonCount = 4 THEN ButtonCount = 1
GOTO SWITCHLOOP
END

RedProgram:
PWM Red,127,100
RETURN

GreenProgram:
PWM Green,127,100
RETURN

BlueProgram:
PWM Blue,127,100
RETURN

END

cncmachineguy
- 14th August 2011, 00:31
Once you enter switchloop, you seem to never leave

brianmorris
- 14th August 2011, 00:53
Once you enter switchloop, you seem to never leave

I put the IF statement that is in the MAIN portion into the SWITCHLOOP portion and same thing happens. Am I just doing it wrong altogether?

bogdan
- 14th August 2011, 01:40
At the start up you going straight to the SWITCHLOOP routine, light up the red, then you never go back to the Main routine to check when SwitchPin became 1 (to increment the ButtonCount to change the color)

mister_e
- 14th August 2011, 01:46
Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 1

;Make sure LEDs are off
HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin THEN
If ButtonCount = 1 THEN GOSUB RedProgram
If ButtonCount = 2 THEN GOSUB GreenProgram
If ButtonCount = 3 THEN GOSUB BlueProgram
If ButtonCount = 4 THEN ButtonCount = 1
ButtonCount = ButtonCount + 1
endif
GOTO MAIN

RedProgram:
PWM Red,127,100
RETURN

GreenProgram:
PWM Green,127,100
RETURN

BlueProgram:
PWM Blue,127,100
RETURN

Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 1

;Make sure LEDs are off
HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin THEN
If ButtonCount = 1 THEN
PWM Red,127,100
ENDIF
If ButtonCount = 2 THEN
PWM Green,127,100
ENDIF
If ButtonCount = 3 THEN
PWM Blue,127,100
ENDIF
If ButtonCount = 4 THEN ButtonCount = 1
ButtonCount = ButtonCount + 1
endif
GOTO MAIN

mister_e
- 14th August 2011, 01:58
Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 0

;Make sure LEDs are off
HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin = 1 THEN
If ButtonCount<3 THEN
PWM GPIO.0(ButtonCount),127,100
ButtonCount = ButtonCount + 1
ELSE
ButtonCount = 0
ENDIF
endif
GOTO MAIN

mister_e
- 14th August 2011, 02:09
Mistake in my 2 first example. Change made in RED



Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 1

;Make sure LEDs are off
HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin THEN
If ButtonCount = 1 THEN GOSUB RedProgram
If ButtonCount = 2 THEN GOSUB GreenProgram
If ButtonCount = 3 THEN GOSUB BlueProgram
If ButtonCount = 4 THEN
ButtonCount = 1
else
ButtonCount = ButtonCount + 1
endif
endif
GOTO MAIN

RedProgram:
PWM Red,127,100
RETURN

GreenProgram:
PWM Green,127,100
RETURN

BlueProgram:
PWM Blue,127,100
RETURN


Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 1

;Make sure LEDs are off
HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin THEN
If ButtonCount = 1 THEN
PWM Red,127,100
ENDIF
If ButtonCount = 2 THEN
PWM Green,127,100
ENDIF
If ButtonCount = 3 THEN
PWM Blue,127,100
ENDIF
If ButtonCount = 4 THEN
ButtonCount = 1
else
ButtonCount = ButtonCount + 1
endif
endif
GOTO MAIN
sorry :(

cncmachineguy
- 14th August 2011, 02:13
Steve, wonderful examples, I think th OP may have a few questions for you, especially in the last with port indexing. But why doesn't his work after he moved the IF into the switchloop section? BTW, he will still have problems with button press being 1 for a zillion loop iterations, don't ya think?

@Brian, if you press the switch repeatedly, will that make the other LED's light? If so, does it seem to be random as to which will light?

brianmorris
- 14th August 2011, 02:22
Steve, wonderful examples, I think th OP may have a few questions for you, especially in the last with port indexing. But why doesn't his work after he moved the IF into the switchloop section? BTW, he will still have problems with button press being 1 for a zillion loop iterations, don't ya think?

@Brian, if you press the switch repeatedly, will that make the other LED's light? If so, does it seem to be random as to which will light?

Many thanks Steve for those examples. I tried both of your last ones just above this post and neither seem to be working. It does not light upon circuit power nor if i press the button to cycle through the other colors.

cncmachineguy
- 14th August 2011, 02:52
Ok, new test for you to try. First make sure all three PWM's are working.Let you main call them 1 at a time like this:


main:
gosub red
gosub blue
gosub green
goto main

I know you said in your first post that all three worked, lets just be sure, then we can build on that .

brianmorris
- 14th August 2011, 02:56
Ok, new test for you to try. First make sure all three PWM's are working.Let you main call them 1 at a time like this:


main:
gosub red
gosub blue
gosub green
goto main

I know you said in your first post that all three worked, lets just be sure, then we can build on that .

Works perfectly. Didnt work at first, but then i forgot the OPTION_REG.5 = 0 had to be put into the code for GPIO.2 to work.

cncmachineguy
- 14th August 2011, 03:18
Does the whole thing work, button and everything?

brianmorris
- 14th August 2011, 03:38
No just the code that you asked me to try.

cncmachineguy
- 14th August 2011, 03:48
Now let's make sure that button is working. Modify the main to be this:


main:
Gosub red
If button =1 then goto skipgreen
Gosub green
Skipgreen:
Gosub blue
Goto main


That should have all three lit as before unless you press the button, then it should skip the green gosub. Btw, is the tris for the button set to input? Also I am a lazy typer, so I assume you will make the names be what they need to be like button shoud really be switchpin (i think)

brianmorris
- 14th August 2011, 03:58
Im not sure how to set tris on GPIO.3 I assumed because the datasheet says its an input only pin, i didnt have to futz with it. When i run your code above, i get Red, Green, Back to Red. If i tap the switch, the whole circuit turns off and stays off while i hold it. I never see blue.

cncmachineguy
- 14th August 2011, 04:09
Well thats just wierd that it skips blue always. but the the whole circuit turning off makes perfect sense. If you look at the gp3 pin, it is input only because it is shared with MCLR. that is the external reset pin. Since you have no configs listed, I assume you are using the defaults. I went and looked at 10f202.inc in the PBP folder and the default config has MCLR_ON. this means GP3 is set to be the reset when low. so I am sure you are reseting the chip, or at least not looking at it as an input.

2 choices here: Assuming you are NOT using PBP3, you can change the .inc file to MCLR_OFF and leave it as that, or you can comment the configs all together and put them in your code;

If you are using PBP3, you can use the brand new #config block format and set your configs in your code

BTW, I am going to assume the blue not working may have something to do with the MCLR issue for now. Also I guess you are right about the TRIS, but I would get in the habit of just ALWAYS setting it. Life will be easier later for you that way.

Last edit: Please post your current code now so we know where you are.

brianmorris
- 14th August 2011, 04:59
Code posted below. I dont get whats going on now. When i power the circuit on, it blinks red, pauses, then blinks green, pauses and goes back to red. I am not pressing the switch at all. And i turned off MCLRE in the INC file like you suggested. I see nothing in the code saying to blink. When i press the button, it stops doing everything until i power cycle the circuit.



OPTION_REG.5 = 0

Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3

ButtonCount VAR BYTE
ButtonCount = 1

HIGH Red
HIGH Green
HIGH Blue

MAIN:
IF SwitchPin = 1 THEN
If ButtonCount = 1 THEN
PWM Red,127,100
ENDIF
If ButtonCount = 2 THEN
PWM Green,127,100
ENDIF
If ButtonCount = 3 THEN
PWM Blue,127,100
ENDIF
If ButtonCount = 4 THEN
ButtonCount = 1
else
ButtonCount = ButtonCount + 1
endif
endif
GOTO MAIN

Jerson
- 14th August 2011, 06:40
Hi Brian

Take a look at this code. ButtonCount starts with 0 and then loops between 1 and 3 depending on your button presses. You may want it to roll over to 0 and have an all off program too. EDIT : My post assumes you are using a high input as indication of switch being pressed.



Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3
ButtonCount VAR BYTE

ButtonCount = 0 ;Make sure LEDs are off

HIGH Red
HIGH Green
HIGH Blue

MAIN:
SWITCHLOOP:
if SwitchPin = 1 then
ButtonCount = ButtonCount+1
if ButtonCount > 3 then ButtonCount = 1
while SwitchPin = 1: wend ' wait here while the switch remains pressed
endif

If ButtonCount = 1 THEN GOSUB RedProgram
If ButtonCount = 2 THEN GOSUB GreenProgram
If ButtonCount = 3 THEN GOSUB BlueProgram
GOTO SWITCHLOOP

END

RedProgram:
PWM Red,127,100
RETURN

GreenProgram:
PWM Green,127,100
RETURN

BlueProgram:
PWM Blue,127,100
RETURN

END

brianmorris
- 14th August 2011, 06:45
I wish i could reply back and say that works.. But it doesnt. Its randomly cycling through each color in order (i.e.; Red, Green, Blue, etc.) without me pressing anything. Also, Im thinking i screwed up in the code i provided where the color should always stay lit UNTIL the switch is pressed, so the program always needs to be waiting for a switch press, then it changes color.

Jerson
- 14th August 2011, 08:02
I suspect the switch is connected between the pin and ground effectively giving you a logic low for a switch press. That is why the colors are cycling.
If this is truly the case, then you can do well to change these lines in switchloop


if SwitchPin = 0 then ButtonCount = ButtonCount+1 if ButtonCount > 3 then ButtonCount = 1 while SwitchPin = 0: wend ' wait here while the switch remains pressed endif

cncmachineguy
- 14th August 2011, 11:14
Good morning Brian, well morning for me anyway. We need to know a bit about your circuit. How is the switch actually connect to the pic? Do you have a pull up/down resistor?

mister_e
- 14th August 2011, 11:26
Yup, or there's no pull-up/down resistor. whatever ;)

'-------------------------------------------------------------------------------------------
'
' The following have been tested, no problem at all
'
' Hardware description
' 1) LEDs connected betwee PIC I/O and GND via resistor
' 2) Push button between PIC and GND... nothing else.
'
'-------------------------------------------------------------------------------------------

@ __config _WDT_OFF & _MCLRE_OFF & _CP_OFF
' Disable watch dog timer
' Disable MLCR, GP3 as I/O
' Disable Code Protection

Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3 ' Active low

OPTION_REG = %00000000
' -0-------- _GPWU -- Disable Wake-Up on pin change
' --0------- _GFPPU - Enable Weak Pull-Ups
' ---0------ T0CS --- Timer0 Clock Source Internal (O T0CKI pin, 0 Internal)
' ----xxxxx- Timer0 related stuff... don't care

TRISIO = 0 ' Clear all I/Os
GPIO = %00001000 ' GP3: Input, other pin: Output

ButtonCount VAR BYTE

MAIN:
IF !SwitchPin THEN ' Switch down?
' - YES
If ButtonCount = 3 THEN ButtonCount = 0 ' - Currently on GP3 (MCLR)? If so, pass go, claim 200$
pwm ButtonCount,127,100 ' - Light Show
WHILE !SwitchPin : wend ' - Spin 'till button is released
pAUSE 50 ' - arbitrary debouince delay
ButtonCount = ButtonCount + 1 ' - Point to next LED
endif '
GOTO MAIN

brianmorris
- 14th August 2011, 17:37
Simple circuit. VDD hooked up to 3.7v, GPIO.0 to Red Lead, GPIO.1 to Green Lead, GPIO.2 to Blue Lead, GPIO.3 to Switch and ground when pressed, Vss hooked to Ground. And of course resistors coming from each of the I/O pins to the LED cathodes while the anode goes to 3.7v.

Steve, When i run your code i get all three leds on, making white, press button i get Bright Blue, press button again i get more of a regular blue, again i get a dim green with both green and red leds lit.

But at least the switch is working.

cncmachineguy
- 14th August 2011, 18:09
So If the switch is not pressed, the input pin is floating, unless there are weak pull-ups in play here. If not, try adding a pull-up to the input. This way the input is either high or low, but never allowed to float.

brianmorris
- 14th August 2011, 18:52
I connected a 10k resistor from VCC to GPIO.3 and then the button is still connected to GPIO.3 and GND. Push the button, the color changes, but still the same as above.

sayzer
- 14th August 2011, 19:55
Your LEDs are conected to Vdd instead of GND.
And GPIO is set to 0, so initially all pins are low at the beginning and thus LEDs are on.
When a PWM cycle is done, the pin stays low again and the LED is still on.

I am guessing Steve forgat to add these in his last posted code above.
:)

Before "pwm ButtonCount,127,100", insert these:


HIGH RED
HIGH GREEN
HIGH BLUE



So the new code will be as below:

MAIN:

IF !SwitchPin THEN ' Switch down?

' - YES

If ButtonCount = 3 THEN ButtonCount = 0 ' - Currently on GP3 (MCLR)? If so, pass go, claim 200$

HIGH RED

HIGH GREEN

HIGH BLUE
pwm ButtonCount,127,100 ' - Light Show

WHILE !SwitchPin : wend ' - Spin 'till button is released

pAUSE 50 ' - arbitrary debouince delay

ButtonCount = ButtonCount + 1 ' - Point to next LED

endif '
GOTO MAIN

brianmorris
- 14th August 2011, 20:06
I have one RGB LED with a common anode thats hooked up to VDD. The individual pins are connected to the I/O ports on the chip. I entered in the code above but all colors are still on. The even funnier part is i even commented out those lines including the PWM line and all three are still on. I commented out just the PWM line, same thing. I even erased the chip, verified erase and reprogrammed.

sayzer
- 14th August 2011, 20:10
cmcon0 = 0


:)

brianmorris
- 14th August 2011, 20:17
Error[113] c:\pbp\pbppic12.lib 423 : Symbol not previously defined (CMCON0). A have a whole bunch of these.

Bruce
- 14th August 2011, 22:29
The 10F202 doesn't have a CMCON0 register so leave that out.

See if this works;


@ __config _WDT_OFF & _MCLRE_OFF & _CP_OFF

Red var GPIO.0
Green var GPIO.1
Blue var GPIO.2
SwitchPin var GPIO.3 ' Active low

RD CON %11111110 ' red
GR CON %11111101 ' green
BL CON %11111011 ' blue
YL CON GR & RD ' green + red = yellow
CY CON GR & BL ' green + blue = cyan
PR CON BL & RD ' blue + red = purple
WT CON GR & BL & RD ' green + blue + red = white
OF con %11111111 ' all LEDs off

ButtonCount VAR BYTE

OPTION_REG = %00000000 ' GPIO.2 for GP use
TRISIO = %00001000 ' GP3: Input, other pin: Output
GPIO = %00000111 ' LEDs off
ButtonCount = 0

MAIN:
IF !SwitchPin THEN ' Switch down?
WHILE !SwitchPin : wend ' - Spin 'till button is released
PAUSE 50 ' - arbitrary debouince delay
ButtonCount = ButtonCount + 1

SELECT CASE ButtonCount
CASE 1
GPIO = RD
CASE 2
GPIO = GR
CASE 3
GPIO = BL
CASE 4
GPIO = YL
case 5
GPIO = CY
case 6
GPIO = PR
case 7
GPIO = WT
case IS > 7
GPIO = OF
ButtonCount = 0
END SELECT

ENDIF
GOTO MAIN
END

The PWM command returns the output pin to an input after it finishes so that might be causing some funkiness. Try just placing color values on the port and see if this helps.

brianmorris
- 15th August 2011, 00:04
Switch works. Just not following the colors you have in each of the case statements. In order of presses in goes: Red, Green,Blue, Yellow for a split second, White, Cyan, Red, No White then cycles over again as it should.

We are getting closer though and very much appreciate this. This is my first project with any of PIC MCUs as well as my first with PICBASIC Pro.

Bruce
- 15th August 2011, 00:13
My bad. Change the + to & in the color mix constants...;o)

YL CON GR & RD, etc,,

mister_e
- 15th August 2011, 00:14
Steve, When i run your code i get all three leds on, making white, press button i get Bright Blue, press button again i get more of a regular blue, again i get a dim green with both green and red leds lit.

But at least the switch is working.
I guess i've been lucky then, modifs in RED



TRISIO = 0 ' Clear all I/Os
GPIO = %00001000 ' GP3: Input, other pin: Output

ButtonCount VAR BYTE
CLEAR
MAIN:
IF !SwitchPin THEN ' Switch down?
' - YES
If ButtonCount = 3 THEN ButtonCount = 0 ' - Currently on GP3 (MCLR)? If so, pass go, claim 200$
PWM ButtonCount,127,100 ' - Light Show
LOW ButtonCount
WHILE !SwitchPin : WEND ' - Spin 'till button is released
PAUSE 50 ' - arbitrary debouince delay
ButtonCount = ButtonCount + 1 ' - Point to next LED
endif '
GOTO MAIN

brianmorris
- 15th August 2011, 00:30
Bruce, your code is working perfectly. I can cycle through every color, the only thing I am having trouble with is maybe the switch is a tad too sensitive. I am thinking maybe using TMR0 where if i hold down the switch for 1-2 seconds, it will then change colors.

Bruce
- 15th August 2011, 00:35
Probably just a really bad case of switch bounce. Try adjusting the delay something like this;



WHILE !SwitchPin
PAUSE 50 ' adjust this until it starts acting right.
WEND

That might do it. If not place this same code at the bottom just before it returns to Main.

You could also increment a variable in the WHILE loop. When it reaches a certain value it will indicate approx how long the switch was held down.

sayzer
- 15th August 2011, 02:19
In Steve's code, PWM command should be in the main loop.

mister_e
- 15th August 2011, 15:07
..........huh?

Dick Ivers
- 15th August 2011, 22:42
Just curious, but what is the meaning of the exclamation point notation?
WHILE !SwitchPin

cncmachineguy
- 16th August 2011, 00:10
That's an easy way to check if the value is 0. It means if switch is not 1.....
Alternately, if switch means if switch =1 then. ....

brianmorris
- 17th August 2011, 05:19
Well guys, things are working great so i more than thank you all for your wonderful help. This has been an experience for me and will continue to be as I convert all the code for this 10f202 to a 16f684. I wanted the EEPROM to store the state of the color after every change of color (after 10 seconds of on the same color so as to prevent excessive writes), so when the circuit comes back on again, it will grab the value from EEPROM. I also wanted the hardware PWM to control the LED color.

brianmorris
- 19th August 2011, 19:02
I think for the most part i have been able to successfully port it, except that the SwitchPin does not work properly on the 16F684. My code is below:



SwitchPin var PORTA.2 ' Active low

RD CON %00010010 ' red
GR CON %00110000 ' green
BL CON %00100010 ' blue
YL CON GR & RD ' green + red = yellow
CY CON GR & BL ' green + blue = cyan
PR CON BL & RD ' blue + red = purple
WT CON GR & BL & RD ' green + blue + red = white
OF con %11111111 ' all LEDs off

ButtonCount VAR BYTE
ButtonCount = 0

PORTC = %00111010 ' LEDs off
TRISC = %00000010 ' PC2: Input, other pin: Output

MAIN:
IF !SwitchPin THEN
WHILE !SwitchPin
pAUSE 200
wend
ButtonCount = ButtonCount + 1

SELECT CASE ButtonCount
CASE 1
PORTC = RD
CASE 2
PORTC = GR
CASE 3
PORTC = BL
CASE 4
PORTC = YL
case 5
PORTC = CY
case 6
PORTC = PR
case 7
PORTC = WT
case IS > 7
PORTC = OF
ButtonCount = 0
END SELECT

ENDIF
GOTO MAIN
END


I hope its something simple like my TRIS or PORT registers. Thanks much.

mister_e
- 19th August 2011, 19:34
http://www.picbasic.co.uk/forum/showthread.php?t=561&page=1