Control Module for Character LCD


layer

EZ-LCD is a generic control module for character LCD based on HD44780 or compatibles. It enables to use all character LCD modules without consideration of panel organization and interface type. Because EZ-LCD is written in ANSI C, it can be easily ported to most microcontroller.

Application Interface

EZ-LCD module provides the following functions to the application programs. Only three functions from top will sufficient for most applications but it has some useful functions in addition. When you wish to use formatted string output to the LCD like regular printf function, it can be added with Embedded String Functions.

How to Port

lcd sch

Right image shows an example circuit and the following explains to port the EZ-LCD in three steps.

Step 1. Select Function

First of all configure the function of EZ-LCD module. It is defined in hd44780.h. At least the panel organization of the LCD module to be used must be set properly. The module size can be reduced by disabling unused functions.

#define _LCD_ROWS   2    /* Number of rows */
#define _LCD_COLS   16   /* Number of columns */

Step 2. Fill Hardware Control Macros

Fill hardware depndent bit-banging macros defined in hd44780.c and add control functions if needed. Note that the EZ-LCD module does not support R/W signal so that set the R/W signal low if it is wired to the port.

#include <avr/io.h>                     /* Device specific include file */

#define	IF_BUS       4                  /* Bus width */

#define	IF_INIT()    {PORTB &= 0x03; DDRB |= 0xFC;} /* Initialize control port (can be blanked if initialized by other code) */
#define E1_HIGH()    PORTB |= 0x08      /* Set E high */
#define E1_LOW()     PORTB &= 0xF7      /* Set E low */
#define	RS_HIGH()    PORTB |= 0x04      /* Set RS high */
#define	RS_LOW()     PORTB &= 0xFB      /* Set RS low */
#define	OUT_DATA(d)  PORTB = (PORTB & 0x0F) | (d & 0xF0)  /* Put d on the data buf (upper 4 bits in 4-bit mode) */

#define	IF_DLY60()                      /* Delay >60ns for RS to E (can be blanked on most uC) */
#define	IF_DLY450()  {PINB; PINB;}      /* Delay >=450ns@3V, >=250ns@5V for E pulse */
#define DELAY_US(n)  lcd_delay_us(n)    /* Delay in unit of microsecond (defined below) */

static void lcd_delay_us (uint16_t n)
{
    do {   /* 8 clocks per loop for Atmel AVR/8MHz */
        PINB; PINB; PINB; PINB;
    } while (--n);
}

Step 3. Check Function

Finally, write a simple test program and check if it works or not. This code fills the LCD with characters from '0' in order of ASCII code.

#include "hd44780.h"  /* EZ-LCD include file */

int main (void)
{
    uint8_t c;

    lcd_init();                    /* Initialize LCD */

    for (c = '0'; c != 128; c++) { /* Put characters to the LCD */
        lcd_putc(c);
    }

    return 0;
}

Resource


Return