Home » Microcontrollers & Electronics » C Library for LCD displays that use the HD44780 interface, Part 1

C Library for LCD displays that use the HD44780 interface, Part 1

Hello all,


I have started working with Texas Instruments MSP430 value line micro controllers.  I still enjoy working with Ardunio but I can pick up a 16 bit TI MSP-EXp430G2  for $9.95.


Texas Instruments is a large company and has really embraced the DIY, maker community.  Their online support and documentation is fabulous.

https://store.ti.com/MSP430-LaunchPad-Value-Line-Development-kit-P2031.aspx

I started getting familiar with the chip architecture for the above MSP430.  What better way to learn about the chip then to write some code.

I have a few LCD display lying around and decided that this would be a good project to develop.

In doing some digging out on the web I discovered that many LCD displays use the Hitachi HD44780 controller as a programmable interface.

https://www.sparkfun.com/datasheets/LCD/HD44780.pdf

I did some searching out on the web and found a few code examples and one library that implements code for the HD44780.  

But most of the code was only small samples of code and really was not formatted for for real production use.

Most of the code I found was incomplete, and really buggy, When I pressed the reset button several times the display when nuts and displayed corrupted characters.  

I did find one example of someone who put together a rather nice library, well documented but was missing some common functions that I wanted. 


Also nowhere did I fine any really good code samples that implemented the features of the HD44780 interface.


So I decided to take the following library and modify it to add the features that I wanted and then provide some working code examples.


So a bit thanks goes to nikosapi at

http://nikosapi.org/w/index.php/MSP430_HD44780_Controller_Software

Below I will publish some links to my Kiln repository, with public access so everyone can download the code.

Over the next few posts I will also discuss the coding samples I put together.  

I have put together a YouTube video of the code samples printing out to a 2 line 40 character display that prints 4 lines, 20 characters each.  If you have a different LCD display the library functions will still work, you may have to alter the text length that you are sending in the example code.

Here is the link to the Kiln repository,  Click on the “Download Archive” to download the files to your workstation:

https://objetek.kilnhg.com/Code/msp430-hd44780-4-bit-library/Group/Release/Files


The library was tested using the above MSP430 launchpad,  but the library should work with most micro-controllers.

The library and code samples were created in Texas Instruments Code Composer Studio, which is an Eclipse application with custom plug ins.  

But the code can be taken with run with any tool chain.

Download the code so you can follow along:


Code Examples

SpecialCharacters.c


Look over the below code and we will review


Here is a screen shot of the sample code running on the display:

specialCharcters

 

Notice the line through the 5 x 10 characters.  My display is an ERM2004-1

Looking at the specification the below character detail, might explain this line.

But I cannot tell for sure as documentation is limited.

character

 #include <msp430.h>  
 #include "hd44780_4bit_lib.h"  
 /*  
  * This code example uses 5 x 10 character and one line  
  * to display the special characters show in table 4, ROM Code A00  
  * in the hd44780 specification.  
  * Note using the 5 x 10 characters you can only write to one line on  
  * the display.  
  *  
  */  
 void main()  
 { 
 
   WDTCTL = WDTPW | WDTHOLD; // Disable watchdog timer 
 
   // according to spec give the display 10ms to come up to voltage  
   __delay_cycles(10000 * CPUSPEED);  

   // set up data length, number of lines and font  
   // Note 1 line and 5 x 10 characters  
   hd44780_init(N_1LINE, FONT_10);  

   // if needed set up the cursor and blink  
   // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR);  
   // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR_BLINK);  

   // set Increment direction and cursor direction  
   hd44780_send_command(HD44780_CMD_INCREMENT);  
   hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT);  
   
   // refer to table 4 ROM Code OO  
   // on how to convert upper 4 bits and  
   // lower 4 bits to formulate the below characters  
   hd44780_write_special_char(0xEB);  
   hd44780_write_special_char(0xEB);  
   hd44780_write_special_char(0xEB); 
 
   hd44780_write_special_char(0xEF);  
   hd44780_write_special_char(0xEF);  
   hd44780_write_special_char(0xEF);
  
   hd44780_write_special_char(0xFF);  
   hd44780_write_special_char(0xFF);  
   hd44780_write_special_char(0xFF);  
   
   // this character and the next  
   // character takes up the full  
   // 10 dots in height to print the  
   // decending j and g  
   hd44780_write_special_char(0xE7);  
   hd44780_write_special_char(0xE7);  
   hd44780_write_special_char(0xE7);
  
   hd44780_write_special_char(0xEA);  
   hd44780_write_special_char(0xEA);  
   hd44780_write_special_char(0xEA);
  
   hd44780_write_special_char(0xEB);  
   hd44780_write_special_char(0xEB);  
   hd44780_write_special_char(0xEB);  
 }  

