Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RAM access functions, removed overloaded begin() function #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 103 additions & 2 deletions DS1307.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ uint8_t DS1307::bcdToDec(uint8_t val) {
return ((val / 16 * 10) + (val % 16));
}

/**
* \brief Start the I2C port
*/
void DS1307::begin() {
Wire.begin();
}

#ifndef Arduino_h
/**
* \brief The functio to start the I2C port with specified pins
* \brief The function to start the I2C port with specified pins
*
* \param SDA The pin number which is used as SDA pin
* \param SCL The pin number which is used as SCL pin
Expand All @@ -54,6 +58,7 @@ void DS1307::begin() {
void DS1307::begin(uint16_t SDA ,uint16_t SCL) {
Wire.begin(SDA,SCL);
}
#endif

/*Function: The clock timing will start */
void DS1307::startClock(void) { // set the ClockHalt bit low to start the rtc
Expand Down Expand Up @@ -97,7 +102,7 @@ void DS1307::getTime() {
year = bcdToDec(Wire.read());
}
/*******************************************************************/
/*Frunction: Write the time that includes the date to the RTC chip */
/*Function: Write the time that includes the date to the RTC chip */
void DS1307::setTime() {
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Expand All @@ -110,18 +115,114 @@ void DS1307::setTime() {
Wire.write(decToBcd(year));
Wire.endTransmission();
}

/**
* \brief Set the time
*
* \param _hour Hour between 0-23
* \param _minute Minute between 0-59
* \param _second Second between 0-59
*
*/
void DS1307::fillByHMS(uint8_t _hour, uint8_t _minute, uint8_t _second) {
// assign variables
hour = _hour;
minute = _minute;
second = _second;
}

/**
* \brief Set the date
*
* \param _year Year: 2000-2099
* \param _month Month: 1-12
* \param _day Day: 1-31
*
*/
void DS1307::fillByYMD(uint16_t _year, uint8_t _month, uint8_t _day) {
year = _year - 2000;
month = _month;
dayOfMonth = _day;
}

/**
* \brief Sets the day of week. The increments at midnight.
*
* \param _dow MON, TUE, WED, THU, FRI, SAT, SUN
*
*/
void DS1307::fillDayOfWeek(uint8_t _dow) {
dayOfWeek = _dow;
}

/**
* \brief Save the contents of the RAM buffer to the DS1307
*
* \param _addr The zero-index memory address (0-55)
* \param _value The 8 bit value to store
*
*/
void DS1307::setRamAddress(uint8_t _addr, uint8_t _value) {
uint8_t address = _addr + DS1307_RAM_OFFSET;

ram[_addr] = _value;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(address);
Wire.write(_value);
Wire.endTransmission();

}

/**
* \brief Save the contents of the RAM buffer to the DS1307
*/
void DS1307::setRam(){
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x08);
for (uint8_t pos = 0; pos < 55; ++pos) {
Wire.write(ram[pos]);
}
Wire.endTransmission();
}

/**
* \brief Get the contents of a specific address
*
* \param _addr The address to retrieve
*
*/
uint8_t DS1307::getRamAddress(uint8_t _addr) {
uint8_t address = _addr + DS1307_RAM_OFFSET;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(address);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
ram[_addr] = Wire.read();
return ram[_addr];
}

/**
* \brief Retrieve the contents of RAM to the ram buffer
*/
void DS1307::getRam() {
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x08);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
for (uint8_t pos = 0; pos < 55; ++pos) {
ram[pos] = Wire.read();
}
}

/**
* \brief Returns whether or not the clock is started
*/
bool DS1307::isStarted(){
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
return !(bool)(Wire.read() & 0x80); // bit 7 (sart/stop bit) = clock started
}
9 changes: 9 additions & 0 deletions DS1307.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <Arduino.h>

#define DS1307_I2C_ADDRESS 0x68
#define DS1307_RAM_OFFSET 0x08

#define MON 1
#define TUE 2
Expand All @@ -50,21 +51,29 @@ class DS1307 {

public:
void begin();
#ifndef Arduino_h
void begin(uint16_t SDA ,uint16_t SCL);
#endif
void startClock(void);
void stopClock(void);
void setTime(void);
void getTime(void);
void fillByHMS(uint8_t _hour, uint8_t _minute, uint8_t _second);
void fillByYMD(uint16_t _year, uint8_t _month, uint8_t _day);
void fillDayOfWeek(uint8_t _dow);
void setRamAddress(uint8_t _addr, uint8_t _value);
void setRam();
uint8_t getRamAddress(uint8_t _addr);
void getRam();
bool isStarted();
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t dayOfWeek;// day of week, 1 = Monday
uint8_t dayOfMonth;
uint8_t month;
uint16_t year;
uint8_t ram[56];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This blocks 56 bytes of precious memory, even if the user doesn't plan to manipulate the RAM directly. What about setRam and getRam accepting this as a parameter instead of an internal variable? In this way, only the users requiring it need to allocate the 56 bytes beforehand.

};

#endif