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...

ADXL 355 Accelerometer

A few days ago I went to get components for a project I was building. and came across a 3-axis accelerometer. So I decided to test it and make a post to give you an idea of things work and how you can use it. ADXL 355                   The accelerometer I have is ADXL 355 v2.0 board. there is are 5 headder pins, ground, 5V supply, X-axis output,Y-axis output and Z-axis output. It is quite compact. The one I have measured around 15mm x 15mm. These sensors are low cost. And can be incorporated in Projects or robots we build. The ADXL 355 board I have has an onboard Red led (smd) for the indication of power. The accelerometer has a wide range of applications. ADXL 355 is a basic accelerometer which gives analog values for all the three axis depending upon the position of the IC. ADXL 345 is an advanced accelerometr which can detect free-fall,activity,tap,double tap and communicates via I2C. The ADXL is a a ultra-lowpower as it consumes only 25 u...

Latch using JK Flip Flop

In this post we will learn how to use JK flip flop as a latch. i.e. for one pulse, the circuit continues to stay in the ON state until the next pulse. and the circuit continues to be in the OFF state till the next pulse. This can be achieved by using toggle mode in JK flip flop. TRUTH TABLE: IC 7476 J       k       set '      reset'        Q      Q' 1       1        1            1               Toggle IC CD4026 J       k       set       reset         Q      Q' 1       1        0           0               Toggle Note: Set is referred as PRE or preset. Reset is referred as CLR or clear. Refer the Datasheet of the JK flip flop you are using ...