LED 'Dot' Bargraph Issues - Newbie Code!


Results 1 to 12 of 12

Threaded View

  1. #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

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