You are viewing an older revision! See the latest version

Text LCD Enhanced

This page describes an enhanced forked version of the basic driver code library for Text LCD panels using the 4-bit HD44780 lcd display driver.

New features

  • Support for more LCD types
    • TextLCD::LCD8x1 8x1 LCD panel
    • TextLCD::LCD8x2 8x2 LCD panel
    • TextLCD::LCD16x4 16x4 LCD panel
    • TextLCD::LCD24x2 24x2 LCD panel
    • TextLCD::LCD24x4 24x4 LCD panel (for KS0078 controller)
    • TextLCD::LCD40x2 40x2 LCD panel
  • Support for Cursor and/or Blinking current character
  • Support for User Defined Characters to display special symbols
  • Improved methods to get and set the memory address of the current character

Background

The LCD driver HD44780 is designed for 2 rows of max 40 chars. Note that it needs some support drivers to control that maximum number of chars.

The address for the first char on the first line is 0x00. The address for the first char on the second line is 0x40. Four line displays are generally created by splitting the two line display and arranging the two parts above eachother. For example 2x40 is split in 2 parts of 2x20 and arranged as 4x20. The adddresses for the 3rd and 4th line just continue where the split was made:

The address for the first char on the first line is 0x00, the last char is 0x13.

The address for the first char on the second line is 0x40, the last char is 0x53.

The address for the first char on the third line is 0x14, the last char is 0x27.

The address for the first char on the fourth line is 0x54, the last char is 0x67.

The original TextLCD lib uses the address(column,row) method to return the memory address. However, the method adds 0x80 to the returned addressvalue to include the 'set display datamemory address' command. This is rather confusing when you try to make sense of the actual address. See discussion here.

The enhanced library provides the getAddress(column,row) method to return the correct address. The original method is still provided for compatibility reasons.

Notes: The HD44780 is pretty flexible and variations of driverhardware and LCD glass layout may result in differences. Some 4 line displays actually use two controllers with separate enable pins. The HD44780 supports left/right shifting of memory locations (address) wrt displayposition. That changes the address of the char that is shown on a displaylocation.

Text LCD Enhanced Library

Import library

Public Types

enum LCDType {
LCD8x1 , LCD8x2 , LCD16x1 , LCD16x2 ,
LCD16x2B , LCD16x4 , LCD20x2 , LCD20x4 ,
LCD24x2 , LCD24x4 , LCD40x2
}

LCD panel format.

More...
enum LCDCursor { CurOff_BlkOff , CurOn_BlkOff , CurOff_BlkOn , CurOn_BlkOn }

LCD Cursor control.

More...

Public Member Functions

TextLCD (PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type=LCD16x2)
Create a TextLCD interface for using regural mbed pins.
TextLCD (I2C *i2c, char deviceAddress, LCDType type=LCD16x2)
Create a TextLCD interface using an I2C PC8574 portexpander.
int putc (int c)
Create a TextLCD interface using an SPI 74595 portexpander.
int printf (const char *format,...)
Write a formated string to the LCD.
void locate (int column, int row)
Locate to a screen column and row.
int getAddress (int column, int row)
Return the memoryaddress of screen column and row location.
void setAddress (int column, int row)
Set the memoryaddress of screen column and row location.
void cls ()
Clear the screen and locate to 0,0.
int rows ()
Return the number of rows.
int columns ()
Return the number of columns.
void setCursor ( LCDCursor show)
Set the Cursormode.
void setUDC (unsigned char c, char *udc_data)
Set User Defined Characters.

Protected Types

enum _LCDBus { _PinBus , _I2CBus , _SPIBus }

LCD Bus control.

More...

Protected Member Functions

void _init ()
Init the LCD controller 4-bit mode, number of lines, no cursor etc Clear display.

Example Code

// Hello World! for the TextLCD

#include "mbed.h"
#include "TextLCD.h"

// Host PC Communication channels
Serial pc(USBTX, USBRX); // tx, rx

//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x4); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x2); // rs, e, d4-d7 ok
TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD24x2); // rs, e, d4-d7 ok
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD40x2); // rs, e, d4-d7 ok

int main() {
    pc.printf("LCD Test. Columns=%d, Rows=%d\n\r", lcd.columns(), lcd.rows());
    
    for (int row=0; row<lcd.rows(); row++) {
      int col=0;
      
      pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
//      lcd.putc('-');
      lcd.putc('0' + row);      
      
      for (col=1; col<lcd.columns()-1; col++) {    
        lcd.putc('*');
      }

      pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
      lcd.putc('+');
        
    }    
    
// Show cursor as blinking character
    lcd.setCursor(TextLCD::CurOff_BlkOn);

// Set and show user defined characters. A maximum of 8 UDCs are supported by the HD44780.
// They are defined by a 5x7 bitpattern. 
    lcd.setUDC(0, (char *) udc_0);  // Show |>
    lcd.putc(0);    
    lcd.setUDC(1, (char *) udc_1);  // Show <|
    lcd.putc(1);    
    lcd.setUDC(2, (char *) udc_2);
    lcd.putc(2);    
    lcd.setUDC(3, (char *) udc_3);
    lcd.putc(3);    
    lcd.setUDC(4, (char *) udc_4);
    lcd.putc(4);    
    lcd.setUDC(5, (char *) udc_5);
    lcd.putc(5);    
    lcd.setUDC(6, (char *) udc_6);
    lcd.putc(6);    
    lcd.setUDC(7, (char *) udc_7);
    lcd.putc(7);  
 
}

The terminal will print the actual memory addresses like this:

LCD Test. Columns=20, Rows=4

MemAddr(Col=0, Row=0)=0x00

MemAddr(Col=19, Row=0)=0x13

MemAddr(Col=0, Row=1)=0x40

MemAddr(Col=19, Row=1)=0x53

MemAddr(Col=0, Row=2)=0x14

MemAddr(Col=19, Row=2)=0x27

MemAddr(Col=0, Row=3)=0x54

MemAddr(Col=19, Row=3)=0x67

Examples

Some results are shown here:

LCD20x2

/media/uploads/wim/_scaled_img_2636.jpg

LCD24x2

/media/uploads/wim/_scaled_img_2638.jpg

LCD40x2

/media/uploads/wim/_scaled_img_2637.jpg

LCD16x4

/media/uploads/wim/_scaled_img_2640.jpg

LCD20x4

/media/uploads/wim/_scaled_img_2642.jpg

LCD24x4

/media/uploads/wim/_scaled_img_2646.jpg

User Defined Characters

/media/uploads/wim/_scaled_img_2645_-_copy.jpg

References


All wikipages