使用LCD1602和DS1302模块做万年历时钟的代码
时间: 2024-05-14 09:19:55 浏览: 15
好的,下面是使用LCD1602和DS1302模块做万年历时钟的代码:
```C++
#include<time.h>
#include<LiquidCrystal.h>
#include<DS1302.h>
// Define LCD pin
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Define DS1302 pin
const int CE_PIN = 13;
const int IO_PIN = 9;
const int SCLK_PIN = 10;
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("万年历时钟");
// Initialize DS1302
rtc.halt(false);
rtc.writeProtect(false);
}
void loop() {
// Get current time from DS1302
time_t t = rtc.get();
// Convert time to local time
tm* local = localtime(&t);
// Print date and time on LCD
lcd.setCursor(0, 1);
lcd.print(local->tm_year + 1900);
lcd.print("/");
lcd.print(local->tm_mon + 1);
lcd.print("/");
lcd.print(local->tm_mday);
lcd.print(" ");
lcd.print(local->tm_hour);
lcd.print(":");
lcd.print(local->tm_min);
lcd.print(":");
lcd.print(local->tm_sec);
// Wait for 1 second
delay(1000);
}
```
这段代码可以在 Arduino 上运行,并使用 LCD1602 和 DS1302 模块显示当前的日期和时间。要使用这段代码,您需要将 LCD1602 和 DS1302 模块连接到正确的引脚并上传到您的 Arduino 板上。