Skip to content

Commit

Permalink
spplinted!
Browse files Browse the repository at this point in the history
  • Loading branch information
acosinwork committed Sep 18, 2015
1 parent 95797ef commit f7a7b6d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 93 deletions.
123 changes: 64 additions & 59 deletions TroykaRTC.cpp
@@ -1,8 +1,8 @@
/****************************************************************************/
/****************************************************************************/
// Function: Cpp file for troyka-RTC
// Hardware: DS1307
// Arduino IDE: Arduino-1.6.5
// Author: Igor Dementiev
// Author: Igor Dementiev
// Date: Sep 10,2015
// Version: v1.0
// by www.amperka.ru
Expand All @@ -12,70 +12,72 @@
#include "TroykaRTC.h"
#include <EEPROM.h>


uint8_t RTC::decToBcd(uint8_t val)
{
uint8_t RTC::decToBcd(uint8_t val) {
return ( (val/10*16) + (val%10) );
}

//Convert binary coded decimal to normal decimal numbers
uint8_t RTC::bcdToDec(uint8_t val)
{
// Convert binary coded decimal to normal decimal numbers
uint8_t RTC::bcdToDec(uint8_t val) {
return ( (val/16*10) + (val%16) );
}

void RTC::begin()
{
void RTC::begin() {
Wire.begin();
}
/*Function: The clock timing will start */
void RTC::start() // set the ClockHalt bit low to start the rtc
{

/* Function: The clock timing will start */

void RTC::start() { // set the ClockHalt bit low to start the rtc
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00); // Register 0x00 holds the oscillator start/stop bit
// Register 0x00 holds the oscillator start/stop bit
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
_second = Wire.read() & 0x7f; // save actual seconds and AND sec with bit 7 (sart/stop bit) = clock started
// save actual seconds and AND sec with bit 7 (sart/stop bit) = clock started
_second = Wire.read() & 0x7f;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write((uint8_t)_second); // write seconds back and start the clock
// write seconds back and start the clock
Wire.write((uint8_t)_second);
Wire.endTransmission();
}
/*Function: The clock timing will stop */
void RTC::stop() // set the ClockHalt bit high to stop the rtc
{
void RTC::stop() { // set the ClockHalt bit high to stop the rtc
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00); // Register 0x00 holds the oscillator start/stop bit
// Register 0x00 holds the oscillator start/stop bit
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
_second = Wire.read() | 0x80; // save actual seconds and OR sec with bit 7 (sart/stop bit) = clock stopped
// save actual seconds and OR sec with bit 7 (sart/stop bit) = clock stopped
_second = Wire.read() | 0x80;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write((uint8_t)_second); // write seconds back and stop the clock
// write seconds back and stop the clock
Wire.write((uint8_t)_second);
Wire.endTransmission();
}
/****************************************************************/
/*Function: Read time and date from RTC */
void RTC::read()
{
void RTC::read() {
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
_second = bcdToDec(Wire.read() & 0x7f);
_minute = bcdToDec(Wire.read());
_hour = bcdToDec(Wire.read() & 0x3f);// Need to change this if 12 hour am/pm
// Need to change this if 12 hour am/pm
_hour = bcdToDec(Wire.read() & 0x3f);
_dayOfWeek = bcdToDec(Wire.read());
_day = bcdToDec(Wire.read());
_month = bcdToDec(Wire.read());
_year = bcdToDec(Wire.read()) + 2000;
}
/*******************************************************************/
/*Frunction: Write the time that includes the date to the RTC chip */
void RTC::set(uint8_t hour, uint8_t minute, uint8_t second, uint16_t day, uint8_t month, uint8_t year, uint8_t dow)
{
void RTC::set(uint8_t hour, uint8_t minute, uint8_t second, uint16_t day,
uint8_t month, uint8_t year, uint8_t dow) {
_hour = hour;
_minute = minute;
_second = second;
Expand All @@ -86,24 +88,25 @@ void RTC::set(uint8_t hour, uint8_t minute, uint8_t second, uint16_t day, uint8_

Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write(decToBcd(_second));// 0 to bit 7 starts the clock
// 0 to bit 7 starts the clock
Wire.write(decToBcd(_second));
Wire.write(decToBcd(_minute));
Wire.write(decToBcd(_hour)); // If you want 12 hour am/pm you need to set bit 6
// If you want 12 hour am/pm you need to set bit 6
Wire.write(decToBcd(_hour));
Wire.write(decToBcd(_dayOfWeek));
Wire.write(decToBcd(_day));
Wire.write(decToBcd(_month));
Wire.write(decToBcd(_year));
Wire.endTransmission();
}

void RTC::set(const char* compileTimeStamp)
{
void RTC::set(const char* compileTimeStamp) {
int i = 0;
// Serial.println(compileTimeStamp);

/*--------------------DayOfWeek----------------------------*/
char dow[4];
for(i = 0; i < 3; i++)
for (i = 0; i < 3; i++)
dow[i] = compileTimeStamp[i];
dow[i] = '\0';

Expand All @@ -112,7 +115,7 @@ void RTC::set(const char* compileTimeStamp)
else if (strcmp(dow, "Tue") == 0)
_dayOfWeek = 2;
else if (strcmp(dow, "Wed") == 0)
_dayOfWeek = 3;
_dayOfWeek = 3;
else if (strcmp(dow, "Thu") == 0)
_dayOfWeek = 4;
else if (strcmp(dow, "Fri") == 0)
Expand All @@ -127,7 +130,7 @@ void RTC::set(const char* compileTimeStamp)

/*--------------------------Month---------------------------*/
char month[4];
for(i = 0; i < 3; i++)
for (i = 0; i < 3; i++)
month[i] = compileTimeStamp[i+4];
month[i] = '\0';

Expand All @@ -136,7 +139,7 @@ void RTC::set(const char* compileTimeStamp)
else if (strcmp(month, "Feb") == 0)
_month = 2;
else if (strcmp(month, "Mar") == 0)
_month = 3;
_month = 3;
else if (strcmp(month, "Apr") == 0)
_month = 4;
else if (strcmp(month, "May") == 0)
Expand All @@ -157,21 +160,31 @@ void RTC::set(const char* compileTimeStamp)
_month = 12;

/*--------------------------Year----------------------------*/
_year = ((compileTimeStamp[20] - '0') * 1000 + (compileTimeStamp[21] - '0') * 100 + (compileTimeStamp[22] - '0') * 10 + (compileTimeStamp[23] - '0')) - 2000;
_year = ((compileTimeStamp[20] - '0') * 1000
+ (compileTimeStamp[21] - '0') * 100
+ (compileTimeStamp[22] - '0') * 10
+ (compileTimeStamp[23] - '0')) - 2000;
/*--------------------------Time----------------------------*/
_hour = ((compileTimeStamp[11] - '0') * 10 + (compileTimeStamp[12] - '0'));
_minute = ((compileTimeStamp[14] - '0') * 10 + (compileTimeStamp[15] - '0'));
_second = ((compileTimeStamp[17] - '0') * 10 + (compileTimeStamp[18] - '0'));
_hour = ((compileTimeStamp[11] - '0') * 10
+ (compileTimeStamp[12] - '0'));

_minute = ((compileTimeStamp[14] - '0') * 10
+ (compileTimeStamp[15] - '0'));

_second = ((compileTimeStamp[17] - '0') * 10
+ (compileTimeStamp[18] - '0'));

unsigned int hash = _hour * 60 * 60 + _minute * 60 + _second;

if (EEPROMReadInt(0) != hash) {
EEPROMWriteInt(0, hash);
EEPROMWriteInt(0, hash);
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write(decToBcd(_second));// 0 to bit 7 starts the clock
// 0 to bit 7 starts the clock
Wire.write(decToBcd(_second));
Wire.write(decToBcd(_minute));
Wire.write(decToBcd(_hour)); // If you want 12 hour am/pm you need to set bit 6
// If you want 12 hour am/pm you need to set bit 6
Wire.write(decToBcd(_hour));
Wire.write(decToBcd(_dayOfWeek));
Wire.write(decToBcd(_day));
Wire.write(decToBcd(_month));
Expand All @@ -180,13 +193,12 @@ void RTC::set(const char* compileTimeStamp)
}
}

void RTC::getTimeStr(char* output, uint8_t len) const
{
void RTC::getTimeStr(char* output, uint8_t len) const {
char buff[8];
if (_hour < 10)
buff[0] = '0';
else
buff[0] = char((_hour / 10)+ 48);
buff[0] = char((_hour / 10) + 48);
buff[1] = char((_hour % 10) + 48);
buff[2] = ':';

Expand All @@ -197,7 +209,7 @@ void RTC::getTimeStr(char* output, uint8_t len) const
buff[4] = char((_minute % 10) + 48);
buff[5] = ':';

if (_second < 10)
if (_second < 10)
buff[6] = '0';
else
buff[6] = char((_second / 10) + 48);
Expand All @@ -212,12 +224,9 @@ void RTC::getTimeStr(char* output, uint8_t len) const
len--;
}
output[i] = '\0';


}

void RTC::getDateStr(char* output, uint8_t len) const
{
void RTC::getDateStr(char* output, uint8_t len) const {
char buff[10];
if (_day < 10)
buff[0] = '0';
Expand Down Expand Up @@ -246,15 +255,11 @@ void RTC::getDateStr(char* output, uint8_t len) const
len--;
}
output[i] = '\0';

}

void RTC::getDOWStr(char* output, uint8_t len) const
{

void RTC::getDOWStr(char* output, uint8_t len) const {
char buff[10];
switch (_dayOfWeek)
{
switch (_dayOfWeek) {
case 1: {
strcpy(buff, "Monday");
break;
Expand All @@ -268,7 +273,7 @@ void RTC::getDOWStr(char* output, uint8_t len) const
break;
}
case 4: {
strcpy(buff,"Thursday");
strcpy(buff, "Thursday");
break;
}
case 5: {
Expand All @@ -280,7 +285,7 @@ void RTC::getDOWStr(char* output, uint8_t len) const
break;
}
case 7: {
strcpy(buff,"Sunday");
strcpy(buff, "Sunday");
break;
}
}
Expand Down Expand Up @@ -310,4 +315,4 @@ unsigned int EEPROMReadInt(int address)
byte highByte = EEPROM.read(address + 1);

return (highByte << 8) | lowByte;
}
}
69 changes: 35 additions & 34 deletions TroykaRTC.h
@@ -1,57 +1,58 @@
/****************************************************************************/
// Function: Header file for troyka-RTC
// Hardware: DS1307
// Arduino IDE: Arduino-1.6.5
// Author: Igor Dementiev
// Date: Sep 10,2015
// Version: v1.0
// by www.amperka.ru
/****************************************************************************/
#ifndef __troyka_RTC_H__
#define __troyka_RTC_H__
// Function: Header file for troyka-RTC
// Hardware: DS1307
// Arduino IDE: Arduino-1.6.5
// Author: Igor Dementiev
// Date: Sep 10,2015
// Version: v1.0
// by www.amperka.ru
/****************************************************************************/
#ifndef TROYKARTC_H_
#define TROYKARTC_H_

#include <Arduino.h>
#define DS1307_I2C_ADDRESS 0x68

class RTC
{
public:
void begin();
void start();
void stop();
void set(uint8_t hour, uint8_t minute, uint8_t second, uint16_t day, uint8_t month, uint8_t year, uint8_t dow);
void set(const char *compileTimeStamp);
void read();
void getTimeStr(char* output, uint8_t len) const;
void getDateStr(char* output, uint8_t len) const;
void getDOWStr(char* output, uint8_t len) const;

public:
void begin();
void start();
void stop();
void set(uint8_t hour, uint8_t minute, uint8_t second, uint16_t day,
uint8_t month, uint8_t year, uint8_t dow);

void set(const char *compileTimeStamp);
void read();
void getTimeStr(char* output, uint8_t len) const;
void getDateStr(char* output, uint8_t len) const;
void getDOWStr(char* output, uint8_t len) const;

uint8_t getSecond() const { return _second; }
uint8_t getMinute() const { return _minute; }
uint8_t getHour() const { return _hour; }
uint8_t getDOW() const { return _dayOfWeek; }
uint8_t getDay() const { return _day; }
uint8_t getMonth() const { return _month; }
uint16_t getYear() const { return _year; }
private:
uint8_t decToBcd(uint8_t val);
uint8_t bcdToDec(uint8_t val);

uint8_t _second;
uint8_t _minute;
uint8_t _hour;
uint8_t _dayOfWeek;// day of week, 1 = Monday
uint8_t _day;
uint8_t _month;
uint16_t _year;
private:
uint8_t decToBcd(uint8_t val);
uint8_t bcdToDec(uint8_t val);
uint8_t _second;
uint8_t _minute;
uint8_t _hour;
uint8_t _dayOfWeek; // day of week, 1 = Monday
uint8_t _day;
uint8_t _month;
uint16_t _year;
};


//Запись двухбайтового числа в память
void EEPROMWriteInt(int address, int value);


//Чтение числа из памяти
unsigned int EEPROMReadInt(int address);

#endif
#endif // TROYKARTC_H_

0 comments on commit f7a7b6d

Please sign in to comment.