I have a bargraph in CCS-C with PIC MCU and it works. However it (randomly) leaves some of the bars behind if the value drops very quickly. Anybody faced this problem?
|||||||||||||||||||||||||||||||||||||||| <--full bar
||||||| | | <--two bars left behind
Bargraph functions (CCS-C):
//User defined characters are 5x7 but remember the cursor on 8
void init_user_chars(void) {
int8 i;
//Setup an array of special characters (CGRAM) to form the bars
const char user_char[32] = {
// char 0x00 no bar
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// char 0x01 left vertical line of bar
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
// char 0x02 two units of bar
0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00,
// char 0x03 three units of bar (full bar)
0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00
};
lcd_send_byte(0,64); // Sets ram address to beginning of CGRAM area
for (i=0; i<32;i++) // fill in CGRAM with the data from array above.
lcd_send_byte(1,user_char[i]);
lcd_gotoxy(1,1); // Resets address to top line / position 1
}
void bargraph(BYTE percent) {
int Bars; //Number of full ||| bars to draw.
int Balance; //Balance left after all |||s are drawn.
int i; //counting variable
Bars = percent/3; // how many full bars to print?
Balance = percent%3; // are partial bars required?
lcd_gotoxy(1,2); // go to beginning of line 2
for (i=0; i<Bars; i++)
lcd_send_byte(1,0x03); // print the bars
switch (Balance) {
case 0: lcd_send_byte(1, 0x00);
break;
case 1: lcd_send_byte(1, 0x01);
break;
case 2: lcd_send_byte(1, 0x02);
break;
}
}