Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Embedded System on AVR Microcontroller

(ATMEGA32)

Exp5: Display Text and Custom Character on LCD 16x2

Submitted by
Ronit Dutta, MS in IOT and Signal Processing
Department of Electrical Engineering, IIT Kharagpur

Under the guidance of


Aurobinda Routray, Professor, Electrical Engineering

Department of Electrical Engineering


Indian Institute of Technology Kharagpur
March, 2024
Embedded System on AVR ATMEGA32:Exp5

 Introduction to Alphanumeric 16x2 LCD


Liquid Crystal Display (LCD) is widely used in various electronics applications. It is
commonly used in various systems to show different status and parameters. LCD16x2
has 2 lines with 16 characters in each line. Each character is made up of a 5x8 (column
x row) pixel matrix but each character is displayed in a 5×7 pixel matrix.

LCD is finding widespread use replacing LEDs


o The declining prices of LCD
o The ability to display numbers, characters and graphics
o Comes with built in controller to refresh LCD
o Provides LED backlight and contrast control
LCD comes in majority two flavours
i. Alphanumeric LCD: Capable of displaying alphanumeric characters only.
Available options are 16x1, 16x2, 20x2, 40x2 etc.
ii. Graphical LCD: Capable of displaying all types of graphical characters. Available
options are 122x32, 128x64, 240x64, 240x128 etc.
 LCD 16x2 Pin Description

Pin
Function Name
No.

Ground (0V)
1 Ground

Supply voltage; 5V (4.7V – 5.3V) Vcc


2

2
Embedded System on AVR ATMEGA32:Exp5

Contrast adjustment; the best way is to use a variable


resistor such as a potentiometer. The output of the
potentiometer is connected to this pin. Rotate the
Vo / VEE
3 potentiometer knob forward and backward to adjust the LCD
contrast. When Vo is grounded then LCD contrast will be
maximum.

Selects command register when low, and data register when RS (Register
4
high. Select )

5 Low to write to the register; High to read from the register Read/write

Sends data to data pins when a high to low pulse is given;


Extra voltage push is required to execute the instruction and
EN(enable) signal is used for this purpose. Usually, we set
6 Enable
en=0, when we want to execute the instruction we make it
high en=1 for some times. After this we again make it
ground that is, en=0.

7 D0 (LSB)

8 D1

9 D2

10 D3
8-bit data pins
11 D4

12 D5

13 D6

14 D7 (MSB)

15 LED Backlight VCC (5V) A or Led+

16 LED Backlight Ground (0V) K or Led-


Note: Connect one current controlling resistance of Backlight LED externally.

 Register Select (RS)


A 16X2 LCD has two registers, namely, command and data. The register select is used
to switch from one register to other. RS=0 for the command register, whereas RS=1
for the data register.

3
Embedded System on AVR ATMEGA32:Exp5

Command Register: The command register stores the command instructions given to
the LCD. A command is an instruction given to an LCD to do a predefined task.
Examples: Initializing 8bit mode or 4bit mode
Clear screen
Setting the cursor position etc.
Data Register: The data register stores the data to be displayed on the LCD. The data
is the ASCII value of the character to be displayed on the LCD. When we send data to
LCD, it goes to the data register and is processed there. When RS=1, the data register
is selected.

 Important Command Codes for 16×2 LCD


Code Execution
Command to LCD
(HEX) Time

0x01 Clear the display screen 1.64ms

0x02 Return home 40us

0x04 Decrement cursor (shift cursor to left) 40us

0x05 Shift display right 40us

0x07 Shift display left 40us

Shift the cursor right (e.g. data gets written in an incrementing


0x06 40us
order, left to right)

0x08 Display off, cursor off 40us

0x0A Display off, cursor on 40us

0x0C Display on, cursor off 40us

0x0E Display on, cursor blinking 40us

0x0F Display on, cursor blinking 40us

0x80 Address of the beginning of the 1st line 40us

0xC0 Address of the beginning of the 2nd line 40us

0x10 Shift cursor position to the left 40us

0x14 Shift cursor position to the right 40us

0x18 Shift entire display to the left 40us

0x1C Shift entire display to the right 40us

0x38 2 lines, 5x7 matrix, 8-bit mode 40us

4
Embedded System on AVR ATMEGA32:Exp5

0x28 2 lines, 5x7 matrix,4-bit mode 40us

0x30 1 line, 8-bit mode 40us

0x20 1 line, 4-bit mode 40us

 Command Address of the Line Coordinate for 16×2 LCD


