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

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

Let us continue with reviewing the hd44780 LCD library code examples. Please refer to the previous post for links to the source code and an explanation of what we are working on.

We will now look at the next code example:

Code Examples

CursorPosition.c

CusorPosition

 

Note:  In the above image, the lines that start with ‘0’ and ‘1’ are line one.

The lines that start with ‘2’ and ‘3’ are line two.


The display I have is an ERM2004-1.  It supports two 40 character lines split across 4 20 character lines on the display.

If you have not done so, refer to the last post, for the code repository link, and download the source code so you can follow along.

We will first display the entire code example and then discuss the code section by section.

 

 #include <msp430.h>  
 #include "hd44780_4bit_lib.h"  
 
/*  
  * This code example uses the hd44780_setCursorPosition() functioh  
  * to set the starting cursor position using row, col information  
  * This cursor info is then written to the DDRAM address.  
  * Using this method it is very easy to postion text on the dissplay  
  */  

 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  
   // notice we have setup the display for 2 lines and using the 5 x 8 character size  
   hd44780_init(N_2LINE, FONT_8);  

   // if needed set up the cursor  
   // 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);  
   hd44780_send_command(HD44780_CMD_RETURN_HOME);  
   hd44780_send_command(HD44780_CMD_CLEAR_DISPLAY);
  
   // The display I am testing with is a two line 40 character display  
   // The display shows 20 characters per line so each line wraps twice  
   // for what looks like a 4 line display.  
   // On line one if you write past the 20th character the text shows up on what appears  
   // to be the third line.  
   // Same goes for the second line, any character past the 20th character is written  
   // to what appears to be the fourth line.  
   // 
 
   hd44780_setCursorPosition(0, 0);  
   hd44780_write_string("0123456789");  

   hd44780_setCursorPosition(0, 10);  
   hd44780_write_string("0123456789");
  
   hd44780_setCursorPosition(0, 20);  
   hd44780_write_string("11234567890123456789"); 
 
   hd44780_setCursorPosition(1, 0);  
   hd44780_write_string("21234567890123456789"); 
 
   hd44780_setCursorPosition(1, 20);  
   hd44780_write_string("31234567890123456789"); 
 
   hd44780_send_command(HD44780_CMD_RETURN_HOME);  
 }  

The first section of the code is explained in the previous post. 

The  hd44780_send_command() function sets up the hd44780 interface to send commands to the IR, instruction register.  These commands are just setting up the display for writing.

If you are interested, please refer to the hd4780 specification for details.  Refer to the previous post for a link for the specification.

hd44780_setCursorPosition(0, 0);   
hd44780_write_string("0123456789");  

hd44780_setCursorPosition(0, 10); 
hd44780_write_string("0123456789"); 
 
 hd44780_setCursorPosition(0, 20);  
 hd44780_write_string("11234567890123456789");
  
 hd44780_setCursorPosition(1, 0);  
 hd44780_write_string("21234567890123456789");  

 hd44780_setCursorPosition(1, 20);  
 hd44780_write_string("31234567890123456789");
  
 hd44780_send_command(HD44780_CMD_RETURN_HOME);  

The hd44780_setCursorPosition() function calls an Instruction, called ‘Set DDRAM address”

The arguments passed set up the line and cursor position so that the next time data is sent to the display, that data is positioned at the correct location.

The hd44780_write_string() function takes the passed string and prints one character at a time to the starting location specified by the hd44780_setCursorPosition() function.  The AC Address Counter works it magic so you do not have to worry about incrementing or decrementing the cursor position.


Code Example


readDDRAM.c

readDDRAM

This display does not look like much, but take a look at what is really happening.

First we are writing some text to the display, then we reset the starting position of the AC, Address Counter and read the characters that were written.  

Then we write the characters that were read back to the display.

