HOW TO WRITE YOUR OWN CODE FOR AN LCD DISPLAY (16X2 OR 16X4)
The Arduino ide default comes with an excellent library with examples for LCD display it’s really easy! One to use.
Mine
is not best one but the reason behind this post is to experience the feel of
writing your own code and understanding the embedded displays and their working
technology and how they works with u-controllers, maybe in future it can helps
you to write a code for lcd display with other new microcontrollers or other
programming languages.
So
let’s start with the one important things:
The datasheet is your friend in this quest
for any quest, for real! So getting the data sheet is important here I’m using
a 16x2 LCD. This kind LCD is based on parallel interface LCD controller chip from
Hitachi called the HD44780.
Next
is physical pinout the display uses a matrix of dots and
displays only characters in 16x2 matrixes. Each character
is displayed in a 5 column × 8 row dot matrix or a 5 column × 10 row dot matrix
The pinout is LCD has
totally 16 pins they are:
1. GND: used for GND purpose the –ve connection.
2. VCC: used for VCC purpose the +Ve
connection.
3. VSS: used
for the potentiometer output voltage (0-5V), this voltage is used to control
the contrast of LCD.
4. RS: “REGISTER CONTROL” used
for to control the register to whether send data or send commands to LCD
display. The HD44780 has two registers: an
Instruction Register (IR) and a Data Register (DR).
If
RS_PIN is written as HIGH then it in data mode and we can write
data/characters, if it is written LOW then it is in command mode.
5. R/W_PIN: used
for whether to read from or write to LCD Register. If you want to read from lcd
register set it HIGH, since we only want it write so Pull it to GND hence LOW
sets in write mode.
6. Enable: used
to enable the LCD, when it is set to LOW LCD doesn’t care if you are sending
data or commands, so usually we set it to HIGH for LCD to work.
7. D0-D7_PINS: used
to write data to LCD, the D0 to D7 pins are 8-BIT data transfer lines hence we
can either use whole 8 pins for data or just use 4 pins D4-D8 to send data in
nibble (4-bits) at a time.
LCDs
are designed in a way that we can talk to the LCD using only 4 data pins(4-bit
mode) instead of 8(8-bit mode). This saves us 4 pins!
8. A_PIN: used as ANODE +ve connection
for backlight
9. K_PIN: used
as KATHODE –ve connection for backlight
If we
insert a potentiometer between A and K we control the Backlit brightness.
Here are the commands for lcd display
16x2:
Sr.No. |
Hex Code |
Command to LCD instruction Register |
1 |
01 |
Clear display screen |
2 |
02 |
Return home |
3 |
04 |
Decrement cursor (shift cursor to left) |
4 |
06 |
Increment cursor (shift cursor to right) |
5 |
05 |
Shift display right |
6 |
07 |
Shift display left |
7 |
08 |
Display off, cursor off |
8 |
0A |
Display off, cursor on |
9 |
0C |
Display on, cursor off |
10 |
0E |
Display on, cursor blinking |
11 |
0F |
Display on, cursor blinking |
12 |
10 |
Shift cursor position to left |
13 |
14 |
Shift cursor position to right |
14 |
18 |
Shift the entire display to the left |
15 |
1C |
Shift the entire display to the right |
16 |
80 |
Force cursor to beginning ( 1st line) |
17 |
C0 |
Force cursor to beginning ( 2nd line) |
18 |
38 |
2 lines and 5×7 matrix |
That being said lets write our own code:
#define
lcd_dpins PORTD
/*
const
unsigned int d0 = 1;
const
unsigned int d4 = 2;
const
unsigned int d4 = 3;
const
unsigned int d4 = 4;
const
unsigned int d4 = 5;
const
unsigned int d5 = 6;
const
unsigned int d6 = 7;
const
unsigned int d7 = 8;
*/
const
unsigned int RS = 9;
const
unsigned int RW = 10;
const
unsigned int EN = 11;
void
lcd_cmd(char cmd)
{
digitalWrite(RS,LOW);
digitalWrite(RW,LOW);
digitalWrite(EN,HIGH);
delay(2);
digitalWrite(RS,LOW);
delay(5);
}
void
lcd_on()
{
delay(20);
/* LCD Power ON Initialization time >15ms */
lcd_cmd(0x38); /* Initialization of 16X2 LCD
in 8bit mode */
lcd_cmd(0x0C); /* Display ON Cursor OFF */
lcd_cmd(0x06); /* Auto Increment cursor */
lcd_cmd(0x01); /* clear display */
lcd_cmd(0x80); /* cursor at home position */
}
void
lcd_char(unsigned char char_data)
{
lcd_dpins = char_data;
digitalWrite(RS,HIGH);
digitalWrite(RW,LOW);
digitalWrite(EN,HIGH);
delay(2);
digitalWrite(RS,LOW);
delay(5);
}
void lcd_string(unsigned char *str)
{
int i=0;
for(i=0;str[i]!=0;i++)
{
lcd_char(str[i]);
}
}
void lcd_pos(char x, char y ,char *str)
{
if (x == 0)
lcd_cmd((y & 0x0F)|0x80);
else if (x == 1)
lcd_cmd((y & 0x0F)|0xC0);
lcd_string(str); /* Call LCD string function */
}
void
setup()
{
DDRD = B11111111;
DDRB = B11001110;
}
void
loop()
{
lcd_on();
lcd_string("Hi");
lcd_cmd(0xc0);
lcd_string("hello world");
}
Comments
Post a Comment