This article is another step forward in learning more about AVR microcontrollers. We have demonstrated interfacing of LCD module with ATmega328 microcontroller, which will help you to learn its basic concepts. ATmega328 is an eight bit AVR (Advanced Virtual RISC) based microcontroller. It is a powerful microcontroller with a built-in internal memory of around 32Kb. Atmega 328 has only 28 pins.
Therefore it have only limited number of GPIO pins. While designing complex projects we need sufficient number of I/O pins . An LCD module can be interfaced with a microcontroller either in 8 bit mode or in 4 bit mode. 8 bit mode is the conventional mode which uses 8 data lines and RS, R/W, E pins for functioning. However 4 bit mode uses only 4 data lines along with the control pins. This will saves the number of GPIO pins needed for other purpose.
Let’s begin to build our project!
As shown in the circuit diagram, port B and port D of the controller is used for interfacing it with LCD module. In 4 bit mode only 4 lines D4-D7, along with RS, R/W and E pins are used. This will save us 4 pins of our controller which we can use it for other purpose. Here we only need to write to the LCD module. So the R/W pin can be ground it as shown in the schematic diagram.
In this way the total number of pins can be reduced to 6. In 4 Bit mode the data bytes are split into two four bits and are transferred in the form of a nibble. The data transmission to a LCD is performed by assigning logic states to the control pins RS and E. The reset circuit, oscillator circuit and power supply need to be provided for the proper working of the circuit.
Important Aspects of the Program
The programming part is done in embedded C using Atmel studio 7 .At the beginning of the program a pre-processor named “F_CPU” is defined. It is simply your way to tell some of the library code how many CPU cycles per second the processor is executing. Here we defined the F_CPU as 16 MHz. “#include <avr/io.h>” is a header files which provides you with various i/o operations like DDRx, PINx, PORTx, etc. “#include <util/delay.h>” is a header file which provides you with inbuilt delay functions like _delay_ms(), _delay_us(), etc. “_delay_ms(1000)” provides a delay of 1000 milliseconds (i.e., equivalent to 1 second).
DDRx – Data Direction Register configures data direction of the port(Input/Output). The instruction “
PORTx – Port register is for assigning appropriate values for the port pins.
Writing to PORTx.n will immediately change state of the port pins according to given value.
The port and the corresponding pins that we used are defined at the beginning. According to your circuit, you can alter the port name and pin number in the program.
A subroutine called “ LCD_CmdWrite()” is used for sending commands to the LCD. In this function the 8 bit data is break into two 4 bit data and send to LCD as nibbles. The command register of the LCD is the selected by writing a ‘0’ to the RS pin. The writing operation is done by simply enabling the E pin of LCD for few microseconds. Similarly the subroutine called “ LCD_DataWrite()” is used for sending data to the LCD. The data register of the LCD is the selected by writing a ‘1’ to the RS pin.