ds1302时钟lcd1602显示(可以按键设置时钟)代码
时间: 2023-10-16 17:09:04 浏览: 77
DS1302时钟LCD1602显示(可以按键设置时钟).zip_DS1302 LCD1602_DS1302加lcd1602_lc
5星 · 资源好评率100%
以下是DS1302时钟与LCD1602显示屏结合的Arduino代码,可以通过按键设置时钟:
```
#include <DS1302.h>
#include <LiquidCrystal.h>
// Define DS1302 pins
const int RTC_SCLK = 2;
const int RTC_IO = 3;
const int RTC_CE = 4;
// Define LCD1602 pins
const int LCD_RS = 12;
const int LCD_EN = 11;
const int LCD_D4 = 5;
const int LCD_D5 = 6;
const int LCD_D6 = 7;
const int LCD_D7 = 8;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
DS1302 rtc(RTC_SCLK, RTC_IO, RTC_CE);
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
void setup() {
lcd.begin(16, 2);
rtc.begin();
// Set the clock to 12:00:00 AM on January 1, 2020
rtc.writeProtect(false);
rtc.halt(false);
rtc.setDOW(MONDAY);
rtc.setTime(12, 0, 0);
rtc.setDate(1, 1, 2020);
rtc.writeProtect(true);
}
void loop() {
// Get the current time from the DS1302
second = rtc.getSecond();
minute = rtc.getMinute();
hour = rtc.getHour();
dayOfWeek = rtc.getDOW();
dayOfMonth = rtc.getDay();
month = rtc.getMonth();
year = rtc.getYear();
// Display the current time on the LCD1602
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(hour, DEC);
lcd.print(":");
if (minute < 10) {
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second < 10) {
lcd.print("0");
}
lcd.print(second, DEC);
// Display the current date on the LCD1602
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(year, DEC);
// Check if any buttons are pressed to set the time
if (digitalRead(9) == LOW) { // Set Hour Button
delay(50); // Debounce Button
if (digitalRead(9) == LOW) { // Check if Button is still pressed
hour++;
if (hour > 23) {
hour = 0;
}
rtc.setTime(hour, minute, second);
}
}
if (digitalRead(10) == LOW) { // Set Minute Button
delay(50); // Debounce Button
if (digitalRead(10) == LOW) { // Check if Button is still pressed
minute++;
if (minute > 59) {
minute = 0;
}
rtc.setTime(hour, minute, second);
}
}
if (digitalRead(11) == LOW) { // Set Second Button
delay(50); // Debounce Button
if (digitalRead(11) == LOW) { // Check if Button is still pressed
second++;
if (second > 59) {
second = 0;
}
rtc.setTime(hour, minute, second);
}
}
if (digitalRead(13) == LOW) { // Set Date Button
delay(50); // Debounce Button
if (digitalRead(13) == LOW) { // Check if Button is still pressed
dayOfMonth++;
if (dayOfMonth > 31) {
dayOfMonth = 1;
}
rtc.setDate(dayOfMonth, month, year);
}
}
if (digitalRead(14) == LOW) { // Set Month Button
delay(50); // Debounce Button
if (digitalRead(14) == LOW) { // Check if Button is still pressed
month++;
if (month > 12) {
month = 1;
}
rtc.setDate(dayOfMonth, month, year);
}
}
if (digitalRead(15) == LOW) { // Set Year Button
delay(50); // Debounce Button
if (digitalRead(15) == LOW) { // Check if Button is still pressed
year++;
if (year > 2099) {
year = 2020;
}
rtc.setDate(dayOfMonth, month, year);
}
}
}
```
代码中使用了DS1302库和LiquidCrystal库,需要在Arduino IDE中安装这两个库才能编译通过。代码中的数字9、10、11、13、14、15分别对应6个按键的引脚,按键的具体接法可以根据自己的需求进行修改。
阅读全文