You are on page 1of 11

AVR Atmega

BABotre,Scientist
AgriElectronicsGroup,
CSIRCentralElectroEngineeringnicsResearchInstitute,
l l
h
CSIRCEERI,Pilani.
;
g
Email:bhau@ceeri.ernet.in;bbotre@gmail.com

LCD Interface

Pin description for LCD

RS,registerselect:

If RS 0 I t ti
IfRS=0,Instruction
commandcoderegisteris
selected

IfRS=1,dataregisteris
If
RS 1 d
i
i
selected

R/W allowsusertowrite
i f
informationtoLCDorread
i
d
informationfromLCD
Eenable usedtolatch
informationpresentedtoitsdata
pins.
Whendataissuppliedtodata
pins,ahightolowpulsemustbe
appliedtothispininordertoLCD
latchthedatapresentedtoits
d t i Th
datapins.Thepulsemustbe
l
tb
450nswide

LCD command codes

LCD timing diagram 4-bit write

Cursor addresses

LCD programming in C
Defineport
Initialize LCD
InitializeLCD

LCD_init()

DATAwritetoLCD

LCD_data()

CommandwritetoLCD

LCD command()
LCD_command()

Define port
#define LCD PRT
#defineLCD_PRT

PORTC

// LCD data PORT


//LCDdataPORT

#defineLCD_DDR

DDRC

//LCDdataDDR

#defineLCD_PIN

PINC

//LCDdataPIN

#defineLCD_RS

#defineLCD_E

LCD_init
LCD_DDR

=0xFF;

//LCDdataportoutput

LCD_PRT=0x00

//LCDdata=0x00

_delay_ms(15);
d l
(15)

// d l 15
//delay15ms

LCD_command(0x33);
_delay_ms(1);
LCD_command(0x32);
_delay_ms(1);
LCD command(0x28);
LCD_command(0x28);
_delay_ms(1);
LCD_command(0x0e);

//4bitmode
//wait
//4bitmode
//wait
// 4 bit mode
//4bitmode
//wait
//displayon,cursoron

_delay_ms(1);
LCD_command(0x01);

//clearlcd

_delay_ms(1);
delay ms(1);
LCD_command(0x06);
_delay_ms(1);

//shiftcursortoright

LCD_command
LCD_command(unsignedcharcmd)
{
LCD PRT ((LCD PRT & 0 0F) | ( d&0b11110000))
LCD_PRT=((LCD_PRT&0x0F)|(cmd&0b11110000));
LCD_PRT&=~(1<<LCD_RS);
LCD_PRT|=1<<LCD_E;

//RS=0
//E=1

_delay_us(2);
LCD_PRT&=~(1<<LCD_E);

//E=0

_delay_us(100);
delay us(100);
LCD_PRT=((LCD_PRT&0x0F)|((cmd&0b00001111)<<4));
LCD_PRT|=1<<LCD_E;

//E=1

_delay_us(2);
LCD_PRT&=~(1<<LCD_E);
_delay_us(20);
delay us(20);
}

//E=0

LCD_data
LCD_data(unsignedchardata)
{
LCD PRT ((LCD PRT & 0 0F) | (d &0b11110000))
LCD_PRT=((LCD_PRT&0x0F)|(data&0b11110000));
LCD_PRT|=(1<<LCD_RS);

//RS=1

LCD_PRT|=1<<LCD_E;

//E=1

_delay_us(2);
LCD_PRT&=~(1<<LCD_E);

//E=0

_delay_us(100);
delay us(100);
LCD_PRT=((LCD_PRT&0x0F)|((data&0b00001111)<<4));
LCD_PRT|=1<<LCD_E;

//E=1

_delay_us(2);
LCD_PRT&=~(1<<LCD_E);
_delay_us(20);
delay us(20);
}

//E=0