Libreria para trabajar con DS1307 y usar su memoria

Dependents:   Proyect_Patric_electronic_door_MSC_Ok_ESP

Committer:
sherckuith
Date:
Tue Sep 17 01:31:17 2013 +0000
Revision:
0:ab60603c51d1
Arreglos minimos;

Who changed what in which revision?

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