Parallax 2X16 Serial LCD Bar Graph Demo Code
Chris Savage
Parallax EngineeringPosts: 14,406
·· Recently I've had a lot of requests for code for displaying various things on our 2X16 Serial LCD Display.· Everything from temperature/degree displays to Clocks and Bar Graphs.· Well the Temperature and Clock stuff has been covered in the Project, "Binary Digital Clock".· Now for the Bar Graph code.· In my programs in the past I have used Bar Graphs to show the percentage of progress of data files loading, scanning of data or even for settings, such as Bass, Treble and Volume on my Amplifier Systems.· I am attaching·2 Demo programs which adequately demonstrate how to create Simple and Advanced Bar Graphs on our LCD Display.· There is even a Random version of each which shows how the data looks when it's not linear going from 0-100%.· If you have any questions, comments or suggestions, please post them here or to my e-mail address.· Enjoy!
UPDATE!· SEE FURTHER DOWN THIS THREAD FOR HI-RES VERSIONS OF THIS CODE.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Post Edited (Chris Savage (Parallax)) : 10/13/2005 2:18:39 PM GMT
UPDATE!· SEE FURTHER DOWN THIS THREAD FOR HI-RES VERSIONS OF THIS CODE.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Post Edited (Chris Savage (Parallax)) : 10/13/2005 2:18:39 PM GMT
Comments
I would like to use this to graph the output of my ADC, which has a normal output around 780 to 820.· If I changed highVal from a byte to a word would this do it, or would I have to make other changes as well.
Also, in studying the display, it appears that all percentages are calculated but the the LCD displays only the integer values of the calculation.
I'm trying to figure out how to run this on a 2x20 or a 2x24 and use the full display.
Thanks for the codes.
Sid
I used the·advanced one, a few months ago, in a project involving a 2x16 parallel LCD that measures the voltage of a solar panel:
http://users.sch.gr/xrysoxoidou/user_projects/Analog_to_Digital_NE555/analog_to_digital_NE555.htm
Well... the article is in Greek but I have a video below that shows the bar graph in action!
http://users.sch.gr/xrysoxoidou/user_projects/Analog_to_Digital_NE555/AD555_operation_video.zip
thanks Thanos
Post Edited (Tronic (Greece)) : 10/12/2005 12:59:38 PM GMT
Sid,
·· Thank you for pointing out something I should've addressed in the original post.· I posted the examples I did because I felt they were easier to understand.· I also decided to keep variable usage down and stick with Byte variables.· However, if you're already using Word variables in your program I guess it won't make much difference, and I should've given that in an example.
·· The change that needs to be made for this to work is not only the variable sizes, but the way the perCent is calculated.· I chose a formula used in schools to teach kids how to calculate a percentage.· This is rounded to an integer intentionally.· However the routine relies on multiplying the current element (index)·by 100, then dividing by the highVal.· When you switch to Word variables once you get past 255 you start having problems with overflow in the math using this type of formula.
·· The fix is to calculate the percent differently.· Instead we will use the formula perCent = (index / (highVal / 100)) which will allow us to use values up to 65535 in our calculations again.· I will attach a modified version of the code for your convenience.· Let me know if you are able to utilize the new routines instead.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Sid
·· Patience...I was working on it at that very moment.· Here it is.· I put a Step of 100 in the count so it gets to the higher numbers more quickly, but be careful using step if you change the value of highVal.· Again, this is concept code...It could be easily integrated into your programs.· The subroutine is the main thing here.
·· As for working with other displays, it certainly could.· But I put this code together specifically for our displays to help our customers by providing more sample code that works with our displays.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
·from Parallax about a week ago.· I then tried to run it on an old 2x16 from
Parallax that I have had a long, long time.· To get it to work, I had to change
the baud to 16468 and go back to
I·············· CON 254
L1············· CON 128
L2············· CON 192
clr············ CON 1
and
SEROUT LCD, Baud, [noparse][[/noparse]I, clr]
SEROUT LCD, Baud, [noparse][[/noparse]I, L1, DEC3 index...............
SEROUT LCD, Baud, [noparse][[/noparse]I, L2, "0%", REP 0..............
In addition, it does not recognize the custom character.· In place of that
it displays a right angle triangle.· Readable, but not as nice the display
on the newer 2x16.
Sid
·· The reason it doesn't work perfectly is it requires different commands to perform it's operations.· You would need to re-write the section that loads the custom characters.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
to 700, when the index is 700 then 'index*100' = 70000, which is beyond the |
capability· of the Stamp to read and the display rolls over.
I·tried several little changes to the program but was unable to work my way
around it.· I would really like to·graph values in the vicinity of 750 to 850.· Any
suggestions?
Sid
·SEROUT lcd, baud, [noparse][[/noparse]I, L1,"··· ", DEC ((percent*highval)/100)*4]
Now the LCD displays 1000 on Line 1.· So now, if I expect a max
ADC reading of·AD0 I can write:
SERIN pin, baud, [noparse][[/noparse]AD0]
highVal = AD0 /4
valinc = AD0//4
highval = highval + valinc
Do you think that will work?
Sid
·· highVal is the 100% parameter.· In other words, the highest value you should be getting into the code (Up to 65535).· The parameter that calculates the perCent is index.· Here is an example.· Say you get readings from a device ranging from 0 - 1000.· When the current value is 500, you should see 50%.· highVal would be set to 1000 and index would be set to the current reading, which in this case is 500.· I hope that helps.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
index*100 = 70000, which the Stamp can not handle.· I entered highVal=700,
·and when the display got to about 650, it rolled over to 0 and started
counting again up to about 417 or so.· My feeling is that " index*100 " can
not exceed 65535.
·· Where are you getting index * 100?· I posted new code for you to solve that problem with a complete explanation of the change in the formula.· Why not use that one?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
2x16.· Besides, I stopped being perfect when I passed the 80 year mark.
Sid
Line 2 to "REP 6" the LCD displays the same character as the custom
character.· The program is working perfectly now on my old 2x16.· Thanks
for all the help.· I needed it.
Sid
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Edwards LCD,which Parallax sold before they had their own.· I also
managed to find an old data sheet on the web for this LCD so now I
have all the instruction codes and the character table.
I have attached a really chewed up version of your HiRes simple code.·
I hope you have to time and inclination to convert it to a code that
will run on your LCD.
Basically what happens is this.· I set highVal to an initial value of
4095, which is the max output of my 12-bit ADC.· Then simulating a
serin input, I decreased highVal by 500 each time the program cycled.
I modified the display so that at the right end of the bar graph is displays
the ADC output, modified with a */ operator to show the actual voltage.
When the voltage gets to 1.000 or less, highVal is reset to 4095 and the
program starts all over.· In real life, of course, the serial ADC input would
go up or down, or remain steady, so a reset would not be necessary.
I also modified Line 1 so that it shows the actual ADC digital output,
unmodified.
After watching the LCD display, I think I can probably work with a new
ADC serined value every 10 seconds or so.
If you were using a 10-bit ADC, the initial value of highVal would be
set to 1023, and the */ operator would have to be modified accordingly.
Really hope you have time to try this - would be interested in your
comments.
Sid
·· I will try to find some time to check it out at some point, but I can tell you right now that your problem is you're changing the highVal.· That value should almost never need to be changed, unless you're using the Bar Graph for multiple inputs with different ranges.· highVal should always represent the highest value, and index should represent what's being piped in.· What's probably happening is that, while you are decreasing·highVal (Which shouldn't happen), you are getting an index value higher than highVal, which will certainly mess things up.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
·FOR index = 0 TO highVal STEP highval/10··· ' Count To highVal·····
GOSUB Disp_Graph··················· ' Update Display
··· NEXT
so as highVal changes the index also changes.
I have been running this thing since last night and if the voltage is about
2.50, the bar graph shows 5 segments instead of 10.· I really hope you
have time to run the program.
Sid
·· You modified the perCent routine (Why?) and now all percentage calculations are being based on 4095, regardless of what highVal is.· That routine shouldn't need to be changed.· And you should never use a constant in a routine that calculates variable data.· It won't ever change.· Or in this case, highVal as far as that routine is concerned won't ever change.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
2047/4095 x 5000 = 2500mv.· That is what the LCD displays and that is the
data I want to see.· Don't forget, the current ADC count is serined every 10
seconds, and this translates to the voltage the ADC is seeing at that moment.
Just try the program and you will see.
Sid
·· You just confirmed that you replaced the highVal variable in the perCent calculation with a 4095 constant, but that means the percent will always be calculated based on 4095, regardless of what highVal is.· This is what is screwing up your Bar Graph.· If you're changing highVal, it needs to change in the perCent routine, but you locked that at 4095, while the Bar routine is still using highVal, except where you changed part of it to reset highVal to 4095 again...Which could also be messing up things.· I don't know how to help you Sid.· This is a basic concept and has been modified in such a way as to not follow the original program logic.· When I mentioned modifying the code, I meant altering the appearance of the display, or changing the range, or variable names to correspond with an application you want to use this on.· By altering the fundamental routines, it's no longer my Bar Graph code, and I can't make it do what you want.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
full scale unless the count is 4095 = 5 volts.· If the count is 2047 = 2.50
volts then I want the graph to go halfway.· If the count is 3071 = 3.75 volts
then I want the graph to go 3/4 of the way.· That is what it is doing - exactly
what I wanted it to do.
Sid
·· You said earlier it wasn't working as expected, now you say it works exactly as you want, so I am confused.· highVal shouldn't be changing.· Not regularly, so there's no reason to adjust highVal on the fly, therefore no reason to convert the highVal sections of the subroutine to constants.· index is all that should be changing since that's the input value, the variable value in relation to highVal.· What is it you wanted me to run the program for if it's working as you expected?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Sid
Post edited by Newzed