【Arduino】IIC 1602液晶屏显示

今天从办公室垃圾堆里发现了6块1602液晶屏,准备试试好坏,结果就1块能用,把另外5块默默送回了垃圾堆

1、硬件连线
(1)Arduino UNO开发板
(2)带IIC接口的PCF8574芯片1602液晶屏
(3)IIC只需要四根线:VCC5V、GND、SCL、SDA
在这里插入图片描述
2、Arduino库添加
(1)工具->管理库->搜索LiquidCrystal_I2C->安装
在这里插入图片描述
(2)加载贡献库
)
3、代码

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

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

void setup()
{
    
    
lcd.init(); // initialize the lcd 
lcd.backlight(); //Open the backlight
lcd.print("Hello "); // Print a message to the LCD.
lcd.setCursor(0,1); //newline
lcd.print("Women ");// Print a message to the LCD
}
void loop()
{
    
    
}

4、下载运行
(1)这发现每行只显示了第一个字符
在这里插入图片描述
(2)上网冲浪了一会,找到个帖子,说是只需要更改 LiquidCrystal_I2C.cpp 文件中的一个语句就可以了,更改如下:

inline size_t LiquidCrystal_I2C::write(uint8_t value) {
    
    
        send(value, Rs);
        return 0; 改为 return 1;
}

但是发现已经是return 1;了,我还改个啥?
(3)换成Ardino1.5试试,但是发现1.6.2以下的版本没有库管理工具,加入LiquidCrystal_I2C库很麻烦
(4)那有没有可能是LiquidCrystal_I2C库有问题,自定义显示函数试试

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

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

void MyLCD(String MyString)
{
    
    
  for (int i=0;i<MyString.length();i++)
  lcd.write(MyString.charAt(i));
}
    
void setup()
{
    
    
lcd.init(); // initialize the lcd 
lcd.backlight(); //Open the backlight
MyLCD("Hello "); // Print a message to the LCD.
lcd.setCursor(0,1); //newline
MyLCD("Women ");// Print a message to the LCD
}
void loop()
{
    
    
  
}

竟然可以了,啧啧
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33033059/article/details/107845564