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.cpp@0:c3e4da8feb10, 2012-06-24 (annotated)
- Committer:
- harrypowers
- Date:
- Sun Jun 24 22:09:35 2012 +0000
- Revision:
- 0:c3e4da8feb10
Changes to documentation only for auto generating
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harrypowers | 0:c3e4da8feb10 | 1 | #include "ds1307.h" |
harrypowers | 0:c3e4da8feb10 | 2 | |
harrypowers | 0:c3e4da8feb10 | 3 | DS1307::DS1307(PinName sda, PinName scl ) : ds1307i2c(sda,scl) { |
harrypowers | 0:c3e4da8feb10 | 4 | ds1307i2c.frequency(DS1307_freq); |
harrypowers | 0:c3e4da8feb10 | 5 | } |
harrypowers | 0:c3e4da8feb10 | 6 | |
harrypowers | 0:c3e4da8feb10 | 7 | DS1307::~DS1307() { |
harrypowers | 0:c3e4da8feb10 | 8 | } |
harrypowers | 0:c3e4da8feb10 | 9 | |
harrypowers | 0:c3e4da8feb10 | 10 | int DS1307::read( int addr, int quantity, char *data) { |
harrypowers | 0:c3e4da8feb10 | 11 | // note the char array at data must contain 63 locations or unpredictable behavior will happen |
harrypowers | 0:c3e4da8feb10 | 12 | // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access |
harrypowers | 0:c3e4da8feb10 | 13 | // quantity must be 1 - 63 as the 64th ram location is clobered in this method of access |
harrypowers | 0:c3e4da8feb10 | 14 | int test = 0 ; |
harrypowers | 0:c3e4da8feb10 | 15 | char temp_data[65]; |
harrypowers | 0:c3e4da8feb10 | 16 | |
harrypowers | 0:c3e4da8feb10 | 17 | if (addr > DS1307_lastram) return (1); // fail because address greater then what chip has to read from |
harrypowers | 0:c3e4da8feb10 | 18 | if (addr < 0 ) return (1); // fail because address less then 0 is not available |
harrypowers | 0:c3e4da8feb10 | 19 | if (quantity > DS1307_lastreg) return (1); // fail because quantity greater then what can be read |
harrypowers | 0:c3e4da8feb10 | 20 | if ((addr + quantity) > DS1307_lastreg ) return (1); // fail because cant read past reg 63 |
harrypowers | 0:c3e4da8feb10 | 21 | if ( quantity == 0 ) return (1); // fail because zero quantity wanted |
harrypowers | 0:c3e4da8feb10 | 22 | temp_data[0] = DS1307_lastreg ; // note this ram location is used to set the addressing pointer in DS1307 |
harrypowers | 0:c3e4da8feb10 | 23 | temp_data[1] = 0; // just junk to clober this address with |
harrypowers | 0:c3e4da8feb10 | 24 | test = ds1307i2c.write(DS1307_addr,temp_data,2); |
harrypowers | 0:c3e4da8feb10 | 25 | if (test == 1) return (1); // the write operation failed |
harrypowers | 0:c3e4da8feb10 | 26 | ds1307i2c.stop(); // now the DS1307 is pointing to the first register |
harrypowers | 0:c3e4da8feb10 | 27 | if ( addr != 0 ) test = ds1307i2c.read(DS1307_addr,temp_data,addr); // now the DS1307 address pointer is pointing to correct address |
harrypowers | 0:c3e4da8feb10 | 28 | if (test == 1) return (1); // the read operation failed |
harrypowers | 0:c3e4da8feb10 | 29 | test = ds1307i2c.read(DS1307_addr,data,quantity); // read the DS1307 registers now |
harrypowers | 0:c3e4da8feb10 | 30 | if (test == 1) return (1); // read operation failed |
harrypowers | 0:c3e4da8feb10 | 31 | return(0); // looks like the data read was good |
harrypowers | 0:c3e4da8feb10 | 32 | } |
harrypowers | 0:c3e4da8feb10 | 33 | |
harrypowers | 0:c3e4da8feb10 | 34 | int DS1307::read(int addr, int *data) { |
harrypowers | 0:c3e4da8feb10 | 35 | // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access |
harrypowers | 0:c3e4da8feb10 | 36 | int test = 0; |
harrypowers | 0:c3e4da8feb10 | 37 | char temp_data[65]; |
harrypowers | 0:c3e4da8feb10 | 38 | test = DS1307::read(addr, 1, &temp_data[0]); |
harrypowers | 0:c3e4da8feb10 | 39 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 40 | *data = (int)temp_data[0]; // returing the read data by pointer |
harrypowers | 0:c3e4da8feb10 | 41 | return (0); // the single read is successfull |
harrypowers | 0:c3e4da8feb10 | 42 | } |
harrypowers | 0:c3e4da8feb10 | 43 | |
harrypowers | 0:c3e4da8feb10 | 44 | int DS1307::write( int addr, int quantity, char *data) { |
harrypowers | 0:c3e4da8feb10 | 45 | // note the char array at data must contain 63 locations or unpredictable behavior will happen |
harrypowers | 0:c3e4da8feb10 | 46 | // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access |
harrypowers | 0:c3e4da8feb10 | 47 | // quantity must be 1 - 63 as the 64th ram location is clobered in this method of access |
harrypowers | 0:c3e4da8feb10 | 48 | int test = 0 ; |
harrypowers | 0:c3e4da8feb10 | 49 | char temp_data[65] ; |
harrypowers | 0:c3e4da8feb10 | 50 | int loop = 0; |
harrypowers | 0:c3e4da8feb10 | 51 | |
harrypowers | 0:c3e4da8feb10 | 52 | if (addr > DS1307_lastram) return (1); // fail because address greater then what chip has to read from |
harrypowers | 0:c3e4da8feb10 | 53 | if (addr < 0 ) return (1); // fail because address less then 0 is not available |
harrypowers | 0:c3e4da8feb10 | 54 | if (quantity > DS1307_lastreg) return (1); // fail because quantity greater then what can be read |
harrypowers | 0:c3e4da8feb10 | 55 | if (quantity == 0) return (1); // fail because zero quantity is wanted |
harrypowers | 0:c3e4da8feb10 | 56 | if ((addr + quantity) > DS1307_lastreg ) return (1); // fail because cant read past reg 63 |
harrypowers | 0:c3e4da8feb10 | 57 | |
harrypowers | 0:c3e4da8feb10 | 58 | temp_data[0] = (char)addr; |
harrypowers | 0:c3e4da8feb10 | 59 | for ( ; loop < quantity ; loop++ ) { |
harrypowers | 0:c3e4da8feb10 | 60 | temp_data[loop+1] = *(data + loop); |
harrypowers | 0:c3e4da8feb10 | 61 | } |
harrypowers | 0:c3e4da8feb10 | 62 | test = ds1307i2c.write(DS1307_addr, temp_data, (quantity + 1)); |
harrypowers | 0:c3e4da8feb10 | 63 | ds1307i2c.stop(); |
harrypowers | 0:c3e4da8feb10 | 64 | return(test); // 0 for success 1 for failure to write |
harrypowers | 0:c3e4da8feb10 | 65 | } |
harrypowers | 0:c3e4da8feb10 | 66 | |
harrypowers | 0:c3e4da8feb10 | 67 | int DS1307::write( int addr, int data ) { |
harrypowers | 0:c3e4da8feb10 | 68 | // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access |
harrypowers | 0:c3e4da8feb10 | 69 | int test = 0 ; |
harrypowers | 0:c3e4da8feb10 | 70 | char temp_data[2] ; |
harrypowers | 0:c3e4da8feb10 | 71 | |
harrypowers | 0:c3e4da8feb10 | 72 | temp_data[0] = (char)addr; |
harrypowers | 0:c3e4da8feb10 | 73 | temp_data[1] = (char)data; |
harrypowers | 0:c3e4da8feb10 | 74 | if (addr > DS1307_lastram) return (1); // fail because address greater then what chip has to read from |
harrypowers | 0:c3e4da8feb10 | 75 | if (addr < 0 ) return (1); // fail because address less then 0 is not available |
harrypowers | 0:c3e4da8feb10 | 76 | test = ds1307i2c.write(DS1307_addr, temp_data, 2); |
harrypowers | 0:c3e4da8feb10 | 77 | ds1307i2c.stop(); |
harrypowers | 0:c3e4da8feb10 | 78 | return(test); |
harrypowers | 0:c3e4da8feb10 | 79 | } |
harrypowers | 0:c3e4da8feb10 | 80 | |
harrypowers | 0:c3e4da8feb10 | 81 | int DS1307::start_clock(void) { // start the clock |
harrypowers | 0:c3e4da8feb10 | 82 | int test = 0; |
harrypowers | 0:c3e4da8feb10 | 83 | int junk = 0; |
harrypowers | 0:c3e4da8feb10 | 84 | |
harrypowers | 0:c3e4da8feb10 | 85 | test = DS1307::read(DS1307_sec, &junk); |
harrypowers | 0:c3e4da8feb10 | 86 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 87 | junk = ( 0x7F & junk); // basicaly i mask bit 8 to set it to zero |
harrypowers | 0:c3e4da8feb10 | 88 | test = DS1307::write(DS1307_sec,junk); // now write the seconds back to register and because bit 8 is zero this starts clock. |
harrypowers | 0:c3e4da8feb10 | 89 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 90 | return(test); // |
harrypowers | 0:c3e4da8feb10 | 91 | } |
harrypowers | 0:c3e4da8feb10 | 92 | |
harrypowers | 0:c3e4da8feb10 | 93 | int DS1307::stop_clock(void) { // stop clock |
harrypowers | 0:c3e4da8feb10 | 94 | int test = 0; |
harrypowers | 0:c3e4da8feb10 | 95 | int junk = 0; |
harrypowers | 0:c3e4da8feb10 | 96 | |
harrypowers | 0:c3e4da8feb10 | 97 | test = DS1307::read(DS1307_sec, &junk); |
harrypowers | 0:c3e4da8feb10 | 98 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 99 | junk = ( 0x7F & junk); // basicaly i mask bit 8 to set it to zero but keep all other bits |
harrypowers | 0:c3e4da8feb10 | 100 | junk = ( 0x80 | junk); // basicaly i mask bit 8 to set it to one |
harrypowers | 0:c3e4da8feb10 | 101 | test = DS1307::write(DS1307_sec,junk); // now write the seconds back to register and because bit 8 is one this starts clock. |
harrypowers | 0:c3e4da8feb10 | 102 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 103 | return(test); // |
harrypowers | 0:c3e4da8feb10 | 104 | } |
harrypowers | 0:c3e4da8feb10 | 105 | |
harrypowers | 0:c3e4da8feb10 | 106 | int DS1307::twelve_hour(void) { // set 12 hour mode |
harrypowers | 0:c3e4da8feb10 | 107 | int test = 0; |
harrypowers | 0:c3e4da8feb10 | 108 | int junk = 0; |
harrypowers | 0:c3e4da8feb10 | 109 | |
harrypowers | 0:c3e4da8feb10 | 110 | test = DS1307::read(DS1307_hour, &junk); |
harrypowers | 0:c3e4da8feb10 | 111 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 112 | if ((junk & 0x40) == 0x40) return(0); // return because 12 mode is active now all done! |
harrypowers | 0:c3e4da8feb10 | 113 | |
harrypowers | 0:c3e4da8feb10 | 114 | junk = ( junk & 0x3F); // only use 24 hour time values |
harrypowers | 0:c3e4da8feb10 | 115 | if (junk == 0x00) |
harrypowers | 0:c3e4da8feb10 | 116 | junk = 0x12; |
harrypowers | 0:c3e4da8feb10 | 117 | else if (junk >= 0x13) |
harrypowers | 0:c3e4da8feb10 | 118 | if (junk < 0x20) { |
harrypowers | 0:c3e4da8feb10 | 119 | junk = junk - 0x12; |
harrypowers | 0:c3e4da8feb10 | 120 | junk = (junk | 0x20); // add back the pm indicator |
harrypowers | 0:c3e4da8feb10 | 121 | } else |
harrypowers | 0:c3e4da8feb10 | 122 | switch (junk) { |
harrypowers | 0:c3e4da8feb10 | 123 | case 0x20: |
harrypowers | 0:c3e4da8feb10 | 124 | junk = 0x28; |
harrypowers | 0:c3e4da8feb10 | 125 | break; |
harrypowers | 0:c3e4da8feb10 | 126 | case 0x21: |
harrypowers | 0:c3e4da8feb10 | 127 | junk = 0x29; |
harrypowers | 0:c3e4da8feb10 | 128 | break; |
harrypowers | 0:c3e4da8feb10 | 129 | case 0x22: |
harrypowers | 0:c3e4da8feb10 | 130 | junk = 0x30; |
harrypowers | 0:c3e4da8feb10 | 131 | break; |
harrypowers | 0:c3e4da8feb10 | 132 | case 0x23: |
harrypowers | 0:c3e4da8feb10 | 133 | junk = 0x31; |
harrypowers | 0:c3e4da8feb10 | 134 | break; |
harrypowers | 0:c3e4da8feb10 | 135 | } |
harrypowers | 0:c3e4da8feb10 | 136 | |
harrypowers | 0:c3e4da8feb10 | 137 | test = DS1307::write(DS1307_hour,(0x40 | junk)); // set bit 6 with the new 12 hour time converted from the 24 hour time |
harrypowers | 0:c3e4da8feb10 | 138 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 139 | |
harrypowers | 0:c3e4da8feb10 | 140 | return(0); |
harrypowers | 0:c3e4da8feb10 | 141 | } |
harrypowers | 0:c3e4da8feb10 | 142 | |
harrypowers | 0:c3e4da8feb10 | 143 | int DS1307::twentyfour_hour(void) { // set 24 hour mode |
harrypowers | 0:c3e4da8feb10 | 144 | int test = 0; |
harrypowers | 0:c3e4da8feb10 | 145 | int junk = 0; |
harrypowers | 0:c3e4da8feb10 | 146 | |
harrypowers | 0:c3e4da8feb10 | 147 | test = DS1307::read(DS1307_hour, &junk); |
harrypowers | 0:c3e4da8feb10 | 148 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 149 | if ((junk & 0x40) == 0) return(0); // return because 24 mode is active now all done! |
harrypowers | 0:c3e4da8feb10 | 150 | |
harrypowers | 0:c3e4da8feb10 | 151 | junk = (junk & 0xBF); // get value bits and am/pm indicator bit but drop 12/24 hour bit |
harrypowers | 0:c3e4da8feb10 | 152 | |
harrypowers | 0:c3e4da8feb10 | 153 | if (junk > 0x12) |
harrypowers | 0:c3e4da8feb10 | 154 | if ( junk <= 0x27 ) |
harrypowers | 0:c3e4da8feb10 | 155 | junk = junk - 0x0E; |
harrypowers | 0:c3e4da8feb10 | 156 | else |
harrypowers | 0:c3e4da8feb10 | 157 | junk = junk - 0x08; |
harrypowers | 0:c3e4da8feb10 | 158 | |
harrypowers | 0:c3e4da8feb10 | 159 | test = DS1307::write(DS1307_hour,( 0xBF & junk)); // clear bit 6 and set the new 24 hour time converted from 12 hour time |
harrypowers | 0:c3e4da8feb10 | 160 | if (test == 1) return(1); // fail because read to DS1307 failed |
harrypowers | 0:c3e4da8feb10 | 161 | |
harrypowers | 0:c3e4da8feb10 | 162 | return(0); |
harrypowers | 0:c3e4da8feb10 | 163 | } |
harrypowers | 0:c3e4da8feb10 | 164 | |
harrypowers | 0:c3e4da8feb10 | 165 | int DS1307::settime(int sec, int min, int hour, int day, int date, int month, int year) { // to set the current time and start clock |
harrypowers | 0:c3e4da8feb10 | 166 | // sec = 0 to 59, min = 0 to 59, hours = 0 to 23 ( 24 hour mode only ), day = 1 to 7 ( day of week ), date = 1 to 31, month = 1 to 12, year 0 to 99 ( this is for 2000 to 2099) |
harrypowers | 0:c3e4da8feb10 | 167 | DS1307::stop_clock(); |
harrypowers | 0:c3e4da8feb10 | 168 | |
harrypowers | 0:c3e4da8feb10 | 169 | if (1 == DS1307::hilow_check( 59, 0, sec)) { |
harrypowers | 0:c3e4da8feb10 | 170 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 171 | } else { |
harrypowers | 0:c3e4da8feb10 | 172 | if (1 == (DS1307::write(DS1307_sec,DS1307::dectobcd(sec)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 173 | } |
harrypowers | 0:c3e4da8feb10 | 174 | |
harrypowers | 0:c3e4da8feb10 | 175 | if (1 == DS1307::hilow_check( 59, 0, min)) { |
harrypowers | 0:c3e4da8feb10 | 176 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 177 | } else { |
harrypowers | 0:c3e4da8feb10 | 178 | if (1 == (DS1307::write(DS1307_min,DS1307::dectobcd(min)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 179 | } |
harrypowers | 0:c3e4da8feb10 | 180 | |
harrypowers | 0:c3e4da8feb10 | 181 | if (1 == DS1307::twentyfour_hour()) return(1); // failed to set 24 hour format |
harrypowers | 0:c3e4da8feb10 | 182 | if (1 == DS1307::hilow_check( 23, 0, hour)) { // note setting 24 hour mode befor and after writing the hour value ensures 24 hour mode is set |
harrypowers | 0:c3e4da8feb10 | 183 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 184 | } else { |
harrypowers | 0:c3e4da8feb10 | 185 | if (1 == (DS1307::write(DS1307_hour,DS1307::dectobcd(hour)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 186 | } |
harrypowers | 0:c3e4da8feb10 | 187 | if (1 == DS1307::twentyfour_hour()) return(1); // failed to set 24 hour format |
harrypowers | 0:c3e4da8feb10 | 188 | |
harrypowers | 0:c3e4da8feb10 | 189 | if (1 == DS1307::hilow_check( 7, 1, day)) { |
harrypowers | 0:c3e4da8feb10 | 190 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 191 | } else { |
harrypowers | 0:c3e4da8feb10 | 192 | if (1 == (DS1307::write(DS1307_day,DS1307::dectobcd(day)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 193 | } |
harrypowers | 0:c3e4da8feb10 | 194 | |
harrypowers | 0:c3e4da8feb10 | 195 | if (1 == DS1307::hilow_check( 31, 1, date)) { |
harrypowers | 0:c3e4da8feb10 | 196 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 197 | } else { |
harrypowers | 0:c3e4da8feb10 | 198 | if (1 == (DS1307::write(DS1307_date,DS1307::dectobcd(date)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 199 | } |
harrypowers | 0:c3e4da8feb10 | 200 | |
harrypowers | 0:c3e4da8feb10 | 201 | if (1 == DS1307::hilow_check( 12, 1, month)) { |
harrypowers | 0:c3e4da8feb10 | 202 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 203 | } else { |
harrypowers | 0:c3e4da8feb10 | 204 | if (1 == (DS1307::write(DS1307_month,DS1307::dectobcd(month)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 205 | } |
harrypowers | 0:c3e4da8feb10 | 206 | |
harrypowers | 0:c3e4da8feb10 | 207 | if (1 == DS1307::hilow_check( 99, 0, year)) { |
harrypowers | 0:c3e4da8feb10 | 208 | return(1); // failed because recieved value is not in bounds |
harrypowers | 0:c3e4da8feb10 | 209 | } else { |
harrypowers | 0:c3e4da8feb10 | 210 | if (1 == (DS1307::write(DS1307_year,DS1307::dectobcd(year)))) return(1); // failed to write for some reason |
harrypowers | 0:c3e4da8feb10 | 211 | } |
harrypowers | 0:c3e4da8feb10 | 212 | |
harrypowers | 0:c3e4da8feb10 | 213 | DS1307::start_clock(); |
harrypowers | 0:c3e4da8feb10 | 214 | return (0); // time is now set |
harrypowers | 0:c3e4da8feb10 | 215 | } |
harrypowers | 0:c3e4da8feb10 | 216 | |
harrypowers | 0:c3e4da8feb10 | 217 | int DS1307::gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year) { // to get the current time information |
harrypowers | 0:c3e4da8feb10 | 218 | // sec = 0 to 59, min = 0 to 59, hours = 0 to 23 ( 24 hour mode only ), day = 1 to 7 ( day of week ), date = 1 to 31, month = 1 to 12, year 0 to 99 ( this is for 2000 to 2099) |
harrypowers | 0:c3e4da8feb10 | 219 | if (1 == DS1307::read(DS1307_sec,sec)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 220 | *sec = (*sec & 0x7F ); // drop the clock start stop bit |
harrypowers | 0:c3e4da8feb10 | 221 | *sec = DS1307::bcdtodec( *sec); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 222 | |
harrypowers | 0:c3e4da8feb10 | 223 | if (1 == DS1307::read(DS1307_min,min)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 224 | *min = (*min & 0x7F ); // drop bit 7 because it should be 0 anyways |
harrypowers | 0:c3e4da8feb10 | 225 | *min = DS1307::bcdtodec( *min); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 226 | |
harrypowers | 0:c3e4da8feb10 | 227 | if (1 == DS1307::read(DS1307_hour,hour)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 228 | if ((*hour & 0x40) == 0x40) { // if true then 12 hour mode is set currently so change to 24 hour, read value, and return to 12 hour mode |
harrypowers | 0:c3e4da8feb10 | 229 | if (1 == DS1307::twentyfour_hour()) return(1); // failed to set 24 hour mode for some reason |
harrypowers | 0:c3e4da8feb10 | 230 | if (1 == DS1307::read(DS1307_hour,hour)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 231 | *hour = (*hour & 0x3F ); // drop bit 7 & 6 they are not used for 24 hour mode reading |
harrypowers | 0:c3e4da8feb10 | 232 | *hour = DS1307::bcdtodec( *hour); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 233 | if (1 == DS1307::twelve_hour()) return(1); // failed to return to 12 hour mode for some reason |
harrypowers | 0:c3e4da8feb10 | 234 | } else { // in 24 hour mode already so just read the hour value |
harrypowers | 0:c3e4da8feb10 | 235 | if (1 == DS1307::read(DS1307_hour,hour)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 236 | *hour = (*hour & 0x3F ); // drop bit 7 & 6 they are not used for 24 hour mode reading |
harrypowers | 0:c3e4da8feb10 | 237 | *hour = DS1307::bcdtodec( *hour); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 238 | } |
harrypowers | 0:c3e4da8feb10 | 239 | |
harrypowers | 0:c3e4da8feb10 | 240 | if (1 == DS1307::read(DS1307_day,day)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 241 | *day = (*day & 0x07 ); // drop the non used bits |
harrypowers | 0:c3e4da8feb10 | 242 | *day = DS1307::bcdtodec( *day); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 243 | |
harrypowers | 0:c3e4da8feb10 | 244 | if (1 == DS1307::read(DS1307_date,date)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 245 | *date = (*date & 0x3F ); // drop bit 6 and 7 not used for date value |
harrypowers | 0:c3e4da8feb10 | 246 | *date = DS1307::bcdtodec( *date); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 247 | |
harrypowers | 0:c3e4da8feb10 | 248 | if (1 == DS1307::read(DS1307_month,month)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 249 | *month = (*month & 0x1F ); // drop bit 5, 6 and 7 not used for month value |
harrypowers | 0:c3e4da8feb10 | 250 | *month = DS1307::bcdtodec( *month); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 251 | |
harrypowers | 0:c3e4da8feb10 | 252 | if (1 == DS1307::read(DS1307_year,year)) return(1); // failed to read for some reason |
harrypowers | 0:c3e4da8feb10 | 253 | *year = DS1307::bcdtodec( *year); // bcd is now dec value |
harrypowers | 0:c3e4da8feb10 | 254 | |
harrypowers | 0:c3e4da8feb10 | 255 | return (0); // data returned is valid |
harrypowers | 0:c3e4da8feb10 | 256 | } |
harrypowers | 0:c3e4da8feb10 | 257 | |
harrypowers | 0:c3e4da8feb10 | 258 | |
harrypowers | 0:c3e4da8feb10 | 259 | int DS1307::dectobcd( int dec) { |
harrypowers | 0:c3e4da8feb10 | 260 | int low = 0; |
harrypowers | 0:c3e4da8feb10 | 261 | int high = 0; |
harrypowers | 0:c3e4da8feb10 | 262 | |
harrypowers | 0:c3e4da8feb10 | 263 | high = dec / 10; // this gives the high nibble value |
harrypowers | 0:c3e4da8feb10 | 264 | low = dec - (high * 10); // this gives the lower nibble value |
harrypowers | 0:c3e4da8feb10 | 265 | return ((high *16) + low); // this is the final bcd value but in interger format |
harrypowers | 0:c3e4da8feb10 | 266 | } |
harrypowers | 0:c3e4da8feb10 | 267 | |
harrypowers | 0:c3e4da8feb10 | 268 | int DS1307::bcdtodec( int bcd) { |
harrypowers | 0:c3e4da8feb10 | 269 | int low = 0; |
harrypowers | 0:c3e4da8feb10 | 270 | int high = 0; |
harrypowers | 0:c3e4da8feb10 | 271 | |
harrypowers | 0:c3e4da8feb10 | 272 | high = bcd / 16; |
harrypowers | 0:c3e4da8feb10 | 273 | low = bcd - (high * 16); |
harrypowers | 0:c3e4da8feb10 | 274 | return ((high * 10) + low); |
harrypowers | 0:c3e4da8feb10 | 275 | |
harrypowers | 0:c3e4da8feb10 | 276 | } |
harrypowers | 0:c3e4da8feb10 | 277 | |
harrypowers | 0:c3e4da8feb10 | 278 | int DS1307::hilow_check( int hi, int low, int value) { |
harrypowers | 0:c3e4da8feb10 | 279 | if ((value >= low)&(value <= hi)) |
harrypowers | 0:c3e4da8feb10 | 280 | return(0); // value is equal to or inbetween hi and low |
harrypowers | 0:c3e4da8feb10 | 281 | else |
harrypowers | 0:c3e4da8feb10 | 282 | return(1); // value is not equal to or inbetween hi and low |
harrypowers | 0:c3e4da8feb10 | 283 | } |
harrypowers | 0:c3e4da8feb10 | 284 | |
harrypowers | 0:c3e4da8feb10 | 285 | |
harrypowers | 0:c3e4da8feb10 | 286 | |
harrypowers | 0:c3e4da8feb10 | 287 |