Menu

PIC12f675 RGB LED with PS/2 controls

Help
2009-06-26
2013-05-30
  • Andrei Cociuba

    Andrei Cociuba - 2009-06-26

    ok, so i wanted a more or less controllable rgb lamp, with manual options to individually increase and decrease the intensity of each color (red, green, or blue). i figured individual pins for each button of increase/decrease of the colors would need an awful lot of input pins, so i adapted the morse code sender with ps/2 keyboard for this purpose. two pins for tens of input buttons is quite a bargain.

    the pic is supposed to pwm three outputs for each color, and modify the duty-cycle, according to input from keyboard.

    i wrote the soft-pwm subroutine myself, because i could not completely understand the usage of the built-in feature from the help file.

    this test version is supposed to perform initial lighting for each individual color, then for all together, and then set the lights dim.
    pressing enter should fully light the lights, while pressing escape should extinguish them.

    the  only problem is, it won't work. WHY? on keypress the lights should go completely off for a limited amount of time, to indicate recognition of keypress.
    here is the code:

    #chip 12F675, 4

    'PS2 port settings
    #define PS2Clock GPIO.5
    #define PS2Data GPIO.4
    DIR ps2clock in
    DIR ps2data in

    'RGB LED Settings
    #define red gpio.0
    #define blue gpio.1
    #define green gpio.2
    dir red out
    dir blue out
    dir green out

    set green off
    set blue off
    set red off

    'Remaining Port
    #define free gpio.3
    dir free out
    set free off

    'init
    set red on
    wait 1 s
    set red off
    set green on
    wait 1 s
    set green off
    set blue on
    wait 1 s
    set blue off
    set red on
    set blue on
    set green on
    wait 1 s
    set red off
    set green off
    set blue off

    'set initial dim duty-cycles
    ducr=20
    ducg=20
    ducb=20

    'Red-Green-Blue PWM subroutine
    sub rgb(dr, dg, dcb)#NR

    for i = 1 to 255
        if i<=dr then set red on
        if i>dr then set red off
       
        if i<=dg then set green on
        if i>dg then set green off
       
        if i<=dcb then set blue on
        if i>dcb then set blue off
    next

    end sub

    main:
    rgb ducr, ducg, ducb
    KeyIn = INKEY

    if KeyIn = 0 then
    goto main
    end if
    wait 150 ms

    'Escape - turn off lights
    if KeyIn = 27 then
    set red off
    set green off
    set blue off
    wait 1 s
    set red on
    set green on
    set blue on
         ducr=0
        ducg=0
        ducb=0
        goto main
    end if

    'if ENTER is pressed, turn full on
    if KeyIn = 13 then
    set red off
    set green off
    set blue off
    wait 1 s
    set red on
    set green on
    set blue on
        ducr=255
        ducg=255
        ducb=255
        goto main
    end if   

    goto main

     
    • kent_twt4

      kent_twt4 - 2009-06-30

      Hope you got the PS2 interface going, that would give you lots of options.  Have not used that code, but looks good.

      For a more local interface I could envision using three GPIO/PortPin inputs, five buttons, plus the three (rgb) led outputs.

      Two inputs would include a button each, for increase and decrease of dutycycle. The other input would be three buttons multiplexed on a resistor divider network, creating analog voltage/bit windows offset above the maximum ground level read.

      Use a state machine code loop, which could then branch to a given color led, based on the third input (i.e. multiplexed buttons on resistor divider network).  The individual color PWM duty cycle is then incremented/decremented, depending on which of the other two input buttons is pressed.

      Here would be an example of using the MCLR(E) button input to decrement the duty cycle on a GPIO/PortPin using the GCBasic software PWM.  One could then expand/improve on this to accomplish individual control, of all the colors of the rgb led.

      Have used a similar synchronous start like you have implemented in the rgb sub,  that is the way to go for sure.

      'An example for controlling a PWM duty cycle with a button input.
      #chip 18f14K50,12
      #CONFIG FOSC = HS, MCLRE = OFF, WDTEN = OFF, PWRTEN = ON
      #Config  BOREN = OFF, LVP = OFF, PLLEN = OFF, HFOFST = OFF
      #Config WRTC = OFF, IESO = ON
      #CONFIG XINST = OFF, USBDIV = OFF, CPUDIV = NoClkDiv , PCLKEN = ON

      #define PWM_Out1 PortC.0
      #define decrDuty PortA.3
      dir PWM_Out1 out
      dir decrDuty in    'RA3 is always set as input no matter the dir stmt
      #define numcycles 10
      redPWM = 255

      Main:
      Do while decrDuty Off
          redPWM -= 1
          If redPWM = 0 then redPWM = 255
          PWMOut(1, redPWM, numcycles)  'channel 1, duty cycle, number of cycles
      loop
      PWMOut(1, redPWM, numcycles)
      Goto Main

       
    • Nobody/Anonymous

      fascinating approach to limit necessary input pins. i shall try it.

      i know my code looks good. IT LOOKS BRILLINATLY GOOD to me. YET IT DOESN'T WORK.
      and the ps2 interface is exactly what i can't make to function in this case.

      i would still appreciate some help here, to understand what i did wrong.... because something is obviously wrong, since it does not work.
      even if ultimately i will take another approach.....

       
    • Andrei Cociuba

      Andrei Cociuba - 2009-06-30

      sorry about that anonimous post. i did not see i was logged out.... it is still me...

       
    • kent_twt4

      kent_twt4 - 2009-06-30

      Re-looked at the original post and saw that the internal osc has not been enabled.  As default GCBasic assumes _HS_OSC (i.e. external crystal/resonator).  Use #config  _INTRC_OSC_NOCLKOUT and you should be good to go with PS2.

       
    • Andrei Cociuba

      Andrei Cociuba - 2009-06-30

      thsat issue i have already took care of, but later, when transferring the hex file into the PIC with ICPROG. there is a checkbox in icprog for just that.

      for example, the morse code example works fine,(tailored to the pic 12f675, and with no lcd, for i have none...), but as soon as i replace "goto main" with something else.... it looks like it is not working any more:

      [paste from example code]
      'Get a keypress
      KeyIn = INKEY
      if KeyIn = 0 then goto Main

       
    • kent_twt4

      kent_twt4 - 2009-06-30

      You may have to explain more about changing the goto Main, as that could mean any number of things.  As long as you define where the goto's associated label is, it just shouldn't matter.

      Main:
      KeyIn = INKEY
      if KeyIn = 0 then goto Here
      ...
      ...
      ...
      Here:
      ...
      ...
      goto Main

      Or do you mean..........

      Start:
      KeyIn = INKEY
      if KeyIn = 0 then goto Start
      ....
      ....
      goto Start

      By the way the fonts on the new test forum are horrible and have told them so.  Mostly underwhelmed about the new forum look and function, can only hope for more.

       
    • Andrei Cociuba

      Andrei Cociuba - 2009-07-01

      well, in the example, if no key is pressed, the program returns to waiting  for a key. it goes to the main label again. whenever i modify that, and tell it to do something else before waiting for a new key, it stops working properly....

       
    • kent_twt4

      kent_twt4 - 2009-07-01

      The only other thought on this would be to run the InKey function off an interrupt, which would be based on the serial data line.  Presumably you would be looking for a negative going pulse based on the start bit of the PS2 communications.  Good Luck.

       
    • Andrei Cociuba

      Andrei Cociuba - 2009-07-01

      well... how do i do that? i can barely say that i know what in interrupt is, let alone how to generate one....

       
  • Nobody/Anonymous

    #include <ps/2.h>

     
  • Hugh Considine

    Hugh Considine - 2011-05-13

    Will have to lock this discussion due to spambots filling it with 118 spam posts, hopefully no-one needs to add to an almost 2 year old topic anyway!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.