HD44780 Display Library for Atmel AVR Microcontrollers
Just include the file
lcd.c (not .h!) into your main program.
As long as the interrupt does not change the port that the display is connected to, the library should be interrupt-safe.
In 4-bit mode, 6 IO pins are needed. Data lines D4-D7, E and RS are connected to the microcontroller. RW can be directly connected to ground. If you already connected RW to the microcontroller, define LCD_RW_PIN and it will automatically be set to low. D0-D3 can be left unconnected. 4-bit-mode needs only about 1 microsecond longer per command than 8-bit-mode, but 8-bit-mode needs four more IO pins.
This mode is selected by defining LCD_MODE_4BIT. LCD_PORT, LCD_DDR, LCD_EN_PIN, LCD_RS_PIN and LCD_D4_PIN need to be defined. D4,D5,D6,D7 are connected to LCD_D4_PIN, LCD_D4_PIN+1, and so on.
Example:
#define LCD_MODE_4BIT
#define LCD_PORT PORTA
#define LCD_DDR DDRA
#define LCD_EN_PIN PA4
#define LCD_RS_PIN PA5
#define LCD_D4_PIN PA0
#define LCD_RW_PIN PA6
Only three IO pins are needed for shift register mode. These are connected to a shift register (74(V)HC(T)595 or similar, 8 bit, serial input, parallel output with latch).
This mode is selected by defining LCD_MODE_SHIFT_4BIT. LCD_SHIFT_PORT, LCD_SHIFT_DDR, LCD_SHIFT_DATA_PIN, LCD_SHIFT_CLOCK_PIN, LCD_SHIFT_LATCH_PIN define the connections to the shift register. For 595-style shift registers, clock is SCK (shift clock), latch is RCK (register clock) and data is D. Set LCD_EN_PIN, LCD_RS_PIN and LCD_D4_PIN to the pin numbers at the shift register. D4, D5, D6 and D7 of the LCD are connected to output LCD_D4_PIN, LCD_D4_pin+1, and so on.
The display size is set with LCD_COLS and LCD_LINES. For most displays, this is the real value of columns and lines. Usually each line can be a maximum of 40 characters wide, a display may be a maximum of 4 lines high. Displays with more than two lines or non-standard displays require special configuration, which is described in the next sections.
Some displays behave as if they were twice as wide and half as high. They just display every long internal line as two lines. For such displays, use the real value for columns and lines, and define LCD_HALF_LINES. In this mode a display may be up to 8 lines high, but only up to 20 characters wide.
The only difference between using this mode and setting double width and half height is that lcd_set_line works correctly.
Some displays have a memory layout that doesn't follow the default of line 1 = 0x00, line 2 = 0x40. A different memory layout can be used by defining LCD_SPECIAL_LINE_LAYOUT and LCD_LINE1_ADDR to LCD_LINE4_ADDR. The typical layout for 4x16 character displays with only one controller is 0x00, 0x40, 0x10, 0x50. If you configured such a display as 2x32 characters (or 4x16 with LCD_HALF_LINES), the second and third line would be swapped.
Example:
#define LCD_SPECIAL_LINE_LAYOUT
#define LCD_LINE1_ADDR 0x00
#define LCD_LINE2_ADDR 0x40
#define LCD_LINE3_ADDR 0x10
#define LCD_LINE4_ADDR 0x50
If a display has two controllers, it has two separate EN pins. You need to define the EN2 pin number with LCD_EN2_PIN. Such a display has to be configured for at least two lines. If you really have a one-line display that is more than 80 characters wide, configure it as two lines high and twice as wide.
#define LCD_TWO_CONTROLLERS