Setting bits


Closed Thread
Results 1 to 10 of 10

Thread: Setting bits

  1. #1
    Join Date
    May 2004
    Posts
    81

    Default Setting bits

    Maybe some one can help me wrap my brain around this problem. I have a display which will eventually indicate all kinds of information in an automotive environment. For instants, Fuel gallons, oil pressure, water temp etc.

    I am using a micro to control a MAX7219 LED driver. I am using 6 of the 8 banks and using my own custom font so all banks are in direct addressing mode. The first 4 banks display the digits, the last two display a bargraph. The bargaph consists of 16 LED's (8 each with common annode)


    Problem 1: How to turn the LED's on

    I need to turn on the LED's by sending out the "on" bit for each LED or "Segment". So for instance, to turn on all LED's I would have to send %11111111 to both banks 5 and 6 (banks are labeled 0 - 7). So lets say I know I have to display the number 12. I need 12 bits set (%1111111111110000)

    Since the actual value is going to be calculated, is there a pic basic command to set x number of bits on in picbasic? I have been reading through the manual and just cant seem to find it. At the moment I am resorting to a look up table but that brings me to problem #2

    Problem 2: The scale for the bargraph will be constantly changing. For instance take oil pressure. A typical guage will show 0psi to 32psi. So the scale will want to indicate 0 PSI as all 16 LED's off and 32psi as all 16 on. In this case its prety easy to calculate because that works out to be 2 led's per psi. But now lets say we want to indicate Water temp. Now we are on a whole different playing feild. The guage wants to show anywhere from say 100F to 260F. So at 100F we want either no LED's on or just one at most, and at 260F we want them all on (along with all kinds of warning buzzers your engine is about to seize lol ). So anyway, now we want a resolution of 1 LED / every 10 degrees. (260 - 100 = 160. 160/16 = 10). For this reason I am thinking my look up table is not going to work out too well. Hopefully this makes sense to some one because right now, while it seems like this should be cake I just cant seem to figure out what formulas to use.

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


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply. Actually, I got the displaying the digits thing all set. The display chip is quite handy as it handles all the multiplexing of the digits. Just have to send it what digit to update and then the micro can go do what ever else it needs to do. To rephrase the question , I need to know how to set a certain number of bits in a byte or word variable. For instance if the byte is %00011111, how would I easily set the first 5 bits to on? Since this part of the display shows the bargraph I need to make sure if led 5 is on, so are all the other led's (0-4).

    As far as the formula part goes if anyone is following along, I decided Im just going to have to pass in an upper and lower bound to the micro to tell it how to scale the bargraph. So I will just send in an Upper bound of 260, and a lower bound of 100 for the temp display, and then the micro can subtract the lower from the upper and divide by 16.


    Resolution = (Upperbound - lowerbound)/16

    I know this will give me a resolution of 10 degrees for each LED. Im VERY tired and probably need to sleep.. just cant seem to get the rest of it.

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


    Did you find this post helpful? Yes | No

    Default

    I was hoping the part of that thread explaining
    ~DCD
    would help
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    Ok, I kind of see where your going. What I need to do is something very similar. If I understand correctly DCD will set the specified bit. Here is what the help in MCS says:

    "DCD returns the decoded value of a bit number. It changes a bit number (0 - 15) into a binary number with only that bit set to 1. All other bits are set to 0.

    Example

    B0 = DCD 2 ' Sets B0 to %00000100"

    Ok, thats cool. And after re-reading that other thread I see where your going with this. Lets say I know my bargraph needs to display 50% or 8 segments. I would need to say something like DCD = 8. This gives me %10000000. So in effect only my 8th LED will light up. My problem is I need ALL the LED's from 1 to 8 to light up... what I need to see is %11111111.

    Using ~DCD is ALMOST what I need. For the number 8 I would now have 011111111. Close but not quite. Just for fun, lets say that I really need only 4 led's lit up. (%00001111). Using ~DCD I would actually get %11110111). Do I understand this correctly?

    Anyway, now that I have actually had some sleep. I can think a wee bit more clearly. Using the look up table should work, just seems there should be an easier way to do this. For anyone following along I figured out my final formulat. I will need three peices of information: Bargraph upper bound, Bargraph lower bound, and value. (And yes, I have spent WAY too many years messing around with arrays in VB ).

    Here is my final formula then:

    Bargraph = (Data - LowBound) / ((highbound - lowbound)/16)

    Simplest samples to plug in with oil pressure values: Upper is 32, lower is 0. For a presure of 16 (50% on the bargraph) I need to light 8 led's

    Bargraph = (16 - 0)/((32 - 0)/16)
    bargraph = 16/2 = 8.

    So now I just need to tell the pic to light up LED's 1-8 and I am done. Until I get a better lightbulb over my head, Im just using a look up table. then of course, there will be the issue of getting the values IN to the processor.. but thats a whole other war to battle.

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    I think you wish to build a bar graph with a value

    This will help you

    Code:
    ' Build a bar from the value in ChPeak
    ' Each led gets 1 bit - 0 for on, 1 for off
    ' Output in BarGraph 16 bits
    ' Gr[0] is a temporary variable
    '
    BuildBar:
        for gr[0] = 0 to 15
          if gr[0] < ChPeak then
            ' put this LED off 1
            BarGraph.0(gr[0])=1
          else
            ' put this LED on 0
            BarGraph.0(gr[0])=0
          endif
        next
        return

  7. #7
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    Well that certainly would work as well. Thanks. I was hoping there would be some kind of command like DCD that would just magicaly set all the bits on but I guess not. I might look into just manipulating the bits some how. then again, I havnt looked but how many CPU cycles does the lookup command take? anyone know?

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


    Did you find this post helpful? Yes | No

    Default

    No magic commands, but maybe this will do ...
    BarGraph is a Word var.

    Code:
    BarGraph=$FFFF<<(LEDcount-((BarData-RangeLOW)*LEDcount)/(RangeHIGH-RangeLOW))
    If the BAR is going the wrong way, just change it to shift right (>>).
    Last edited by Darrel Taylor; - 26th November 2009 at 20:02. Reason: >>
    DT

  9. #9
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Wow Darell, that's an ace. I use this for scaling readings but never thought of it this way. Nice tip.

    Superb

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


    Did you find this post helpful? Yes | No

    Default

    Thanks Jerson,

    bearpawz had most of it already, I just filled in the blanks.
    Then compiled, tested and heard ... Ohhh Yeeaaah, before I realized it was me talking.
    DT

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Configuration bits setting problem
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th August 2009, 17:25
  3. PICKit2 - warning about configuration words
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 26
    Last Post: - 4th August 2009, 14:01
  4. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31
  5. Trouble setting config. bits
    By jswayze in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 13th March 2004, 22:22

Members who have read this thread : 1

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