I2C LCD1602液晶显示屏

213 阅读2分钟

前言

本文实现在 LCD1602显示屏的使用。 显示屏使用带I2C转接板的,如下图所示。 并且会做一些有趣的显示方式。

5fc9a40d89d4fa77dc88ea773ff8658.jpg

效果预览

b0624d8d5ef4708655f8edfd9315728.jpg

材料准备

材料数量价格
Arduino nuo118
杜邦线41
1602A显示屏110

依赖库下载

本文使用都库在 arduino ide 中搜索 LiquidCrystal_I2C 即可找到。

注意作者,别下载错!

db27d0c7f565933c2f4e01134ef6cf3.png

接线

1602AArduino nuo
VCC5v
GNDGND
SDAA4
SCLA5

简单显示一段字符


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {
  // 初始化
  lcd.init();  
  lcd.backlight();
  
  // 简单的一个开始
  lcd.setCursor(0,0); // 设置文字开始位置(列, 行)
  lcd.print("Hello, world!");  
} 

void loop() {   
}

b0624d8d5ef4708655f8edfd9315728.jpg

文字逐个显示


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void 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);
#endif

uint8_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 display

void 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()
{

}

4be21146e7962022f6320fc04ce68d3.jpg