This is a class for using the DS1307 Real Time Clock chip from Dallas Semiconductor. This class uses mbeds i2c class to talk to the chip. I have tested this currently on the LPC1768 mbed device with the Spark fun breakout board from http://www.sparkfun.com/products/99
Dependents: Mbell Astromed_build20121123 Raymon_pub_ver CCRMonitor12_sp07_120ver ... more
ds1307.h
00001 #ifndef DS1307_H 00002 #define DS1307_H 00003 00004 /* mbed Dallas Semiconductor DS1307 serial real time clock 00005 * Copyright (c) 2012 pksmith 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a copy 00008 * of this software and associated documentation files (the "Software"), to deal 00009 * in the Software without restriction, including without limitation the rights 00010 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 * copies of the Software, and to permit persons to whom the Software is 00012 * furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included in 00015 * all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00023 * THE SOFTWARE. 00024 */ 00025 00026 #include "mbed.h" 00027 00028 #define DS1307_addr 0xD0 // this is fixed by Dallas 00029 #define DS1307_freq 100000 // this is the Dallas spec for operating i2c for this device 00030 #define DS1307_sec 0x00 // seconds 00031 #define DS1307_min 0x01 // min 00032 #define DS1307_hour 0x02 // hours 00033 #define DS1307_day 0x03 // day 00034 #define DS1307_date 0x04 // date 00035 #define DS1307_month 0x05 // month 00036 #define DS1307_year 0x06 // year 00037 #define DS1307_sqrout 0x07 // square output register 00038 #define DS1307_ramstart 0x08 // register address that ram starts at 00039 #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) 00040 #define DS1307_lastram 0x3E // last usable ram by this class as the lastreg is clobbered by code for normal operation 00041 00042 /** DS1307 control and communication class using mbed's i2c class 00043 * 00044 * Example: 00045 * @code 00046 * // show how the DS1307 class works 00047 * #include "ds1307.h" 00048 * #include "mbed.h" 00049 * 00050 * Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications 00051 * 00052 * DS1307 my1307(p9,p10); // start DS1307 class and give it pins for connections of the DS1307 device 00053 * 00054 * int sec = 0; 00055 * int min = 0; 00056 * int hours = 0; 00057 * int day = 0; 00058 * int date = 0; 00059 * int month = 0; 00060 * int year = 0; 00061 * 00062 * void test_rw(int test) { 00063 * if (test == 0) pc.printf("Last R/W operaion passed!\n\r"); 00064 * else pc.printf("Last R/W operation failed!\n\r"); 00065 * } 00066 * 00067 * int main() { 00068 * int junk = 0; 00069 * 00070 * sec = 24; // 24 seconds 00071 * min = 13; // 13 min 00072 * hours = 13; // 1 pm 00073 * day = 4; // wednesday 00074 * date = 20; // June 20 00075 * month = 6; 00076 * year = 12; // 2012 00077 * // set time to these values on the ds1307 connected device 00078 * 00079 * test_rw(my1307.settime( sec, min, hours, day, date, month, year)); 00080 * pc.printf("seconds set are %.2D \n\r",sec); 00081 * pc.printf("min set are %.2D \n\r",min); 00082 * pc.printf("hour set are %.2D \n\r",hours); 00083 * pc.printf("day set are %.2D \n\r",day); 00084 * pc.printf("date set are %.2D \n\r",date); 00085 * pc.printf("month set are %.2D \n\r",month); 00086 * pc.printf("year set are %.2D \n\r",year); 00087 * wait(3); 00088 * // now read the time of the DS1307 device and see what time it is 00089 * // note that because of the 3 second wait this time should be 3 seconds past what it was set to earlier 00090 * 00091 * test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year)); 00092 * pc.printf("seconds read are %.2D \n\r",sec); 00093 * pc.printf("min read are %.2D \n\r",min); 00094 * pc.printf("hour read are %.2D \n\r",hours); 00095 * pc.printf("day read are %.2D \n\r",day); 00096 * pc.printf("date read are %.2D \n\r",date); 00097 * pc.printf("month read are %.2D \n\r",month); 00098 * pc.printf("year read are %.2D \n\r",year); 00099 * 00100 * junk = 0x39; // just a junk value do read and write test to DS1307 ram 00101 * test_rw(my1307.write( 0x20, junk)); // this should write the value of junk to register 0x20 (a ram location) in the ds1307. 00102 * pc.printf("Value written to register 0x20 %.2X \n\r",junk); 00103 * junk = 0; // clear junk to show that when the register is read from the correct value is obtained 00104 * test_rw(my1307.read( 0x20, &junk)); // this should read register 0x20 00105 * pc.printf("Value read from register 0x20 %.2X \n\r",junk); 00106 * } 00107 * @endcode 00108 */ 00109 class DS1307 { 00110 public: 00111 /** Create object connected to DS1307 pins ( remember both pins need pull up resisters) 00112 * 00113 * Ensure the pull up resistors are used on these pins. Also note there is no checking on 00114 * if you use thes pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device 00115 * 00116 * @param sda pin that DS1307 connected to (p9 or p28 as defined on LPC1768) 00117 * @param slc pin that DS1307 connected to (p10 or p27 ad defined on LPC1768) 00118 */ 00119 DS1307( PinName sda, PinName slc) ; // constructor 00120 00121 ~DS1307(); // destructor 00122 00123 /** Bulk read of several registers at a time 00124 * 00125 * Ensure the variable data pointer passed to this function has the room needed to recieve the quantity! 00126 * 00127 * @param addr the address to read from 00128 * @param quantity the amount of registers to read from 00129 * @param data the place to put the values read 00130 * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason 00131 */ 00132 int read( int addr, int quantity, char *data); // to read some of the 63 bytes from DS1307 00133 00134 /** Read one register of DS1307 device 00135 * 00136 * @param addr the address to read from 00137 * @param data read from the one register 00138 * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason 00139 */ 00140 int read(int addr, int *data); // to read one byte only 00141 00142 /** Bulk write of several registers at a time 00143 * 00144 * @param addr the address to write to 00145 * @param quantity the amount of registers to write to 00146 * @param data that contains the values to be written to the registers 00147 * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason 00148 */ 00149 int write( int addr, int quantity, char *data); // to write bytes to some of the 63 locations in the DS1307 00150 00151 /** Write one register of DS1307 device 00152 * 00153 * @param addr the address to write to 00154 * @param data to write to register 00155 * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason 00156 */ 00157 int write( int addr, int data ); // to write one byte only 00158 00159 /** Start DS1307 clock 00160 * 00161 * @param returns 0 if clock started 1 if the write command to DS1307 failed for some reason 00162 */ 00163 int start_clock(void); // start the clock 00164 00165 /** Stop DS1307 clock 00166 * 00167 * @param returns 0 if clock stopped 1 if the write command to DS1307 failed for some reason 00168 */ 00169 int stop_clock(void); // stop clock 00170 00171 /** Set twelve hour mode on DS1307 (note this also converts 24 hour time to 12 time if needed on DS1307) 00172 * 00173 * Note this will convert DS1307 time values in registers to 12 hour values from 24 hour values if needed 00174 * 00175 * @param returns 0 if DS1307 is now in 12 hour mode 1 if the command to DS1307 failed for some reason 00176 */ 00177 int twelve_hour(void); // set 12 hour mode 00178 00179 /** Set twenty four hour mode on DS1307 00180 * 00181 * Note this will convert DS1307 time values in registers to 24 hour values from 12 hour values if needed 00182 * 00183 * @param returns 0 if DS1307 is now in 24 hour mode 1 if the command to DS1307 failed for some reason 00184 */ 00185 int twentyfour_hour(void); // set 24 hour mode 00186 00187 /** Set the time to some current or other value ( note that this will start the clock after it is set!) 00188 * 00189 * Note this will return 1 if any of the values passed to this function are not as listed below! 00190 * 00191 * @param sec the seconds value (0 - 59) 00192 * @param min the minute value (0 - 59) 00193 * @param hour the hour value (0 - 23) always in 24 hour 00194 * @param day the day value ( sunday is 1 ) 00195 * @param date the date value (1 - 31) 00196 * @param month the month value (1-12) 00197 * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! 00198 * @param returns 0 if time is set 1 if the time setting failed in some way 00199 */ 00200 int settime(int sec, int min, int hour, int day, int date, int month, int year); // to set the current time and start clock 00201 00202 /** Read the current time of the DS1307 00203 * 00204 * @param sec the seconds value (0 - 59) 00205 * @param min the minute value (0 - 59) 00206 * @param hour the hour value (0 - 23) always in 24 hour 00207 * @param day the day value ( sunday is 1 ) 00208 * @param date the date value (1 - 31) 00209 * @param month the month value (1-12) 00210 * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! 00211 * @param returns 0 if time is read correctly 1 if the time was not recieved correctly for some reason 00212 */ 00213 int gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year); // to get the current time information 00214 00215 00216 protected: 00217 I2C ds1307i2c; 00218 int dectobcd( int ); 00219 int bcdtodec( int ); 00220 int hilow_check( int, int, int); 00221 00222 }; 00223 00224 #endif
Generated on Wed Jul 13 2022 15:31:01 by 1.7.2