HD44780 LCD tutorial

From Miosix Wiki
Jump to navigation Jump to search

Include statements and namespace required to use this library

#include <util/lcd44780.h>
using namespace miosix;

This is a C++ library, it cannot be used from a C source file.

The HD44780 is a standard controller for alphanumeric LCD displays. Miosix supports these displays through the Lcd44780 class.

Hardware connections

The connection to the display requires 6 GPIOs connected to the display. Here are the LCD pinout connections

  • Pin 1: GND, connect to ground
  • Pin 2: VCC, connect to a 5V power supply. Some displays may also work with 3.3V
  • Pin 3: Contrast adjustment, connect the center tap of a potentiometer with the other two ends connected to VCC and GND
  • Pin 4: RS, connect to a GPIO
  • Pin 5: RW. the Miosix driver requires to connect this pin to ground
  • Pin 6: E, connecto to a GPIO
  • Pin 7: D0, leave unconnected or connect to VCC
  • Pin 8: D1, leave unconnected or connect to VCC
  • Pin 9: D2, leave unconnected or connect to VCC
  • Pin 10: D3, leave unconnected or connect to VCC
  • Pin 11: D4, connect to a GPIO
  • Pin 12: D5, connect to a GPIO
  • Pin 13: D6, connect to a GPIO
  • Pin 14: D7, connect to a GPIO

Additionally, some display may have two more pins to power a backlight, as LCDs cannot be seen in the dark.

Software interfacing

To use the LCD with Miosix, you first have to connect the 6 pins of the LCD to GPIOs on your board. Any GPIO can be used, so long as it is a free GPIO not used for something else. Then you have to declare the GPIOs in your program as explained in the GPIO tutorial.

typedef Gpio<GPIOB_BASE,12> d4;
typedef Gpio<GPIOB_BASE,13> d5;
typedef Gpio<GPIOB_BASE,14> d6;
typedef Gpio<GPIOB_BASE,15> d7;
typedef Gpio<GPIOC_BASE,1> rs;
typedef Gpio<GPIOC_BASE,2> e;

You can then declare an instance of the Lcd44780 class, whose constructor takes 8 parameters: the 6 GPIOs (in te following order: RS, E, D4, D5, D6, D7), and the number of rows and columns of the display. The most common display type is 2 lines by 16 columns.

Lcd44780 display(rs::getPin(),e::getPin(),d4::getPin(),d5::getPin(),d6::getPin(),d7::getPin(),2,16);

The getPin() member function of the GPIO class returns an object that allows to pass GPIOs as arguments to a function.

Once you have an instance of the Lcd44780, which in the example above is called display, you can call the following member functions on it:

  • clear() clears the display
  • go(int x, int y) moves the cursor to the specified position
  • printf() prints to the display, using the standard C formatting rules.

Thread safety considerations

The Lcd44780 is not thread-safe. Concurrent access from multiple threads requires the use of a mutex to protect it against concurrent accesses.

Examples

An example program for driving an HD44780-compatible display can be found in the mioisx/_examples/hd44780 directory.

Lcd44780.jpg

Note that you are not restricted to LCD display, there are other display technologies that are HD44780-compatible, for example some VFD displays like this work as well

Vfd44780.jpg

Related pages