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!
|AVR microcontroller||ATmega 328||1|
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.
â€œLCD_Init()â€ is the sub routine used for initializing theÂ lcd module. And â€œLCD_Disp()â€ is for displaying strings. The function â€œLCD_setCursorâ€ is usedÂ for setting the cursor position.