I wanted to display vertical bar graphs on a 2 line LCD display. I hope the following code fragments will help and inspire others.
I'm using a Boarduino, Ardunio 0011 Alpha, the stock LCD4bit library and a 2x16 display from the UK's Maplin Electronics (N27AZ – DEM16217).
I used the "user defined characters" to define eight new characters: one with one full row of dots at the bottom, one with two rows, one with three etc, up to all eight rows. Doing this looks a bit scary reading the display data sheet but the Everyday Practical Electronics PDF articles online at Web Hosting, Reseller Hosting & Domain Names from Heart Internet are very helpful.
Basically you do an lcd.commandWrite (0x40) to set the display to accept writes to the character generator RAM instead of the screen, and then send the character definitions, eight bytes at a time. The first one is character 0, then 1, etc. Afterwards you use an lcd.clear to get back to normal operations.
The characters get defined in the setup () procedure. I worked out that I didn't need to spell out all the characters and used two nested for loops:
lcd.init ();
lcd.commandWrite (0x40); //define bar code chars
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
if (i+j>6)
{
lcd.print (0x1F);
}
else
{
lcd.print (0x00);
}
}
}
lcd.clear ();
[/color]
That's not the most easily understood code in the world but if you work through you'll find it defines exactly the right characters in character slots 0 to 7.
0:
........
........
........
........
........
........
........
XXXXXXXX
1:
........
........
........
........
........
........
XXXXXXXX
XXXXXXXX
etc.
None of the these characters is blank – that character already exists (“space”) at 0x20.
In my case I wanted eight dynamic bar graphs in the left hand columns. These get written in two phases – the first row for the top of the bars and the second row for the bottoms. Clearly if the bar does not extend into the top row then the space character is used. I always wanted a baseline on the bottom row so this never really shows “zero”.
The code for loop () looks like this. Array c[8] contains the values, scaled to the range 0-15.
lcd.cursorTo(1, 0); //top row
for (int k=0; k<8; k++)
{
if (c[k] < 8)
{
lcd.print (0x20);//space if bar does not reach top row
}
else
{
lcd.print (c[k]-8);
};
};
lcd.cursorTo(2, 0);//bottom row
for (int k=0; k<8; k++)
{
if (c[k] < 8)
{
lcd.print (c[k]);
}
else
{
lcd.print (7); //full block if bar extends to top row
};
};
I've taken this out of my code which had various scaling factors in it. I'll try to post a full working sketch shortly. I may have worked out how to quote code fragments by then!
Enjoy!