用单片机编写时间采集记录仪的程序,分模块化编程,用到DS1302、IIC、LCD1602
时间: 2023-07-16 17:15:49 浏览: 85
基于ds1302和单片机的时钟设计.doc
好的,下面是一个简单的时间采集记录仪程序,分模块化编程,使用了DS1302、IIC和LCD1602。
首先,需要定义一些常量和变量:
```
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS1302.h>
#define DS1302_SCLK 8
#define DS1302_IO 9
#define DS1302_CE 10
DS1302 rtc(DS1302_SCLK, DS1302_IO, DS1302_CE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int seconds, minutes, hours, day, month, year;
```
接着,需要写一些初始化函数:
```
void setup() {
rtc.halt(false);
rtc.writeProtect(false);
rtc.clockMode(DS1302_CLOCK_MODE_CONTINUOUS);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Time Recorder");
delay(2000);
lcd.clear();
}
```
这些函数用来初始化DS1302和LCD1602,以及显示欢迎信息。
接下来是主要的循环:
```
void loop() {
rtc.readTime(seconds, minutes, hours, day, month, year);
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(day);
lcd.print("/");
lcd.print(month);
lcd.print("/");
lcd.print(year);
delay(1000);
}
```
这个循环从DS1302读取时间,然后显示在LCD1602上。每隔1秒钟更新一次显示。
最后是完整的代码:
```
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS1302.h>
#define DS1302_SCLK 8
#define DS1302_IO 9
#define DS1302_CE 10
DS1302 rtc(DS1302_SCLK, DS1302_IO, DS1302_CE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int seconds, minutes, hours, day, month, year;
void setup() {
rtc.halt(false);
rtc.writeProtect(false);
rtc.clockMode(DS1302_CLOCK_MODE_CONTINUOUS);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Time Recorder");
delay(2000);
lcd.clear();
}
void loop() {
rtc.readTime(seconds, minutes, hours, day, month, year);
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(day);
lcd.print("/");
lcd.print(month);
lcd.print("/");
lcd.print(year);
delay(1000);
}
```
阅读全文