/LCD1602

AVR Library for LCD1602 based on HD44780

Primary LanguageCMIT LicenseMIT

lm016l Banner

Simple AVR library for LCD1602(based on HD44780)

This library was developed and tested for microcontroller Atmega328p. But I'm sure that it will work for others microcontrollers of the AVR family. Library implements most necessary functions(according to datasheet) and several high-level functions for comfortable and easy work with this LCD.

Also this library allow display russian characters regardless of CGROM model(A00 or A02). If you don't need russian characters or CGROM of your lcd contains Russian characters(ROM Code A02) then just comment or remove line #define RUSSIAN in header file.

Display Commands:

  • Clear all display data and reset AC(address counter):
void ClearDisplay() 
  • Reset AC(address counter):
void ReturnHome() 
  • Set entry mode. I_D - (1: cursor moves right and DDRAM address increased by 1, 1: cursor moves left and DDRAM address decreased by 1), SH - shifting enable
void EntryModeSet(uint8_t I_D, uint8_t SH)
  • ON/OFF Display/Cursor/Blink == 1 then turn on else turn off
void Display_On_Off(uint8_t Display, uint8_t Cursor, uint8_t Blink)
  • Cursor or Display Shift. S_C - (0: moves cursor, 1: moves all the display), R_L - (0: left, 1: right)
void CursourOrDisplayShift(uint8_t S_C, uint8_t R_L)
  • Set CGRAM address to AC:
void SetCGRAMAddress(uint8_t address)
  • Set DDRAM address to AC:
void SetDDRAMAddress(uint8_t address)
  • Write binary 8-bit data to DDRAM/CGRAM:
void WriteDataToRAM(uint8_t data)

Setting functions:

  • Set pin numbers using 4-bit data bus:
void SetPinout4Bits(uint8_t rs, uint8_t e, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  • Set pin numbers using 8-bit data bus:
void SetPinout8Bits(uint8_t rs, uint8_t e, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  • Display initializing
void InitLCD()

Note: For 8-bit data bus (pins D0-D7) use method "SetPinout8Bits", for 4-bit data bus (pins D4-D7) - "SetPinout4Bits"

High-Level functions:

  • Write character pattern to CGRAM
void CreatePattern(uint8_t * pattern, uint8_t address)
  • Print custom pattern to display
void PrintCustomPattern(uint8_t n)
  • Shift all display data left on 1 cell
void ShiftLeft()
  • Shift all display data right on 1 cell
void ShiftRight()
  • Set position(row, column) for output
void SetPosition(char row, char col)
  • Print text to display
void PrintText(char * string)

Note: Function PrintText can display russian characters. To allow russian characters add #define RUSSIAN in lcd1602.h

Example 1 (4-bit data bus)

Circuit

lm016l Banner

Code

#define F_CPU 8000000UL //8Mhz - frequency of CPU (Change this value if frequency of your CPU is different)
#include "lcd1602.h"
#include <util/delay.h>

int main(void)
{
	//Set pinout and mode of data transmission(4-bit or 8-bit)
	SetPinout4Bits(0, 1, 4, 5, 6, 7);

	//Initialize Display
	InitLCD();
	
	//Print famous phrase
	PrintText("Hello World");

	while (1)
	{
		//ticker effect(interval 1 second)
		_delay_ms(1000);
		ShiftLeft(1, 1);
	}
}

Example 2 (8-bit data bus)

Circuit

lm016l Banner

Code

#define F_CPU 8000000UL //8Mhz - frequency of CPU (Change this value if frequency of your CPU is different)
#include "lcd1602.h"
#include <util/delay.h>

int main(void)
{
	//Set pinout and mode of data transmission(4-bit or 8-bit)
	SetPinout8Bits(0, 1, 0, 1, 2, 3, 4, 5, 6, 7);

	//Initialize Display
	InitLCD();
	
	//Print famous phrase
	PrintText("Hello World");

	while (1)
	{
		//ticker effect(interval 1 second)
		_delay_ms(1000);
		ShiftLeft(1, 1);
	}
}