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