1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 关于ARDUINO使用DS1302高精度时钟模块的探索(01)

关于ARDUINO使用DS1302高精度时钟模块的探索(01)

时间:2024-02-08 17:58:18

相关推荐

关于ARDUINO使用DS1302高精度时钟模块的探索(01)

DS1302模块的使用

DS1302时钟模块的特性是在外部切断电源时可以启用内部电源(纽扣电池)供电,保证时间记录不会被消除,只需要设定写入一次正确时间,此后每次读取到的都是正确时间。

硬件连接

DS1302:

**注意vcc------3.3v**

程序解释(以库自带实例程序为例)

位置如图

代码如下

// DS1302_Serial_Easy (C) Henning Karlsen// web: /electronics//// A quick demo of how to use my DS1302-library to // quickly send time and date information over a serial link//// I assume you know how to connect the DS1302.// DS1302: CE pin -> Arduino Digital 2//I/O pin -> Arduino Digital 3//SCLK pin -> Arduino Digital 7#include <DS1302.h>// Init the DS1302DS1302 rtc(2, 3, 7);void setup(){// Set the clock to run-mode, and disable the write protectionrtc.halt(false);rtc.writeProtect(false);// Setup Serial connectionSerial.begin(9600);// The following lines can be commented out to use the values already stored in the DS1302rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAYrtc.setTime(12, 0, 0);// Set the time to 12:00:00 (24hr format)rtc.setDate(6, 8, ); // Set the date to August 6th, }void loop(){// Send Day-of-WeekSerial.print(rtc.getDOWStr());Serial.print(" ");// Send dateSerial.print(rtc.getDateStr());Serial.print(" -- ");// Send timeSerial.println(rtc.getTimeStr());// Wait one second before repeating :)delay (1000);}

代码解释如下

#include <DS1302.h>

^ 调用DS1302库文件(下载链接在文章末)

DS1302 rtc(2, 3, 7);//RST, DAT, CLK

^ 定义DS1302引脚

// Set the clock to run-mode, and disable the write protectionrtc.halt(false);rtc.writeProtect(false);

^ 第一行时设置时钟的工作模式,第二行是设置写入保护的开关

// Setup Serial connectionSerial.begin(9600);

^ 设置串口通信频率

// The following lines can be commented out to use the values already stored in the DS1302rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAYrtc.setTime(12, 0, 0);// Set the time to 12:00:00 (24hr format)rtc.setDate(6, 8, ); // Set the date to August 6th,

这里是设置时间日期的代码,星期的设定必须用大写,设置好之后需要注释掉这部分重新烧录一次!!!不然每次上电后会重新设置时间

// Send Day-of-WeekSerial.print(rtc.getDOWStr());Serial.print(" ");

rtc.getDOWStr()是读取星期的函数

// Send dateSerial.print(rtc.getDateStr());Serial.print(" -- ");

rtc.getDateStr()是读取日期的函数,这里要注意的是这个函数的使用方法可以改变输出的日期的格式

getDateStr(uint8_t slformat, uint8_t eformat, char divider)

uint8_t slformat 确定格式的长短型,

FORMAT_LONG长型是 10 02

FORMAT_SHORT 短型是20 10 没有日只有年月uint8_t eformat 这个决定年月日的排列顺序,有三种排列方式:

FORMAT_LITTLEENDIAN 对应是日月年;

FORMAT_BIGENDIAN对应的是年月日;

FORMAT_MIDDLEENDIAN对应的是月日年char divider 确定数字之间的连接符

最后举个例子rtc.getDateStr(FORMAT_LONG, FORMAT_BIGENDIAN, '-')输出的是-10-02

// Send timeSerial.println(rtc.getTimeStr());

读取时间的函数rtc.getTimeStr()默认输出格式是24小时制,例如12:14:30

小结

我在修改输出年月日格式的过程中发现,年-月-日这种排序方式不能正确显示在oled屏幕上,于是我查看库文件发现原来多定义了一个参数,我把参数删掉把原来的参数式改成常数后,显示正常,下面是我修改后的部分。在我分享的库文件中是我修改后的文件,原来的文件大家可以去网上找一下吧。

case FORMAT_BIGENDIAN:if (slformat==FORMAT_SHORT){yr=t.year-2000;if (yr<10)output[0]=48;elseoutput[0]=char((yr / 10)+48);output[1]=char((yr % 10)+48);output[2]=divider;}else{yr=t.year;output[0]=char((yr / 1000)+48);output[1]=char(((yr % 1000) / 100)+48);output[2]=char(((yr % 100) / 10)+48);output[3]=char((yr % 10)+48);output[4]=divider;}if (t.mon < 10)output[5] = 48;elseoutput[5] = char((t.mon / 10) + 48);output[6] = char((t.mon % 10) + 48);output[7] = divider;if (t.date < 10)output[8] = 48;else output[8] = char((t.date / 10) + 48);output[9] = char((t.date % 10) + 48);output[10] = 0;break;

链接: DS1302库文件.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。