Each line can hold 40 characters. Visible characters on the screen are 16 only among
them. Visible addresses of the 1st line are 0x80 to 0x8F and remaining 0x90 – 0xA7 are
invisible addresses. Visible addresses of the 2nd line are 0xC0 to 0xCF and remaining
0xD0 – 0xE7 are invisible addresses.

 Display characters on LCD in 8bit Mode


//Text print on LCD 16x2 in 8bit Mode
.INCLUDE "M32DEF.INC"
.ORG 0x0000

LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16

// Data Direction Register Settings


LDI R16,0xFF
OUT DDRB,R16

SBI DDRD,PIND0 //Falling Edged Enable


SBI DDRD,PIND1 //Register Select

// LCD Initialization
CBI PORTD,PIND1 // Command Register Enable
LDI R16,0x38 //2 lines and 5x7 matrix
OUT PORTB,R16
CALL ENABLE
LDI R16,0x02 // Return Home
OUT PORTB,R16
CALL ENABLE
LDI R16,0x01 // Clear display screen
OUT PORTB,R16
CALL ENABLE
LDI R16,0x0C //Display on, cursor off
OUT PORTB,R16
CALL ENABLE

5
Embedded System on AVR ATMEGA32:Exp5

//Set Cursor Coordinate


LDI R16,0x85 //Set Cursor at begining of 1st Line
OUT PORTB,R16
CALL ENABLE

// Writing Text on it
SBI PORTD,PIND1 // Data Register Enable
LDI R16,0x49 // Ascii of I
OUT PORTB,R16
CALL ENABLE

CBI PORTD,PIND1 // Command Register Enable


LDI R16,0x06 // Shift Cursor to right
OUT PORTB,R16
CALL ENABLE

SBI PORTD,PIND1 // Data Register Enable


LDI R16,0x49 // Ascii of I
OUT PORTB,R16
CALL ENABLE

CBI PORTD,PIND1 // Command Register Enable


LDI R16,0x06 // Shift Cursor to right
OUT PORTB,R16
CALL ENABLE

SBI PORTD,PIND1 // Data Register Enable


LDI R16,'T' // Ascii of T
OUT PORTB,R16
CALL ENABLE

LOOP_INF: NOP
JMP LOOP_INF

ENABLE : SBI PORTD,PIND0


LDI R18,0xFF
LOOP2: LDI R17,0xFF
LOOP1: NOP
DEC R17
BRNE LOOP1
DEC R18
BRNE LOOP2
CBI PORTD,PIND0
RET

Make the above circuit to simulate the above code.

6
Embedded System on AVR ATMEGA32:Exp5

 Display characters with SRAM calling of ATMEGA32 on


LCD in 8bit Mode
// LCD text display with SRAM calling

.INCLUDE "M32DEF.INC"
.ORG 0x0000

LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16

//Data Loading to SRAM


LDI R26,0x80
LDI R27,0x00

LDI R16,'I'
ST X+,R16
LDI R16,'I'
ST X+,R16
LDI R16,'T'
ST X+,R16
LDI R16,0
ST X+,R16
LDI R16,'K'
ST X+,R16
LDI R16,'H'
ST X+,R16
LDI R16,'A'
ST X+,R16
LDI R16,'R'
ST X+,R16
LDI R16,'A'
ST X+,R16
LDI R16,'G'
ST X+,R16
LDI R16,'P'
ST X+,R16
LDI R16,'U'
ST X+,R16
LDI R16,'R'
ST X+,R16
LDI R16,0
ST X,R16

// Data Direction Register Settings


LDI R16,0xFF
OUT DDRB,R16

SBI DDRD,PIND0 //Falling Edged Enable


SBI DDRD,PIND1 //Register Select

// LCD Initialization
CBI PORTD,PIND1 // Command Register Enable
LDI R16,0x38 //2 lines and 5x7 matrix
OUT PORTB,R16
CALL ENABLE
LDI R16,0x02 // Return Home
OUT PORTB,R16

7
Embedded System on AVR ATMEGA32:Exp5

CALL ENABLE
LDI R16,0x01 // Clear display screen
OUT PORTB,R16
CALL ENABLE
LDI R16,0x0C //Display on, cursor off
OUT PORTB,R16
CALL ENABLE
LDI R16,0x06 // Shift Cursor to right automatically after print on LCD
OUT PORTB,R16
CALL ENABLE

//Set Cursor Coordinate


