100% found this document useful (2 votes)
70 views19 pages

1602 Liquid Crystal Experiment

The document describes an experiment using an Arduino to directly drive a 1602 liquid crystal display (LCD). It provides technical specifications of the 1602 LCD, defines the pin connections, and includes Arduino code to initialize the LCD and display text. The code initializes the LCD, displays a welcome message on the first line and another message on the second line, then replaces part of the text before the program ends.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
70 views19 pages

1602 Liquid Crystal Experiment

The document describes an experiment using an Arduino to directly drive a 1602 liquid crystal display (LCD). It provides technical specifications of the 1602 LCD, defines the pin connections, and includes Arduino code to initialize the LCD and display text. The code initializes the LCD, displays a welcome message on the first line and another message on the second line, then replaces part of the text before the program ends.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

Experiment 21: 1602 liquid crystal experiment

This test uses the Arduino direct drive 1602 liquid crystal display text
1602 liquid crystal in the application is very wide, the initial 1602 liquid crystal is used HD44780
controller, and now the 1602 modules of the various manufacturers are basically using a
compatible with IC, so the characteristics are basically the same.
Main technical parameters of 1602LCD
Display capacity is 16 x 2 characters;
Chip operating voltage is 4.5 ~ 5.5V;
Operating current is 2.0mA (5.0V);
The best operating voltage of the module is 5.0V;
Character size 2.95 x 4.35 (W * H) mm.

1602 LCD interface pin definition

interface specification :
1, a group of two groups of power supply is a module of the power supply is a group of backlight
power supply is generally used 5V power supply. This test backlight using 3.3V power supply can
work.
2、VL is a regulator of the contrast of the pin, series is not greater than 5K potentiometer to adjust.
This experiment uses 1K ohm resistor to set the contrast. Its connection points high potential and
low potential connection, the use of low potential connection, series 1K ohm resistance after
GND.
3、RS is a lot of liquid crystal on the pin is the command / data selection pin the pin level is high
when the data indicated that the data will be carried out.。
4、RW is also a lot of liquid crystal on the pin is the choice of reading and writing the pin level is
high is the representation of the liquid crystal to read operation for the low time that the write
operation.
5、E also a lot of liquid crystal module this pin is usually on the bus signal stability after the signal
to a positive pulse notice to read the data in this pin for the high level when the bus is not allowed
to change.
6、D7 - D0 8 two-way parallel bus, used to transmit commands and data.

7、BLA is the back light source cathode, BLK is the back light source cathode.

1602 the basic operation of the liquid crystal is divided into the following four kinds:
The figure is 1602 LCD physical map
1602 direct communication with Arduino, according to the product manual description, divided
into 8 connection method and 4 connection method, we first use the 8 connection method for the
experiment.

Code as follows

int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//Use an array to define the pins needed by the bus
int Enable = 2;

void LcdCommandWrite(int value) {


// Define all pins
int i = 0;
for (i=DB[0]; i <= DI; i++) //Bus assignment
{
   digitalWrite(i,value & 01);//Because the 1602 liquid crystal signal recognition is D7-D0 (not
D0-D7), here is used to reverse the signal.
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);  // 延时 1ms
digitalWrite(Enable,LOW);
delayMicroseconds(1);  // 延时 1ms
}

void LcdDataWrite(int value) {


// Define all pins
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
   digitalWrite(i,value & 01);
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1);  //Delay 1ms
}

void setup (void) {


int i = 0;
for (i=Enable; i <= DI; i++) {
   pinMode(i,OUTPUT);
}
delay(100);
// After a short pause, LCD
// For LCD control needs
LcdCommandWrite(0x38);  // Set to 8-bit interface, 2 line display, 5x7 text size                
delay(64);                     
LcdCommandWrite(0x38);  // Set to 8-bit interface, 2 line display, 5x7 text size                      
delay(50);                     
LcdCommandWrite(0x38);  //Set to 8-bit interface, 2 line display, 5x7 text size                       
delay(20);                     
LcdCommandWrite(0x06);  // Input mode setting
                         // Auto increment, no display shift
delay(20);                     
LcdCommandWrite(0x0E);  // Display settings
                         // Turn on the display, the cursor shows, no flicker
delay(20);                     
LcdCommandWrite(0x01);  // The screen is empty, the cursor position is zero  
delay(100);                     
LcdCommandWrite(0x80);  // Display settings
                         // Turn on the display, the cursor shows, no flicker
delay(20);                     
}

