-
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.
-
-
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.
-
I was hoping the part of that thread explaining
~DCD
would help
-
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.
-
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
-
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?
-
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 (>>).
-
Wow Darell, that's an ace. I use this for scaling readings but never thought of it this way. Nice tip.
Superb
-
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. :)