LDI R16,0x80 //Set Cursor at begining of 1st Line
OUT PORTB,R16
CALL ENABLE

//Data Read and print on LCD


LDI R26,0x80
LCD_PRINT1: SBI PORTD,PIND1 // Data Register Enable
LD R16,X+
OUT PORTB,R16
CALL ENABLE

LD R16,X
CPI R16,0
BRNE LCD_PRINT1

//2nd Line set cursor coordinate


CBI PORTD,PIND1
LDI R16,0xC0 //Set Cursor at begining of 2nd Line
OUT PORTB,R16
CALL ENABLE

//Data Read and print on LCD


INC R26 // To Skip previous null character
LCD_PRINT2: SBI PORTD,PIND1 // Data Register Enable
LD R16,X+
OUT PORTB,R16
CALL ENABLE

LD R16,X
CPI R16,0
BRNE LCD_PRINT2
LOOP_INF: NOP
JMP LOOP_INF

ENABLE : SBI PORTD,PIND0


LDI R18,0x50
LOOP2: LDI R17,0xFF
LOOP1: NOP
DEC R17
BRNE LOOP1
DEC R18
BRNE LOOP2
CBI PORTD,PIND0
RET

8
Embedded System on AVR ATMEGA32:Exp5

Make the below circuit to simulate the above code.

Class Assignment 1: Write Assembly Code for LCD 16x2 in 8bit Mode to display your name
with call SRAM and hold your name on the screen 5sec approximately then clear the screen
to display your department & Roll No. on 1st line, 2nd line respectively from SRAM. Hold
these also 5sec approximately then repeat again your name for looping. The Circuit is same
as above to simulate and make the hardware.

Class Assignment 2: Write Assembly Code for LCD 16x2 in 8bit Mode to display “AVR
ATMEGA32“ at 2nd line on 16x2 LCD form SRAM. Scroll entire display right with an interrupt
and to scroll entire display left with another interrupt. The Circuit is same as below to
simulate and make the hardware.

9
Embedded System on AVR ATMEGA32:Exp5

 LCD in 4bit Mode


To configure LCD 16x2 in a 4-bit mode, we should send 2 at higher nibble (D4 - D7) to
LCD 16x2. As only four (D4 - D7) data lines need to be connected to the
microcontroller. Make the below circuit to simulate the below code.

// LCD 4-bit Mode

.INCLUDE "M32DEF.INC"
.ORG 0x0000

LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16

// Data Direction Register Settings


LDI R16,0xF0
OUT DDRB,R16

SBI DDRD,PIND0 //Falling Edged Enable


SBI DDRD,PIND1 //Register Select

//Data Loading to SRAM


LDI R26,0x80
LDI R27,0x00

LDI R16,'E'
ST X+,R16
LDI R16,'m'
ST X+,R16
LDI R16,'b'
ST X+,R16
LDI R16,'e'
ST X+,R16
LDI R16,'d'
ST X+,R16
LDI R16,'d'
ST X+,R16

10
Embedded System on AVR ATMEGA32:Exp5

LDI R16,'e'
ST X+,R16
LDI R16,'d'
ST X+,R16
LDI R16,' '
ST X+,R16
LDI R16,'S'
ST X+,R16
LDI R16,'y'
ST X+,R16
LDI R16,'s'
ST X+,R16
LDI R16,'t'
ST X+,R16
LDI R16,'e'
ST X+,R16
LDI R16,'m'
ST X+,R16
LDI R16,'s'
ST X+,R16
LDI R16,0
ST X,R16

// LCD 4-bit mode initialization


CBI PORTD,PIND1 // Command Register Enable
LDI R16,0x20 // 4-bit Mode
OUT PORTB,R16
CALL ENABLE
LDI R16,0x28 // 4-bit mode for a 2 line module with 5 x 7 dots per character
CALL FourBit_Mode
LDI R16,0x01 // Clear display screen
CALL FourBit_Mode
LDI R16,0x0C //Display on, cursor off
CALL FourBit_Mode
LDI R16,0x06 // Shift Cursor to right automatically after print on LCD
CALL FourBit_Mode

//Set Cursor Coordinate


LDI R16,0x80 //Set Cursor at begining of 1st Line
CALL FourBit_Mode

//Data Read and print on LCD


LDI R26,0x80
LCD_PRINT1: SBI PORTD,PIND1 // Data Register Enable
LD R16,X+
CALL FourBit_Mode

LD R16,X
CPI R16,0
BRNE LCD_PRINT1

