Newbie help. RGB Led on a PIC10F202


Closed Thread
Results 1 to 40 of 42

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default 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.

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default 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.
    Last edited by cncmachineguy; - 14th August 2011 at 04:13.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default 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

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default 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

  5. #5
    Join Date
    Aug 2011
    Posts
    16


    Did you find this post helpful? Yes | No

    Default 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.

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default 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

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default 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?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default 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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts