Dallas DS1307 real-time clock minimalistic driver

Dependents:   testing_RTC_OneWire EMIRv2

Committer:
alpov
Date:
Fri Apr 25 18:15:47 2014 +0000
Revision:
0:f29e45a25cba
Child:
1:60383faa35a9
initial version, working getNow() and setNow()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alpov 0:f29e45a25cba 1 /* DS1307.h */
alpov 0:f29e45a25cba 2 /*
alpov 0:f29e45a25cba 3 Copyright (c) 2014 Ales Povalac
alpov 0:f29e45a25cba 4 Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
alpov 0:f29e45a25cba 5
alpov 0:f29e45a25cba 6 Permission is hereby granted, free of charge, to any person obtaining a copy
alpov 0:f29e45a25cba 7 of this software and associated documentation files (the "Software"), to deal
alpov 0:f29e45a25cba 8 in the Software without restriction, including without limitation the rights
alpov 0:f29e45a25cba 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
alpov 0:f29e45a25cba 10 copies of the Software, and to permit persons to whom the Software is
alpov 0:f29e45a25cba 11 furnished to do so, subject to the following conditions:
alpov 0:f29e45a25cba 12
alpov 0:f29e45a25cba 13 The above copyright notice and this permission notice shall be included in
alpov 0:f29e45a25cba 14 all copies or substantial portions of the Software.
alpov 0:f29e45a25cba 15
alpov 0:f29e45a25cba 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
alpov 0:f29e45a25cba 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
alpov 0:f29e45a25cba 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
alpov 0:f29e45a25cba 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
alpov 0:f29e45a25cba 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
alpov 0:f29e45a25cba 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
alpov 0:f29e45a25cba 22 THE SOFTWARE.
alpov 0:f29e45a25cba 23 */
alpov 0:f29e45a25cba 24 #ifndef __DS1307_H__
alpov 0:f29e45a25cba 25 #define __DS1307_H__
alpov 0:f29e45a25cba 26
alpov 0:f29e45a25cba 27
alpov 0:f29e45a25cba 28 class DS1307
alpov 0:f29e45a25cba 29 {
alpov 0:f29e45a25cba 30 protected:
alpov 0:f29e45a25cba 31 I2C* m_rtc;
alpov 0:f29e45a25cba 32 static const char *m_weekDays[];
alpov 0:f29e45a25cba 33
alpov 0:f29e45a25cba 34 public:
alpov 0:f29e45a25cba 35 DS1307(PinName sda, PinName scl);
alpov 0:f29e45a25cba 36 ~DS1307();
alpov 0:f29e45a25cba 37
alpov 0:f29e45a25cba 38 const char* weekdayToString(int wday) {
alpov 0:f29e45a25cba 39 return m_weekDays[wday % 7];
alpov 0:f29e45a25cba 40 }
alpov 0:f29e45a25cba 41
alpov 0:f29e45a25cba 42 time_t getNow();
alpov 0:f29e45a25cba 43 bool setNow(time_t time);
alpov 0:f29e45a25cba 44
alpov 0:f29e45a25cba 45 private:
alpov 0:f29e45a25cba 46 bool read(int address, char* buffer, int len);
alpov 0:f29e45a25cba 47 bool write(int address, char* buffer, int len);
alpov 0:f29e45a25cba 48
alpov 0:f29e45a25cba 49 static int bcdToDecimal(int bcd) {
alpov 0:f29e45a25cba 50 return ((bcd&0xF0)>>4)*10 + (bcd&0x0F);
alpov 0:f29e45a25cba 51 }
alpov 0:f29e45a25cba 52
alpov 0:f29e45a25cba 53 static int decimalToBcd(int dec) {
alpov 0:f29e45a25cba 54 return (dec%10) + ((dec/10)<<4);
alpov 0:f29e45a25cba 55 }
alpov 0:f29e45a25cba 56 };
alpov 0:f29e45a25cba 57
alpov 0:f29e45a25cba 58 #endif // __DS1307_H__