我的 1602a 使用 i2c 接口
不同芯片有不同的 I2C 地址, 从下面的链接查看
I2C LCD与Arduino接口
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2); //声明I2C地址和点阵的规格为16字符和2行
// 定义字符.1, 指定哪个像素亮,哪个灭
byte Heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
0b00000};
void setup()
{
Serial.begin(9600);
initLcd();
}
void loop()
{
char timestamp[20];
getCurrentTime(timestamp);
lcd.clear();
lcd.setCursor(0, 0);
// 定义字符.3 输出
lcd.write(0);
lcd.setCursor(2, 0);
lcd.print(timestamp);
delay(300);
}
// 初始化 lcd
void initLcd()
{
lcd.init();
lcd.backlight();
lcd.clear();
// 定义字符.2,第一个参数 0-7, 只支持 8 个字符
lcd.createChar(0, Heart);
}
// 返回当前的时间
void getCurrentTime(char* timestamp){
// Division constants
const unsigned long MSECS_PER_SEC = 1000;
const unsigned long SECS_PER_MIN = 60;
const unsigned long SECS_PER_HOUR = 3600;
const unsigned long SECS_PER_DAY = 86400;
// Total time
const unsigned long msecs = millis();
const unsigned long secs = msecs / MSECS_PER_SEC;
// Time in components
const unsigned long MilliSeconds = msecs % MSECS_PER_SEC;
const unsigned long Seconds = secs % SECS_PER_MIN;
const unsigned long Minutes = (secs / SECS_PER_MIN) % SECS_PER_MIN;
const unsigned long Hours = (secs % SECS_PER_DAY) / SECS_PER_HOUR;
// char timestamp[20]; // Time as string
sprintf(timestamp, "%02ld:%02ld:%02ld.%03ld ", Hours, Minutes, Seconds, MilliSeconds);
}