Menu

ADC and Charlieplexed LED's with 12F675 and PICKit 1

2015-02-09
2015-02-09
  • playinmyblues

    playinmyblues - 2015-02-09

    Here is some code I wrote today using a 12F675 in my PICKit 1. I thought it would be good to figure out how to use the 8-pin PIC instead of directly porting code from a 16F886 to a 16F684 (14 pins). The LED's on the PICKit 1 are charlieplexed so it is a little more involved than using 3 pins for 3 LED's. 3 Charlieplexed pins will let you use 6 LED's.

    The code is commented so it should be fairly easy to make your own version. The sample code that I looked at on the GCB site was the Potentiomter code called adc_pot.gcb.

    Code:
    [code]
    ' Program: adc3leds12f675.gcb
    ' The original Sample Program is adc_pot.gcb and uses an array of LEDs to represent the
    ' level of the turn of the pot. This program will use three LEDs to represent the level
    ' of the pot.

    'Chip Settings

    chip 12F675, 4

    ' for the PICKit 1 and a 12F675, the LED's D0 to D7 are charlieplexed
    ' Furthermore, the 12F675 takes up pins 1 to 4 and 11 to 14 in the Evaluation Socket.
    ' The pins are RA4, RA5, RA2, and RA1, or pins 3, 2, 5, and 6 of the 12F675 respecticely.
    ' See pages 31
    ' and 32 of the PICKit 1 Userguide for schematics. Here is a general description of how
    ' the LED's are charlieplexed (On in that direction means Off in the other):
    ' D0 is On from pins 3 to 2, D1 is on from pins 2 to 3
    ' D2 is On from pins 3 to 5, D3 is On from pins 5 to 3
    ' D4 is On from pins 2 to 5, D5 is On from pins 5 to 2
    ' D6 is On from pins 5 to 6, D7 is On from pins 6 to 5
    ' The above works when the first pin provided is the anode, the 2nd pin the cathode

    define LP1 GPIO.4 ' charlieplexed pin that LED is on 12F675 P3

    define LP2 GPIO.5 ' charlieplexed pin that LED is on 12F675 P2

    define LP3 GPIO.2 ' charlieplexed pin that LED is on 12F675 P5

    define LP4 GPIO.1 ' charlieplexed pin that LED is on 12F675 P6

    define potPin AN0 ' pin that AN0 is on and here, called potPin - pin 7

    Start:
    potValue = ReadAD(potPin)

    if potValue > 210 Then
       Goto ThreeLeds
    End If
    
    If potValue > 120 Then
       Goto TwoLeds
    End If
    
    If potValue > 10 Then
       Goto OneLed
    End If
    
    If potValue < 10 Then
       Goto ZeroLeds
    End If
    
    Goto Start
    

    ThreeLeds:
    Dir LP1 Out ' the GPIO pins have to be set to prevent unwanted LED's On
    Dir LP2 Out
    Dir LP3 Out
    Dir LP4 In
    Set LP1 On
    Set LP2 Off
    Set LP3 Off
    Wait 15 ms ' this period On and Off allows the LED's appear to be on all the time

    Dir LP1 Out
    Dir LP2 Out
    Dir LP3 In
    Dir LP4 In
    Set LP1 Off
    Set LP2 On
    Wait 15 ms
    
    Goto Start
    

    TwoLeds:
    Set LP1 On
    Set LP2 Off
    Wait 15 ms

    Set LP1 Off
    Set LP2 On
    Wait 15 ms
    
    Goto Start
    

    OneLed:
    Set LP1 On
    Set LP2 Off

    Goto Start
    

    ZeroLeds:
    Set LP1 Off
    Set LP2 Off

    Goto Start
    

    [/code]

     
  • playinmyblues

    playinmyblues - 2015-02-09

    Wherever you see bolded text above, place a # in the character space immediately before it or it probably will not work. I am still trying to figure out how to post code properly in this forum.

     
  • Hugh Considine

    Hugh Considine - 2015-02-09

    Nice work!

    The trick to posting code is to have lines before and after consisting of just tildes (these things: ~~). Not sure how many you need, but about 5 usually does it. Example:

    #chip 12F675
    #define LED GPIO.0
    PulseOut LED, 1 s
    

    Between the ~~~~~ lines, you can paste the code without needing to do any other special formatting.

     
  • playinmyblues

    playinmyblues - 2015-02-09

    Thank you Hugh Considine for the answer above. I see that the tildes work and the Code here HTML markup is not necessary.

     

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.