极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2794|回复: 0

使用DS107做的数字时钟时间不同步,大神们帮忙看看

[复制链接]
发表于 2019-5-30 20:15:11 | 显示全部楼层 |阅读模式
使用DS107做的数字时钟时间不同步,大神们帮忙看看

#include <Wire.h>
#include <RTClib.h>
void printDateTime(DateTime dateTime);
//创建实例
RTC_DS1307 RTC;
int clk = 3;
int cs = 4;
int din = 5;
unsigned char disp3[60][8] = {
{0x0, 0x0, 0xEE, 0xAB, 0xAA, 0xAB, 0xEE, 0x0}, //0
{0x0, 0x0, 0xE4, 0xAD, 0xA4, 0xA5, 0xEE, 0x0}, //1
{0x0, 0x0, 0xEE, 0xA3, 0xAE, 0xA9, 0xEE, 0x0}, //2
{0x0, 0x0, 0xEE, 0xA3, 0xAE, 0xA3, 0xEE, 0x0}, //3
{0x0, 0x0, 0xEA, 0xAB, 0xAE, 0xA3, 0xE2, 0x0}, //4
{0x0, 0x0, 0xEE, 0xA9, 0xAE, 0xA3, 0xEE, 0x0}, //5
{0x0, 0x0, 0xEE, 0xA9, 0xAE, 0xAB, 0xEE, 0x0}, //6
{0x0, 0x0, 0xEE, 0xA3, 0xA2, 0xA3, 0xE2, 0x0}, //7
{0x0, 0x0, 0xEE, 0xAB, 0xAE, 0xAB, 0xEE, 0x0}, //8
{0x0, 0x0, 0xEE, 0xAB, 0xAE, 0xA3, 0xEE, 0x0}, //9
{0x0, 0x0, 0x4E, 0xCB, 0x4A, 0x4B, 0xEE, 0x0}, //10
{0x0, 0x0, 0x44, 0xCD, 0x44, 0x45, 0xEE, 0x0}, //11
{0x0, 0x0, 0x4E, 0xC3, 0x4E, 0x49, 0xEE, 0x0}, //12
{0x0, 0x0, 0x4E, 0xC3, 0x4E, 0x43, 0xEE, 0x0}, //13
{0x0, 0x0, 0x4A, 0xCB, 0x4E, 0x43, 0xE2, 0x0}, //14
{0x0, 0x0, 0x4E, 0xC9, 0x4E, 0x43, 0xEE, 0x0}, //15
{0x0, 0x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, //58
{0x0, 0x0, 0xEE, 0x8B, 0xEE, 0x23, 0xEE, 0x0} //59
};
long timer = 0;
int temp[3] = {0, 0, 0};
void setup() {
Serial.begin(9600);
//初始化总线
Wire.begin();
//初始化实时时钟
RTC.begin();
pinMode(cs, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(din, OUTPUT);
//初始点阵屏
Init_MAX7219();
}
void loop() {
if (millis() - timer > 999) {
//获取当前日期和时间
DateTime now = RTC.now();
//通过串口传送当前的日期和时间
printDateTime(now);
//把取当前时间放置temp[]变量
Date_Time(now);
//初始计时变量
timer = millis();
for (int i = 1; i < 9; i++) {
Write_Max7219_1(i, disp3[temp[0]][i - 1]);//点阵显示时
Write_Max7219_2(i, disp3[temp[1]][i - 1]);//点阵显示分
Write_Max7219_3(i, disp3[temp[2]][i - 1]);//点阵显示秒
}
}
}
void Date_Time(DateTime dateTime) {
temp[0] = dateTime.hour();
temp[1] = dateTime.minute();
temp[2] = dateTime.second();
}

void printDateTime(DateTime dateTime) {
//传送年份
Serial.print(dateTime.year(), DEC);
Serial.print(’/’);
//传送月份
Serial.print(dateTime.month(), DEC);
Serial.print(’/’);
//传送月份中的第几天
Serial.print(dateTime.day(), DEC);
Serial.print(’ ‘);
//传送小时
Serial.print(dateTime.hour(), DEC);
Serial.print(’:’);
//传送分钟
Serial.print(dateTime.minute(), DEC);
Serial.print(’:’);
//传送秒
Serial.print(dateTime.second(), DEC);
Serial.println();
}

void Write_Max7219_byte(char DATA)
{
char i;
digitalWrite(cs, 0);
for (i = 8; i >= 1; i–)
{
digitalWrite(clk, 0);
if ((DATA & 0x80) > 0) {
digitalWrite(din, 1);
}
else {
digitalWrite(din, 0);
}
//提取最高位给DIN端口
DATA = DATA << 1; //左移一位
digitalWrite(clk, 1);
}
}

void Write_Max7219_1(char address1, char dat1)
{
digitalWrite(cs, 0);
Write_Max7219_byte(address1); //写入地址,即数码管编号
Write_Max7219_byte(dat1); //写入数据,即数码管显示数字
digitalWrite(cs, 1);
}

void Write_Max7219_2(char address2, char dat2)
{
digitalWrite(cs, 0);
Write_Max7219_byte(address2); //写入地址,即数码管编号
Write_Max7219_byte(dat2);//写入数据,即数码管显示数字
digitalWrite(clk, 1);
Write_Max7219_byte(0x00);
Write_Max7219_byte(0x00);
digitalWrite(cs, 1);

}

void Write_Max7219_3(char address3, char dat3)
{
digitalWrite(cs, 0);
Write_Max7219_byte(address3); //写入地址,即数码管编号
Write_Max7219_byte(dat3);//写入数据,即数码管显示数字
digitalWrite(clk, 1);
Write_Max7219_byte(0x00);
Write_Max7219_byte(0x00);
Write_Max7219_byte(0x00);
Write_Max7219_byte(0x00);
digitalWrite(cs, 1);

}

void Init_MAX7219(void)
{
Write_Max7219_1(0x09, 0x00);//译码方式:BCD码
Write_Max7219_1(0x0a, 0x01);//亮度
Write_Max7219_1(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_1(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_1(0x0f, 0x00);

Write_Max7219_2(0x09, 0x00);//译码方式:BCD码
Write_Max7219_2(0x0a, 0x01);//亮度
Write_Max7219_2(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_2(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_2(0x0f, 0x00);

Write_Max7219_3(0x09, 0x00);//译码方式:BCD码
Write_Max7219_3(0x0a, 0x01);//亮度
Write_Max7219_3(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_3(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_3(0x0f, 0x00);

delay(500);
}

我用的是点阵模块
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2020-9-30 18:00 , Processed in 0.044455 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表