MCookie-RTC/zh
Language | English |
---|
mCookie-RTC模块是时钟模块,可获取时间,采用IIC接口通信。 超级电容提供了一定的掉电计时能力,断电后时钟芯片还可运行。
特色
规格
文档开发应用测试掉电续时#include <Wire.h>
#include <Rtc_Pcf8563.h>
//init the real time clock
Rtc_Pcf8563 rtc;
void setup()
{
Serial.begin(9600);
settime(15, 8, 10, 4, 15, 57, 36);//年,月,日,星期,时,分,秒
}
void loop()
{
//both format functions call the internal getTime() so that the
//formatted strings are at the current time/date.
Serial.println("CODE_1:");
Serial.print(rtc.formatDate());//获取日期
Serial.print(" ");
Serial.println(rtc.formatTime());//获取时间
Serial.println("CODE_2:");
Serial.print("20");
Serial.print(rtc.getYear());//获取年
Serial.print("/");
Serial.print(rtc.getMonth());//获取月
Serial.print("/");
Serial.print(rtc.getDay());//获取日
Serial.print(" ");
Serial.print(rtc.getHour());//获取时
Serial.print(":");
Serial.print(rtc.getMinute());//获取分
Serial.print(":");
Serial.println(rtc.getSecond());//获取秒
delay(1000);
Serial.print("\r\n");
}
void settime(int _year, int _month, int _day, int _week, int _hour, int _min, int _sec)
{
//clear out the registers
rtc.initClock();
//set a time to start with.
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
rtc.setDate(_day, _week, _month, 0, _year);
//hr, min, sec
rtc.setTime(_hour, _min, _sec);
}
测试EEPROM读写#include <EEPROM.h>
long randNumber, data; //定义随机变量使用数据名称
//EEPROM配置
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}
//定义EEPROMdata
struct config_type
{
int EEPROMdata;
};
void setup()
{
Serial.begin(115200);
/*EEPROM读取赋值*/
config_type config_readback;
EEPROM_read(0, config_readback);
data = config_readback.EEPROMdata;
}
void loop()
{
randNumber = random(10, 100);//随机函数,randNumber的值从10到99变化
delay(1000);//每隔1s刷新一次
if (randNumber != data) //判断EEPROM数据是否变化,变化则写入
eeprom_write();//EEPROM写入函数
Serial.print("EEPROM Write:");
Serial.println(randNumber);
Serial.print("EEPROM Read:");
Serial.println(data);
delay(1000);
Serial.println("");
}
//=======================EEPROM写函数===========================//
void eeprom_write()
{
config_type config; // 定义结构变量config,并定义config的内容
config.EEPROMdata = randNumber;
EEPROM_write(0, config); // 变量config存储到EEPROM,地址0写入
}
其他应用: 购买图库 |