lab 7

Dependencies:   SDFileSystem mbed

Committer:
jedh
Date:
Sat Dec 10 21:08:30 2016 +0000
Revision:
0:f6d3b930f382
jgk

Who changed what in which revision?

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