Lets look at the code:

 

 #include <msp430.h>  
 #include "hd44780_4bit_lib.h"  
 #include <string.h>  
 /*  
  * This code example show how to read text from DDRAM, 
  * in this example we will write some text to the display  
  * then read the text and then write the read text back to the display.  
  */  
 void main()  
 {  
   WDTCTL = WDTPW | WDTHOLD; // Disable watchdog timer 
 
   uint8_t rtn = 0;  
   uint8_t readStr[80];  
   uint8_t *pint;  
   unsigned int cnt = 0;  
   unsigned int charCnt = 0; 
 
   // 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  
   // notice we have setup the display for 2 lines and using the 5 x 8 character size  
   hd44780_init(N_2LINE, FONT_8);
  
   // turn display on and clear the display  
   // and set cursor to home position 0x00  
   // hd44780_send_command(HD44780_CMD_DISPLAY_ON);  
   // hd44780_send_command(HD44780_CMD_RETURN_HOME);  
   // hd44780_send_command(HD44780_CMD_CLEAR_DISPLAY);  

   hd44780_setCursorPosition(0, 0);  
   hd44780_write_string("this"); 
 
   hd44780_setCursorPosition(0, 0);  
   pint = readStr;
  
   // reads characters from display  
   while(1)  
   {  
        rtn = hd44780_readByte();  
        if (rtn == ' ')  
        {  
             break;  
        }  
        *pint = rtn;  
        pint++;  
        charCnt++;  
   }  

   // writes read characters back to display  
   for(cnt = 0; cnt < charCnt; cnt++)  
   {  
        hd44780_write_special_char(readStr[cnt]);  
   }
  
   hd44780_send_command(HD44780_CMD_RETURN_HOME);  
 }  

Let us break up the code and see what is going on:

uint8_t rtn = 0;  
  uint8_t readStr[80];  
  uint8_t *pint;  
  unsigned int cnt = 0;  
  unsigned int charCnt = 0;
  
  // 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  
  // notice we have setup the display for 2 lines and using the 5 x 8 character size  
  hd44780_init(N_2LINE, FONT_8);  

 

First off we setup some variables that we can use during the reading and writing of the characters extracted from the display.

Notice we are setting up the display for 2 lines and 5 x 8 character dot pattern.

hd44780_setCursorPosition(0, 0);  
hd44780_write_string("this"); 
 
hd44780_setCursorPosition(0, 0);  
pint = readStr;  
 
// reads characters from display  
 while(1)  
{  
     rtn = hd44780_readByte();  
     if (rtn == ' ')  
     {  
         break;  
     } 
 
     *pint = rtn;  
     pint++;  
     charCnt++;  
}  

 

We now set the cursor position, on the display to the row 0, column 0 and write the text to the display.

We setup a pointer to the readStr array.  Now we loop through the DDRAM, grabbing a character at a time and placing the character into the array.

If the next received character is ‘ ‘, we then assume that there are no more characters and we exit the loop.

This is just an example,  if you expect a series of words then your logic will have to be a bit more complex, such as looping through the entire line, an easy fix.

// writes read characters back to display  
  
for(cnt = 0; cnt < charCnt; cnt++)  
{  
     hd44780_write_special_char(readStr[cnt]);  
}  

hd44780_send_command(HD44780_CMD_RETURN_HOME);  

 

Since the cursor position is being tracked by the AC, Address Counter, our cursor is already sitting at a location after the read text.  All we have to do is read the characters from the array and display them one at a time back to the display.


Code Example


animate.c

anaimate

 

This image is rather boring.  But check out the video later in the post.

animate.c code example shows the animateLeft() and animateRight() functions.

I wanted to spice up task of writing text to displays.

Using hd44780_set_cursorPosition() function, the Increment/Decrement, MoveLeft and MoveRight commands, you can do some rather nice text animation.

