-
Newbie help. RGB Led on a PIC10F202
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.
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
Once you enter switchloop, you seem to never leave
-
Re: Newbie help. RGB Led on a PIC10F202
Quote:
Originally Posted by
cncmachineguy
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?
-
Re: Newbie help. RGB Led on a PIC10F202
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)
-
Re: Newbie help. RGB Led on a PIC10F202
Code:
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
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
Mistake in my 2 first example. Change made in RED
Code:
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
Code:
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 :(
-
Re: Newbie help. RGB Led on a PIC10F202
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?
-
Re: Newbie help. RGB Led on a PIC10F202
Quote:
Originally Posted by
cncmachineguy
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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:
Code:
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 .
-
Re: Newbie help. RGB Led on a PIC10F202
Quote:
Originally Posted by
cncmachineguy
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:
Code:
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.
-
Re: Newbie help. RGB Led on a PIC10F202
Does the whole thing work, button and everything?
-
Re: Newbie help. RGB Led on a PIC10F202
No just the code that you asked me to try.
-
Re: Newbie help. RGB Led on a PIC10F202
Now let's make sure that button is working. Modify the main to be this:
Code:
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)
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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.
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
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.
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
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?
-
Re: Newbie help. RGB Led on a PIC10F202
Yup, or there's no pull-up/down resistor. whatever ;)
Code:
'-------------------------------------------------------------------------------------------
'
' 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
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
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:
Code:
HIGH RED
HIGH GREEN
HIGH BLUE
So the new code will be as below:
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
-
Re: Newbie help. RGB Led on a PIC10F202
Error[113] c:\pbp\pbppic12.lib 423 : Symbol not previously defined (CMCON0). A have a whole bunch of these.
-
Re: Newbie help. RGB Led on a PIC10F202
The 10F202 doesn't have a CMCON0 register so leave that out.
See if this works;
Code:
@ __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.
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
My bad. Change the + to & in the color mix constants...;o)
YL CON GR & RD, etc,,
-
Re: Newbie help. RGB Led on a PIC10F202
Quote:
Originally Posted by
brianmorris
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
Code:
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
-
Re: Newbie help. RGB Led on a PIC10F202
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.
-
Re: Newbie help. RGB Led on a PIC10F202
Probably just a really bad case of switch bounce. Try adjusting the delay something like this;
Code:
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.
-
Re: Newbie help. RGB Led on a PIC10F202
In Steve's code, PWM command should be in the main loop.
-
Re: Newbie help. RGB Led on a PIC10F202
-
Re: Newbie help. RGB Led on a PIC10F202
Just curious, but what is the meaning of the exclamation point notation?
WHILE !SwitchPin
-
Re: Newbie help. RGB Led on a PIC10F202
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. ....
-
Re: Newbie help. RGB Led on a PIC10F202
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.