Lets break the code down and analyse it line by line:

 

 // according to spec give the display 10ms to come up to voltage  
 __delay_cycles(10000 * CPUSPEED);  
 // set up data length, number of lines and font  
  // Note 1 line and 5 x 10 characters  
 hd44780_init(N_1LINE, FONT_10);  

The __delay_cycles() function pauses 10 milliseconds to give the display voltage to come up to operating range.  Take a look at the hd44780_4bit_lib.h file.  There are some CPU speed related constants that are defined for use with the __delay_cycles() function.

Note:  __delay_cycles() function can only take constants as an argument so you cannot use a variable with this function.

The hd44780_init() function initializes the display and arguments can be passed to establish number of lines and font size.  This function also sets up the hd44780 interface to use 4 bit operation instead of 8 bit operation.  Check the specification for details.

 

 // if needed set up the cursor and blink  
 // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR);  
 // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR_BLINK);  
 // set Increment direction and cursor direction  
 hd44780_send_command(HD44780_CMD_INCREMENT);  
 hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT);  

The hd44780_send_command() function is used to send commands to the hd44780 interface.  The hd44780 can accept both commands and data.  By changing some bits in the MPU interface you can either send data or instructions.

So in this next example, if we un-comment either of the two lines we can display a cursor and or blink the cursor. 

If you are interested see the reference to the RS Register Selector bit, RS and the RW, Read Write bit for more information.

One of the cool things you can do with the hd44780 interface is determine whether you want to write data from the left to right or right to left.  These next two commands determine these settings.  We can increment or decrement the cursor move direction, and move the cursor to the left or right when data is sent to the display.

 

 // refer to table 4 ROM Code AOO  
 // on how to convert upper 4 bits and  
 // lower 4 bits to formulate the below characters  
 hd44780_write_special_char(0xEB);  
 hd44780_write_special_char(0xEB);  
 hd44780_write_special_char(0xEB);
  
 hd44780_write_special_char(0xEF);  
 hd44780_write_special_char(0xEF);  
 hd44780_write_special_char(0xEF); 
 
 hd44780_write_special_char(0xFF);  
 hd44780_write_special_char(0xFF);  
 hd44780_write_special_char(0xFF);  

There is a table in the specification, Table 4 ROM Code A00 that lists characters and symbols that are supported by the hd44780.  If you follow arcoss the top of the table to get the column of the symbol you are looking for, (write down the upper four bits)  then follow down to the row that contains the symbol you are looking for, write down the lower 4 bits, of that symbol. 

You now can construct a full 8 bit symbol,  convert this binary number to hex and this hex value is what you can send using the hd44780_write_special{char() function.

The hd44780_write_special_char() function changes the RW and RS bits in the MPU interface and sends the symbol to the display (DDRAM) register (DR).

After writing to DR register the AC, address counter is incremented or decremented based on the status of the Increment/Decrement, Cursor Left/Cursor Right settings.  Once you set the starting address the address counter keeps track of where the characters/symbols are written.

 

 // this character and the next  
 // character takes up the full  
 // 10 dots in height to print the  
 // decending j and g  
 hd44780_write_special_char(0xE7);  
 hd44780_write_special_char(0xE7);  
 hd44780_write_special_char(0xE7);
  
 hd44780_write_special_char(0xEA);  
 hd44780_write_special_char(0xEA);  
 hd44780_write_special_char(0xEA);
  
 hd44780_write_special_char(0xEB);  
 hd44780_write_special_char(0xEB);  
 hd44780_write_special_char(0xEB);  

This next section of code also uses the hd44780_write_special_char() function to write characters to the display.  Two of those characters are the lowercase ‘g’ and ‘j’.  The interesting thing to note is that these two characters are extended longer and use the full 10 dot pattern of the 5 x 10 character pattern.  

So when printing these characters you must change the display upon calling the above hd44780_init() function.  The limitation of setting the display to 5 x 10 character dot pattern is you loose one line of the display,  you now can only print to one line on the display.

This post is getting rather long, so I will continue in the next post, covering the example code of the hd44780 LCD display interface.


Stay tuned

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>