Introduction

This tutorial continues from Character LCD Displays - Part 1. In this part we will connect the LCD module to an Atmega8 microcontroller, then write some code to drive it.

Image

The Circuit

Our first task is to build the circuit.

Image

We will be using an AVR 28 pin Development Board. For the power supply we will use a USB to DC Barrel plug cable, so we won't need to build a 5V regulator circuit. Instead we will run a wire from the barrel connector to the positive power rail, as shown in the photo below.

Also we won't need an external crystal or AVCC filter block. We still need to run power to AVCC and this is done with a piece of wire.

Image

Because we want the ability to remove the LCD module later, we will solder a 16 pin female header to the board.

Next we wire-up VDD and BLA to the positive power rail along with VSS, RW, D0, D1, D2, D3 and BLK to the ground power rail. D0 to D3 are being tied to ground because we will drive the display in 4 bit mode. This lets us drive the display with just 6 I/O lines.

Image

Next we add a trimpot for the LCD contrast. I used a 5K pot, but a range of other values would work as well.

Image

We now connect RS, EN and D4-7 to PC0-PC5. These could have been connected to any of the atmega8 I/O ports, but port C seemed to be ideal as it has 6 usable I/O pins. If you decide to use other pins just change the source code accordingly.

Image

As with the Breadboard example we will solder a 16 pin header to the LCD module.

Image

Lastly we attach the module to the female headers.

Image

Coding - Part A

Our first "Hello World" program is below. We are following almost the same series of steps as the 4 bit example in Part 1.

  1.  
  2. #include <avr/io.h>
  3. #include <stdlib.h>
  4. #include <util/delay.h>
  5. #include <stdio.h>
  6.  
  7. //Define functions
  8. //==========================================================
  9. void io_init(void);                         //Initializes IO
  10. void send_nibble(unsigned char __rs, unsigned char __data);
  11.  
  12. //==========================================================
  13.  
  14. int main (void)
  15. {
  16.     io_init();
  17.    
  18.    _delay_ms(15);
  19.    send_nibble(0,0b0010);   //Set to 4 bit operation (note: 1 nibble operation)
  20.    _delay_ms(5);
  21.    
  22.    send_nibble(0,0b0010);   //Function set, 4 bit
  23.    send_nibble(0,0b1000);
  24.  
  25.    send_nibble(0,0b0000);   //Display ON, Cursor On, Cursor Blinking
  26.    send_nibble(0,0b1111);
  27.  
  28.    send_nibble(0,0b0000);   //Clear Display
  29.    send_nibble(0,0b0001);
  30.  
  31.    send_nibble(0,0b0000);  //Entry Mode, Increment cursor position, No display shift
  32.    send_nibble(0,0b0110);
  33.  
  34.    send_nibble(1,0b0100);  //H
  35.    send_nibble(1,0b1000);
  36.  
  37.    send_nibble(1,0b0110);  //e
  38.    send_nibble(1,0b0101);
  39.  
  40.    send_nibble(1,0b0110);  //l
  41.    send_nibble(1,0b1100);
  42.  
  43.    send_nibble(1,0b0110);  //l
  44.    send_nibble(1,0b1100);
  45.  
  46.    send_nibble(1,0b0110);  //o
  47.    send_nibble(1,0b1111);
  48.  
  49.    send_nibble(1,0b0010);  //Space
  50.    send_nibble(1,0b0000);
  51.  
  52.    send_nibble(1,0b0101);  //w
  53.    send_nibble(1,0b0111);
  54.  
  55.    send_nibble(1,0b0110);  //o
  56.    send_nibble(1,0b1111);
  57.  
  58.    send_nibble(1,0b0111);  //r
  59.    send_nibble(1,0b0010);
  60.  
  61.    send_nibble(1,0b0110);  //l
  62.    send_nibble(1,0b1100);
  63.    
  64.    send_nibble(1,0b0110);  //d
  65.    send_nibble(1,0b0100);
  66.  
  67.     return(0);
  68. }
  69.  
  70. void io_init (void)
  71. {
  72.     /*
  73.     PC 7: N/A
  74.     PC 6: Reset
  75.     PC 5: Enable
  76.     PC 4: Register Select
  77.     PC 3: Data 7
  78.     PC 2: Data 6
  79.     PC 1: Data 5
  80.     PC 0: Data 4  
  81.     */
  82.    
  83.     DDRC = 0b00111111;
  84. }
  85.  
  86. void send_nibble(unsigned char __rs, unsigned char __data)
  87. {
  88.    PORTC = (__rs<<4) | __data | 0b00100000;      // Set RS &amp; Data. Set EN=High
  89.    _delay_ms(1);
  90.    PORTC = (__rs<<4) | __data;                   // Set RS &amp; Data. Set EN=Low
  91.    _delay_ms(1);
  92. }
  93.  

Image

Source code for Part A

Coding - Part B

The previous example is fine, if all you want to do is understand the principles of how LCD displays are driven, but in practice coding in this manner is a bit painful. To make things easier we need to abstract away the finer implementation details and focus on the operations the developer needs to perform. The easiest way to do this is use a pre-existing library.

A very popular HD44780 AVR library is Peter Fleury's LCD library. This library is very good but I'm going to use alank2's slimmed down LCD library instead. My reasons are:

  • Peter's library requires that R/W be connected to one of the I/O ports
  • alank2's library separates out settings into a separate .h file. I think this is a lot cleaner
  • alank2's library compiles a bit more tightly
  • alank2's library supports up to 4 LCD displays, each sharing the same data and RS lines, but having different EN lines.

To use alank2's library

  • Add hd44780.c and hd44780.h to your project
  • Create hd44780_settings.h (an example is provided in alank2's zip file) and tailor it for you project
  • Use the provided functions

Using alank's library we create the next example program. This program does much more than our previous "Hello World" example and is only 30 lines of code.

  1.  
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdlib.h>
  5. #include "hd44780.h"
  6.  
  7. int main (void)
  8. {
  9.     lcd_init();
  10.    
  11.     lcd_clrscr();
  12.     lcd_puts("Hello World...");  
  13.  
  14.     lcd_goto(40);                    //Position 40 is the start of line 2
  15.     char digit[1];
  16.     for (int i=0;i<16;i++)
  17.     {
  18.        _delay_ms(250);
  19.        itoa(i,digit,16);
  20.        lcd_puts(digit);
  21.     }
  22.  
  23.     _delay_ms(1000);
  24.     lcd_clrscr();
  25.    
  26.     lcd_puts("Goodbye");  
  27.     _delay_ms(2000);
  28.     lcd_clrscr();
  29.    
  30.     return(0);
  31. }

Image

Source code for Part B