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

    Default Whats the quickest way to set bits?

    I am trying to create a bargraph display. It has 16 segments. My routine uses a calculation of an upper bound limit for what the display should be reading and a lower bound limit.

    Code:
    GraphDivider = (GraphMax - GraphMin)  / 16
    GraphVal = (X / GraphDivider)
    Everything to do with this graph is using word variables.

    So lets say my graph top value is 100, and my graph lower value is 0, and X = 50:
    (100 - 0) / 16 = 100 / 16 = 6.25
    50 / 6.25 = 8

    Thats exactly the result I expect to get. Now, to display the number 8 on the graph (having the first 8 LED's lit, the binary number I would have to send would be %0000000011111111

    Is there a way to tell pic basic to set the first 8 bits on? Looking through the documentation I see I can set bit 8 with DCD=7 but that will leave all other bits at 0

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


    Did you find this post helpful? Yes | No

    Default

    How is the display connected to the MCU? How are you sending data to it.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    Using an alegro A6276, so the pic shifts the data out. The displays thems selfs are working fine. I was just wondering if there was a quicker way to set all bits up to a certain bit number on with out having to use a look up table.

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


    Did you find this post helpful? Yes | No

    Default

    <strike>GraphVal = $FFFF >> ((X * 16) / (GraphMax - GraphMin))</strike>

    GraphVal = $FFFF >> (((X - GraphMin) * 16) / (GraphMax - GraphMin))
    <br>
    <br>
    Last edited by Darrel Taylor; - 23rd October 2010 at 02:22. Reason: oops
    DT

  5. #5
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    DOH!

    I knew there was something I wasnt thinking of...

    The key was right shifting the $FFFF. If anyone else is following this, here is the final formula I came up with and it works perfectly.

    GraphVal = $FFFF >> (16 - (X / ((GraphMax - GraphMin) / 16)))

    This is for auto guages so the scale of the graph can swing. For instance, a fuel level gauge might read 0 to 16 galons, where a water temp guage might read 180° - 290°, so it was important to note that the bottom number might change as well as the top.
    Last edited by bearpawz; - 23rd October 2010 at 03:16. Reason: It works now...

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


    Did you find this post helpful? Yes | No

    Default

    And probably something I wasn't thinking of too.
    That might go backwards, and need to be inverted.

    GraphVal = $FFFF >> (16-(((X - GraphMin) * 16) / (GraphMax - GraphMin)))

    I'll test it and see.

    Doh, you beat me to it.
    Oops again.
    DT

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

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

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