watch
Dependents: Xadow_Watch_OLED Seeed_Arch_GPRS_V2_Humidty_Alert
Revision 0:f4f9b627adf9, committed 2014-04-01
- Comitter:
- loovee
- Date:
- Tue Apr 01 07:01:08 2014 +0000
- Commit message:
- watch
Changed in this revision
DS1337.cpp | Show annotated file Show diff for this revision Revisions of this file |
DS1337.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r f4f9b627adf9 DS1337.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1337.cpp Tue Apr 01 07:01:08 2014 +0000 @@ -0,0 +1,146 @@ + +#include "DS1337.h" + + +// NOTE: To keep the math from getting even more lengthy/annoying than it already is, the following constraints are imposed: +// 1) All times are in 24-hour format (military time) +// 2) DayOfWeek field is not used internally or checked for validity. Alarm functions may optionally set alarms repeating on DayOfWeek, but this feature has not been tested yet. +// 3) This library's buffer stores all times in raw BCD format, just as it is sent from the RTC. +// It is not converted to/from 'real' (binary) values until needed via get...() and set...() functions. +// In other words, don't go hacking around and reading from the rtc_bcd[] buffer directly, unless you want the raw BCD results. + + +// Cumulative number of days elapsed at the start of each month, assuming a normal (non-leap) year. +unsigned int monthdays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + + +extern I2C i2c; + +unsigned char DS1337::getRegister(unsigned char registerNumber) +{ + uint8_t val = registerNumber; + + i2c.write(DS1337_CTRL_ID,(char *)&val,1); + i2c.stop(); + i2c.read(DS1337_CTRL_ID, (char *)&val, 1); + return val; +} + +void DS1337::setRegister(unsigned char registerNumber, unsigned char value) +{ + uint8_t val[2]; + val[0] = registerNumber; + val[1] = value; + i2c.write(DS1337_CTRL_ID, (char *)val, 2); +} + +void DS1337::readTime(void) +{ + uint8_t val = 0x00; + + i2c.write(DS1337_CTRL_ID,(char *)&val,1); + i2c.stop(); + i2c.read(DS1337_CTRL_ID, (char *)rtc_bcd, 7); +} + +unsigned char DS1337::getSeconds() +{ + return bcd2bin(rtc_bcd[DS1337_SEC]); +} + +unsigned char DS1337::getMinutes() +{ + return bcd2bin(rtc_bcd[DS1337_MIN]); +} +unsigned char DS1337::getHours() +{ + return bcd2bin(rtc_bcd[DS1337_HR]); +} +unsigned char DS1337::getDays() +{ + return bcd2bin(rtc_bcd[DS1337_DATE]); +} +unsigned char DS1337::getDayOfWeek() +{ + return bcd2bin(rtc_bcd[DS1337_DOW]); +} +unsigned char DS1337::getMonths() +{ + return bcd2bin(rtc_bcd[DS1337_MTH]); +} +unsigned int DS1337::getYears() +{ + return 2000 + bcd2bin(rtc_bcd[DS1337_YR]); +} + + +void DS1337::setSeconds(unsigned char v) +{ + rtc_bcd[DS1337_SEC] = bin2bcd(v); + +} +void DS1337::setMinutes(unsigned char v) +{ + rtc_bcd[DS1337_MIN] = bin2bcd(v); + +} +void DS1337::setHours(unsigned char v) +{ + rtc_bcd[DS1337_HR] = bin2bcd(v); + +} +void DS1337::setDays(unsigned char v) +{ + rtc_bcd[DS1337_DATE] = bin2bcd(v); + +} +void DS1337::setDayOfWeek(unsigned char v) +{ + rtc_bcd[DS1337_DOW] = bin2bcd(v); + +} +void DS1337::setMonths(unsigned char v) +{ + rtc_bcd[DS1337_MTH] = bin2bcd(v); + +} +void DS1337::setYears(unsigned int v) +{ + if (v>1999) + { + v -= 2000; + } + rtc_bcd[DS1337_YR] = bin2bcd(v); + +} + +void DS1337::setTime() +{ + char set_[8]; + set_[0] = 0x00; + for(int i=0; i<7; i++) + { + set_[i+1] = rtc_bcd[i]; + } + i2c.write(DS1337_CTRL_ID, set_, 8); +} + +byte DS1337::bcd2bin(byte v) +{ + return (v&0x0F) + ((v>>4)*10); +} + +byte DS1337::bin2bcd(byte v) +{ + return ((v / 10)<<4) + (v % 10); +} + +void DS1337::stop(void) +{ + setRegister(DS1337_SP, getRegister(DS1337_SP) | DS1337_SP_EOSC); +} + +void DS1337::start(void) +{ + setRegister(DS1337_SP, getRegister(DS1337_SP) & !DS1337_SP_EOSC); +}
diff -r 000000000000 -r f4f9b627adf9 DS1337.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1337.h Tue Apr 01 07:01:08 2014 +0000 @@ -0,0 +1,136 @@ +/* + DS1337.h - library for DS1337 rtc +*/ + +// ensure this library description is only included once +#ifndef DS1337_h +#define DS1337_h + +// include types & constants of Wiring core API +#include "mbed.h" + +typedef int8_t byte; + +// indices within the rtc_bcd[] buffer +#define DS1337_SEC 0 +#define DS1337_MIN 1 +#define DS1337_HR 2 +#define DS1337_DOW 3 +#define DS1337_DATE 4 +#define DS1337_MTH 5 +#define DS1337_YR 6 + +#define DS1337_BASE_YR 2000 + +#define DS1337_CTRL_ID 0xD0 + + + +// Define register bit masks +#define DS1337_CLOCKHALT (1<<7) + +#define DS1337_LO_BCD 0xf +#define DS1337_HI_BCD 0xf0 + +#define DS1337_HI_SEC 0x70 +#define DS1337_HI_MIN 0x70 +#define DS1337_HI_HR 0x30 +#define DS1337_LO_DOW 0x07 +#define DS1337_HI_DATE 0x30 +#define DS1337_HI_MTH 0x30 +#define DS1337_HI_YR 0xf0 + +#define DS1337_ARLM1 0x07 +#define DS1337_ARLM1_LO_SEC 0xf +#define DS1337_ARLM1_HI_SEC 0x70 +#define DS1337_ARLM1_LO_MIN 0x70 +#define DS1337_ARLM1_HI_MIN 0xf + +#define DS1337_SP 0x0E +#define DS1337_SP_EOSC (1<<7) +#define DS1337_SP_RS2 (1<<4) +#define DS1337_SP_RS1 (1<<3) +#define DS1337_SP_INTCN (1<<2) +#define DS1337_SP_A2IE (1<<1) +#define DS1337_SP_A1IE (1<<0) + +#define DS1337_STATUS 0x0F +#define DS1337_STATUS_OSF (1<<7) +#define DS1337_STATUS_A2F (1<<1) +#define DS1337_STATUS_A1F (1<<0) + +/* Definitions for alarm repeat */ +/* The private variable alarm_repeat holds the user's alarm repeat preference. However, the DS1337 encodes these in the topmost bit(s) of the 4 alarm registers. */ +/* Splattering these bits across the alarm regs is handled in the writeAlarm() function. */ +/* If DY/DT is set, the day field is interpreted as a DayOfWeek (1 ~ 7), else it is interpreted as a DayOfMonth.*/ + +/* user alarm_repeat bit mask: + 7 6 5 4 3 2 1 0 + [x x x A1M4 DY/DT A1M3 A1M2 A1M1] +*/ + +#define EVERY_SECOND B00010111 +#define EVERY_MINUTE B00010110 +#define EVERY_HOUR B00010100 +#define EVERY_DAY B00010000 +#define EVERY_WEEK B00001000 +#define EVERY_MONTH B00000000 + + +/* typedef struct { + unsigned int year; + unsigned char month; + unsigned char day; + unsigned char dayOfWeek; + unsigned char hour; + unsigned char minute; + unsigned char second; +} TIME; */ + + +// library interface description +class DS1337 { + // user-accessible "public" interface +public: + + void readTime(void); + unsigned char getSeconds(); + unsigned char getMinutes(); + unsigned char getHours(); + unsigned char getDays(); + unsigned char getDayOfWeek(); + unsigned char getMonths(); + unsigned int getYears(); + + void setSeconds(unsigned char); + void setMinutes(unsigned char); + void setHours(unsigned char); + void setDays(unsigned char); + void setDayOfWeek(unsigned char); + void setMonths(unsigned char); + void setYears(unsigned int); + + void setTime(); + + + void start(void); + void stop(void); + unsigned char getRegister(unsigned char registerNumber); + void setRegister(unsigned char registerNumber, unsigned char registerValue); + //void unsetRegister(unsigned char registerNumber, unsigned char registerMask); + + // library-accessible "private" interface +private: + + byte time_set; + byte alarm_repeat; + byte rtc_bcd[7]; // used prior to read/set DS1337 registers; + void read(void); + void save(void); + byte bcd2bin(byte); + byte bin2bcd(byte); +}; + +//extern DS1337 RTC; + +#endif