Shop OBEX P1 Docs P2 Docs Learn Events
Parallax 2X16 Serial LCD Bar Graph Demo Code — Parallax Forums

Parallax 2X16 Serial LCD Bar Graph Demo Code

Chris SavageChris Savage Parallax EngineeringPosts: 14,406
edited 2005-10-14 13:03 in Robotics
·· 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

Comments

  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 12:31
    Chirs, I tried your bar graph codes on my BS2E and they work great.· The only thing is I had to change the baud rate to 84.

    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
  • Tronic (Greece)Tronic (Greece) Posts: 130
    edited 2005-10-12 12:40
    These sample codes are really cool.

    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


    project_overview_ad555.jpg


    Post Edited (Tronic (Greece)) : 10/12/2005 12:59:38 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 14:33
    Newzed said...(trimmed)
    Chirs, I tried your bar graph codes on my BS2E and they work great.· The only thing is I had to change the baud rate to 84. 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.

    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 14:41
    Where is the attachment, Mr. Savage?

    Sid
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 14:55
    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 SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 14:58
    Tronic (Greece) said...(trimmed)
    These sample codes are really cool.
    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:
    Thank you!· And thanks for the links.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 15:44
    Program works great, Chris.· I was running it on a 2x16 Serial I just received
    ·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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 16:03
    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 17:33
    Chris, it appears tha the max workable value for highVal is about 650.· If you set it
    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 17:58
    Chris, I think I figured it out.· I set highVal = 250.· Then I wrote:

    ·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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 18:05
    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 18:15
    All very true, Chris, but when the index gets to 700, then
    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.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 18:17
    Sid,

    ·· 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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 19:00
    'cause I forgot.· I was so busy trying to make the code work with my old
    2x16.· Besides, I stopped being perfect when I passed the 80 year mark.

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-10-12 20:36
    Chris, I don't need to generate a custom character.· If I change "REP 0" in
    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 SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 21:38
    No problem Sid.· Just remember, I test any code I upload for the devices it's written for, so I know it works on a given setup.· =)· At least you were able to use it.· I am thinking maybe I should create a Hi-Res version of each and poat that as well.· Thanks for pointing that out.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-12 23:44
    Okay, here are the versions of the Bar Graph Code that support Word variables up to 65535.· These are Hi-Res versions.· The perCent formula was changed to one I used to use a while back.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • NewzedNewzed Posts: 2,503
    edited 2005-10-13 13:42
    Chris, I finally figured out that the 2x16 LCD I have is an old Scott
    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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-13 14:16
    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-13 14:29
    Chris,· that can never happen.· I wrote:

    ·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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-13 14:44
    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-13 14:57
    Correct, Chris.· 4095 equates to 5000mv.· If highVal changes to 2047, then
    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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-13 15:33
    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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-13 16:02
    Chris, I don't think I am explaining this very well.· I do not want the graph to go
    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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-13 16:23
    Newzed said...(trimmed)
    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 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
  • NewzedNewzed Posts: 2,503
    edited 2005-10-14 13:03
    I guess I would just like for you to run the program so you can see how it is working.

    Sid

    Post edited by Newzed
Sign In or Register to comment.