void loop (void) {


  LcdCommandWrite(0x01);  // The screen is empty, the cursor position is zero
  delay(10);
  LcdCommandWrite(0x80+3);
  delay(10);                     
  // 写入欢迎信息
  LcdDataWrite('W');
  LcdDataWrite('e');
  LcdDataWrite('l');
  LcdDataWrite('c');
  LcdDataWrite('o');
  LcdDataWrite('m');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('t');
  LcdDataWrite('o');
  delay(10);
  LcdCommandWrite(0xc0+1);  // Defines the cursor position as second rows and second positions
  delay(10);
  LcdDataWrite('g');
  LcdDataWrite('e');
  LcdDataWrite('e');
  LcdDataWrite('k');
  LcdDataWrite('-');
  LcdDataWrite('w');
  LcdDataWrite('o');
  LcdDataWrite('r');
  LcdDataWrite('k');
  LcdDataWrite('s');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('p');
  delay(5000);
  LcdCommandWrite(0x01);  // The screen is empty, the cursor position is zero  
  delay(10);
  LcdDataWrite('I');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('m');
  LcdDataWrite(' ');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('n');
  LcdDataWrite('g');
  LcdDataWrite('y');
  LcdDataWrite('i');
  delay(3000);
  LcdCommandWrite(0x02); //Set the pattern for the new text to replace the old text, no new text
display the same place
  delay(10);
  LcdCommandWrite(0x80+5); //Defines the cursor position as the first line of the sixth position
  delay(10);  
  LcdDataWrite('t');
  LcdDataWrite('h');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('d');
  LcdDataWrite('m');
  LcdDataWrite('i');
  LcdDataWrite('n');
  delay(5000);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//Use an array to define the pins needed by the bus
int Enable = 2;

void LcdCommandWrite(int value) {
 // Define all pins
 int i = 0;
 for (i=DB[0]; i <= DI; i++) //Bus assignment
{
   digitalWrite(i,value & 01);//Because the 1602 liquid crystal signal recognition is D7-D0
(not D0-D7), here is used to reverse the signal.
   value >>= 1;
 }
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);
 digitalWrite(Enable,HIGH);
 delayMicroseconds(1);  // delay1ms
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);  // delay 1ms
}

void LcdDataWrite(int value) {
 // Define all pins
 int i = 0;
 digitalWrite(DI, HIGH);
 digitalWrite(RW, LOW);
 for (i=DB[0]; i <= DB[7]; i++) {
   digitalWrite(i,value & 01);
   value >>= 1;
 }
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);
 digitalWrite(Enable,HIGH);
 delayMicroseconds(1);
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);  //delay 1ms
}

void setup (void) {
 int i = 0;
 for (i=Enable; i <= DI; i++) {
   pinMode(i,OUTPUT);
 }
 delay(100);
 // After a short pause, LCD
 // For LCD control needs
 LcdCommandWrite(0x38);  // Set to 8-bit interface, 2 line display, 5x7 text
size                  
 delay(64);                      
 LcdCommandWrite(0x38);  // Set to 8-bit interface, 2 line display, 5x7 text
size                  
 delay(50);                      
 LcdCommandWrite(0x38);  //Set to 8-bit interface, 2 line display, 5x7 text
size                      
 delay(20);                      
 LcdCommandWrite(0x06);  //Input mode setting
                         // Auto increment, no display shift
 delay(20);                      
 LcdCommandWrite(0x0E);  // Display settings
                         // Turn on the display, the cursor shows, no flicker
 delay(20);                      
 LcdCommandWrite(0x01);  //The screen is empty, the cursor position is zero 
 delay(100);                      
 LcdCommandWrite(0x80);  //Display settings
                         // Turn on the display, the cursor shows, no flicker
 delay(20);                      
}

void loop (void) {
  LcdCommandWrite(0x01);  // The screen is empty, the cursor position is zero
  delay(10); 
  LcdCommandWrite(0x80+3); 
  delay(10);                     
/ / write a welcome message
  LcdDataWrite('W');
  LcdDataWrite('e');
  LcdDataWrite('l');
  LcdDataWrite('c');
  LcdDataWrite('o');
  LcdDataWrite('m');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('t');
  LcdDataWrite('o');
  delay(10);
  LcdCommandWrite(0xc0+1);  // Defines the cursor position as second rows and second
positions. 
  delay(10); 
  LcdDataWrite('g');
  LcdDataWrite('e');
  LcdDataWrite('e');
  LcdDataWrite('k');
  LcdDataWrite('-');
  LcdDataWrite('w');
  LcdDataWrite('o');
  LcdDataWrite('r');
  LcdDataWrite('k');
  LcdDataWrite('s');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('p');
  delay(5000);
  LcdCommandWrite(0x01);  // The screen is empty, the cursor position is zero
  delay(10);
  LcdDataWrite('I');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('m');
  LcdDataWrite(' ');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('n');
  LcdDataWrite('g');
  LcdDataWrite('y');
  LcdDataWrite('i');
  delay(3000);
  LcdCommandWrite(0x02); //Set the pattern for the new text to replace the old text, no new
text display the same place
  delay(10);
  LcdCommandWrite(0x80+5); //Defines the cursor position as the first line of the sixth
position
  delay(10);  
  LcdDataWrite('t');
  LcdDataWrite('h');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('d');
  LcdDataWrite('m');
  LcdDataWrite('i');
  LcdDataWrite('n');
  delay(5000);
}
4 Bit connection method
Under normal use, the 8 bit access to the basic Arduino digital port is full, and if you want to pick
a few sensors on the no port, and how to deal with this situation, we can use the 4 bit access
method.
4 bit connection method of the hardware connection method as shown below
After the hardware connection, the following code is uploaded to the control panel to see the
effect.

