working RTC. Only set once then just use get.

Dependents:   finalV1 finalv2 finalv3

Fork of DS1307 by Philip Smith

Committer:
seedteam20
Date:
Wed Apr 22 16:37:51 2015 +0000
Revision:
1:9702e2e4aef9
Parent:
0:c3e4da8feb10
asd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harrypowers 0:c3e4da8feb10 1 #ifndef DS1307_H
harrypowers 0:c3e4da8feb10 2 #define DS1307_H
harrypowers 0:c3e4da8feb10 3
harrypowers 0:c3e4da8feb10 4 /* mbed Dallas Semiconductor DS1307 serial real time clock
harrypowers 0:c3e4da8feb10 5 * Copyright (c) 2012 pksmith
harrypowers 0:c3e4da8feb10 6 *
harrypowers 0:c3e4da8feb10 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
harrypowers 0:c3e4da8feb10 8 * of this software and associated documentation files (the "Software"), to deal
harrypowers 0:c3e4da8feb10 9 * in the Software without restriction, including without limitation the rights
harrypowers 0:c3e4da8feb10 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
harrypowers 0:c3e4da8feb10 11 * copies of the Software, and to permit persons to whom the Software is
harrypowers 0:c3e4da8feb10 12 * furnished to do so, subject to the following conditions:
harrypowers 0:c3e4da8feb10 13 *
harrypowers 0:c3e4da8feb10 14 * The above copyright notice and this permission notice shall be included in
harrypowers 0:c3e4da8feb10 15 * all copies or substantial portions of the Software.
harrypowers 0:c3e4da8feb10 16 *
harrypowers 0:c3e4da8feb10 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
harrypowers 0:c3e4da8feb10 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
harrypowers 0:c3e4da8feb10 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
harrypowers 0:c3e4da8feb10 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
harrypowers 0:c3e4da8feb10 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
harrypowers 0:c3e4da8feb10 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
harrypowers 0:c3e4da8feb10 23 * THE SOFTWARE.
harrypowers 0:c3e4da8feb10 24 */
harrypowers 0:c3e4da8feb10 25
harrypowers 0:c3e4da8feb10 26 #include "mbed.h"
harrypowers 0:c3e4da8feb10 27
harrypowers 0:c3e4da8feb10 28 #define DS1307_addr 0xD0 // this is fixed by Dallas
harrypowers 0:c3e4da8feb10 29 #define DS1307_freq 100000 // this is the Dallas spec for operating i2c for this device
harrypowers 0:c3e4da8feb10 30 #define DS1307_sec 0x00 // seconds
harrypowers 0:c3e4da8feb10 31 #define DS1307_min 0x01 // min
harrypowers 0:c3e4da8feb10 32 #define DS1307_hour 0x02 // hours
harrypowers 0:c3e4da8feb10 33 #define DS1307_day 0x03 // day
harrypowers 0:c3e4da8feb10 34 #define DS1307_date 0x04 // date
harrypowers 0:c3e4da8feb10 35 #define DS1307_month 0x05 // month
harrypowers 0:c3e4da8feb10 36 #define DS1307_year 0x06 // year
harrypowers 0:c3e4da8feb10 37 #define DS1307_sqrout 0x07 // square output register
harrypowers 0:c3e4da8feb10 38 #define DS1307_ramstart 0x08 // register address that ram starts at
harrypowers 0:c3e4da8feb10 39 #define DS1307_lastreg 0x3F // this is the last register in the device (note also this register is used to address everything so it gets clobbered)
harrypowers 0:c3e4da8feb10 40 #define DS1307_lastram 0x3E // last usable ram by this class as the lastreg is clobbered by code for normal operation
harrypowers 0:c3e4da8feb10 41
harrypowers 0:c3e4da8feb10 42 /** DS1307 control and communication class using mbed's i2c class
harrypowers 0:c3e4da8feb10 43 *
harrypowers 0:c3e4da8feb10 44 * Example:
harrypowers 0:c3e4da8feb10 45 * @code
harrypowers 0:c3e4da8feb10 46 * // show how the DS1307 class works
harrypowers 0:c3e4da8feb10 47 * #include "ds1307.h"
harrypowers 0:c3e4da8feb10 48 * #include "mbed.h"
harrypowers 0:c3e4da8feb10 49 *
harrypowers 0:c3e4da8feb10 50 * Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications
harrypowers 0:c3e4da8feb10 51 *
harrypowers 0:c3e4da8feb10 52 * DS1307 my1307(p9,p10); // start DS1307 class and give it pins for connections of the DS1307 device
harrypowers 0:c3e4da8feb10 53 *
harrypowers 0:c3e4da8feb10 54 * int sec = 0;
harrypowers 0:c3e4da8feb10 55 * int min = 0;
harrypowers 0:c3e4da8feb10 56 * int hours = 0;
harrypowers 0:c3e4da8feb10 57 * int day = 0;
harrypowers 0:c3e4da8feb10 58 * int date = 0;
harrypowers 0:c3e4da8feb10 59 * int month = 0;
harrypowers 0:c3e4da8feb10 60 * int year = 0;
harrypowers 0:c3e4da8feb10 61 *
harrypowers 0:c3e4da8feb10 62 * void test_rw(int test) {
harrypowers 0:c3e4da8feb10 63 * if (test == 0) pc.printf("Last R/W operaion passed!\n\r");
harrypowers 0:c3e4da8feb10 64 * else pc.printf("Last R/W operation failed!\n\r");
harrypowers 0:c3e4da8feb10 65 * }
harrypowers 0:c3e4da8feb10 66 *
harrypowers 0:c3e4da8feb10 67 * int main() {
harrypowers 0:c3e4da8feb10 68 * int junk = 0;
harrypowers 0:c3e4da8feb10 69 *
harrypowers 0:c3e4da8feb10 70 * sec = 24; // 24 seconds
harrypowers 0:c3e4da8feb10 71 * min = 13; // 13 min
harrypowers 0:c3e4da8feb10 72 * hours = 13; // 1 pm
harrypowers 0:c3e4da8feb10 73 * day = 4; // wednesday
harrypowers 0:c3e4da8feb10 74 * date = 20; // June 20
harrypowers 0:c3e4da8feb10 75 * month = 6;
harrypowers 0:c3e4da8feb10 76 * year = 12; // 2012
harrypowers 0:c3e4da8feb10 77 * // set time to these values on the ds1307 connected device
harrypowers 0:c3e4da8feb10 78 *
harrypowers 0:c3e4da8feb10 79 * test_rw(my1307.settime( sec, min, hours, day, date, month, year));
harrypowers 0:c3e4da8feb10 80 * pc.printf("seconds set are %.2D \n\r",sec);
harrypowers 0:c3e4da8feb10 81 * pc.printf("min set are %.2D \n\r",min);
harrypowers 0:c3e4da8feb10 82 * pc.printf("hour set are %.2D \n\r",hours);
harrypowers 0:c3e4da8feb10 83 * pc.printf("day set are %.2D \n\r",day);
harrypowers 0:c3e4da8feb10 84 * pc.printf("date set are %.2D \n\r",date);
harrypowers 0:c3e4da8feb10 85 * pc.printf("month set are %.2D \n\r",month);
harrypowers 0:c3e4da8feb10 86 * pc.printf("year set are %.2D \n\r",year);
harrypowers 0:c3e4da8feb10 87 * wait(3);
harrypowers 0:c3e4da8feb10 88 * // now read the time of the DS1307 device and see what time it is
harrypowers 0:c3e4da8feb10 89 * // note that because of the 3 second wait this time should be 3 seconds past what it was set to earlier
harrypowers 0:c3e4da8feb10 90 *
harrypowers 0:c3e4da8feb10 91 * test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year));
harrypowers 0:c3e4da8feb10 92 * pc.printf("seconds read are %.2D \n\r",sec);
harrypowers 0:c3e4da8feb10 93 * pc.printf("min read are %.2D \n\r",min);
harrypowers 0:c3e4da8feb10 94 * pc.printf("hour read are %.2D \n\r",hours);
harrypowers 0:c3e4da8feb10 95 * pc.printf("day read are %.2D \n\r",day);
harrypowers 0:c3e4da8feb10 96 * pc.printf("date read are %.2D \n\r",date);
harrypowers 0:c3e4da8feb10 97 * pc.printf("month read are %.2D \n\r",month);
harrypowers 0:c3e4da8feb10 98 * pc.printf("year read are %.2D \n\r",year);
harrypowers 0:c3e4da8feb10 99 *
harrypowers 0:c3e4da8feb10 100 * junk = 0x39; // just a junk value do read and write test to DS1307 ram
harrypowers 0:c3e4da8feb10 101 * test_rw(my1307.write( 0x20, junk)); // this should write the value of junk to register 0x20 (a ram location) in the ds1307.
harrypowers 0:c3e4da8feb10 102 * pc.printf("Value written to register 0x20 %.2X \n\r",junk);
harrypowers 0:c3e4da8feb10 103 * junk = 0; // clear junk to show that when the register is read from the correct value is obtained
harrypowers 0:c3e4da8feb10 104 * test_rw(my1307.read( 0x20, &junk)); // this should read register 0x20
harrypowers 0:c3e4da8feb10 105 * pc.printf("Value read from register 0x20 %.2X \n\r",junk);
harrypowers 0:c3e4da8feb10 106 * }
harrypowers 0:c3e4da8feb10 107 * @endcode
harrypowers 0:c3e4da8feb10 108 */
harrypowers 0:c3e4da8feb10 109 class DS1307 {
harrypowers 0:c3e4da8feb10 110 public:
harrypowers 0:c3e4da8feb10 111 /** Create object connected to DS1307 pins ( remember both pins need pull up resisters)
harrypowers 0:c3e4da8feb10 112 *
harrypowers 0:c3e4da8feb10 113 * Ensure the pull up resistors are used on these pins. Also note there is no checking on
harrypowers 0:c3e4da8feb10 114 * if you use thes pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device
harrypowers 0:c3e4da8feb10 115 *
harrypowers 0:c3e4da8feb10 116 * @param sda pin that DS1307 connected to (p9 or p28 as defined on LPC1768)
harrypowers 0:c3e4da8feb10 117 * @param slc pin that DS1307 connected to (p10 or p27 ad defined on LPC1768)
harrypowers 0:c3e4da8feb10 118 */
harrypowers 0:c3e4da8feb10 119 DS1307( PinName sda, PinName slc) ; // constructor
harrypowers 0:c3e4da8feb10 120
harrypowers 0:c3e4da8feb10 121 ~DS1307(); // destructor
harrypowers 0:c3e4da8feb10 122
harrypowers 0:c3e4da8feb10 123 /** Bulk read of several registers at a time
harrypowers 0:c3e4da8feb10 124 *
harrypowers 0:c3e4da8feb10 125 * Ensure the variable data pointer passed to this function has the room needed to recieve the quantity!
harrypowers 0:c3e4da8feb10 126 *
harrypowers 0:c3e4da8feb10 127 * @param addr the address to read from
harrypowers 0:c3e4da8feb10 128 * @param quantity the amount of registers to read from
harrypowers 0:c3e4da8feb10 129 * @param data the place to put the values read
harrypowers 0:c3e4da8feb10 130 * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 131 */
harrypowers 0:c3e4da8feb10 132 int read( int addr, int quantity, char *data); // to read some of the 63 bytes from DS1307
harrypowers 0:c3e4da8feb10 133
harrypowers 0:c3e4da8feb10 134 /** Read one register of DS1307 device
harrypowers 0:c3e4da8feb10 135 *
harrypowers 0:c3e4da8feb10 136 * @param addr the address to read from
harrypowers 0:c3e4da8feb10 137 * @param data read from the one register
harrypowers 0:c3e4da8feb10 138 * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 139 */
harrypowers 0:c3e4da8feb10 140 int read(int addr, int *data); // to read one byte only
harrypowers 0:c3e4da8feb10 141
harrypowers 0:c3e4da8feb10 142 /** Bulk write of several registers at a time
harrypowers 0:c3e4da8feb10 143 *
harrypowers 0:c3e4da8feb10 144 * @param addr the address to write to
harrypowers 0:c3e4da8feb10 145 * @param quantity the amount of registers to write to
harrypowers 0:c3e4da8feb10 146 * @param data that contains the values to be written to the registers
harrypowers 0:c3e4da8feb10 147 * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 148 */
harrypowers 0:c3e4da8feb10 149 int write( int addr, int quantity, char *data); // to write bytes to some of the 63 locations in the DS1307
harrypowers 0:c3e4da8feb10 150
harrypowers 0:c3e4da8feb10 151 /** Write one register of DS1307 device
harrypowers 0:c3e4da8feb10 152 *
harrypowers 0:c3e4da8feb10 153 * @param addr the address to write to
harrypowers 0:c3e4da8feb10 154 * @param data to write to register
harrypowers 0:c3e4da8feb10 155 * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 156 */
harrypowers 0:c3e4da8feb10 157 int write( int addr, int data ); // to write one byte only
harrypowers 0:c3e4da8feb10 158
harrypowers 0:c3e4da8feb10 159 /** Start DS1307 clock
harrypowers 0:c3e4da8feb10 160 *
harrypowers 0:c3e4da8feb10 161 * @param returns 0 if clock started 1 if the write command to DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 162 */
harrypowers 0:c3e4da8feb10 163 int start_clock(void); // start the clock
harrypowers 0:c3e4da8feb10 164
harrypowers 0:c3e4da8feb10 165 /** Stop DS1307 clock
harrypowers 0:c3e4da8feb10 166 *
harrypowers 0:c3e4da8feb10 167 * @param returns 0 if clock stopped 1 if the write command to DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 168 */
harrypowers 0:c3e4da8feb10 169 int stop_clock(void); // stop clock
harrypowers 0:c3e4da8feb10 170
harrypowers 0:c3e4da8feb10 171 /** Set twelve hour mode on DS1307 (note this also converts 24 hour time to 12 time if needed on DS1307)
harrypowers 0:c3e4da8feb10 172 *
harrypowers 0:c3e4da8feb10 173 * Note this will convert DS1307 time values in registers to 12 hour values from 24 hour values if needed
harrypowers 0:c3e4da8feb10 174 *
harrypowers 0:c3e4da8feb10 175 * @param returns 0 if DS1307 is now in 12 hour mode 1 if the command to DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 176 */
harrypowers 0:c3e4da8feb10 177 int twelve_hour(void); // set 12 hour mode
harrypowers 0:c3e4da8feb10 178
harrypowers 0:c3e4da8feb10 179 /** Set twenty four hour mode on DS1307
harrypowers 0:c3e4da8feb10 180 *
harrypowers 0:c3e4da8feb10 181 * Note this will convert DS1307 time values in registers to 24 hour values from 12 hour values if needed
harrypowers 0:c3e4da8feb10 182 *
harrypowers 0:c3e4da8feb10 183 * @param returns 0 if DS1307 is now in 24 hour mode 1 if the command to DS1307 failed for some reason
harrypowers 0:c3e4da8feb10 184 */
harrypowers 0:c3e4da8feb10 185 int twentyfour_hour(void); // set 24 hour mode
harrypowers 0:c3e4da8feb10 186
harrypowers 0:c3e4da8feb10 187 /** Set the time to some current or other value ( note that this will start the clock after it is set!)
harrypowers 0:c3e4da8feb10 188 *
harrypowers 0:c3e4da8feb10 189 * Note this will return 1 if any of the values passed to this function are not as listed below!
harrypowers 0:c3e4da8feb10 190 *
harrypowers 0:c3e4da8feb10 191 * @param sec the seconds value (0 - 59)
harrypowers 0:c3e4da8feb10 192 * @param min the minute value (0 - 59)
harrypowers 0:c3e4da8feb10 193 * @param hour the hour value (0 - 23) always in 24 hour
harrypowers 0:c3e4da8feb10 194 * @param day the day value ( sunday is 1 )
harrypowers 0:c3e4da8feb10 195 * @param date the date value (1 - 31)
harrypowers 0:c3e4da8feb10 196 * @param month the month value (1-12)
harrypowers 0:c3e4da8feb10 197 * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it!
harrypowers 0:c3e4da8feb10 198 * @param returns 0 if time is set 1 if the time setting failed in some way
harrypowers 0:c3e4da8feb10 199 */
harrypowers 0:c3e4da8feb10 200 int settime(int sec, int min, int hour, int day, int date, int month, int year); // to set the current time and start clock
harrypowers 0:c3e4da8feb10 201
harrypowers 0:c3e4da8feb10 202 /** Read the current time of the DS1307
harrypowers 0:c3e4da8feb10 203 *
harrypowers 0:c3e4da8feb10 204 * @param sec the seconds value (0 - 59)
harrypowers 0:c3e4da8feb10 205 * @param min the minute value (0 - 59)
harrypowers 0:c3e4da8feb10 206 * @param hour the hour value (0 - 23) always in 24 hour
harrypowers 0:c3e4da8feb10 207 * @param day the day value ( sunday is 1 )
harrypowers 0:c3e4da8feb10 208 * @param date the date value (1 - 31)
harrypowers 0:c3e4da8feb10 209 * @param month the month value (1-12)
harrypowers 0:c3e4da8feb10 210 * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it!
harrypowers 0:c3e4da8feb10 211 * @param returns 0 if time is read correctly 1 if the time was not recieved correctly for some reason
harrypowers 0:c3e4da8feb10 212 */
harrypowers 0:c3e4da8feb10 213 int gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year); // to get the current time information
harrypowers 0:c3e4da8feb10 214
harrypowers 0:c3e4da8feb10 215
harrypowers 0:c3e4da8feb10 216 protected:
harrypowers 0:c3e4da8feb10 217 I2C ds1307i2c;
harrypowers 0:c3e4da8feb10 218 int dectobcd( int );
harrypowers 0:c3e4da8feb10 219 int bcdtodec( int );
harrypowers 0:c3e4da8feb10 220 int hilow_check( int, int, int);
harrypowers 0:c3e4da8feb10 221
harrypowers 0:c3e4da8feb10 222 };
harrypowers 0:c3e4da8feb10 223
harrypowers 0:c3e4da8feb10 224 #endif