RTC Driver
Revision 0:4399479f355f, committed 2016-02-20
- Comitter:
- sk398
- Date:
- Sat Feb 20 13:32:01 2016 +0000
- Commit message:
- Initial commit.; ; Much work still needs done to interface the DS3231 correctly. ; ; I2C read/write functions work
Changed in this revision
DS3231.cpp | Show annotated file Show diff for this revision Revisions of this file |
DS3231.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS3231.cpp Sat Feb 20 13:32:01 2016 +0000 @@ -0,0 +1,110 @@ +#include "mbed.h" +#include "DS3231.h" + +DS3231::DS3231(PinName sda,PinName scl) +{ + _DS3231 = new I2C(sda,scl) ; +} + +void DS3231::readControl() +{ + int *a; + a = DS3231::readBytes(MSB_OF_TEMP,1); + int b = *a; + printf("Control: %d\r\n",b); +} + + + +/*----------------------------------------- I2C Functions -----------------------------------------*/ + +int DS3231::readByte(uint8_t registerAddress) +{ + char address[1]; + address[0] = registerAddress; + if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,1)) + { + return -2; + } + else + { + uint8_t inBuffer[1]; + if(_DS3231 -> read(DS3231_ADDRESS,(char *)inBuffer,SINGLE_BYTE,0)) + { + return -1; + } + else + { + return inBuffer[0]; + } + } +} + +int *DS3231::readBytes(uint8_t registerAddress, uint8_t numBytes) +{ + int g = -1; + int *gPtr = &g; + + char address[1]; + address[0] = registerAddress; + if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,TRUE)) + { + return gPtr; + } + else + { + uint8_t inBuffer[numBytes]; + uint8_t *inBufferPtr = &inBuffer[0]; + if(_DS3231 -> read(DS3231_ADDRESS,(char *)inBuffer,numBytes,FALSE)) + { + return gPtr; + } + else + { + printf("Inside: %d\r\n",inBuffer[0]); + return (int *)inBufferPtr; + } + } + +} + +int DS3231::writeByte(uint8_t registerAddress,uint8_t outBuffer) +{ + char address[1]; + address[0] = registerAddress; + if(_DS3231 -> write(DS3231_ADDRESS,address,SINGLE_BYTE,TRUE)) + { + return 1; + } + else + { + char buffer[1]; + buffer[0] = outBuffer; + if(_DS3231 -> write(DS3231_ADDRESS,buffer,SINGLE_BYTE,FALSE)) + { + return 1; + } + else + { + return 0; + } + } +} + +//int DS3231::writeBytes(uint8_t registerAddress, uint8_t *outBuffer, uint8_t numBytes) +//{ +// if(_DS3231 -> write(DS3231_ADDRESS,(char *)registerAddress,SINGLE_BYTE,TRUE)) +// { +// return 1; +// } +// else +// { +// if(_DS3231 -> write(DS3231_ADDRESS,(char *)outBuffer,numBytes,FALSE)) +// { +// return 1; +// } +// else +// { +// return 0; +// } +// } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS3231.h Sat Feb 20 13:32:01 2016 +0000 @@ -0,0 +1,55 @@ +#ifndef DS3231_H +#define DS3231_H + +#define TRUE 1 +#define FALSE 0 + +#define SINGLE_BYTE 1 + +#define DS3231_ADDRESS 0x68<<1/*0xAE*/ + +#define SECONDS 0x00 +#define MINUTES 0x01 +#define HOURS 0x02 +#define DAY 0x03 +#define DATE 0x04 +#define MONTH_CENTURY 0x05 +#define YEAR 0x06 + +#define ALARM_1_SECONDS 0x07 +#define ALARM_1_MINUTES 0x08 +#define ALARM_1_HOURS 0x09 +#define ALARM_1_DAY_DATE 0x0A +#define ALARM_2_MINUTES 0x0B +#define ALARM_2_HOURS 0x0C +#define ALARM_2_DAY_DATE 0x0D + +#define CONTROL 0x0E +#define CONTROL_STATUS 0x0F + +#define AGING_OFFSET 0x10 + +#define MSB_OF_TEMP 0x11 +#define LSB_OF_TEMP 0x12 + + + +class DS3231 +{ +public: + DS3231(PinName sda, PinName scl); + void readControl(); +private: + int readByte(uint8_t registerAddress); + int *readBytes(uint8_t registerAddress,uint8_t numBytes); + int writeByte(uint8_t registerAddress,uint8_t outBuffer); +// int writeBytes(uint8_t registerAddress, uint8_t *outBuffer,uint8_t numBytes); + +protected: + +I2C *_DS3231; + + +}; + +#endif \ No newline at end of file