ATmega8 and 2×16 HD44780 LCD
By Radu Motisan Posted on February 13th, 2009 , 4606 Views (Rate 2.19)
Following my first article on the initial findings and results of my tests on the ATMega8, this article will show basic steps on how to connect a 2x16 characters LCD to the micro chip.
The reason for this is to have a way of debugging the microchip functionality, to output the results, the data and various other stuff. I can tell you this is a must have.
To use a minimum of I/O pins, I've decided to build a 4bit interface with the LCD. The hard part was to write the code, but finally after fixing several bugs and reading a lot of HD44780 documentation, I can say that I've completed a nice flexible code - meaning that you can easily configure the LCD pin connection.
The code interface is also attached, not much to say about it here, since it's full of technical details and bit-wise operations, but if anyone has questions feel free to use the comments form at the bottom.
To edit some parameters like frequency or PIN connections (you can change the I/O pins used by the LCD on the ATMega), have a look in lcd.h . Zip attached: (update available, see below).
Function lcd_string2 is very useful for outputting text, since it supports variable parameters similar to printf. So you can use it for outputting misc content: lcd_string2("Hello World! %d\npocketmagic.net",i);
Here are some pictures with my LCD running:
Next thing to do is to start connecting various sensors.
Update October 16, 2009
LCD Code update to fix some issues in the delay functions. (update available, see below).
Update May 1, 2012
Some time has passed since this article was written, meanwhile I had the chance to improve the code a lot. The issue with character corruption requiring a first reset is fixed, so are other small issues I found. All in one, here is a better solution for you to use.
The LCD PINS are configured as following:
LCD D4 -> PORTD 5
LCD D5 -> PORTD 6
LCD D6 -> PORTD 7
LCD D7 -> PORTB 0
LCD RS -> PORTD 3
LCD E -> PORTD 4
You can change all these in HD44780.h
The speed of your MCU should be configured in aux_globals.h, by default it is set for 16MHz:
#define F_CPU 16000000UL
Here is the code for C:
And here is the code for C++, with the LCD interface as a dedicated class! To compile this code, you will need to set your AVR as C++ compatible. It is easy and well worth the time. See my tutorial here, to learn how to do it. C++ code: LCDTestCPP
This code will work perfectly with 1line, 2Line or 4 Lines HD44780 LCDs.
Update February 21, 2013
I have published two updates for this library. One using direct LCD connections (similar to what I have shown here) and another variant that uses a 74HC164 shift register to reduce the connections to only 3 wires. See it here.