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