GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
Experiment No.-7
Aim- To study and Perform LCD Interfacing in 8 Bit & 4 Bit mode with ATmega32.
Apparatus - Atmel Studio, Easy AVR7
Theory:
This lab demonstrated that an LCD can be interfaced in 4-bit mode and 8 bit mode to an ATmega32
AVR microcontroller. LCD can also be used in 8-bit mode but the advantage of using it in 4-bit
mode is that we can save the I/O ports of a microcontroller. In 4-bit mode, we need to send the
character after splitting it into higher and lower nibbles (4-bits for data only).
PIN SYMBOL I/O DESCRIPTION
1 Vss - Power supply (GND)
2 Vcc - Power supply (+5V)
3 Vdd - Contrast Settings (0~2V)
0 = Select command reg.
4 RS I
1 = Select data reg. of LCD
0 = Write to LCD
5 R/W I
1 = Read from LCD
The Enable (E) line allows access to the display through R/W and RS
6 E I
lines
7 DB0 I/O Data bit line 0 (LSB)
8 DB1 I/O Data bit line 1
9 DB2 I/O Data bit line 2
10 DB3 I/O Data bit line 3
11 DB4 I/O Data bit line 4
12 DB5 I/O Data bit line 5 For 4-bit Mode, only these pins
13 DB6 I/O Data bit line 6 are used as data bits
14 DB7 I/O Data bit line 7 (MSB)
15 BLA - Backlight Anode (+)
16 BLK - Backlight Cathode (-)
Supply connections and pin diagram of 16x2 LCD
Following table shows some useful command list which is generally required to interface and
program the microcontroller with LCD.
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
Line No. Character Positions DDRAM Address
00 01 02 03 – 04 05 06 07 80 81 82 83 – 84 85 86 87
1
08 09 10 11 – 12 13 14 15 88 89 8A 8B – 8C 8D 8E 8F
00 01 02 03 – 04 05 06 07 C0 C1 C2 C3 – C4 C5 C6 C7
2
08 09 10 11 – 12 13 14 15 C8 C9 CA CB – CC CD CE CF
Hitachi 44780 16 x 2 LCD Character Locations
Steps to write on LCD:
To write the data or command on LCD, following steps are undertaken:
1. Set R/W bit to low
2. Set RS bit to logic 0 or 1 (command or character)
3. Set data to data lines (if it is writing)
4. Set E to high and then low for some time
5. Finally write the data from data lines (if it is reading)
C Code to Display a Character Type Array on LCD: [4- bit MODE]
// Program to interface LCD in 4 bit mode with AVR microcontroller
#define F_CPU 1000000UL
#include<avr/io.h>
#include<util/delay.h>
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
//Macros Definition
#define BitSet(p,m) ((p) |= (m))
#define BitClr(p, m) ((p) &= ~(m))
#define BitFlip(p,m) ((p) ^= (m))
#define Bit(x) (0x01 << (x))
void LCD_init();
void cmd_4b(unsigned char);
void data_4b(unsigned char);
void LCDcmd(unsigned char);
void LCDdata(unsigned char);
unsigned int main(void)
{
unsigned char data0[]="GEC,EC DEPT.";
unsigned char data1[]="GANDHINAGAR";
unsigned int i=0;
DDRA=0xFF;
LCD_init(); //Initialize LCD
cmd_4b(0x81); //Cursor at Line 1, Position 7
while(data0[i]!='\0') //continue loop until null is arrived
{
data_4b(data0[i]); //send all characters for 4-bit data conversion
_delay_ms(50);
i++; //to access next character of array
}
cmd_4b(0Xc1); //Cursor at Line 2, Position 4
i=0;
while(data1[i]!='\0') //continue loop until null is arrived
{
data_4b(data1[i]); //send all characters for 4-bit data conversion
_delay_ms(50);
i++; //to access next character of array
}
while(1);
}
void LCD_init() // Initialize LCD
{
cmd_4b(0x02); //to initialize LCD in 4-bit mode.
cmd_4b(0x28); //to initialize LCD in 2 lines, 5x7 dots and 4bit mode
cmd_4b(0x0C); //Display is ON, cursor OFF
}
void cmd_4b(unsigned char Cmd)
{
char Cmd1;
Cmd1 = Cmd & 0xF0; //mask lower nibble because PA4-PA7 pins are used
LCDcmd(Cmd1); // send to LCD
Cmd = Cmd << 4; //shift left 4 bits
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
LCDcmd(Cmd); // send to LCD
}
void data_4b(unsigned char data_value)
{
char data_value1;
data_value1 = data_value & 0xF0;
LCDdata(data_value1);
data_value1 = data_value << 4;
LCDdata(data_value1);
}
void LCDcmd(unsigned char cmdout)
{
PORTA = cmdout;
BitClr(PORTA, Bit(0)); //Clear RS
BitClr(PORTA, Bit(1)); //Clear RW
BitSet(PORTA, Bit(2)); //Set Enable
_delay_us(200);
BitClr(PORTA, Bit(2)); //Clear Enable
}
void LCDdata(unsigned char dataout)
{
PORTA = dataout;
BitSet(PORTA, Bit(0)); //Set RS
BitClr(PORTA, Bit(1)); //Clear RW
BitSet(PORTA, Bit(2)); //Set Enable
_delay_us(200);
BitClr(PORTA, Bit(2)); //Clear Enable
}
LAYOUT
C Code to Display a Character Type Array on LCD: [8- bit MODE]
// Program to interface LCD in 8 bit mode with AVR microcontroller
#define F_CPU 800000UL
#include<avr/io.h>
#include<util/delay.h>
//Macros Definition
#define BitSet(p,m) ((p) |= (m))
#define BitClr(p, m) ((p) &= ~(m))
#define BitFlip(p,m) ((p) ^= (m))
#define Bit(x) (0x01 << (x))
void LCD_init();
void LCDcmd(unsigned char);
void LCDdata(unsigned char);
unsigned int main(void)
{
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
unsigned char data0[]=" GEC,EC DEPT.";
unsigned char data1[]="GANDHINAGAR";
unsigned int i=0;
DDRA=0xFF;
DDRB=0XFF;
LCD_init(); //Initialize LCD
LCDcmd(0x80); //Cursor at Line 1, Position 1
while(data0[i]!='\0') //continue loop until null is arrived
{
LCDdata(data0[i]); //send all characters for 8-bit data conversion
_delay_ms(500);
i++; //to access next character of array
}
LCDcmd(0xC1); //Cursor at Line 2, Position 4
i=0;
while(data1[i]!='\0') //continue loop until null is arrived
{
LCDdata(data1[i]); //send all characters for data display
_delay_ms(500);
i++; //to access next character of array
}
while(1);
return(0);
}
void LCD_init() // Initialize LCD
{
LCDcmd(0x38); //to initialize LCD in 8-bit mode.
LCDcmd(0x0E); //to initialize LCD in 2 lines, 5x7 dots and 4bit mode
LCDcmd(0x02); //Display is ON, cursor OFF
}
void LCDcmd(unsigned char cmdout)
{
PORTA = cmdout;
BitClr(PORTB, Bit(0)); //Clear RS
BitClr(PORTB, Bit(1)); //Clear RW
BitSet(PORTB, Bit(2)); //Set Enable
_delay_us(200);
BitClr(PORTB, Bit(2)); //Clear Enable
_delay_us(200);
}
void LCDdata(unsigned char dataout)
{
PORTA = dataout;
BitSet(PORTB, Bit(0)); //Set RS
BitClr(PORTB, Bit(1)); //Clear RW
BitSet(PORTB, Bit(2)); //Set Enable
_delay_us(200);
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
BitClr(PORTB, Bit(2)); //Clear Enable
_delay_us(200);
}
LAYOUT
LCD1
LM016L
U1
VDD
VSS
VEE
9 22
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
VCC RESET PC0/SCL
E
23
PC1/SDA
13 24
XTAL1 PC2/TCK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
16×2 LCD can be interfaced with a microcontroller in 8 Bit or 4 Bit mode. These differ in how data
and commands are send to LCD. In 8 Bit mode character data (as 8 bit ASCII) and LCD command are
sent through the data lines D0 to D7. That is 8 bit data is send at a time and data strobe is given
through E of the LCD. But 4 Bit mode uses only 4 data lines D4 to D7. In this 8 bit data is divided
into two parts and are sent sequentially through the data lines. The idea of 4 bit communication is
introduced to save pins of microcontroller. 4 bit communication is bit slower than 8 bit but this
speed difference has no significance as LCDs are slow speed.
Exercise: LOAD ABOVE PROGRAM IN EASY AVR 7 AND VERIFY THE OUTPUT ON LCD.
HOMEWORK
1. Implement Proteus Project and Write an Assembly Language Program to Interface 16x2
LCD with ATmega32 using 4 Bit Mode.
Code:
.INCLUDE"8515DEF.INC"
.ORG 0X0000
LDI R16,LOW(RAMEND)
OUT SPL,R16
LDI R16,HIGH(RAMEND)
OUT SPH,R16
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
LDI R16,0XFF
OUT DDRB,R16
LDI R17,0X2C
RCALL SEND_COMMAND
LDI R17,0X0C
RCALL SEND_COMMAND
LDI R17,0X80
RCALL SEND_COMMAND
LDI R17,0X01
RCALL SEND_COMMAND
LDI R17,'H'
RCALL SEND_DATA
LDI R17,'E'
RCALL SEND_DATA
LDI R17,'L'
RCALL SEND_DATA
LDI R17,'L'
RCALL SEND_DATA
LDI R17,'0'
RCALL SEND_DATA
LDI R17,0XC0 ;START AT THE
RCALL SEND_COMMAND ; SECOND ROW
LDI R17,'W'
RCALL SEND_DATA
LDI R17,'O'
RCALL SEND_DATA
LDI R17,'R'
RCALL SEND_DATA
LDI R17,'L'
RCALL SEND_DATA
LDI R17,'D'
RCALL SEND_DATA
AGAIN: RJMP AGAIN
SEND_COMMAND:
RCALL LCD_READY
MOV R1,R17
ANDI R17,0XF0
OUT PORTB,R17
CBI PORTB,0
SBI PORTB,1
CBI PORTB,1
MOV R17,R1
SWAP R17
ANDI R17,0XF0
OUT PORTB,R17
CBI PORTB,0
SBI PORTB,1
CBI PORTB,1
RET
SEND_DATA:
RCALL LCD_READY
MOV R1,R17
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
ANDI R17,0XF0
OUT PORTB,R17
SBI PORTB,0
SBI PORTB,1
CBI PORTB,1
MOV R17,R1
SWAP R17
ANDI R17,0XF0;
OUT PORTB,R17
SBI PORTB,0
SBI PORTB,1
CBI PORTB,1
RET
LCD_READY:
LDI R30,0XFF
LDI R31,0X0F
DECO1:SBIW ZH:ZL,0X01
BRNE DECO1
RET
2. Implement Proteus Project and Write an Assembly Language Program to Interface 16x2 LCD
with ATmega32 using 8 Bit Mode.
Code :
INCLUDE"M32DEF.INC"
.EQU LCD RS=0
.EQU LCD RW-1
.EQU LCD_E=2
LDI R16,HIGH (RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
LDI R16, 0xFF
OUT DDRD, R16
OUT DDRB, R20
CBI PORTB LCD E
CALL DELAY
LDI R16, 0x38
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
CALL COMMAND
CALL DELAY
LDI R16, 0x0E
CALL COMMAND
LDI R16, 0x01
CALL COMMAND
CALL DELAY
LDI R16,0x06
CALL COMMAND
CALL DELAY
LDI R16, 0xC0
CALL COMMAND
LDI R16,'H'
CALL DATA
CALL DELAY
LDI R16,'E'
CALL DATA
CALL DELAY
LDI R16,'L'
CALL DATA
CALL DELAY
LDI R16,'L'
CALL DATA
CALL DELAY
LDI R16, 'O'
CALL DATA
CALL DELAY
HERE : JMP HERE
COMMAND : OUT PORTD, R16
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
CBI PORTB
LCD RS
CBI PORTB,
LCD RW
SBI PORTB, LCD E
CALL DELAY_1
CBI PORTB, LCD E
CALL DELAY 1
RET
DATA : OUT PORTD, R16
SBI PORTB.
LCD RS
CBI PORTB, LCD RW
SBI PORTB, LCD E
CALL DELAY_1
CBI PORTB, LCD E
CALL DELAY 1
RET
DELAY : PUSH R15
LDI R15, 20
wait : CALL DELAY_1
DEC R15
BRNE wait
POP R15
RET
DELAY_1 : PUSH R15
LDI R15,60
agn : DEC R15
BRNE agn
Microprocessor and Microcontroller Subject Code: 3141008
GOVERNMENT ENGINEERING COLLEGE, GANDHINAGAR
DEPT. OF ELECTRONICS AND COMMUNICATION ENGG .
POP R15
RET
Conclusion:
By this practical we learn that how to interface LCD with atmega32 microcontroller with
assembly language programming.
Microprocessor and Microcontroller Subject Code: 3141008