LCD1602 库函数
Posted myautomation
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LCD1602 库函数相关的知识,希望对你有一定的参考价值。
This library allows an Arduino board to control LiquidCrystal displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-basedLCDs. The library works with in either 4- or 8-bit mode (i.e. using 4 or 8 data lines in addition to the rs, enable, and, optionally, the rw control lines).
Function
详细解释
LiquidCrystal() 建立一个LiquidCrystal类型变量
Description描述
Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those lines unconnected. The RW pin can be tied to ground instead of connected to a pin on the Arduino; if so, omit it from this function‘s parameters.
Syntax语法
LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)
Parameters参数
rs: the number of the Arduino pin that is connected to the RS pin on the LCD 寄存器选择引脚
rw: the number of the Arduino pin that is connected to the RW pin on the LCD (optional) 读写引脚
enable: the number of the Arduino pin that is connected to the enable pin on the LCD 使能引脚
d0, d1, d2, d3, d4, d5, d6, d7: the numbers of the Arduino pins that are connected to the corresponding data pins on the LCD. d0, d1, d2, and d3 are optional; if omitted, the LCD will be controlled using only the four data lines (d4, d5, d6, d7). 数据引脚,8个或4个
Example示例
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
lcd.begin(16,1);
lcd.print("hello, world!");
}
void loop() {}
begin() 初始化LCD的界面
Description
Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display. begin()
needs to be called before any other LCD library commands.
Syntax
lcd.begin(cols, rows)
Parameters
lcd: a variable of type LiquidCrystal
cols: the number of columns that the display has
rows: the number of rows that the display has
clear() 清屏,光标在左上角
Description
Clears the LCD screen and positions the cursor in the upper-left corner.
Syntax
lcd.clear()
Parameters
lcd: a variable of type LiquidCrystal
home() 光标定位于左上角
Description
Positions the cursor in the upper-left of the LCD. That is, use that location in outputting subsequent text to the display. To also clear the display, use the clear() function instead.
Syntax
lcd.home()
Parameters
lcd: a variable of type LiquidCrystal
setCursor() 设置光标位置
Description
Position the LCD cursor; that is, set the location at which subsequent text written to the LCD will be displayed.
Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiquidCrystal
col: the column at which to position the cursor (with 0 being the first column)
row: the row at which to position the cursor (with 0 being the first row)
write() 写一个字符
Description
Write a character to the LCD.
Syntax
lcd.write(data)
Parameters
lcd: a variable of type LiquidCrystal
data: the character to write to the display
Returns
byte
write() will return the number of bytes written, though reading that number is optional
Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
lcd.write(Serial.read());
}
}
print() 在LCD上显示一个文本
Description
Prints text to the LCD.
Syntax
lcd.print(data)
lcd.print(data, BASE)
Parameters
lcd: a variable of type LiquidCrystal
data: the data to print (char, byte, int, long, or string)
BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
Returns
byte
print() will return the number of bytes written, though reading that number is optional
Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
lcd.print("hello, world!");
}
void loop() {}
cursor() 显示光标
Description
Display the LCD cursor: an underscore (line) at the position to which the next character will be written.
Syntax
lcd.cursor()
Parameters
lcd: a variable of type LiquidCrystal
noCursor() 不显示光标
Description
Hides the LCD cursor.
Syntax
lcd.noCursor()
Parameters
lcd: a variable of type LiquidCrystal
blink() 光标闪烁
Description
Display the blinking LCD cursor. If used in combination with the cursor() function, the result will depend on the particular display.
Syntax
lcd.blink()
Parameters
lcd: a variable of type LiquidCrystal
noBlink() 光标不闪烁
Description
Turns off the blinking LCD cursor.
Syntax
lcd.noBlink()
Parameters
lcd: a variable of type LiquidCrystal
display() LCD显示
Description
Turns on the LCD display, after it‘s been turned off with noDisplay(). This will restore the text (and cursor) that was on the display.
Syntax
lcd.display()
Parameters
lcd: a variable of type LiquidCrystal
noDisplay() LCD不显示
Description
Turns off the LCD display, without losing the text currently shown on it.
Syntax
lcd.noDisplay()
Parameters
lcd: a variable of type LiquidCrystal
scrollDisplayLeft() 滚动显示的内容往左一个位置
Description
Scrolls the contents of the display (text and cursor) one space to the left.
Syntax
lcd.scrollDisplayLeft()
Parameters
lcd: a variable of type LiquidCrystal
scrollDisplayRight() 滚动显示的内容往右一个位置
Description
Scrolls the contents of the display (text and cursor) one space to the right.
Syntax
lcd.scrollDisplayRight()
Parameters
lcd: a variable of type LiquidCrystal
autoscroll() 输入时光标在一个固定位置,字符自动移动
Description
Turns on automatic scrolling of the LCD. This causes each character output to the display to push previous characters over by one space. If the current text direction is left-to-right (the default), the display scrolls to the left; if the current direction is right-to-left, the display scrolls to the right. This has the effect of outputting each new character to the same location on the LCD.
Syntax
lcd.autoscroll()
Parameters
lcd: a variable of type LiquidCrystal
noAutoscroll() 关闭输入时字符自动移动
Description
Turns off automatic scrolling of the LCD.
Syntax
lcd.noAutoscroll()
Parameters
lcd: a variable of type LiquidCrystal
leftToRight() 文本显示时的移动方向是从左往右
Description
Set the direction for text written to the LCD to left-to-right, the default. This means that subsequent characters written to the display will go from left to right, but does not affect previously-output text.
Syntax
lcd.leftToRight()
Parameters
lcd: a variable of type LiquidCrystal
rightToLeft() 文本显示时的移动方向是从右往左
Description
Set the direction for text written to the LCD to right-to-left (the default is left-to-right). This means that subsequent characters written to the display will go from right to left, but does not affect previously-output text.
Syntax
lcd.rightToLeft()
Parameters
lcd: a variable of type LiquidCrystal
createChar() 建立一个自定义的图形字符
Description
Create a custom character (glyph) for use on the LCD. Up to eight characters of 5x8 pixels are supported (numbered 0 to 7). The appearance of each custom character is specified by an array of eight bytes, one for each row. The five least significant bits of each byte determine the pixels in that row. To display a custom character on the screen, write() its number.
NB : When referencing custom character "0", if it is not in a variable, you need to cast it as a byte, otherwise the compiler throws an error. See the example below.
Syntax
lcd.createChar(num, data)
Parameters
lcd: a variable of type LiquidCrystal
num: which character to create (0 to 7)
data: the character‘s pixel data
Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte smiley[8] =
{
B00000,
B10001,
B00000,
B00000,
B10001,
B01110,
B00000,
};
void setup()
{
lcd.createChar(0, smiley);
lcd.begin(16, 2);
lcd.write(byte(0));
}
void loop() {}
以上是关于LCD1602 库函数的主要内容,如果未能解决你的问题,请参考以下文章
Arduino下LCD1602综合探究(中)——如何减少1602的连线,LiquidCrystal库,LiquidCrystal库中bug的解决方法
ESP3212.I2C LCD1602液晶显示实验(LiquidCrystal_I2C库)