Dallas DS1307 real-time clock minimalistic driver

Dependents:   testing_RTC_OneWire EMIRv2

Committer:
alpov
Date:
Mon Apr 28 13:08:38 2014 +0000
Revision:
3:c5018c71887f
Parent:
1:60383faa35a9
fixed wrong 12/24-hour config

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alpov 1:60383faa35a9 1 #ifndef _DS1307_H
alpov 1:60383faa35a9 2 #define _DS1307_H
alpov 0:f29e45a25cba 3
alpov 1:60383faa35a9 4 /* Dallas DS1307 real-time clock minimalistic driver
alpov 1:60383faa35a9 5 *
alpov 1:60383faa35a9 6 * Copyright (c) 2014 Ales Povalac, MIT License
alpov 1:60383faa35a9 7 *
alpov 1:60383faa35a9 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
alpov 1:60383faa35a9 9 * and associated documentation files (the "Software"), to deal in the Software without restriction,
alpov 1:60383faa35a9 10 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
alpov 1:60383faa35a9 11 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
alpov 1:60383faa35a9 12 * furnished to do so, subject to the following conditions:
alpov 1:60383faa35a9 13 *
alpov 1:60383faa35a9 14 * The above copyright notice and this permission notice shall be included in all copies or
alpov 1:60383faa35a9 15 * substantial portions of the Software.
alpov 1:60383faa35a9 16 *
alpov 1:60383faa35a9 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
alpov 1:60383faa35a9 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
alpov 1:60383faa35a9 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
alpov 1:60383faa35a9 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
alpov 1:60383faa35a9 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
alpov 1:60383faa35a9 22 */
alpov 0:f29e45a25cba 23
alpov 1:60383faa35a9 24 #define DS1307_ADDR 0xD0 // I2C address
alpov 1:60383faa35a9 25 #define DS1307_FREQ 100000 // bus speed
alpov 0:f29e45a25cba 26
alpov 1:60383faa35a9 27 /** Dallas DS1307 real-time clock minimalistic driver
alpov 1:60383faa35a9 28 */
alpov 0:f29e45a25cba 29 class DS1307
alpov 0:f29e45a25cba 30 {
alpov 0:f29e45a25cba 31 public:
alpov 1:60383faa35a9 32 /** Create DS1307 instance on the specified pins of I2C bus
alpov 1:60383faa35a9 33 */
alpov 0:f29e45a25cba 34 DS1307(PinName sda, PinName scl);
alpov 0:f29e45a25cba 35
alpov 1:60383faa35a9 36 /** Read current real time from DS1307
alpov 1:60383faa35a9 37 *
alpov 1:60383faa35a9 38 * @returns
alpov 1:60383faa35a9 39 * current time on success,
alpov 1:60383faa35a9 40 * 0 on error (I2C fail, clock not set)
alpov 1:60383faa35a9 41 */
alpov 1:60383faa35a9 42 time_t now();
alpov 1:60383faa35a9 43
alpov 1:60383faa35a9 44 /** Write current real time to DS1307
alpov 1:60383faa35a9 45 *
alpov 1:60383faa35a9 46 * @param time Real time to set up
alpov 1:60383faa35a9 47 * @returns
alpov 1:60383faa35a9 48 * true on success,
alpov 1:60383faa35a9 49 * false on error (I2C fail)
alpov 1:60383faa35a9 50 */
alpov 1:60383faa35a9 51 bool set_time(time_t time);
alpov 0:f29e45a25cba 52
alpov 0:f29e45a25cba 53 private:
alpov 1:60383faa35a9 54 I2C ds1307_i2c;
alpov 0:f29e45a25cba 55
alpov 0:f29e45a25cba 56 static int bcdToDecimal(int bcd) {
alpov 1:60383faa35a9 57 return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F);
alpov 0:f29e45a25cba 58 }
alpov 0:f29e45a25cba 59
alpov 0:f29e45a25cba 60 static int decimalToBcd(int dec) {
alpov 1:60383faa35a9 61 return (dec % 10) + ((dec / 10) << 4);
alpov 0:f29e45a25cba 62 }
alpov 0:f29e45a25cba 63 };
alpov 0:f29e45a25cba 64
alpov 1:60383faa35a9 65 #endif // __DS1307_H