Whats the quickest way to set bits?


Closed Thread
Results 1 to 34 of 34

Hybrid View

  1. #1
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    Uh Oh. Ok, it work flawlessly until you start working with a very tight scale. An example is the volt meter. It usually displays information from 8V to 18V with the guages I am replacing. Unfortunatly, based on the formula there ends up being too small of a number to even light one LED. (or at least its not lighting anything up). I passed in a value of 13 for my test data. This is a valid number between 8 and 18. However, when you start using the formula (the one I came up with) here is what happens:

    GraphVal = (16 - (X / ((GraphMax - GraphMin) / 16)))

    GraphVal = (16-(13/((18 - 8)/16)))

    GraphVal = (16-(13/(10/16)))

    GraphVal= (16-(13/.625)

    GraphVal = 16-20

    GraphVal = -4!


    Sorry, Ive never been horribly great at actual math. Now what? lol.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    You will not be getting a value volts at the ADC. You will have something between 0 and 255 or 0 and 1023. 8 or 10 bits.

    So maybe we should start there.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    What about this for you ADC in
    http://www.picbasic.co.uk/forum/content.php?r=249
    Oversample to get you 16bit rez. Then you are done.

    Or am I missing something.
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    Not even worried about that part right now. What will be monitoring the actual car will be a whole other system. At the moment just need to know how to get the displays to display correctly.

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


    Did you find this post helpful? Yes | No

    Default

    Try the last formula I gave in post #6

    Testing here, making animation to prove it.
    It works.
    DT

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


    Did you find this post helpful? Yes | No

    Default

    <object id='stUkhdQ01IR1FYSF9cWVxcU1Nc' width='425' height='344' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name='movie' value='http://www.screentoaster.com/swf/STPlayer.swf'/><param name='allowFullScreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='flashvars' value='video=stUkhdQ01IR1FYSF9cWVxcU1Nc'/></object>

    Code:
    DEFINE OSC 4
    
    ;----[USART Settings]-------------------------------------------------------
    DEFINE HSER_RCSTA 90h    ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h    ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 25     ' 9600 Baud @ 4MHz, 0.16%
    DEFINE HSER_CLROERR 1    ' Clear overflow automatically
    
    ;----[LCD Settings]---------------------------------------------------------
    DEFINE LCD_DREG PORTA     ' LCD data port 
    DEFINE LCD_DBIT 4         ' LCD data starting bit 0 or 4 
    DEFINE LCD_RSREG PORTA    ' LCD register select port 
    DEFINE LCD_RSBIT 2        ' LCD register select bit 
    DEFINE LCD_EREG PORTA     ' LCD enable port 
    DEFINE LCD_EBIT 3         ' LCD enable bit 
    DEFINE LCD_BITS 4         ' LCD bus size 4 or 8 
    DEFINE LCD_LINES 2        ' Number lines on LCD 
    DEFINE LCD_COMMANDUS 2000 ' Command delay time in us 
    DEFINE LCD_DATAUS 50      ' Data delay time in us 
    
    ;----[A/D Settings]---------------------------------------------------------
    DEFINE ADC_BITS 10        ' ADCIN resolution  (Bits)
    DEFINE ADC_CLOCK 1        ' ADC clock source  (Fosc/8)
    DEFINE ADC_SAMPLEUS 11    ' ADC sampling time (uSec)
    
    ;----[Variables]------------------------------------------------------------
    GraphMAX   VAR WORD
    GraphMIN   VAR WORD
    GraphVal   VAR WORD
    X          VAR WORD
    ADval      VAR WORD
    Xmax       VAR WORD
    Xmin       VAR WORD
    char       VAR BYTE
    OverRange  VAR BIT
    UnderRange VAR BIT
    
    ;----[Aliases]--------------------------------------------------------------
    RCIF       VAR PIR1.5
    OverPin    VAR PORTC.5
    UnderPin   VAR PORTC.4
    
    ;----[Default Values]-------------------------------------------------------
    GraphMAX   = 100
    GraphMIN   = 0
    Xmax    = 100
    Xmin      = 0
    
    ;----[Initialize]-----------------------------------------------------------
    PORTB = 0
    TRISB = 0
    PORTD = 0
    TRISD = 0
    ANSEL = %00000001
    ANSELH = 0
    ADCON1.7 = 1
    PAUSE 250 : LCDOUT $FE,1 : PAUSE 250 : LCDOUT $FE,1
    GOSUB ShowMenu
    GOSUB ShowLimits
    OverRange = 0
    UnderRange = 0
    
    ;----[Main Program Loop]----------------------------------------------------
    Main:
        IF RCIF THEN GOSUB Terminal
        GOSUB GetAD
        GOSUB ShowGraph
        GOSUB CheckOverUnder
    GOTO Main
    
    ;----[Get and Scale Analog Reading]-----------------------------------------
    GetAD:
        ADCIN 0, ADval
        X = ADval * (Xmax - Xmin)
        X = DIV32 1023 + Xmin
        LCDOUT $FE,$80, "AD=",DEC ADval, "    "
        LCDOUT $FE,$8B, "X=",DEC X, "    "
    RETURN
    
    ;----[Show the BarGraph]----------------------------------------------------
    ShowGraph:
        IF X > GraphMAX THEN
            OverRange = 1
        ELSE
            OverRange = 0
        ENDIF
        IF X < GraphMIN THEN
            UnderRange = 1
        ELSE
            UnderRange = 0
        ENDIF
        X = (X MIN GraphMAX) MAX GraphMIN
        
        ;-- This is the formula being tested ---------------------
        GraphVal = $FFFF >> (16-(((X - GraphMIN) * 16) / (GraphMAX - GraphMIN)))
        
        PORTD = GraphVal & $FF
        PORTB = GraphVal >> 8
    RETURN
    
    ;----[Show Menu for Terminal]-----------------------------------------------
    ShowMenu:
        HSEROUT ["MENU: 1=GraphMAX, 2=GraphMIN, 3=Xmax, 4=Xmin :"]
    RETURN
    
    ;----[Handle the RS232 Terminal]--------------------------------------------
    Terminal:
        HSERIN [char]
        SELECT CASE char
          CASE "1" : HSEROUT [13,10,"Enter the GraphMAX: "]
                     HSERIN  [DEC GraphMAX]
          CASE "2" : HSEROUT [13,10,"Enter the GraphMIN: "]
                     HSERIN  [DEC GraphMIN]
          CASE "3" : HSEROUT [13,10,"Enter the Xmax: "]
                     HSERIN  [DEC Xmax] 
          CASE "4" : HSEROUT [13,10,"Enter the Xmin: "]
                     HSERIN  [DEC Xmin]
          CASE ELSE : HSEROUT [7]
        END SELECT
    
    ;----[Show Limits]----------------------------------------------------------
    ShowLimits:
        LCDOUT $FE,$C0,"    GRAPH    X      "
        LCDOUT $FE,$94,"MAX:                "
        LCDOUT $FE,$D4,"MIN:                "
        LCDOUT $FE,$99,DEC GraphMAX
        LCDOUT $FE,$D9,DEC GraphMIN
        LCDOUT $FE,$A1,DEC Xmax
        LCDOUT $FE,$E1,DEC Xmin
    
    RETURN
    
    ;----[Look for Over and Under Range]----------------------------------------
    BlinkSpeed  CON 50
    BlinkCount  VAR WORD
    
    CheckOverUnder:
        BlinkCount = BlinkCount + 1
        IF BlinkCount = BlinkSpeed THEN
            BlinkCount = 0
            IF OverRange THEN
                TOGGLE OverPin
            ELSE
                LOW OverPin
            ENDIF
            IF UnderRange THEN
                TOGGLE UnderPin
            ELSE
                LOW UnderPin
            ENDIF
        ENDIF
    RETURN
    
    This post has been promoted to a bar graph wiki page, located here: http://www.picbasic.co.uk/forum/cont...ge-min-and-max
    Last edited by ScaleRobotics; - 23rd October 2010 at 17:33. Reason: Added promotion tag
    DT

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Wow!!!!!
    That is cooool!!!!!!!
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Assuming you have to measure max 18 volts, then you will need a resistor devider by 4 which will yield 4,5 volts to the ADC input. Assuming you will use a 10 bits convertion then:

    45000/1024 = 44 (rounded)

    Your 16 leds graphs = $FFFF >>( 16- ((ADC * 44 * 16)/45000))

    Cheers

    Al.
    All progress began with an idea

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