Xadow RTC 实时时钟

From Wiki
Jump to: navigation, search

产品介绍


300px-Xadow RTC 01.jpg


Xadow RTC是基于DS1337的高精度实时时钟模块,使用I2C的通信方式,可读取实时的年、月、日、星期、时、分、秒。扣上纽扣电池时,即使模块掉电,时钟芯片会持续计时。

规格参数

实时时钟能提供:秒,分,时,日,星期,月,年等信息 可选择12小时制或者24小时制 可计时到2100年 外形尺寸: 25.43mm x 20.35mm 注意事项

为了保证时钟芯片能够持续计时,请不要移除纽扣电池。如果仅仅由Xadow主板来提供电源,时钟芯片是不能正常工作的。

应用示例

下面将展示如何从RTC中读取时间信息。 硬件连接如下图所示:


XadwoRTC.jpg


测试代码:

  1. include <Wire.h>
  2. define DS1337_CTRL_ID 0x68

unsigned char second,minute,hour; unsigned char week,day,month; unsigned int year;

void setup() {

 Wire.begin();
 Serial.begin(38400);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }
  
fillByYMD(2013,6,17);//Jun 19,2013
fillByHMS(23,15,43);//23:15 43"
fillWeek(1);//Monday
setTime();

}

void loop() {

      readTime();
 	Serial.print(year+2000, DEC);
       Serial.print("/");
       Serial.print(month, DEC);
        Serial.print("/");
       Serial.print(day, DEC);
      	Serial.print("  ");
       Serial.print(hour, DEC);

Serial.print(":"); Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); Serial.print(" "); switch (week)// Friendly printout the weekday { case 1: Serial.print("MON"); break; case 2: Serial.print("TUE"); break; case 3: Serial.print("WED"); break; case 4: Serial.print("THU"); break; case 5: Serial.print("FRI"); break; case 6: Serial.print("SAT"); break; case 7: Serial.print("SUN"); break; } Serial.println(" ");

       delay(1000);

} /*Function: Read time and date from RTC */ void readTime(void) {

 // use the Wire lib to connect to tho rtc
 // reset the register pointer to zero
 Wire.beginTransmission(DS1337_CTRL_ID);
 Wire.write(0x00);
 Wire.endTransmission();
 // request the 7 bytes of data    (secs, min, hr, dow, date. mth, yr)
 Wire.requestFrom(DS1337_CTRL_ID, 7);

// A few of these need masks because certain bits are control bits second = bcdToDec(Wire.read() & 0x7f); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read() & 0x3f);// Need to change this if 12 hour am/pm week = bcdToDec(Wire.read()); day = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); } /*Frunction: Write the time that includes the date to the RTC chip */ void setTime(void) { Wire.beginTransmission(DS1337_CTRL_ID); Wire.write(0x00);

       Wire.write(decToBcd(second));// 0 to bit 7 starts the clock

Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set bit 6 Wire.write(decToBcd(week)); Wire.write(decToBcd(day)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); }

unsigned char decToBcd(unsigned char val) { return ( (val/10*16) + (val%10) ); }

//Convert binary coded decimal to normal decimal numbers unsigned char bcdToDec(unsigned char val) { return ( (val/16*10) + (val%16) ); } void fillByYMD(unsigned int _year, unsigned char _month, unsigned char _day) { year = _year-2000; month = _month; day = _day; } void fillWeek(unsigned char _dow) { week = _dow; } void fillByHMS(unsigned char _hour, unsigned char _minute, unsigned char _second) { // assign variables hour = _hour; minute = _minute; second = _second; } 设置时间。将下面函数的参数更改为当前的时间,注意格式要和之前的保持一致.

fillByYMD(2013,6,17);//Jun 17,2013
fillByHMS(23,15,43);//23:15 43"
fillWeek(1);//Monday

设置好时间后,下载程序到Xadow Main Board。输出结果如下:

Xadow - RTC Result.jpg

只需要在你第一次使用RTC模块时设置时间,以后再下载这个程序的时候,需要注释掉下面的函数,然后再下载。 //setTime();


File:DS1307.pdf