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
Revision 0:c3e4da8feb10, committed 2012-06-24
- Comitter:
- harrypowers
- Date:
- Sun Jun 24 22:09:35 2012 +0000
- Commit message:
- Changes to documentation only for auto generating
Changed in this revision
ds1307.cpp | Show annotated file Show diff for this revision Revisions of this file |
ds1307.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r c3e4da8feb10 ds1307.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds1307.cpp Sun Jun 24 22:09:35 2012 +0000 @@ -0,0 +1,287 @@ +#include "ds1307.h" + +DS1307::DS1307(PinName sda, PinName scl ) : ds1307i2c(sda,scl) { + ds1307i2c.frequency(DS1307_freq); +} + +DS1307::~DS1307() { +} + +int DS1307::read( int addr, int quantity, char *data) { + // note the char array at data must contain 63 locations or unpredictable behavior will happen + // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access + // quantity must be 1 - 63 as the 64th ram location is clobered in this method of access + int test = 0 ; + char temp_data[65]; + + if (addr > DS1307_lastram) return (1); // fail because address greater then what chip has to read from + if (addr < 0 ) return (1); // fail because address less then 0 is not available + if (quantity > DS1307_lastreg) return (1); // fail because quantity greater then what can be read + if ((addr + quantity) > DS1307_lastreg ) return (1); // fail because cant read past reg 63 + if ( quantity == 0 ) return (1); // fail because zero quantity wanted + temp_data[0] = DS1307_lastreg ; // note this ram location is used to set the addressing pointer in DS1307 + temp_data[1] = 0; // just junk to clober this address with + test = ds1307i2c.write(DS1307_addr,temp_data,2); + if (test == 1) return (1); // the write operation failed + ds1307i2c.stop(); // now the DS1307 is pointing to the first register + if ( addr != 0 ) test = ds1307i2c.read(DS1307_addr,temp_data,addr); // now the DS1307 address pointer is pointing to correct address + if (test == 1) return (1); // the read operation failed + test = ds1307i2c.read(DS1307_addr,data,quantity); // read the DS1307 registers now + if (test == 1) return (1); // read operation failed + return(0); // looks like the data read was good +} + +int DS1307::read(int addr, int *data) { + // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access + int test = 0; + char temp_data[65]; + test = DS1307::read(addr, 1, &temp_data[0]); + if (test == 1) return(1); // fail because read to DS1307 failed + *data = (int)temp_data[0]; // returing the read data by pointer + return (0); // the single read is successfull +} + +int DS1307::write( int addr, int quantity, char *data) { + // note the char array at data must contain 63 locations or unpredictable behavior will happen + // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access + // quantity must be 1 - 63 as the 64th ram location is clobered in this method of access + int test = 0 ; + char temp_data[65] ; + int loop = 0; + + if (addr > DS1307_lastram) return (1); // fail because address greater then what chip has to read from + if (addr < 0 ) return (1); // fail because address less then 0 is not available + if (quantity > DS1307_lastreg) return (1); // fail because quantity greater then what can be read + if (quantity == 0) return (1); // fail because zero quantity is wanted + if ((addr + quantity) > DS1307_lastreg ) return (1); // fail because cant read past reg 63 + + temp_data[0] = (char)addr; + for ( ; loop < quantity ; loop++ ) { + temp_data[loop+1] = *(data + loop); + } + test = ds1307i2c.write(DS1307_addr, temp_data, (quantity + 1)); + ds1307i2c.stop(); + return(test); // 0 for success 1 for failure to write +} + +int DS1307::write( int addr, int data ) { + // addr must be 0 - 62 as the 64th(or 63rd as indexed from 0) ram location is clobered in this method of access + int test = 0 ; + char temp_data[2] ; + + temp_data[0] = (char)addr; + temp_data[1] = (char)data; + if (addr > DS1307_lastram) return (1); // fail because address greater then what chip has to read from + if (addr < 0 ) return (1); // fail because address less then 0 is not available + test = ds1307i2c.write(DS1307_addr, temp_data, 2); + ds1307i2c.stop(); + return(test); +} + +int DS1307::start_clock(void) { // start the clock + int test = 0; + int junk = 0; + + test = DS1307::read(DS1307_sec, &junk); + if (test == 1) return(1); // fail because read to DS1307 failed + junk = ( 0x7F & junk); // basicaly i mask bit 8 to set it to zero + test = DS1307::write(DS1307_sec,junk); // now write the seconds back to register and because bit 8 is zero this starts clock. + if (test == 1) return(1); // fail because read to DS1307 failed + return(test); // +} + +int DS1307::stop_clock(void) { // stop clock + int test = 0; + int junk = 0; + + test = DS1307::read(DS1307_sec, &junk); + if (test == 1) return(1); // fail because read to DS1307 failed + junk = ( 0x7F & junk); // basicaly i mask bit 8 to set it to zero but keep all other bits + junk = ( 0x80 | junk); // basicaly i mask bit 8 to set it to one + test = DS1307::write(DS1307_sec,junk); // now write the seconds back to register and because bit 8 is one this starts clock. + if (test == 1) return(1); // fail because read to DS1307 failed + return(test); // +} + +int DS1307::twelve_hour(void) { // set 12 hour mode + int test = 0; + int junk = 0; + + test = DS1307::read(DS1307_hour, &junk); + if (test == 1) return(1); // fail because read to DS1307 failed + if ((junk & 0x40) == 0x40) return(0); // return because 12 mode is active now all done! + + junk = ( junk & 0x3F); // only use 24 hour time values + if (junk == 0x00) + junk = 0x12; + else if (junk >= 0x13) + if (junk < 0x20) { + junk = junk - 0x12; + junk = (junk | 0x20); // add back the pm indicator + } else + switch (junk) { + case 0x20: + junk = 0x28; + break; + case 0x21: + junk = 0x29; + break; + case 0x22: + junk = 0x30; + break; + case 0x23: + junk = 0x31; + break; + } + + test = DS1307::write(DS1307_hour,(0x40 | junk)); // set bit 6 with the new 12 hour time converted from the 24 hour time + if (test == 1) return(1); // fail because read to DS1307 failed + + return(0); +} + +int DS1307::twentyfour_hour(void) { // set 24 hour mode + int test = 0; + int junk = 0; + + test = DS1307::read(DS1307_hour, &junk); + if (test == 1) return(1); // fail because read to DS1307 failed + if ((junk & 0x40) == 0) return(0); // return because 24 mode is active now all done! + + junk = (junk & 0xBF); // get value bits and am/pm indicator bit but drop 12/24 hour bit + + if (junk > 0x12) + if ( junk <= 0x27 ) + junk = junk - 0x0E; + else + junk = junk - 0x08; + + test = DS1307::write(DS1307_hour,( 0xBF & junk)); // clear bit 6 and set the new 24 hour time converted from 12 hour time + if (test == 1) return(1); // fail because read to DS1307 failed + + return(0); +} + +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 + // 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) + DS1307::stop_clock(); + + if (1 == DS1307::hilow_check( 59, 0, sec)) { + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_sec,DS1307::dectobcd(sec)))) return(1); // failed to write for some reason + } + + if (1 == DS1307::hilow_check( 59, 0, min)) { + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_min,DS1307::dectobcd(min)))) return(1); // failed to write for some reason + } + + if (1 == DS1307::twentyfour_hour()) return(1); // failed to set 24 hour format + 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 + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_hour,DS1307::dectobcd(hour)))) return(1); // failed to write for some reason + } + if (1 == DS1307::twentyfour_hour()) return(1); // failed to set 24 hour format + + if (1 == DS1307::hilow_check( 7, 1, day)) { + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_day,DS1307::dectobcd(day)))) return(1); // failed to write for some reason + } + + if (1 == DS1307::hilow_check( 31, 1, date)) { + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_date,DS1307::dectobcd(date)))) return(1); // failed to write for some reason + } + + if (1 == DS1307::hilow_check( 12, 1, month)) { + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_month,DS1307::dectobcd(month)))) return(1); // failed to write for some reason + } + + if (1 == DS1307::hilow_check( 99, 0, year)) { + return(1); // failed because recieved value is not in bounds + } else { + if (1 == (DS1307::write(DS1307_year,DS1307::dectobcd(year)))) return(1); // failed to write for some reason + } + + DS1307::start_clock(); + return (0); // time is now set +} + +int DS1307::gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year) { // to get the current time information + // 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) + if (1 == DS1307::read(DS1307_sec,sec)) return(1); // failed to read for some reason + *sec = (*sec & 0x7F ); // drop the clock start stop bit + *sec = DS1307::bcdtodec( *sec); // bcd is now dec value + + if (1 == DS1307::read(DS1307_min,min)) return(1); // failed to read for some reason + *min = (*min & 0x7F ); // drop bit 7 because it should be 0 anyways + *min = DS1307::bcdtodec( *min); // bcd is now dec value + + if (1 == DS1307::read(DS1307_hour,hour)) return(1); // failed to read for some reason + 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 + if (1 == DS1307::twentyfour_hour()) return(1); // failed to set 24 hour mode for some reason + if (1 == DS1307::read(DS1307_hour,hour)) return(1); // failed to read for some reason + *hour = (*hour & 0x3F ); // drop bit 7 & 6 they are not used for 24 hour mode reading + *hour = DS1307::bcdtodec( *hour); // bcd is now dec value + if (1 == DS1307::twelve_hour()) return(1); // failed to return to 12 hour mode for some reason + } else { // in 24 hour mode already so just read the hour value + if (1 == DS1307::read(DS1307_hour,hour)) return(1); // failed to read for some reason + *hour = (*hour & 0x3F ); // drop bit 7 & 6 they are not used for 24 hour mode reading + *hour = DS1307::bcdtodec( *hour); // bcd is now dec value + } + + if (1 == DS1307::read(DS1307_day,day)) return(1); // failed to read for some reason + *day = (*day & 0x07 ); // drop the non used bits + *day = DS1307::bcdtodec( *day); // bcd is now dec value + + if (1 == DS1307::read(DS1307_date,date)) return(1); // failed to read for some reason + *date = (*date & 0x3F ); // drop bit 6 and 7 not used for date value + *date = DS1307::bcdtodec( *date); // bcd is now dec value + + if (1 == DS1307::read(DS1307_month,month)) return(1); // failed to read for some reason + *month = (*month & 0x1F ); // drop bit 5, 6 and 7 not used for month value + *month = DS1307::bcdtodec( *month); // bcd is now dec value + + if (1 == DS1307::read(DS1307_year,year)) return(1); // failed to read for some reason + *year = DS1307::bcdtodec( *year); // bcd is now dec value + + return (0); // data returned is valid +} + + +int DS1307::dectobcd( int dec) { + int low = 0; + int high = 0; + + high = dec / 10; // this gives the high nibble value + low = dec - (high * 10); // this gives the lower nibble value + return ((high *16) + low); // this is the final bcd value but in interger format +} + +int DS1307::bcdtodec( int bcd) { + int low = 0; + int high = 0; + + high = bcd / 16; + low = bcd - (high * 16); + return ((high * 10) + low); + +} + +int DS1307::hilow_check( int hi, int low, int value) { + if ((value >= low)&(value <= hi)) + return(0); // value is equal to or inbetween hi and low + else + return(1); // value is not equal to or inbetween hi and low +} + + + +
diff -r 000000000000 -r c3e4da8feb10 ds1307.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds1307.h Sun Jun 24 22:09:35 2012 +0000 @@ -0,0 +1,224 @@ +#ifndef DS1307_H +#define DS1307_H + +/* mbed Dallas Semiconductor DS1307 serial real time clock +* Copyright (c) 2012 pksmith +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "mbed.h" + +#define DS1307_addr 0xD0 // this is fixed by Dallas +#define DS1307_freq 100000 // this is the Dallas spec for operating i2c for this device +#define DS1307_sec 0x00 // seconds +#define DS1307_min 0x01 // min +#define DS1307_hour 0x02 // hours +#define DS1307_day 0x03 // day +#define DS1307_date 0x04 // date +#define DS1307_month 0x05 // month +#define DS1307_year 0x06 // year +#define DS1307_sqrout 0x07 // square output register +#define DS1307_ramstart 0x08 // register address that ram starts at +#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) +#define DS1307_lastram 0x3E // last usable ram by this class as the lastreg is clobbered by code for normal operation + +/** DS1307 control and communication class using mbed's i2c class + * + * Example: + * @code + * // show how the DS1307 class works + * #include "ds1307.h" + * #include "mbed.h" + * + * Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications + * + * DS1307 my1307(p9,p10); // start DS1307 class and give it pins for connections of the DS1307 device + * + * int sec = 0; + * int min = 0; + * int hours = 0; + * int day = 0; + * int date = 0; + * int month = 0; + * int year = 0; + * + * void test_rw(int test) { + * if (test == 0) pc.printf("Last R/W operaion passed!\n\r"); + * else pc.printf("Last R/W operation failed!\n\r"); + * } + * + * int main() { + * int junk = 0; + * + * sec = 24; // 24 seconds + * min = 13; // 13 min + * hours = 13; // 1 pm + * day = 4; // wednesday + * date = 20; // June 20 + * month = 6; + * year = 12; // 2012 + * // set time to these values on the ds1307 connected device + * + * test_rw(my1307.settime( sec, min, hours, day, date, month, year)); + * pc.printf("seconds set are %.2D \n\r",sec); + * pc.printf("min set are %.2D \n\r",min); + * pc.printf("hour set are %.2D \n\r",hours); + * pc.printf("day set are %.2D \n\r",day); + * pc.printf("date set are %.2D \n\r",date); + * pc.printf("month set are %.2D \n\r",month); + * pc.printf("year set are %.2D \n\r",year); + * wait(3); + * // now read the time of the DS1307 device and see what time it is + * // note that because of the 3 second wait this time should be 3 seconds past what it was set to earlier + * + * test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year)); + * pc.printf("seconds read are %.2D \n\r",sec); + * pc.printf("min read are %.2D \n\r",min); + * pc.printf("hour read are %.2D \n\r",hours); + * pc.printf("day read are %.2D \n\r",day); + * pc.printf("date read are %.2D \n\r",date); + * pc.printf("month read are %.2D \n\r",month); + * pc.printf("year read are %.2D \n\r",year); + * + * junk = 0x39; // just a junk value do read and write test to DS1307 ram + * test_rw(my1307.write( 0x20, junk)); // this should write the value of junk to register 0x20 (a ram location) in the ds1307. + * pc.printf("Value written to register 0x20 %.2X \n\r",junk); + * junk = 0; // clear junk to show that when the register is read from the correct value is obtained + * test_rw(my1307.read( 0x20, &junk)); // this should read register 0x20 + * pc.printf("Value read from register 0x20 %.2X \n\r",junk); + * } + * @endcode + */ +class DS1307 { +public: + /** Create object connected to DS1307 pins ( remember both pins need pull up resisters) + * + * Ensure the pull up resistors are used on these pins. Also note there is no checking on + * if you use thes pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device + * + * @param sda pin that DS1307 connected to (p9 or p28 as defined on LPC1768) + * @param slc pin that DS1307 connected to (p10 or p27 ad defined on LPC1768) + */ + DS1307( PinName sda, PinName slc) ; // constructor + + ~DS1307(); // destructor + + /** Bulk read of several registers at a time + * + * Ensure the variable data pointer passed to this function has the room needed to recieve the quantity! + * + * @param addr the address to read from + * @param quantity the amount of registers to read from + * @param data the place to put the values read + * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason + */ + int read( int addr, int quantity, char *data); // to read some of the 63 bytes from DS1307 + + /** Read one register of DS1307 device + * + * @param addr the address to read from + * @param data read from the one register + * @param returns 0 if read worked 1 if the read of DS1307 failed for some reason + */ + int read(int addr, int *data); // to read one byte only + + /** Bulk write of several registers at a time + * + * @param addr the address to write to + * @param quantity the amount of registers to write to + * @param data that contains the values to be written to the registers + * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason + */ + int write( int addr, int quantity, char *data); // to write bytes to some of the 63 locations in the DS1307 + + /** Write one register of DS1307 device + * + * @param addr the address to write to + * @param data to write to register + * @param returns 0 if write worked 1 if the write to DS1307 failed for some reason + */ + int write( int addr, int data ); // to write one byte only + + /** Start DS1307 clock + * + * @param returns 0 if clock started 1 if the write command to DS1307 failed for some reason + */ + int start_clock(void); // start the clock + + /** Stop DS1307 clock + * + * @param returns 0 if clock stopped 1 if the write command to DS1307 failed for some reason + */ + int stop_clock(void); // stop clock + + /** Set twelve hour mode on DS1307 (note this also converts 24 hour time to 12 time if needed on DS1307) + * + * Note this will convert DS1307 time values in registers to 12 hour values from 24 hour values if needed + * + * @param returns 0 if DS1307 is now in 12 hour mode 1 if the command to DS1307 failed for some reason + */ + int twelve_hour(void); // set 12 hour mode + + /** Set twenty four hour mode on DS1307 + * + * Note this will convert DS1307 time values in registers to 24 hour values from 12 hour values if needed + * + * @param returns 0 if DS1307 is now in 24 hour mode 1 if the command to DS1307 failed for some reason + */ + int twentyfour_hour(void); // set 24 hour mode + + /** Set the time to some current or other value ( note that this will start the clock after it is set!) + * + * Note this will return 1 if any of the values passed to this function are not as listed below! + * + * @param sec the seconds value (0 - 59) + * @param min the minute value (0 - 59) + * @param hour the hour value (0 - 23) always in 24 hour + * @param day the day value ( sunday is 1 ) + * @param date the date value (1 - 31) + * @param month the month value (1-12) + * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! + * @param returns 0 if time is set 1 if the time setting failed in some way + */ + int settime(int sec, int min, int hour, int day, int date, int month, int year); // to set the current time and start clock + + /** Read the current time of the DS1307 + * + * @param sec the seconds value (0 - 59) + * @param min the minute value (0 - 59) + * @param hour the hour value (0 - 23) always in 24 hour + * @param day the day value ( sunday is 1 ) + * @param date the date value (1 - 31) + * @param month the month value (1-12) + * @param year the year value (00 - 99) this is for 2000 to 2099 only as i understand it! + * @param returns 0 if time is read correctly 1 if the time was not recieved correctly for some reason + */ + int gettime(int *sec, int *min, int *hour, int *day, int *date, int *month, int *year); // to get the current time information + + +protected: + I2C ds1307i2c; + int dectobcd( int ); + int bcdtodec( int ); + int hilow_check( int, int, int); + +}; + +#endif \ No newline at end of file