int LCD1602_RS=12;   
int LCD1602_RW=11;   
int LCD1602_EN=10;   
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="geek-workshop";
char str3[]="this is the";
char str4[]="4-bit interface";

void LCD_Command_Write(int command)


{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=command & 0xf0;


for (i=DB[0]; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(command & 0x0f)<<4;


for (i=DB[0]; i <= 10; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)


{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=dat & 0xf0;


for (i=DB[0]; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(dat & 0x0f)<<4;


for (i=DB[0]; i <= 10; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )


{
  int address;
  if (y ==0)    address = 0x80 + x;
  else          address = 0xC0 + x;
  LCD_Command_Write(address);
}

void LCD_Write_Char( int x,int y,int dat)


{
  LCD_SET_XY( x, y );
  LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)


{
LCD_SET_XY( X, Y );    //Set address
    while (*s)             //Write string
  {
      LCD_Data_Write(*s);   
      s ++;
  }
}

void setup (void)


{
  int i = 0;
  for (i=6; i <= 12; i++)
   {
     pinMode(i,OUTPUT);
   }
  delay(100);
  LCD_Command_Write(0x28);//4 lines and 2 lines 5x7
  delay(50);
  LCD_Command_Write(0x06);
  delay(50);
  LCD_Command_Write(0x0c);
  delay(50);
  LCD_Command_Write(0x80);
  delay(50);
  LCD_Command_Write(0x01);
  delay(50);

void loop (void)


{
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(3,0,str1);/First lines, fourth addresses
   delay(50);
   LCD_Write_String(1,1,str2);/Second lines, second addresses
   delay(5000);
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(0,0,str3);
   delay(50);
   LCD_Write_String(0,1,str4);
   delay(5000);
  
}
Common view copy code to save code print code
int LCD1602_RS=12;   
int LCD1602_RW=11;   
int LCD1602_EN=10;   
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="geek-workshop";
char str3[]="this is the";
char str4[]="4-bit interface";

void LCD_Command_Write(int command)
{
 int i,temp;
 digitalWrite( LCD1602_RS,LOW);
 digitalWrite( LCD1602_RW,LOW);
 digitalWrite( LCD1602_EN,LOW);

 temp=command & 0xf0;
 for (i=DB[0]; i <= 9; i++)
 {
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
 }
 
 digitalWrite( LCD1602_EN,HIGH);
 delayMicroseconds(1);
 digitalWrite( LCD1602_EN,LOW);

 temp=(command & 0x0f)<<4;
 for (i=DB[0]; i <= 10; i++)
 {
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
 }

 digitalWrite( LCD1602_EN,HIGH);
 delayMicroseconds(1); 
 digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)
{
 int i=0,temp;
 digitalWrite( LCD1602_RS,HIGH);
 digitalWrite( LCD1602_RW,LOW);
 digitalWrite( LCD1602_EN,LOW);

 temp=dat & 0xf0;
 for (i=DB[0]; i <= 9; i++)
 {
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
 }

 digitalWrite( LCD1602_EN,HIGH);
 delayMicroseconds(1);
 digitalWrite( LCD1602_EN,LOW);

 temp=(dat & 0x0f)<<4;
 for (i=DB[0]; i <= 10; i++)
 {
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
 }

 digitalWrite( LCD1602_EN,HIGH);
 delayMicroseconds(1); 
 digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )
{
  int address;
  if (y ==0)    address = 0x80 + x;
  else          address = 0xC0 + x;
  LCD_Command_Write(address); 
}

void LCD_Write_Char( int x,int y,int dat)
{
  LCD_SET_XY( x, y ); 
  LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)
{
    LCD_SET_XY( X, Y );    //Set address 
    while (*s)            / /Write string
    {
      LCD_Data_Write(*s);   
      s ++;
    }
}

void setup (void) 
{
  int i = 0;
  for (i=6; i <= 12; i++) 
   {
     pinMode(i,OUTPUT);
   }
  delay(100);
  LCD_Command_Write(0x28);//4 lines and 2 lines 5x7
  delay(50); 
  LCD_Command_Write(0x06);
  delay(50); 
  LCD_Command_Write(0x0c);
  delay(50); 
  LCD_Command_Write(0x80);
  delay(50); 
  LCD_Command_Write(0x01);
  delay(50); 

void loop (void)
{
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(3,0,str1);//First lines, fourth addresses from
   delay(50);
   LCD_Write_String(1,1,str2);//Second lines, second addresses from
   delay(5000);
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(0,0,str3);
   delay(50);
   LCD_Write_String(0,1,str4);
   delay(5000);
   
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy