Embedded
Systems
Lecture (5)
LCD
LCD: Liquid-crystal display
➢ LCDs are alphanumeric (or graphic) displays which are frequently used in
microcontroller-based applications.
➢ These display devices come in different shapes and sizes.
➢ Some LCDs have 40 or more character lengths with the capability to display
several lines.
➢ Others can be programmed to display graphic images.
Types of LCD
➢ There are two types of LCDs based
the interfacing technique:
1. Parallel LCDs:
▪ Data is transferred from the
microcontroller to the LCD
using more than one line,
usually four or eight data
lines.
Types of LCD
2. Serial LCDs:
▪ Connected to a microcontroller using one data line only.
▪ Data is transferred using the RS232 asynchronous data
communications protocol.
HD44780 LCD
➢ The HD44780 is one of the most popular LCD controllers.
➢ Parallel LCD.
➢ The module is monochrome and comes in different shapes and sizes.
➢ Modules with 8, 16, 20, 24, 32, and 40 characters are available.
➢ Depending on the model, the display provides a 14-pin or 16-pin connector
for interfacing.
HD44780 LCD
HD44780 LCD
➢ Pin 1 (VSS) is the ground.
➢ Pin 2 (VDD) should be connected to the positive supply.
➢ Pin 3 (VEE) is the contrast control pin. It is used to adjust the contrast of
the display and should be connected to a DC supply using a potentiometer to
adjust the contrast of the display.
➢ Pin 4 (RS) is the register select:
▪ When RS is LOW, data transferred to the LCD is treated as commands.
▪ When RS is HIGH, character data can be transferred to and from LCD.
HD44780 LCD
➢ Pin 5 (R/W) is the read/write pin:
▪ When (R/W) pin is LOW, commands or character data are written to the
LCD module.
▪ When (R/W) pin is HIGH, character data or status information can be
read from the module.
➢ Pin 6 (EN) is the enable pin, which is used to initiate the transfer of
commands or data between the module and the microcontroller.
HD44780 LCD
➢ Pins 7 to 14 (D0 to D7) are the eight data bus lines:
▪ Data can be transferred between the microcontroller and the LCD
module using either a single 8-bit byte or two 4-bit modes.
▪ For 4-bit mode, only the upper four data lines (D4 to D7) are used.
▪ The 4-bit mode has the advantage of requiring fewer I/O lines to
communicate with the LCD.
Connecting
PIC to LCD
LCD Connection
LCD Microcontroller Port Pin
RS 2
EN 3
D4 4
D5 5
D6 6
D7 7
LCD Programming
➢ Initialization (before main):
▪ sbit LCD_RS at RD2_bit; ▪ sbit LCD_RS_Direction at TRISD2_bit;
▪ sbit LCD_EN at RD3_bit; ▪ sbit LCD_EN_Direction at TRISD3_bit;
▪ sbit LCD_D4 at RD4_bit; ▪ sbit LCD_D4_Direction at TRISD4_bit;
▪ sbit LCD_D5 at RD5_bit; ▪ sbit LCD_D5_Direction at TRISD5_bit;
▪ sbit LCD_D6 at RD6_bit; ▪ sbit LCD_D6_Direction at TRISD6_bit;
▪ sbit LCD_D7 at RD7_bit; ▪ sbit LCD_D7_Direction at TRISD7_bit;
LCD Programming
➢ Functions (inside main):
1. Lcd_Init();
▪ Used to configure the interface between the microcontroller and the
LCD when the default connections are made.
2. Lcd_Out(line, column, “text”);
▪ Used to display text at the specified line and column position of the
LCD.
▪ To display text “Computer” at line 1 and column 2 of the LCD:
Lcd_Out(1, 2, “Computer”);
LCD Programming
3. Lcd_Out_Cp(“text”);
▪ Used to display text at the current cursor position.
▪ To display text “Computer” at the current cursor position:
Lcd_Out_Cp(“Computer”);
4. Lcd_Chr(line, column, ‘text’);
▪ Used to display a character at the specified line and column position
of the cursor.
▪ To display character ‘K’ at line 2 and column 4 of the LCD:
LCD_Chr(2, 4, ‘K’);
LCD Programming
5. Lcd_Chr_Cp(‘text’);
▪ Used to display a character at the current cursor position.
▪ To display character ‘M’ at the current cursor position:
Lcd_Chr_Cp(‘M’);
6. Lcd_Cmd(command);
▪ Used to send a command to the LCD from the list of commands.
LCD Command Description
_LCD_CLEAR Clear display
_LCD_RETURN_HOME Return cursor to home position
_LCD_FIRST_ROW Move cursor to first row
LCD _LCD_SECOND_ROW Move cursor to second row
Commands _LCD_BLINK_CURSOR_ON Blink cursor
_LCD_MOVE_CURSOR_LEFT Move cursor one digit left
_LCD_MOVE_CURSOR_RIGHT Move cursor one digit right
_LCD_SHIFT_LEFT Shift text one digit left
_LCD_SHIFT_RIGHT Shift text one digit right
Example (1)
➢ Write a program to print “Hello” on LCD screen, then move the text to right
and left ?
➢ Solution (1):
▪ Using shift right and left:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
H E L L O H E L L O
1 2 3 4 5 6 7 8 9 10 11
Example (1)
Example (1)
➢ Solution (2):
▪ Using LCD_Out:
▪ Write and clear data from position 1 to position 12 then from 12 to 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
H E L L O H E L L O
Example (1)
Example (1)
Converting Numbers to String
LCD_Out(1, 1, 200);
➢ Prints character with ASCII = 200
➢ To display the number 200 on LCD, convert it to string:
LCD_Out(1, 1, “200”);
➢ For example, to write the temp value of 150 on LCD:
LCD_Out(1, 1, “150”);
150
String
➢ Special Functions:
1. InttoStr(integer-value, string-value);
▪ Used to convert int to string.
▪ Example:
unsigned int temp = 150;
char display[7];
IntToStr(temp, display);
Lcd_Out(1, 1, display);
String
▪ IntToStr is a MikroC built-in function that converts an integer to a
null-terminated string.
▪ It requires the destination string to be at least 7 characters long,
because:
❖ It can convert values up to 65535 (5 digits max),
❖ Plus one character for the optional sign (+ or -),
❖ And one character for the null terminator \0.
▪ IntToStr outputs a right-aligned string with leading spaces.
▪ To remove the spaces, use Ltrim(display);
String
2. FloattoStr(float-value, string-value);
▪ Used to convert float to string.
▪ Example:
float temp = 35.6;
char display[15];
FloattoStr(temp, display);
Lcd_Out(1, 1, display);
String
▪ FloatToStr is a MikroC built-in function that converts a float value
to a null-terminated ASCII string.
▪ It requires the destination string to be at least 16 bytes long.
▪ By default, it gives 5 digits after the decimal.
▪ Leading spaces are added for right-alignment.
▪ To remove extra spaces, use:
❖ Ltrim(display); // Removes leading spaces
❖ Rtrim(display); // Removes trailing spaces (e.g., zeros or
whitespace).
Example (2)
➢ Write a program to display numbers
from 0 to 100 on LCD screen ?
➢ Solution:
Example (2)
Keypad
Keypad
Keypad
Keypad
➢ Why using keypad ?
▪ One port pin is required to read a digital input into the controller.
▪ When there are a lot of digital inputs that have to be read, it is not feasible
to allocate one pin for each of them.
▪ So, The keypad is used to reduce the number of port pins required to read
a certain number of digital inputs.
Keypad Connection
➢ Rows are connected to the
microcontroller as inputs.
Connect 10 KΩ parallel resistors
to the ground.
➢ Columns are connected to the
microcontroller using 1 KΩ
resistors as outputs.
How to use Keypad
➢ A logic 1 is applied to the first column.
➢ All rows are read. If the data is nonzero, a
switch is pressed.
➢ A logic 1 is applied to the second column.
➢ All rows are read. If the data is nonzero, a
switch is pressed.
➢ This process is repeated for all columns
continuously.
Example (3)
➢ Write a program to display
the number from 4x3 keypad
matrix on LCD screen ?
➢ Solution:
Example (3)
Example (4)
➢ Write a program
to display the
number from
4x4 keypad
matrix on LCD
screen ?
➢ Solution:
Example (4)
Thank You