Let us look at the code and see how we can accomplish this task.

 

 #include <msp430.h>  
 #include "hd44780_4bit_lib.h"  
 #include <string.h>  
 
 /*  
  * This code example writes text to the screen using the  
  * Cursor Increment, Decrement and Cursor Left and Right  
  * commands, along with some functions that anaimate the  
  * printing of characters to the display  
  * I was going to use timer events for the character  
  * wait time, but I wanted the code to be non CPU specific.  
  */ 
 
 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  
   // notice we have setup the display for 2 lines and using the 5 x 8 character size  
   hd44780_init(N_2LINE, FONT_8);  

   // if needed set up the cursor  
   // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR);  
   // hd44780_send_command(HD44780_CMD_DISPLAY_ON_CURSOR_BLINK); 
 
      hd44780_send_command(HD44780_CMD_INCREMENT);  
      hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT); 
 
      hd44780_setCursorPosition(0, 0); 
 
      // The anaimate functions take three parameters:  
      // The text string to print  
      // The visible width of the display in characters  
      // The justification, 'L' for Left justification  
      // 'C' for centered text  
      // 'R' for right jusificatiopn  
      // The number of clock cycles you want to wait after  
      // each character is printed  

      hd44780_animateRight("This is Right", 20, 'C', 2500); 
 
      hd44780_send_command(HD44780_CMD_DECREMENT);  
      hd44780_send_command(HD44780_CMD_MOVE_CURSOR_LEFT); 
 
      hd44780_setCursorPosition(1, 19);
  
      hd44780_animateLeft("This is Left", 20, 'C', 2500);
  
      hd44780_send_command(HD44780_CMD_INCREMENT);  
      hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT);
  
      hd44780_send_command(HD44780_CMD_RETURN_HOME);  
 }  

Let us take the code and review section by section:

hd44780_send_command(HD44780_CMD_INCREMENT);  
 hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT);
  
 hd44780_setCursorPosition(0, 0);
  
 // The anaimate functions take three parameters:  
 // The text string to print  
 // The visible width of the display in characters  
 // The justification, 'L' for Left justification  
 // 'C' for centered text  
 // 'R' for right jusificatiopn  
 // The number of clock cycles you want to wait after  
 // each character is printed 
 
 hd44780_animateRight("This is Right", 20, 'C', 2500);  

 

First we set the AC Address Counter to Increment the text, then we set up the display to move the cursor to the right.

Then we set the cursor position to row 0, column 0.

We then call the hd44780_animateRight() function.

Notice we have to pass a few arguments to this method.

The text you want to print to the display

The visible width of line on the display,  On my display line one is split between two 20 character lines on the display.


Then we specify the text justification on the display.  ‘L’  for left justification,

‘C’ for center justification and ‘R’ for right justification.

You also have to enter a micro second pause time, this parameter will specify the pause time after each character is printed on the display.  

By changing this parameter you can speed up or slow down the animation of the characters being printed to the display.


Calling the hd44780_animateRight() function will then print the string of text to the display.

 

hd44780_send_command(HD44780_CMD_DECREMENT);  
 hd44780_send_command(HD44780_CMD_MOVE_CURSOR_LEFT);
  
 hd44780_setCursorPosition(1, 19); 
 
 hd44780_animateLeft("This is Left", 20, 'C', 2500); 

 hd44780_send_command(HD44780_CMD_INCREMENT);  
 hd44780_send_command(HD44780_CMD_MOVE_CURSOR_RIGHT);  
 hd44780_send_command(HD44780_CMD_RETURN_HOME);  

 

Likewise, we can then change the AC, Address Counter to Decrement the cursor position and move the characters to the left.

We now set the cursor position to row 1 and column 19, last character of of the first line. Remember display is zero based.  Start counting from 0 instead if 1.

We then call hd44780_animateLeft() to animate the characters to the left.

These coding samples are just a basic example of what you can do with the hd44780 controller interface.

If I come up with more additions to the library I will add them any let everyone know in a new post.

Here is the video

In the video I mention 5 x 8 and 5 x 10 “characters”  should be dots

I am working on my video skills, please be patient.  

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>