本文主要是介绍I2C LCD1602液晶显示屏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
本文实现在 LCD1602显示屏的使用。 显示屏使用带I2C转接板的,如下图所示。
并且会做一些有趣的显示方式。
效果预览
材料准备
材料 | 数量 | 价格 |
---|---|---|
Arduino nuo | 1 | 18 |
杜邦线 | 4 | 1 |
1602A显示屏 | 1 | 10 |
依赖库下载
本文使用都库在 arduino ide 中搜索 LiquidCrystal_I2C
即可找到。
注意作者,别下载错!
接线
1602A | Arduino nuo |
---|---|
VCC | 5v |
GND | GND |
SDA | A4 |
SCL | A5 |
简单显示一段字符
#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line displayvoid setup() {// 初始化lcd.init(); lcd.backlight();// 简单的一个开始lcd.setCursor(0,0); // 设置文字开始位置(列, 行)lcd.print("Hello, world!");
} void loop() {
}
文字逐个显示
#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line displayvoid setup() {// 初始化lcd.init(); lcd.backlight();} void loop() {// 第一行char strArr[] = "Hello, world!";for (int i = 0; i < sizeof(strArr); i++) {lcd.setCursor(i, 0); // 设置文字开始位置(列, 行)strArr[i] && lcd.print(strArr[i]); // 打印文字delay(200);}// 第二行char strArr2[] = "case over";for (int i = 0; i < sizeof(strArr2); i++) {lcd.setCursor(i, 1); // 设置文字开始位置(列, 行)strArr2[i] && lcd.print(strArr2[i]); // 打印文字delay(200);}delay(3000);
}
自定义字符
#include <Wire.h>
#include <LiquidCrystal_I2C.h>#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endifuint8_t bell[8] = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
uint8_t note[8] = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
uint8_t duck[8] = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
uint8_t retarrow[8] = { 0x1,0x1,0x5,0x9,0x1f,0x8,0x4};LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line displayvoid setup()
{lcd.init(); // initialize the lcd lcd.backlight();lcd.createChar(0, bell);lcd.createChar(1, note);lcd.createChar(2, clock);lcd.createChar(3, heart);lcd.createChar(4, duck);lcd.createChar(5, check);lcd.createChar(6, cross);lcd.createChar(7, retarrow);lcd.home();lcd.print("Hello world...");lcd.setCursor(0, 1);lcd.print(" i ");lcd.printByte(3);lcd.print(" arduinos!");delay(5000);displayKeyCodes();}// display all keycodes
void displayKeyCodes(void) {uint8_t i = 0;while (1) {lcd.clear();lcd.print("Codes 0x"); lcd.print(i, HEX);lcd.print("-0x"); lcd.print(i+16, HEX);lcd.setCursor(0, 1);for (int j=0; j<16; j++) {lcd.printByte(i+j);}i+=16;delay(4000);}
}void loop()
{}
这篇关于I2C LCD1602液晶显示屏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!