Demo Clock with Nucleo-F303RE and Starter Shield
Dependents: Nucleo_StarterShileld_Clock
ds1307.h@0:a60e31060229, 2017-05-18 (annotated)
- Committer:
- rogerzuber
- Date:
- Thu May 18 08:03:02 2017 +0000
- Revision:
- 0:a60e31060229
- Child:
- 1:70224be5d68c
Demo Clock with Nucleo-F303RE and Starter Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rogerzuber | 0:a60e31060229 | 1 | #ifndef DS1307_H |
rogerzuber | 0:a60e31060229 | 2 | #define DS1307_H |
rogerzuber | 0:a60e31060229 | 3 | |
rogerzuber | 0:a60e31060229 | 4 | /* mbed Dallas Semiconductor DS1307 serial real time clock |
rogerzuber | 0:a60e31060229 | 5 | * Copyright (c) 2012 pksmith |
rogerzuber | 0:a60e31060229 | 6 | * |
rogerzuber | 0:a60e31060229 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
rogerzuber | 0:a60e31060229 | 8 | * of this software and associated documentation files (the "Software"), to deal |
rogerzuber | 0:a60e31060229 | 9 | * in the Software without restriction, including without limitation the rights |
rogerzuber | 0:a60e31060229 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
rogerzuber | 0:a60e31060229 | 11 | * copies of the Software, and to permit persons to whom the Software is |
rogerzuber | 0:a60e31060229 | 12 | * furnished to do so, subject to the following conditions: |
rogerzuber | 0:a60e31060229 | 13 | * |
rogerzuber | 0:a60e31060229 | 14 | * The above copyright notice and this permission notice shall be included in |
rogerzuber | 0:a60e31060229 | 15 | * all copies or substantial portions of the Software. |
rogerzuber | 0:a60e31060229 | 16 | * |
rogerzuber | 0:a60e31060229 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
rogerzuber | 0:a60e31060229 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
rogerzuber | 0:a60e31060229 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
rogerzuber | 0:a60e31060229 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
rogerzuber | 0:a60e31060229 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
rogerzuber | 0:a60e31060229 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
rogerzuber | 0:a60e31060229 | 23 | * THE SOFTWARE. |
rogerzuber | 0:a60e31060229 | 24 | */ |
rogerzuber | 0:a60e31060229 | 25 | |
rogerzuber | 0:a60e31060229 | 26 | #include "mbed.h" |
rogerzuber | 0:a60e31060229 | 27 | |
rogerzuber | 0:a60e31060229 | 28 | #define DS1307_addr 0xD0 // this is fixed by Dallas |
rogerzuber | 0:a60e31060229 | 29 | #define DS1307_freq 100000 // this is the Dallas spec for operating i2c for this device |
rogerzuber | 0:a60e31060229 | 30 | #define DS1307_sec 0x00 // seconds |
rogerzuber | 0:a60e31060229 | 31 | #define DS1307_min 0x01 // min |
rogerzuber | 0:a60e31060229 | 32 | #define DS1307_hour 0x02 // hours |
rogerzuber | 0:a60e31060229 | 33 | #define DS1307_day 0x03 // day |
rogerzuber | 0:a60e31060229 | 34 | #define DS1307_date 0x04 // date |
rogerzuber | 0:a60e31060229 | 35 | #define DS1307_month 0x05 // month |
rogerzuber | 0:a60e31060229 | 36 | #define DS1307_year 0x06 // year |
rogerzuber | 0:a60e31060229 | 37 | #define DS1307_sqrout 0x07 // square output register |
rogerzuber | 0:a60e31060229 | 38 | #define DS1307_ramstart 0x08 // register address that ram starts at |
rogerzuber | 0:a60e31060229 | 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) |
rogerzuber | 0:a60e31060229 | 40 | #define DS1307_lastram 0x3E // last usable ram by this class as the lastreg is clobbered by code for normal operation |
rogerzuber | 0:a60e31060229 | 41 | |
rogerzuber | 0:a60e31060229 | 42 | class DS1307 { |
rogerzuber | 0:a60e31060229 | 43 | public: |
rogerzuber | 0:a60e31060229 | 44 | /** Create object connected to DS1307 pins ( remember both pins need pull up resisters) |
rogerzuber | 0:a60e31060229 | 45 | * |
rogerzuber | 0:a60e31060229 | 46 | * Ensure the pull up resistors are used on these pins. Also note there is no checking on |
rogerzuber | 0:a60e31060229 | 47 | * if you use thes pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device |
rogerzuber | 0:a60e31060229 | 48 | * |
rogerzuber | 0:a60e31060229 | 49 | * @param sda pin that DS1307 connected to (p9 or p28 as defined on LPC1768) |
rogerzuber | 0:a60e31060229 | 50 | * @param slc pin that DS1307 connected to (p10 or p27 ad defined on LPC1768) |
rogerzuber | 0:a60e31060229 | 51 | */ |
rogerzuber | 0:a60e31060229 | 52 | DS1307( PinName sda, PinName slc) ; // constructor |
rogerzuber | 0:a60e31060229 | 53 | |
rogerzuber | 0:a60e31060229 | 54 | ~DS1307(); // destructor |
rogerzuber | 0:a60e31060229 | 55 | |
rogerzuber | 0:a60e31060229 | 56 | /** Bulk read of several registers at a time |
rogerzuber | 0:a60e31060229 | 57 | * |
rogerzuber | 0:a60e31060229 | 58 | * Ensure the variable data pointer passed to this function has the room needed to recieve the quantity! |
rogerzuber | 0:a60e31060229 | 59 | * |
rogerzuber | 0:a60e31060229 | 60 | * @param addr the address to read from |
rogerzuber | 0:a60e31060229 | 61 | * @param quantity the amount of registers to read from |
rogerzuber | 0:a60e31060229 | 62 | * @param data the place to put the values read |
rogerzuber | 0:a60e31060229 | 63 | * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 64 | */ |
rogerzuber | 0:a60e31060229 | 65 | int read( int addr, int quantity, char *data); // to read some of the 63 bytes from DS1307 |
rogerzuber | 0:a60e31060229 | 66 | |
rogerzuber | 0:a60e31060229 | 67 | /** Read one register of DS1307 device |
rogerzuber | 0:a60e31060229 | 68 | * |
rogerzuber | 0:a60e31060229 | 69 | * @param addr the address to read from |
rogerzuber | 0:a60e31060229 | 70 | * @param data read from the one register |
rogerzuber | 0:a60e31060229 | 71 | * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 72 | */ |
rogerzuber | 0:a60e31060229 | 73 | int read(int addr, int *data); // to read one byte only |
rogerzuber | 0:a60e31060229 | 74 | |
rogerzuber | 0:a60e31060229 | 75 | /** Bulk write of several registers at a time |
rogerzuber | 0:a60e31060229 | 76 | * |
rogerzuber | 0:a60e31060229 | 77 | * @param addr the address to write to |
rogerzuber | 0:a60e31060229 | 78 | * @param quantity the amount of registers to write to |
rogerzuber | 0:a60e31060229 | 79 | * @param data that contains the values to be written to the registers |
rogerzuber | 0:a60e31060229 | 80 | * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 81 | */ |
rogerzuber | 0:a60e31060229 | 82 | int write( int addr, int quantity, char *data); // to write bytes to some of the 63 locations in the DS1307 |
rogerzuber | 0:a60e31060229 | 83 | |
rogerzuber | 0:a60e31060229 | 84 | /** Write one register of DS1307 device |
rogerzuber | 0:a60e31060229 | 85 | * |
rogerzuber | 0:a60e31060229 | 86 | * @param addr the address to write to |
rogerzuber | 0:a60e31060229 | 87 | * @param data to write to register |
rogerzuber | 0:a60e31060229 | 88 | * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 89 | */ |
rogerzuber | 0:a60e31060229 | 90 | int write( int addr, int data ); // to write one byte only |
rogerzuber | 0:a60e31060229 | 91 | |
rogerzuber | 0:a60e31060229 | 92 | /** Start DS1307 clock |
rogerzuber | 0:a60e31060229 | 93 | * |
rogerzuber | 0:a60e31060229 | 94 | * @param returns 0 if clock started 1 if the write command to DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 95 | */ |
rogerzuber | 0:a60e31060229 | 96 | int start_clock(void); // start the clock |
rogerzuber | 0:a60e31060229 | 97 | |
rogerzuber | 0:a60e31060229 | 98 | /** Stop DS1307 clock |
rogerzuber | 0:a60e31060229 | 99 | * |
rogerzuber | 0:a60e31060229 | 100 | * @param returns 0 if clock stopped 1 if the write command to DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 101 | */ |
rogerzuber | 0:a60e31060229 | 102 | int stop_clock(void); // stop clock |
rogerzuber | 0:a60e31060229 | 103 | |
rogerzuber | 0:a60e31060229 | 104 | /** Set twelve hour mode on DS1307 (note this also converts 24 hour time to 12 time if needed on DS1307) |
rogerzuber | 0:a60e31060229 | 105 | * |
rogerzuber | 0:a60e31060229 | 106 | * Note this will convert DS1307 time values in registers to 12 hour values from 24 hour values if needed |
rogerzuber | 0:a60e31060229 | 107 | * |
rogerzuber | 0:a60e31060229 | 108 | * @param returns 0 if DS1307 is now in 12 hour mode 1 if the command to DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 109 | */ |
rogerzuber | 0:a60e31060229 | 110 | int twelve_hour(void); // set 12 hour mode |
rogerzuber | 0:a60e31060229 | 111 | |
rogerzuber | 0:a60e31060229 | 112 | /** Set twenty four hour mode on DS1307 |
rogerzuber | 0:a60e31060229 | 113 | * |
rogerzuber | 0:a60e31060229 | 114 | * Note this will convert DS1307 time values in registers to 24 hour values from 12 hour values if needed |
rogerzuber | 0:a60e31060229 | 115 | * |
rogerzuber | 0:a60e31060229 | 116 | * @param returns 0 if DS1307 is now in 24 hour mode 1 if the command to DS1307 failed for some reason |
rogerzuber | 0:a60e31060229 | 117 | */ |
rogerzuber | 0:a60e31060229 | 118 | int twentyfour_hour(void); // set 24 hour mode |
rogerzuber | 0:a60e31060229 | 119 | |
rogerzuber | 0:a60e31060229 | 120 | /** Set the time to some current or other value ( note that this will start the clock after it is set!) |
rogerzuber | 0:a60e31060229 | 121 | * |
rogerzuber | 0:a60e31060229 | 122 | * Note this will return 1 if any of the values passed to this function are not as listed below! |
rogerzuber | 0:a60e31060229 | 123 | * |
rogerzuber | 0:a60e31060229 | 124 | * @param sec the seconds value (0 - 59) |
rogerzuber | 0:a60e31060229 | 125 | * @param min the minute value (0 - 59) |
rogerzuber | 0:a60e31060229 | 126 | * @param hour the hour value (0 - 23) always in 24 hour |
rogerzuber | 0:a60e31060229 | 127 | * @param day the day value ( sunday is 1 ) |
rogerzuber | 0:a60e31060229 | 128 | * @param date the date value (1 - 31) |
rogerzuber | 0:a60e31060229 | 129 | * @param month the month value (1-12) |
rogerzuber | 0:a60e31060229 | 130 | * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! |
rogerzuber | 0:a60e31060229 | 131 | * @param returns 0 if time is set 1 if the time setting failed in some way |
rogerzuber | 0:a60e31060229 | 132 | */ |
rogerzuber | 0:a60e31060229 | 133 | int settime(int sec, int min, int hour, int day, int date, int month, int year); // to set the current time and start clock |
rogerzuber | 0:a60e31060229 | 134 | |
rogerzuber | 0:a60e31060229 | 135 | /** Read the current time of the DS1307 |
rogerzuber | 0:a60e31060229 | 136 | * |
rogerzuber | 0:a60e31060229 | 137 | * @param sec the seconds value (0 - 59) |
rogerzuber | 0:a60e31060229 | 138 | * @param min the minute value (0 - 59) |
rogerzuber | 0:a60e31060229 | 139 | * @param hour the hour value (0 - 23) always in 24 hour |
rogerzuber | 0:a60e31060229 | 140 | * @param day the day value ( sunday is 1 ) |
rogerzuber | 0:a60e31060229 | 141 | * @param date the date value (1 - 31) |
rogerzuber | 0:a60e31060229 | 142 | * @param month the month value (1-12) |
rogerzuber | 0:a60e31060229 | 143 | * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! |
rogerzuber | 0:a60e31060229 | 144 | * @param returns 0 if time is read correctly 1 if the time was not recieved correctly for some reason |
rogerzuber | 0:a60e31060229 | 145 | */ |
rogerzuber | 0:a60e31060229 | 146 | int gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year); // to get the current time information |
rogerzuber | 0:a60e31060229 | 147 | |
rogerzuber | 0:a60e31060229 | 148 | |
rogerzuber | 0:a60e31060229 | 149 | protected: |
rogerzuber | 0:a60e31060229 | 150 | I2C ds1307i2c; |
rogerzuber | 0:a60e31060229 | 151 | int dectobcd( int ); |
rogerzuber | 0:a60e31060229 | 152 | int bcdtodec( int ); |
rogerzuber | 0:a60e31060229 | 153 | int hilow_check( int, int, int); |
rogerzuber | 0:a60e31060229 | 154 | |
rogerzuber | 0:a60e31060229 | 155 | }; |
rogerzuber | 0:a60e31060229 | 156 | |
rogerzuber | 0:a60e31060229 | 157 | #endif |