Demo Clock with Nucleo-F303RE and Starter Shield
Dependents: Nucleo_StarterShileld_Clock
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 class DS1307 { 00043 public: 00044 /** Create object connected to DS1307 pins ( remember both pins need pull up resisters) 00045 * 00046 * Ensure the pull up resistors are used on these pins. Also note there is no checking on 00047 * if you use thes pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device 00048 * 00049 * @param sda pin that DS1307 connected to (p9 or p28 as defined on LPC1768) 00050 * @param slc pin that DS1307 connected to (p10 or p27 ad defined on LPC1768) 00051 */ 00052 DS1307( PinName sda, PinName slc) ; // constructor 00053 00054 ~DS1307(); // destructor 00055 00056 /** Bulk read of several registers at a time 00057 * 00058 * Ensure the variable data pointer passed to this function has the room needed to recieve the quantity! 00059 * 00060 * @param addr the address to read from 00061 * @param quantity the amount of registers to read from 00062 * @param data the place to put the values read 00063 * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason 00064 */ 00065 int read( int addr, int quantity, char *data); // to read some of the 63 bytes from DS1307 00066 00067 /** Read one register of DS1307 device 00068 * 00069 * @param addr the address to read from 00070 * @param data read from the one register 00071 * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason 00072 */ 00073 int read(int addr, int *data); // to read one byte only 00074 00075 /** Bulk write of several registers at a time 00076 * 00077 * @param addr the address to write to 00078 * @param quantity the amount of registers to write to 00079 * @param data that contains the values to be written to the registers 00080 * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason 00081 */ 00082 int write( int addr, int quantity, char *data); // to write bytes to some of the 63 locations in the DS1307 00083 00084 /** Write one register of DS1307 device 00085 * 00086 * @param addr the address to write to 00087 * @param data to write to register 00088 * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason 00089 */ 00090 int write( int addr, int data ); // to write one byte only 00091 00092 /** Start DS1307 clock 00093 * 00094 * @param returns 0 if clock started 1 if the write command to DS1307 failed for some reason 00095 */ 00096 int start_clock(void); // start the clock 00097 00098 /** Stop DS1307 clock 00099 * 00100 * @param returns 0 if clock stopped 1 if the write command to DS1307 failed for some reason 00101 */ 00102 int stop_clock(void); // stop clock 00103 00104 /** Set twelve hour mode on DS1307 (note this also converts 24 hour time to 12 time if needed on DS1307) 00105 * 00106 * Note this will convert DS1307 time values in registers to 12 hour values from 24 hour values if needed 00107 * 00108 * @param returns 0 if DS1307 is now in 12 hour mode 1 if the command to DS1307 failed for some reason 00109 */ 00110 int twelve_hour(void); // set 12 hour mode 00111 00112 /** Set twenty four hour mode on DS1307 00113 * 00114 * Note this will convert DS1307 time values in registers to 24 hour values from 12 hour values if needed 00115 * 00116 * @param returns 0 if DS1307 is now in 24 hour mode 1 if the command to DS1307 failed for some reason 00117 */ 00118 int twentyfour_hour(void); // set 24 hour mode 00119 00120 /** Set the time to some current or other value ( note that this will start the clock after it is set!) 00121 * 00122 * Note this will return 1 if any of the values passed to this function are not as listed below! 00123 * 00124 * @param sec the seconds value (0 - 59) 00125 * @param min the minute value (0 - 59) 00126 * @param hour the hour value (0 - 23) always in 24 hour 00127 * @param day the day value ( sunday is 1 ) 00128 * @param date the date value (1 - 31) 00129 * @param month the month value (1-12) 00130 * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! 00131 * @param returns 0 if time is set 1 if the time setting failed in some way 00132 */ 00133 int settime(int sec, int min, int hour, int day, int date, int month, int year); // to set the current time and start clock 00134 00135 /** Read the current time of the DS1307 00136 * 00137 * @param sec the seconds value (0 - 59) 00138 * @param min the minute value (0 - 59) 00139 * @param hour the hour value (0 - 23) always in 24 hour 00140 * @param day the day value ( sunday is 1 ) 00141 * @param date the date value (1 - 31) 00142 * @param month the month value (1-12) 00143 * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! 00144 * @param returns 0 if time is read correctly 1 if the time was not recieved correctly for some reason 00145 */ 00146 int gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year); // to get the current time information 00147 00148 00149 protected: 00150 I2C ds1307i2c; 00151 int dectobcd( int ); 00152 int bcdtodec( int ); 00153 int hilow_check( int, int, int); 00154 00155 }; 00156 00157 #endif
Generated on Fri Sep 9 2022 10:37:17 by
1.7.2