Skip to main content

Interfacing 16x2 LCD with AVR Atmega 8

In this post I will walk you through on how to write a program onto your AVR microcontroller to display your name on a 16x2 or 16x1 LCD. You do not require a lot of components or soldering. for simplicity, I used a female to female connector to connect the lcd to the microcontroller. I have soldered a header pin for the LCD module.




First let us understand the LCD pin configuration.It has 16 pins. and works on parallel communication.one must also remember that LCD's are slow compared to the microcontrollers we use. hence we have to use a lot of delay(in order of ms not minutes :-P). among the 16 pins, there are three important pins, namely R/S,E,R/W which are addressed as Register Select, Enable,Read or Write respectively.To make it simple, in this post let us connect R/W to ground. which brings the lcd in write mode. so now we have 2 control pins namely RS and E and 8 data pins from D0-D7 to send parallel data. we also have two pins for power supply and two for LED backlight. before I tell the pin number of the above, I will mention the last pin which is also very important and it is called contrast.a voltage divider is constructed for this pin to adjust the contrast.









LCD Pin configuration:

  1. Gnd
  2. VCC
  3. contrast
  4. RS
  5. RW
  6. E
  7. D0
  8. D1
  9. D2
  10. D3
  11. D4
  12. D5
  13. D6
  14. D7
  15. LED+
  16. LED-

Circuit construction:

The connections are simple. This can be implemented for 8051 or any other Avr microcontrollers with little or no modifications. Here I am using an atmega 8. It is a 28 pin IC. To write data I will be using port B, because it is the only 8 bit port available in atmega 8. for RS,RW,E I used port D 4,5,6 pins respectively.connect a potentiometer to control the contrast to pin 3 of the LCD. if you want backlight, connect the pin 15 and pin 16 of lcd to powersupply. while connecting port B with LCD make sure that PB.0 is connected to D0 and PB.7 is connected to D7. please refer the datasheet for pin details.I havent used any external crystal as the external crystal has to be connected to port B and I require the port B. the AVR microcontrollers have a built in 1Mhz clock so it works fine without an external crystal.

list of commands



CODE:

lcd.c

/*
 * lcd.c
 *
 * Created: 02-05-2014 10.35.07 PM
 *  Author: Jayanth
 */ 

/*
D5 RS
D6 RW
D7 EN
PORTB Data
*/
#include <avr/io.h>
#include <util/delay.h>

int i;
char a[16]="hello world!";
char b[16]="By Jayanth";

void lcd_init (void)
{
DDRB=0xff;//data pins
DDRD=0xff;//control pins
_delay_ms(5);
PORTB=0x38;// function set: 2 line, 8 bit mode
cmd();
PORTB=0x0F;//Display : on,Cursor: on,Cursor Blink: on
cmd();
PORTB=0x0C;//cursor invisible
cmd();
PORTB=0x06;//Increment mode
cmd();
PORTB=0x01;//clr screen
cmd();
PORTB=0x02;// return home
cmd();
}

void cmd(void)
{
PORTD=0x80;
_delay_ms(5);
PORTD=0x00;
_delay_ms(5);
}

void data(void)
{
PORTD=0xA0;
_delay_ms(5);
PORTD=0x20;
_delay_ms(5);
PORTD=0x00;
}

int main(void)
{   
lcd_init();
for(int i=0; i<=11; i++)
{
PORTB=a[i];// loop to display first line
data();
}
PORTB=0xc3;//second line + address
cmd();
for(int i=0; i<=9; i++)
{
PORTB=b[i];// loop to display second line
data();
}
}


Note: if you have any problems with the util/delay.h header file, create a new delay function using a for loop to create some delay.

Comments

Popular posts from this blog

Switch debouncer using SR Latch

We use switch in our day to day life to switch ON/OFF a bulb or a fan or  any electrical devices. But when we use switches in digital circuits, we observe a phenomenon called bouncing. This occurs because, when we turn the switch, the mechanical parts vibrate. i.e It toggles between ON and OFF state for some time until the mechanical contact attain equilibrium. this vibrations are minute and are not at all noticeable in electrical circuit. where as in digital circuits, these vibrations create pulses. which are detected by circuits which results in an error.               In the above circuit, there is a switch connected to VCC. You can toggle it between terminal 'a' and terminal 'b'. which as a voltage drop of 'Va' and 'Vb' respectively. In digital circuits if you can observe, it takes a finite amount of time to toggle between terminal 'a' and 'b'. which might approximately take 15ns. Now let us consider the terminal is at 'a'. we

PIC 18F452 Programming: LED blinking using assembly

Here is the basic program you need to start off with when you learn to use a microcontroller. We are using the microchip family PIC microcontroller. Which stands for Peripheral Interface Controller, PIC 18F452 is a 40 pin 8-bit microcontroller with 32 KBytes flash, 1536 Bytes of RAM and 256 Bytes EEPROM. It also boasts 4 timers, and three external interrupt pins. Coming to our first program, We will be using MPLAB for Building this project. Create a new project and select MPASM Suite for compiling. After writing the program, Dont forget to add the linker file and the source file in the source window. The linker file for the PIC 18F452 can be found in the  C:\Program Files\Microchip\MPASM Suite\LKR. search for 18f452. In this program, the Bit RB0, toggles continuously with some delay. Connect an LED to The RB0 Pin, i.e Pin 33.connect a 330E resistor in series with the LED. PROGRAM #INCLUDE p18f452.INC; CONFIG WDT=OFF; disabling the watchdog timer ORG 0; Specifing the st

PIC Programming: making a port bits toggle using assembly

CIRCUIT: Program: #INCLUDE p18f452.INC ORG 0 CLRF TRISD  L1 SETF PORTD CALL DELAY CLRF PORTD CALL DELAY GOTO L1 DELAY MOVLW 0xff MOVWF 0x10 L2 DECF 0x10,F BNZ L2 RETURN END