LED 'Dot' Bargraph Issues - Newbie Code!


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    50

    Default LED 'Dot' Bargraph Issues - Newbie Code!

    Hello all.
    I have a PIC16F88 with ADC on PortA.0. I am measuring the voltage on a supply rail from 0-5VCD. I have a 35 LED display (3mm LEDs each) that is wired in a 7x5 matrix. I only want one LED to come on at any one time. Hence a moving 'dot'. The code works for the most part, but I have a minor annoyance. If I slowly raise the voltage I am measuring, there will be certain places on the dot display that has 2 x LEDs coming ON and flickering. It is like the ADC is just between bits and oscillates between two thus displaying 2 LEDs instead of one.

    The code most certainly can be re-written a whole lot better and more efficiently. In my code, I simply look for values of adval and turn on the appropriate LED. On each LED turn-on, I also run a subroutine that first sets all the LEDs OFF. The ADC is set up as 8 bit. I know that is way more than what I need for resolution. 6 bit would probably work just fine.

    Any help including a re-write would be greatly appeciated!

    The code:

    Code:
    'Initialize variables PIC16F88
    #CONFIG     
        __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
        __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF
    #ENDCONFIG
    
    OSCCON=$60  'Set Internal Oscillator to 4MHz
    
    DEFINE OSC 4
    DEFINE ADC_BITS 8       ' 8 bit A/D Conversion
    DEFINE ADC_CLOCK 3       ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50   ' 50 uS A/D sample time
    
    ANSEL  = %00000001       ' Set pin (AN0) to analog input, the rest to digital
    ADCON0 = %11000000      ' Set up A/D converter - Right Just., VDD REF., CH 0, ON  
    CMCON = 7
    
    adval var word
    
    pause 1000 'settle
    
    high PortA.1 'Col1
    high PortA.2 'Col2
    high PortA.3 'Col3
    high PortA.4 'Col4    
    high PortB.0 'Col5
    
    low PortB.1 'Row1
    low PortB.2 'Row2
    low PortB.3 'Row3
    low PortB.4 'Row4
    low PortB.5 'Row5
    low PortB.6 'Row6
    low PortB.7 'Row7
    
    Col1 var PortA.1
    Col2 var PortA.2
    Col3 var PortA.3
    Col4 var PortA.4
    Col5 var PortB.0
    
    Row1 var PortB.1
    Row2 var PortB.2
    Row3 var PortB.3      
    Row4 var PortB.4
    Row5 var PortB.5
    Row6 var PortB.6
    Row7 var PortB.7
    
    mainloop: 
    
        ADCIN 0, adval
        
        if adval <5 then 
            gosub ResetLEDs
            goto mainloop 'Zero, all LEDs OFF
        endif
    
        if adval >4 and adval <13 then
            'Led2
            gosub ResetLEDs
            Row1 = 1
            Col1 = 0
        endif
        
        if adval >12 and adval <21 then  
            'Led3
            gosub ResetLEDs
            Row2 = 1
            Col1 = 0
        endif
        
        if adval >20 and adval <29 then  
            'Led4    
            gosub ResetLEDs
            Row3 = 1
            Col1 = 0
        endif
        
         if adval >28 and adval <36 then  
            'Led5    
            gosub ResetLEDs
            Row4 = 1
            Col1 = 0
         endif
         
        if adval >35 and adval <44 then  
            'Led6    
            gosub ResetLEDs
            Row5 = 1
            Col1 = 0 
        endif  
            
        if adval >43 and adval <52 then  
            'Led7    
            gosub ResetLEDs
            Row6 = 1
            Col1 = 0
        endif
               
        if adval >51 and adval <59 then
            'Led8    
            gosub ResetLEDs
            Row7 = 1
            Col1 = 0
        endif
        
        if adval >58 and adval <67 then
            'Led9    
            gosub ResetLEDs
            Row1 = 1
            Col2 = 0
        endif
        
        if adval >66 and adval <75 then    
            'Led10    
            gosub ResetLEDs
            Row2 = 1
            Col2 = 0
        endif
        
        if adval >74 and adval <83 then
            'Led11    
            gosub ResetLEDs
            Row3 = 1
            Col2 = 0
        endif
           
        if adval >82 and adval <90 then
            'Led12    
            gosub ResetLEDs
            Row4 = 1
            Col2 = 0
        endif
        
        if adval >89 and adval <98 then
            'Led13    
            gosub ResetLEDs
            Row5 = 1
            Col2 = 0
        endif
            
        if adval >97 and adval <106 then
            'Led14    
            gosub ResetLEDs
            Row6 = 1
            Col2 = 0
        endif
            
        if adval >105 and adval <114 then
            'Led15    
            gosub ResetLEDs
            Row7 = 1
            Col2 = 0
        endif
            
        if adval >113 and adval <121 then
            'Led16    
            gosub ResetLEDs
            Row1 = 1
            Col3 = 0
        endif
            
        if adval >120 and adval <129 then
            'Led17    
            gosub ResetLEDs
            Row2 = 1
            Col3 = 0
        endif
            
        if adval >128 and adval <137 then
            'Led18    
            gosub ResetLEDs
            Row3 = 1
            Col3 = 0
        endif   
            
        if adval >136 and adval <144 then
            'Led19    
            gosub ResetLEDs
            Row4 = 1
            Col3 = 0
        endif 
            
        if adval >143 and adval <152 then
            'Led20    
            gosub ResetLEDs
            Row5 = 1
            Col3 = 0
        endif   
            
        if adval >151 and adval <160 then
            'Led21    
            gosub ResetLEDs
            Row6 = 1
            Col3 = 0
          endif 
            
        if adval >159 and adval <168 then
            'Led22    
            gosub ResetLEDs
            Row7 = 1
            Col3 = 0
        endif
            
        if adval >167 and adval <175 then
            'Led23    
            gosub ResetLEDs
            Row1 = 1
            Col4 = 0
        endif
            
        if adval >174 and adval <183 then
            'Led24    
            gosub ResetLEDs
            Row2 = 1
            Col4 = 0
         endif
    
       if adval >182 and adval <191 then
            'Led25    
            gosub ResetLEDs
            Row3 = 1
            Col4 = 0
       endif
    
        if adval >190 and adval <199 then        
            'Led26    
            gosub ResetLEDs
            Row4 = 1
            Col4 = 0
        endif
                    
        if adval >198 and adval <206 then
            'Led27    
            gosub ResetLEDs
            Row5 = 1
            Col4 = 0
        endif
        
        if adval >205 and adval <214 then
            'Led28    
            gosub ResetLEDs
            Row6 = 1
            Col4 = 0
        endif
                
        if adval >213 and adval <222 then
            'Led29    
            gosub ResetLEDs
            Row7 = 1
            Col4 = 0
        endif
                
        if adval >221 and adval <229 then
            'Led30    
            gosub ResetLEDs
            Row1 = 1
            Col5 = 0
        endif
                
        if adval >228 and adval <237 then
            'Led31    
            gosub ResetLEDs
            Row2 = 1
            Col5 = 0
        endif
                
        if adval >236 and adval <245 then
            'Led32    
            gosub ResetLEDs
            Row3 = 1
            Col5 = 0
        endif
                
        if adval >244 and adval <253 then
            'Led33    
            gosub ResetLEDs
            Row4 = 1
            Col5 = 0       
        endif
                
        if adval >252 then
            'Led34            
            gosub ResetLEDs
            Row5 = 1
            Col5 = 0
        endif
        
        
    GoTo mainloop
    
    
    ResetLEDs:
        high PortA.1 'Col1
        high PortA.2 'Col2
        high PortA.3 'Col3
        high PortA.4 'Col4
        high PortB.0 'Col5
        
        low PortB.1 'Row1
        low PortB.2 'Row2
        low PortB.3 'Row3
        low PortB.4 'Row4
        low PortB.5 'Row5
        low PortB.6 'Row6
        low PortB.7 'Row7
    return   
    
    End

  2. #2
    Join Date
    Dec 2012
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    maybe take like 10 values and average them, then display the average instead? it may stop the jumpy LEDs a little.

  3. #3
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Good idea Chirpy! I will give it a try....

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    When you find yourself writing the same section of code over and over with only changes to the values ....
    It usually means you are doing it the hardest and least efficient way.

    Instead of a bunch of IF/THEN's, use a little math, and a couple FOR/NEXT loops.
    Code:
    'Initialize variables PIC16F88
    #CONFIG     
        __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
        __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF
    #ENDCONFIG
    
    DEFINE OSC 4
    OSCCON=$60               'Set Internal Oscillator to 4MHz
    
    DEFINE ADC_BITS 10       ' 10 bit A/D Conversion
    DEFINE ADC_CLOCK 3       ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50   ' 50 uS A/D sample time
    
    ANSEL  = %00000001       ' Set pin (AN0) to analog input, the rest to digital
    ADCON1.7 = 1             ' Right Justify A/D results
    
    LED      VAR BYTE
    Row      VAR BYTE
    Col      VAR BYTE
    Idx      VAR BYTE
    Row_Pin  VAR BYTE
    Col_Pin  VAR BYTE
    adval    VAR WORD
    
    LEDcount CON 35
    Spaces   CON LEDcount + 1
    Margin   CON 1023 / Spaces
    
    ;-------------------------------------------------------------------------------
    Main: 
        ADCIN 0, adval
    
        LED = adval * LEDcount                  ; Determine which LED to light
        LED = DIV32 (1023 - Margin) - 1
    
        Row = -1                                ; Turn OFF all dots 
        Col = -1                                ; to prevent ghosting
        GOSUB ShowDot
    
        Row = LED // 7                          ; convert LED to Row/Column
        Col = LED / 7
        GOSUB ShowDot
    GOTO Main
    
    ;-------------------------------------------------------------------------------
    ShowDot:
        FOR Idx = 0 To 6                        ; Set Row's
            LOOKUP Idx,[1,2,3,4,5,6,7],Row_Pin
            IF Idx = Row THEN
                HIGH Row_Pin
            ELSE
                LOW Row_Pin
            ENDIF
        NEXT Idx
    
        FOR Idx = 0 TO 4                        ; Set Column's
            LOOKUP Idx,[9,10,11,12,0],Col_Pin
            IF Idx = Col THEN
                LOW Col_Pin
            ELSE
                HIGH Col_Pin
            ENDIF
        NEXT Idx 
    RETURN
    And, for the two lights on at a time ... you may want to add this ...
    http://www.picbasic.co.uk/forum/cont...ith-hysteresis
    Last edited by Darrel Taylor; - 12th February 2013 at 21:24.
    DT

  5. #5
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Wow, I cannot thank you enough Darrel! What elegant and efficient code! The difference between a newbie and a master. :-)
    I will study the code and get familiar with LOOKUP (never used that fuction before) as well as understanding how it all works.

    For example, setting Row = -1, I never knew you could set to a minus value. Don't really know what that is.

    Brilliant code. Thanks a ton!!

  6. #6
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Hi Darrel,
    I tried the averaging routine but could not get it to work. In the code is has:

    Code:
    GOTO OverAverage
    ' -=-=-=-=-=-=  Average Analog values -=-=-=-=-=-=-=-=-=-=
    however, there is no OverAverage routine. Also, I replaced the word "Value" with "adval", but that did nothing.

    Sorry, I am just not seeing this.
    Thanks!

Similar Threads

  1. Replies: 3
    Last Post: - 24th March 2015, 11:51
  2. Replies: 19
    Last Post: - 26th April 2012, 20:23
  3. Newbie needs example RC code
    By PickyBiker in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 18th March 2010, 02:10
  4. Microchip Code Issues
    By Destovi in forum General
    Replies: 4
    Last Post: - 20th March 2008, 13:32
  5. BaseBall pitching Code sample - NewBie
    By foxstar in forum Code Examples
    Replies: 12
    Last Post: - 8th May 2007, 19:29

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