LOOP_INF: NOP
JMP LOOP_INF

ENABLE : SBI PORTD,PIND0


LDI R18,0x20
LOOP2: LDI R17,0xFF
LOOP1: NOP
DEC R17
BRNE LOOP1
DEC R18
BRNE LOOP2

11
Embedded System on AVR ATMEGA32:Exp5

CBI PORTD,PIND0
RET
FourBit_Mode: OUT PORTB,R16
CALL ENABLE
SWAP R16
OUT PORTB,R16
CALL ENABLE
RET

 LCD 16x2 Custom Character


LCD16x2 can display user defined custom characters. This can be used for displaying
symbols and different patterns.

As we know, to print character symbol on LCD, we have to send ASCII code. LCD16x2
also has feature, where we can print our own custom character, which is not included
in ASCII set of character.

CGRAM: LCD16x2 has memory space of 64 bytes called as CGRAM (character


generator RAM). It is used to make custom patterns. We just have to write custom
pixel values in this memory space.

How to Build custom character for LCD16x2


On LCD16x2 one character is made up by 5x8 pixel matrix. Consider that we have to
design Bell shape custom character. As in diagram, we have to define pixel values, 0
for pixel OFF and 1 for Pixel ON.

12
Embedded System on AVR ATMEGA32:Exp5

Online Custom Character LCD16x2 Web Link: https://omerk.github.io/lcdchargen/

Storing Custom Character: Custom Characters are stored at CGRAM’s memory


location. CGRAM is 64 bytes. Custom character address starts from 0x40 and ends at
0x7F. So we can define total 8 custom characters.

Character No. CGRAM Address


0x00 0x40 - 0x47
0x01 0x48 - 0x4F
0x02 0x50 - 0x57
0x03 0x58 - 0x5F
0x04 0x60 - 0x67
0x05 0x68 - 0x6F
0x06 0x70 - 0x77
0x07 0x78 - 0x7F

// Custom Shape on LCD in 8-bit Mode


.INCLUDE "M32DEF.INC"
.ORG 0x0000

LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16

// Data Direction Register Settings


LDI R16,0xFF
OUT DDRB,R16

SBI DDRD,PIND0 //Falling Edged Enable


SBI DDRD,PIND1 //Register Select

// LCD Initialization
CBI PORTD,PIND1 // Command Register Enable
LDI R16,0x38 //2 lines and 5x7 matrix
OUT PORTB,R16
CALL ENABLE
LDI R16,0x02 // Return Home
OUT PORTB,R16
CALL ENABLE
LDI R16,0x01 // Clear display screen
OUT PORTB,R16
CALL ENABLE
LDI R16,0x0C //Display on, cursor off
OUT PORTB,R16
CALL ENABLE

//Data for custom shape and store to CGRAM of LCD


LDI R16,0x40 //CGRAM Address Select
OUT PORTB,R16
CALL ENABLE

SBI PORTD,PIND1

13
Embedded System on AVR ATMEGA32:Exp5

LDI R16,0x01 //Data of Custom Image


OUT PORTB,R16
CALL ENABLE
LDI R16,0x02
OUT PORTB,R16
CALL ENABLE
LDI R16,0x06
OUT PORTB,R16
CALL ENABLE
LDI R16,0x0C
OUT PORTB,R16
CALL ENABLE
LDI R16,0x1F
OUT PORTB,R16
CALL ENABLE
LDI R16,0x06
OUT PORTB,R16
CALL ENABLE
LDI R16,0x04
OUT PORTB,R16
CALL ENABLE
LDI R16,0x08
OUT PORTB,R16
CALL ENABLE

//Set Cursor Coordinate


CBI PORTD,PIND1
LDI R16,0x82 //Set Cursor
OUT PORTB,R16
CALL ENABLE

//Print the custom Image on LCD


SBI PORTD,PIND1
LDI R16,0x00
OUT PORTB,R16
CALL ENABLE

LOOP_INF: NOP
JMP LOOP_INF

ENABLE : SBI PORTD,PIND0


LDI R18,0x20
LOOP2: LDI R17,0xFF
LOOP1: NOP
DEC R17
BRNE LOOP1
DEC R18
BRNE LOOP2
CBI PORTD,PIND0
RET

14
Embedded System on AVR ATMEGA32:Exp5

Make the below circuit to simulate the above code.

Class Assignment 3: Write Assembly Code for LCD 16x2 in 4bit Mode to display any custom
character. Make the below circuit to simulate the code.

15

You might also like