给你我的代码
unsigned char lcdcustomchar[] =
{
0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, // 0. 0/5 full progress block
0x00, 0x1f, 0x10, 0x10, 0x10, 0x10, 0x1f, 0x00, // 1. 1/5 full progress block
0x00, 0x1f, 0x18, 0x18, 0x18, 0x18, 0x1f, 0x00, // 2. 2/5 full progress block
0x00, 0x1f, 0x1c, 0x1c, 0x1c, 0x1c, 0x1f, 0x00, // 3. 3/5 full progress block
0x00, 0x1f, 0x1e, 0x1e, 0x1e, 0x1e, 0x1f, 0x00, // 4. 4/5 full progress block
0x00, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, // 5. 5/5 full progress block
}
void lcdloadcustomchar(u08* lcdcustomchararray, u08 romcharnum, u08 lcdcharnum)
{
u08 i;
u08 saveddramaddr;
// backup the current cursor position
saveddramaddr = lcdcontrolread() & 0x7f;
// multiply the character index by 8
lcdcharnum = (lcdcharnum<<3); // each character occupies 8 bytes
romcharnum = (romcharnum<<3); // each character occupies 8 bytes
// copy the 8 bytes into cg (character generator) ram
for(i=0; i<8; i++)
{
// set cg ram address
lcdcontrolwrite((1<<lcd_cgram) | (lcdcharnum+i));
// write character data
lcddatawrite( pgm_read_byte(lcdcustomchararray+romcharnum+i) );
}
// restore the previous cursor position
lcdcontrolwrite(1<<lcd_ddram | saveddramaddr);
}
void lcdprogressbar(u16 progress, u16 maxprogress, u08 length)
{
u08 i;
u32 pixelprogress;
u08 c;
// draw a progress bar displaying (progress / maxprogress)
// starting from the current cursor position
// with a total length of "length" characters
// ***note, lcd chars 0-5 must be programmed as the bar characters
// char 0 = empty ... char 5 = full
// total pixel length of bargraph equals length*progresspixels_per_char;
// pixel length of bar itself is
pixelprogress = ((progress*(length*progresspixels_per_char))/maxprogress);
// print exactly "length" characters
for(i=0; i<length; i++)
{
// check if this is a full block, or partial or empty
// (u16) cast is needed to avoid sign comparison warning
if( ((i*(u16)progresspixels_per_char)+5) > pixelprogress )
{
// this is a partial or empty block
if( ((i*(u16)progresspixels_per_char)) > pixelprogress )
{
// this is an empty block
// use space character?
c = 0;
}
else
{
// this is a partial block
c = pixelprogress % progresspixels_per_char;
}
}
else
{
// this is a full block
c = 5;
}
// write character to display
lcddatawrite(c);
}
}
int main(void)
{
lcdloadcustomchar((u08*)lcdcustomchar,0,0);
lcdloadcustomchar((u08*)lcdcustomchar,1,1);
lcdloadcustomchar((u08*)lcdcustomchar,2,2);
lcdloadcustomchar((u08*)lcdcustomchar,3,3);
lcdloadcustomchar((u08*)lcdcustomchar,4,4);
lcdloadcustomchar((u08*)lcdcustomchar,5,5);
lcdprogressbar(56, 100, 16);
return 1;
}