CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Bargraph Code for Hitachi type LCD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hansknec



Joined: 30 Sep 2004
Posts: 25
Location: Virginia, USA

View user's profile Send private message Visit poster's website

Bargraph Code for Hitachi type LCD
PostPosted: Thu Sep 30, 2004 1:02 pm     Reply with quote

Does anybody have a function written to generate a horizontal bargraph on an LCD (with Hitachi HD44780 controller)? I have recently started using PIC C and also use PIC Basic Pro. In PBP I have an "include" file that writes straight bars to the CGRAM area of the display. Later if I wish to make a variable display as a bargraph I simply call a subroutine that automatically writes the bars and fill spaces to form the bar. In the code I have, each character space can contain 0 to 3 vertical lines, therefore a 16 character width display can have a bar that displays from 0 to 48 individual lines. I think I'll be able to convert the Basic to C, but a few of the LCD calls appear to be quite different in C. The basic bargraph code was written by Scott Edwards and released to the PIC basic users. I suppose I could post it if someone thinks they can properly modify it for C. I use 16X2 line parallel (4bit mode) lcd's exclusively, so please don't respond with serial LCD info unless it is somehow directly applicable.

-John
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Thu Sep 30, 2004 1:33 pm     Reply with quote

I have done just what you suggest; the code was used on a VFD using a LCD �compatible� controller. The VFD has the desired custom characters already in its character map. I see that the normal HD44780U LCD character map doesn�t have them so you will need to make your own custom characters.

You need 5, the first one has a single vertical line, the second 2, and so on until you have a completely solid character.

Basically the code works out how many solid characters to display, and then what the last character should be. Looking at it now it generates a 10 character bar graph in a string called bar as I was also giving a description of the variable graphed on the same line. You will have to modify the math to give you 16 characters!

Works very well in practice.


Code:
/*******************************************************************************
* void generatebar(int8 percent)
* Purpose:  This function generates the text line for the bar graph indicator
*/

void generate_bar(int8 percent) {
   int8 solid,last;
   int8 i = 0;
   solid = percent / 10; // number of solid characters to display
   last = percent - (solid * 10); // work out the last character
   last /= 2;
   if (solid) {
      for (i=0;i<solid;i++)
        bar[i] = 0x14;
   } // if (solid)
   if (last) {
      last--;
      bar[i] = 0x10 + last; // select last character
      i++;
   }
   bar[i] = '\0';
} // generate_bar(int8 percent)
hansknec



Joined: 30 Sep 2004
Posts: 25
Location: Virginia, USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Sep 30, 2004 2:00 pm     Reply with quote

Yes, this portion of the code looks straight forward, but it is the actual LCD writing that worries me. When writing to the CGRAM, it looks like I need to have the address and desired value to that address. When wanting the LCD to print a character, either from it's stock list of characters or the CGRAM, I simply enter the address of the character to print. With PBP it seemed easy, but I'm not sure when and where I should be using lcd_putc( char c) or lcd_send_byte(0,1) or the other multitude of commands available to me in C. I'm only in my 3rd day of actually writing to an LCD, so please forgive my ignorance. I know this can't be too hard. I need to tell it where to write and what to write. More code please if you have it. Wink
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Thu Sep 30, 2004 2:37 pm     Reply with quote

Hi, I�ve dug some code out of an old project. This function will set up you user defined characters:

Code:
/*****************************************************************************
* init_user_chars
* Purpose:   Set pointer in LCD memory to CGRAM and load first 8 bytes with
*         user defined characters. Reset pointer to line 1, position 1
*/
void init_user_chars(void) {
   // User defined characters 5 x 7 but remember the cursor on 8
   int8 i;
   // char 0x00 nice degree sign
    const char user_char[49] = {0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00,
   // char 0x01 nice tick sign
                          0x00, 0x00, 0x00, 0x01, 0x02, 0x14, 0x08, 0x00,
   // char 0x02 approx sign
                          0x00, 0x00, 0x08, 0x1D, 0x17, 0x02, 0x00, 0x00,
   // char 0x03 stir forward
                         0x00, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00,
   // char 0x04 stir vertical
                         0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00,
   // char 0x05 stir backward
                         0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x00, 0x00};

   lcd_send_byte(0,64); // Sets ram address

   for (i=0; i<49;i++)
      lcd_send_byte(1,user_char[i]);

   lcd_gotoxy(1,1); // Resets address to top line / position 1
} // init_user_chars


This is the function I always use to write out data to the LCD.

Code:
/*******************************************************************************
* send_to_lcd(int8 location,char *s1)
* Purpose:   Write the supplied strings to the top or bottom line of the LCD
*         16x2 LCD screen depending on location
*/
void send_to_lcd(int8 location,char *s1) {
int i, iLen;
   iLen=strlen(s1);

   // Write to top line
   lcd_gotoxy(1,location);
   // Protect against coding errors
   if (iLen>16)
      iLen=16;
   for(i=0;i<iLen;i++) {
      // Built in auto substitution of Numerical zero with O
      if(s1[i]==48) s1[i]=0x4F;
      lcd_putc(s1[i]);
   } // for
   for (i=iLen; i<16; i++) lcd_putc(' ');
} // send_to_lcd


Code:
void lcd_putc(char c) {
lcd_send_byte(1,c);
} // lcd_putc

This is a typical line of code which uses the send_to_lcd function.

Code:
sprintf(line1,"Low Batteries");
send_to_lcd(1,line1);


To send the user characters just send 0x00 to 0x05 for instance.

Hope these help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group