本实验使用DS1302时钟模块进行演示时间日期相关的功能应用。
电脑,windows系统
零知开发板
micro-usb线
DS1302模块
1、打开零知实验室软件开发工具,然后新建项目,输入以下代码:
2、按照之前的方式先【编译】,然后【上传】到开发板中。
#include "DS1302.h"
// Init the DS1302
DS1302 rtc(2, 3, 4);//rst,data,clk
// Init a Time-data structure
Time t;
void setup()
{
// Set the clock to run-mode, and disable the write protection
rtc.halt(false);
rtc.writeProtect(false);
// Setup Serial connection
Serial.begin(9600);
// The following lines can be commented out to use the values already stored in the DS1302
rtc.setDOW(THURSDAY); // Set Day-of-Week to FRIDAY
rtc.setTime(15, 30, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(27, 6, 2019); // Set the date to August 6th, 2010
}
void loop()
{
// Get data from the DS1302
t = rtc.getTime();
// Send date over serial connection
Serial.print("The Date: ");
Serial.print(t.date, DEC);
Serial.print(".");
Serial.print(rtc.getMonthStr());
Serial.print(". ");
Serial.print(t.year, DEC);
Serial.println(".");
// Send Day-of-Week and time
Serial.print("Day of Week:");
Serial.println(t.dow, DEC);
Serial.print("The Time:");
Serial.print(t.hour, DEC);
Serial.print(" : ");
Serial.print(t.min, DEC);
Serial.print(" : ");
Serial.print(t.sec, DEC);
Serial.println(" .");
// Send a divider for readability
Serial.println(" - - - - - - ");
// Wait one second before repeating :)
delay (1000);
}
将上述代码验证后上传到零知板,就可以看到测试结果了。
完整的工程代码:DS1302_Serial_print.7z(点击下载)