Update

Dependents:   16A_Autopancakemaker

Committer:
pruek
Date:
Sat Dec 12 07:35:54 2015 +0000
Revision:
0:9570e313fcba
A

Who changed what in which revision?

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