Arduino UNO、LCD1602、DS1307制作带日历星期的数字时钟

作者: 可乐丸子
上传时间为: 2018-04-17 08:52 AM
2018-04-17
阅读:

下面介绍使用Arduino UNO、LCD1602、DS1307制作带日历星期的数字时钟

硬件连接:

DS1307模块的SDA SCL连接Arduino UNO数字输入输出上面一排的SDA SCL,DS1307的VCC GND连接Arduino UNO的ICSP的VCC GND

ICSP排针排列如下

LCD1602的VCC GND SDA SCL连接到Arduino UNO的5V GND A4 A5

连接好之后上电,打开Arduino IDE,打开项目--加载库--管理库,搜索安装rtllib库

如果之前没有安装过LiquidCrystal_I2C库还需要安装这个,安装完成后新建sketch,输入下面代码

#include <Wire.h>
#include <RTClib.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2);
RTC_DS1307 RTC;
char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

void setup () {
    Serial.begin(9600);
    lcd.init();
    lcd.backlight();
    Wire.begin();
    RTC.begin();
    RTC.adjust(DateTime(__DATE__, __TIME__));  
}

void loop () {
  
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('-');
    Serial.print(now.month(), DEC);
    Serial.print('-');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.println();
    
    lcd.setCursor(0,0);
    lcd.print(now.year(), DEC);
    lcd.print('-');
    lcd.print(now.month(), DEC);
    lcd.print('-');
    lcd.print(now.day(), DEC);
    lcd.print(' ');
    lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
    lcd.setCursor(0,1);
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);
    lcd.print(' ');
    delay(1000);
}

上传后效果如下

全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示