Exemplo n.º 1
0
void Rtc_Pcf8563::getDate()
{  
	/* set the start byte of the date data */
	Wire.beginTransmission(Rtcc_Addr);
 	Wire.send(RTCC_DAY_ADDR);
 	Wire.endTransmission();
  	
 	Wire.requestFrom(Rtcc_Addr, 4); //request 4 bytes
	//0x3f = 0b00111111
	day = bcdToDec(Wire.receive() & 0x3f);
	//0x07 = 0b00000111
	weekday = bcdToDec(Wire.receive() & 0x07);
	//get raw month data byte and set month and century with it.
	month = Wire.receive();
	if (month & RTCC_CENTURY_MASK) {
		century = 1;
	}
	else {
		century = 0;
	}
	//0x1f = 0b00011111
	month = month & 0x1f;
	month = bcdToDec(month);
	year = bcdToDec(Wire.receive());  
}
Exemplo n.º 2
0
/**
* Get alarm, set values to RTCC_NO_ALARM (99) if alarm flag is not set
*/
void Rtc_Pcf8563::getAlarm()
{
    /* set the start byte of the alarm data */
    Wire.beginTransmission(Rtcc_Addr);
    Wire.send(RTCC_ALRM_MIN_ADDR);
    Wire.endTransmission();

    Wire.requestFrom(Rtcc_Addr, 4); //request 4 bytes
    alarm_minute = Wire.read();
    if(B10000000 & alarm_minute){
        alarm_minute = RTCC_NO_ALARM;
    } else {
        alarm_minute = bcdToDec(alarm_minute & B01111111);
    }
    alarm_hour = Wire.read();
    if(B10000000 & alarm_hour){
        alarm_hour = RTCC_NO_ALARM;
    } else {
        alarm_hour = bcdToDec(alarm_hour & B00111111);
    }
    alarm_day = Wire.read();
    if(B10000000 & alarm_day){
        alarm_day = RTCC_NO_ALARM;
    } else {
        alarm_day = bcdToDec(alarm_day  & B00111111);
    }
    alarm_weekday = Wire.read();
    if(B10000000 & alarm_weekday){
        alarm_weekday = RTCC_NO_ALARM;
    } else {
        alarm_weekday = bcdToDec(alarm_weekday  & B00000111);
    }
}
Exemplo n.º 3
0
int RealTimeClockDS1307::getHours()
{
  if(is12hour()) { 
    //do not include bit 5, the am/pm indicator
    return bcdToDec(_reg2_hour & 0x1f);
  }
  //bits 4-5 are tens of hours
  return bcdToDec(_reg2_hour & 0x3f);
}
Exemplo n.º 4
0
void Time_Adj_1224hrs(BOOL h12)
{
	BYTE hours, bcd10, bcd;

	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	if(datetime._02h.bits_24hrs._1224hr != h12)
	{
		if(h12)
		{
			// Convert from 24h to 12h
			bcdToDec(datetime._02h.bits_24hrs.hours10, datetime._02h.bits_24hrs.hours, &hours);
			if(hours >= 12)
			{
				datetime._02h.bits_ampm.ampm = 1;
				if(hours > 12) hours -= 12;
			}
			else
			{
				datetime._02h.bits_ampm.ampm = 0;
				if(hours == 0) hours = 12;
			}
			decToBcd(&bcd10 , &bcd, hours);
			datetime._02h.bits_ampm.hours10 = bcd10;
			datetime._02h.bits_ampm.hours = bcd;
		}
		else
		{
			// Convert from 12h to 24h
			bcdToDec(datetime._02h.bits_ampm.hours10, datetime._02h.bits_ampm.hours, &hours);
			if(datetime._02h.bits_ampm.ampm == 1 && hours != 12)
			{
				hours += 12;
			}
			else if(datetime._02h.bits_ampm.ampm == 0 && hours == 12)
			{
				hours = 0;
			}
			decToBcd(&bcd10 , &bcd, hours);
			datetime._02h.bits_24hrs.hours10 = bcd10;
			datetime._02h.bits_24hrs.hours = bcd;
		}
	}

	datetime._02h.bits_24hrs._1224hr = h12;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
Exemplo n.º 5
0
void Rtc_Pcf8563::getTime()
{  
	/* set the start byte , get the 2 status bytes */
	Wire.beginTransmission(Rtcc_Addr);
 	Wire.send(RTCC_STAT1_ADDR);
 	Wire.endTransmission();
  	
 	Wire.requestFrom(Rtcc_Addr, 5); //request 5 bytes
 	status1 = Wire.receive();
 	status2 = Wire.receive();
 	//0x7f = 0b01111111
 	sec = bcdToDec(Wire.receive() & 0x7f);
	minute = bcdToDec(Wire.receive() & 0x7f);
	//0x3f = 0b00111111
	hour = bcdToDec(Wire.receive() & 0x3f);
}
Exemplo n.º 6
0
bool RTCx::readTimeSaver(struct tm *tm, uint8_t sz) const
{
  Wire.requestFrom(address, sz);
  tm->tm_sec = 0;
  tm->tm_min = bcdToDec(Wire.read() & 0x7f);
  tm->tm_hour = bcdToDec(Wire.read() & 0x3f);
  tm->tm_wday = 0;
  tm->tm_mday = bcdToDec(Wire.read() & 0x3f);
  uint8_t wdayMonth = Wire.read();
  tm->tm_mon = bcdToDec(wdayMonth & 0x1f) - 1; // Clock uses [1..12]
  tm->tm_wday = (wdayMonth >> 5) - 1; // Clock uses [1..7]
  tm->tm_year = (RTCX_EPOCH - 1900); // not stored
  tm->tm_yday = -1;
  Wire.endTransmission();
  return true;
}
Exemplo n.º 7
0
void Time_Adj_S(BOOL direction)
{
	BYTE seconds, bcd10, bcd;
	
	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	bcdToDec(datetime._00h.bits.seconds10, datetime._00h.bits.seconds, &seconds);

	if(direction == TIME_UP)
	{
		if(seconds < 59) seconds++;
		else seconds = 0;
	}
	else
	{
		if(seconds > 0) seconds--;
		else seconds = 59;
	}

	decToBcd(&bcd10, &bcd, seconds);

	datetime._00h.bits.seconds = bcd;
	datetime._00h.bits.seconds10 = bcd10;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
Exemplo n.º 8
0
void Time_Adj_Mi(BOOL direction)
{
	BYTE minutes, bcd10, bcd;

	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	bcdToDec(datetime._01h.bits.minutes10, datetime._01h.bits.minutes, &minutes);

	if(direction == TIME_UP)
	{
		if(minutes < 59) minutes++;
		else minutes = 0;
	}
	else
	{
		if(minutes > 0) minutes--;
		else minutes = 59;
	}

	decToBcd(&bcd10, &bcd, minutes);

	datetime._01h.bits.minutes = bcd;
	datetime._01h.bits.minutes10 = bcd10;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
Exemplo n.º 9
0
bool Nanoshield_RTC::writeYear(int year)
{
	int mon;

  // Address register containing the months and century
  Wire.beginTransmission(i2cAddr);
  Wire.write(monthAddr);
  if (Wire.endTransmission()) return false;

	// Read month register
  if (Wire.requestFrom((int)i2cAddr, 1) != 1) return false;
  mon = bcdToDec(Wire.read() & 0x1F);

	// Rewrite month along with century bit
  Wire.beginTransmission(i2cAddr);
  Wire.write(monthAddr);              // Start address
	if (year / 100 == 19) {             // Set century bit to zero if 20th century
		Wire.write(decToBcd(mon) & 0x7F); // Month (1-12, century bit (bit 7) = 0)
	} else {
		Wire.write(decToBcd(mon) | 0x80); // Month (1-12, century bit (bit 7) = 1)
	}
  if (Wire.endTransmission() != 0) return false;

	// Write year
  Wire.beginTransmission(i2cAddr);
  Wire.write(yearAddr);              // Start address
  Wire.write(decToBcd(year % 100));  // Year (00-99)
  return Wire.endTransmission() == 0;
}
Exemplo n.º 10
0
void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.receive());
  int minute = bcdToDec(Wire.receive());
  int hour = bcdToDec(Wire.receive() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.receive()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.receive());
  int month = bcdToDec(Wire.receive());
  int year = bcdToDec(Wire.receive());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}
Exemplo n.º 11
0
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();
  
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
  
}
Exemplo n.º 12
0
/**
 * Die Uhrzeit auslesen und in den Variablen ablegen
 */
void MyRTC::readTime() {
    byte returnStatus, count, result, retries = 0;
    do {
        // Reset the register pointer
        Wire.beginTransmission(_address);
        Wire.write((uint8_t) 0x00);
        result = Wire.endTransmission(false); // false, damit der Bus nicht freigegeben wird und eventuell andere dazwischen kommen (in Multi-MCU-Umgebungen)
        DEBUG_PRINT(F("Wire.endTransmission(false) = "));
        DEBUG_PRINTLN(result);

        count = Wire.requestFrom(_address, 7);
        DEBUG_PRINT(F("Wire.requestFrom(_address, 7) = "));
        DEBUG_PRINTLN(count);
        DEBUG_FLUSH();

        if (count == 7) {
            // Success
            // A few of these need masks because certain bits are control bits
            _seconds = bcdToDec(Wire.read() & 0x7f);
            _minutes = bcdToDec(Wire.read());
            _hours = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
            _dayOfWeek = bcdToDec(Wire.read());
            _date = bcdToDec(Wire.read());
            _month = bcdToDec(Wire.read());
            _year = bcdToDec(Wire.read());
        } else {
            // Fail
            // keine 7 Byte zurueck gekommen? Buffer verwerfen...
            for (int i = 0; i < count; i++) {
                Wire.read();
            }
            retries++;
        }

        result = Wire.endTransmission(true); // true, jetzt den Bus freigeben.
        DEBUG_PRINT(F("Wire.endTransmission(true) = "));
        DEBUG_PRINTLN(result);
    } while ((count != 7) && (retries < 8));

    if (retries == 8) {
        // Es konnte nichts gelesen werden
        _seconds = 11;
        _minutes = 11;
        _hours = 11;
        _dayOfWeek = 1;
        _date = 1;
        _month = 1;
        _year = 2014;
    }

    DEBUG_PRINT(F("Time: "));
    DEBUG_PRINT(getHours());
    DEBUG_PRINT(F(":"));
    DEBUG_PRINT(getMinutes());
    DEBUG_PRINT(F(":"));
    DEBUG_PRINTLN(getSeconds());
    DEBUG_FLUSH();
}
Exemplo n.º 13
0
	DateTime I2CFlexel::getTimeAndDate()
	{
		byte timeAndDate[7];
		getTimeAndDate(timeAndDate);
		return DateTime(
			bcdToDec(timeAndDate[0]), bcdToDec(timeAndDate[1]), bcdToDec(timeAndDate[2]), bcdToDec(timeAndDate[3]),
			bcdToDec(timeAndDate[4]), bcdToDec(timeAndDate[5]), bcdToDec(timeAndDate[6])
		);
	}
Exemplo n.º 14
0
/*Function: Read time and date from RTC    */
void rtc_ds1307_get_time(struct __time * time_get)
{
    uint8 dta[1] = {0x00};
    suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 1);
    suli_i2c_read(__I2C_Device_RTC, DS1307_I2C_ADDRESS, time_get->data, 7);
    
    time_get->data[0] &= 0x7f;
    time_get->data[2] &= 0x3f;
    for(int i=0; i<7; i++)
    {
        time_get->data[i] = bcdToDec(time_get->data[i]);
    }
}
Exemplo n.º 15
0
void Time_Adj_Y(BOOL direction)
{
	BYTE date, month, year, bcd10, bcd;

	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	bcdToDec(datetime._04h.bits.date10, datetime._04h.bits.date, &date);
	bcdToDec(datetime._05h.bits.month10, datetime._05h.bits.month, &month);
	bcdToDec(datetime._06h.bits.year10, datetime._06h.bits.year, &year);

	bcd10 = datetime._06h.bits.year10;
	bcd = datetime._06h.bits.year;

	if(direction == TIME_UP)
	{
		year++;
		if(isValidDate(date, month, year))
		{
			decToBcd(&bcd10, &bcd, year);
		}
	}
	else
	{
		year--;
		if(isValidDate(date, month, year))
		{
			decToBcd(&bcd10, &bcd, year);
		}
	}

	datetime._06h.bits.year10 = bcd10;
	datetime._06h.bits.year = bcd;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
Exemplo n.º 16
0
/**
 * Die Uhrzeit auslesen und in den Variablen ablegen
 */
void DS1307::readTimeOnly() {
  // Reset the register pointer
  Wire.beginTransmission(_address);
  Wire.send(0x00);
  Wire.endTransmission();
  // Statt 7 nur einen Teil ermitteln, Datum wird aktuell nicht benötigt
  Wire.requestFrom(_address, 3);

  // A few of these need masks because certain bits are control bits
  _seconds = bcdToDec(Wire.receive() & 0x7f);
  _minutes = bcdToDec(Wire.receive());
  _hours = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm

  _dayOfWeek = 0;
  _date = 0;
  _month = 0;
  _year = 0;  

// Wird aktuell nicht mehr benötigt
//  _dayOfWeek = bcdToDec(Wire.receive());
//  _date = bcdToDec(Wire.receive());
//  _month = bcdToDec(Wire.receive());
//  _year = bcdToDec(Wire.receive());  
}
Exemplo n.º 17
0
void Lamp::setDateTime(){

  byte second =      0;
  byte minute =      tMinute;
  byte hour =        tHour;
  byte weekDay =     tDay;
  byte monthDay =    tDate;
  byte month =       tMonth;
  byte year  =       tYear;


  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0x00); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay-1));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(0x00); //start 
  Wire.endTransmission();



  Wire.beginTransmission(DS1307_ADDRESS);

  Wire.write(0x00);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);



  tSecond = bcdToDec(Wire.read());
  tMinute = bcdToDec(Wire.read());
  tHour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  tDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  tDate = bcdToDec(Wire.read());
  tMonth =bcdToDec(Wire.read());
  tYear =bcdToDec(Wire.read());
  tHour12 = tHour;
  if (tHour > 12)   tHour12 = tHour-12;

  //print the date EG   3/1/11 23:59:59



}
Exemplo n.º 18
0
bool ArduRCT_RealTimeClock::_getMCP7941xTime() {
    // initialize the RTC
    if (_mcp7941xStatus == RTC_MCP7941X_NOT_STARTED) {
        Wire.begin();
        // enable clock
        Wire.beginTransmission(RTC_MCP7941X_ADDRESS);
        Wire.write(RTC_MCP7941X_SECONDS);
        Wire.endTransmission();
        Wire.requestFrom(RTC_MCP7941X_ADDRESS, 1);
        uint8_t seconds = Wire.read();
        if ((seconds & 0x80) == 0) {
            Wire.beginTransmission(RTC_MCP7941X_ADDRESS);
            Wire.write(RTC_MCP7941X_SECONDS);
            Wire.write(seconds | 0x80);  // set second and enable clock (10000000)
            Wire.endTransmission();
        }
        // enable battery: set 1 in bit 3 of DOW
        Wire.beginTransmission(RTC_MCP7941X_ADDRESS);
        Wire.write(RTC_MCP7941X_DOW);
        Wire.endTransmission();
        Wire.requestFrom(RTC_MCP7941X_ADDRESS, 1);
        uint8_t dow = Wire.read() & 0x0F;
        if ((dow & 0x08) == 0) {
            Wire.beginTransmission(RTC_MCP7941X_ADDRESS);
            Wire.write(RTC_MCP7941X_DOW);
            Wire.write(dow | 0x08);  // set dow and enable battery (00001000)
            Wire.endTransmission();
        }
        _mcp7941xStatus = RTC_MCP7941X_STARTED;
    } 
    
    // get the seconds from the RTC chip
    Wire.beginTransmission(RTC_MCP7941X_ADDRESS);
    Wire.write(RTC_MCP7941X_SECONDS);
    Wire.endTransmission();
    Wire.requestFrom(RTC_MCP7941X_ADDRESS, 1);
    uint8_t second = _second;
    _second = bcdToDec(Wire.read() & 0x7F);          // 01111111
    if (_second == second) return false;
    
    // get the rest of the values if required
    Wire.requestFrom(RTC_MCP7941X_ADDRESS, 6);
    _minute = bcdToDec(Wire.read() & 0x7F);          // 01111111
    _hour = bcdToDec(Wire.read() & 0x3F);            // 00111111
    _dayOfWeek = bcdToDec(Wire.read() & 0x07) - 1;   // 01111111
    _day = bcdToDec(Wire.read() & 0x3F);             // 00111111
    _month = bcdToDec(Wire.read() & 0x1F);           // 00011111
    _year = RTC_MCP7941X_YEAR_OFFSET + bcdToDec(Wire.read()); // 11111111
    
    return true;
}
Exemplo n.º 19
0
/* Read a time from the clock. The same function is also used to read
 * the alarms as the register layout is essentially identical but with
 * week day and year omitted.
 */
bool RTCx::readClock(struct tm *tm, timeFunc_t func) const
{
  // Find which register to read from
  uint8_t sz = 0;
  uint8_t reg = getRegister(func, sz);
  
  if (sz == 0)
    return false; // not supported

  if (device == MCP7941x &&
      (func == TIME_POWER_FAILED || func == TIME_POWER_RESTORED))
    return readTimeSaver(tm, sz);

  while (true) {
    // Reset the register pointer
    Wire.beginTransmission(address);
    Wire.write(reg);
    Wire.endTransmission();
    
    Wire.requestFrom(address, sz);
    tm->tm_sec = bcdToDec(Wire.read() & 0x7f);
    tm->tm_min = bcdToDec(Wire.read() & 0x7f);
    uint8_t h = Wire.read();
    if (h & 0x40) {
      // Twelve hour mode
      tm->tm_hour = bcdToDec(h & 0x1f);
      if (h & 0x20)
	tm->tm_hour += 12; // Seems notation for AM/PM is user-defined
    }
    else 
      tm->tm_hour = bcdToDec(h & 0x3f);
    tm->tm_wday = (Wire.read() & 0x03) - 1; // Clock uses [1..7]
    tm->tm_mday = bcdToDec(Wire.read() & 0x3f);
    tm->tm_mon = bcdToDec(Wire.read() & 0x1f) - 1; // Clock uses [1..12]
    if (sz == 7)
      tm->tm_year = bcdToDec(Wire.read()) + 100; // Assume 21st century
    else
      tm->tm_year = (RTCX_EPOCH - 1900);
    tm->tm_yday = -1;
    Wire.endTransmission();

    if ((func != TIME) || (tm->tm_sec == bcdToDec(readData(0) & 0x7f)))
      break;
  }
  return true;
}
Exemplo n.º 20
0
void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year){
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
Exemplo n.º 21
0
void Lamp::getDate(){

  Wire.beginTransmission(DS1307_ADDRESS);

  Wire.write(0x00);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  tSecond = bcdToDec(Wire.read());
  tMinute = bcdToDec(Wire.read());
  tHour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  tDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  tDate = bcdToDec(Wire.read());
  tMonth =bcdToDec(Wire.read());
  tYear =bcdToDec(Wire.read());

  tHour12 = tHour;
  if (tHour > 12)   tHour12 = tHour-12;



  if (!_alarming)
  {
    bool isAM =false;
    if (tHour<12) isAM = true;

    if (tAlarmStarted != tMinute)
    {
      tAlarmStarted = 128;
    }   
    if (getAlarmOn()==true && (isAM == getAlarmAM()) && getAlarmHour() == tHour12 && getAlarmMin() == tMinute && tMinute != tAlarmStarted )
    {
      _alarming = true;
      tAlarmStarted=tMinute;
    }

  }


}
Exemplo n.º 22
0
/*Function: Read time and date from RTC */
void RTC::read() {
// Reset the register pointer
    Wire.beginTransmission(DS1307_I2C_ADDRESS);
    Wire.write((uint8_t)0x00);
    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());
    // 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;
}
Exemplo n.º 23
0
/*Function: Read time and date from RTC	*/
void DS1307::getTime()
{
    // Reset the register pointer
	Wire1.beginTransmission(DS1307_I2C_ADDRESS);
	Wire1.write((uint8_t)0x00);
	Wire1.endTransmission();  
	Wire1.requestFrom(DS1307_I2C_ADDRESS, 7);
	// A few of these need masks because certain bits are control bits
	second	   = bcdToDec(Wire1.read() & 0x7f);
	minute	   = bcdToDec(Wire1.read());
	hour	   = bcdToDec(Wire1.read() & 0x3f);// Need to change this if 12 hour am/pm
	dayOfWeek  = bcdToDec(Wire1.read());
	dayOfMonth = bcdToDec(Wire1.read());
	month      = bcdToDec(Wire1.read());
	year	   = bcdToDec(Wire1.read());
}
Exemplo n.º 24
0
// Disable the clock without changing the date/time:
void MCP7941x::disableClock()
{
  // Get the current seconds value as the enable/disable bit is in the same
  // byte of memory as the seconds value:
  Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
  WireSend(RTC_LOCATION);
  Wire.endTransmission();

  Wire.requestFrom(MCP7941x_RTC_I2C_ADDR, 1);
  
  int second = bcdToDec(WireReceive() & 0x7f);  // 01111111

  // Start Clock:
  Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
  WireSend(RTC_LOCATION);
  WireSend(decToBcd(second));     // set seconds and disable clock (01111111)
  Wire.endTransmission();
}
Exemplo n.º 25
0
// Enable the battery:
void MCP7941x::enableBattery()
{
  // Get the current seconds value as the enable/disable bit is in the same
  // byte of memory as the seconds value:
  Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
  WireSend(RTC_LOCATION + 0x03);
  Wire.endTransmission();

  Wire.requestFrom(MCP7941x_RTC_I2C_ADDR, 1);
  
  int day = bcdToDec(WireReceive() & 0x07);  // 00000111

  // Start Clock:
  Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
  WireSend(RTC_LOCATION + 0x03);
  WireSend(decToBcd(day) | 0x08);     // set day and enable battery (00001000)
  Wire.endTransmission();
}
Exemplo n.º 26
0
// get the time from the rtc, populates a supplied tm struct
// returns true to indicate success
bool ICACHE_FLASH_ATTR ds1307_getTime(struct tm *time) {

	int loop;
	uint8 data[7];

	// start register address
	data[0] = DS1307_ADDR_TIME;
	if (!ds1307_send(data, 1)) {
		return false;
	}

	// read time
	if (!ds1307_recv(data, 7)) {
		return false;
	}

	// convert to unix time structure
	time->tm_sec = bcdToDec(data[0]);
	time->tm_min = bcdToDec(data[1]);
	if (data[2] & DS1307_12HOUR_FLAG) {
		// 12h
		time->tm_hour = bcdToDec(data[2] & DS1307_12HOUR_MASK);
		// pm?
		if (data[2] & DS1307_PM_FLAG) time->tm_hour += 12;
	} else {
		// 24h
		time->tm_hour = bcdToDec(data[2]);
	}
	time->tm_wday = bcdToDec(data[3]) - 1;
	time->tm_mday = bcdToDec(data[4]);
	time->tm_mon  = bcdToDec(data[5]) - 1;
	time->tm_year = bcdToDec(data[6]) + 100;
	time->tm_isdst = 0;

	// apply a time zone (if you are not using localtime on the rtc or you want to check/apply DST)
	//applyTZ(time);

	return true;
	
}
Exemplo n.º 27
0
void DS1307::get_time() {
	LOG << "get_time - Entered";

	char buffer[10];

	buffer[0] = (char)0;
	i2c_bus->write_bytes(buffer, 1);
	int bytes_read = i2c_bus->read_bytes(7, buffer);
	assert(bytes_read == 7);
	LOG << dec << "0 - SECOND - " << int(bcdToDec(buffer[0] & 0x7f));
	LOG << dec << "1 - MINUTE - " << int(bcdToDec(buffer[1]));
	LOG << dec << "2 - HOUR - " << int(bcdToDec(buffer[2] & 0x3f)) << " " << int(buffer[2]); // UTC 8 hours behind.
	LOG << dec << "3 - DayOfWeek - " << int(bcdToDec(buffer[3]));
	LOG << dec << "4 - DayOfMonth - " << int(bcdToDec(buffer[4])) << " " << buffer[4];
	LOG << dec << "5 - Month - " << int(bcdToDec(buffer[5]));
	LOG << dec << "6 - Year - " << int(bcdToDec(buffer[6]));

}
Exemplo n.º 28
0
byte timer1RefreshTimeCallBack(unsigned char result) {
  switch(result) {
    case TW_MR_DATA_NACK : //Результат получен
      zs042_seconds = bcdToDec(commandI2CData.reciveBuf[0]);
      current_time.minut = bcdToDec(commandI2CData.reciveBuf[1]);
      current_time.hour = bcdToDec(commandI2CData.reciveBuf[2]);
      current_time.dayOfWeek = bcdToDec(commandI2CData.reciveBuf[3]);
      current_time.dayOfMonth = bcdToDec(commandI2CData.reciveBuf[4]);
      current_time.month = bcdToDec(commandI2CData.reciveBuf[5]);
      break;
    default : 
      _log(ERR_I2C(result));
  }
  return 0;
}
Exemplo n.º 29
0
void DS1307::loadFromRTC(){
  int16_t zero=0;
  int8_t oldZone;

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);
  bTime.sec = bcdToDec(Wire.read());
  bTime.min = bcdToDec(Wire.read());
  bTime.hour = bcdToDec(Wire.read() & 0b111111);
  bTime.wday = bcdToDec(Wire.read());
  bTime.mday = bcdToDec(Wire.read());
  bTime.mon = bcdToDec(Wire.read());
  bTime.year = 2000+bcdToDec(Wire.read());
  oldZone=bTime.zone;
  bTime.zone = 0; // RTC always UTC
  changeZone(oldZone); // convert to local time
}
Exemplo n.º 30
0
RTCTime RTCClass::time()
{
  RTCTime tm;
  
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  tm.second     = bcdToDec(Wire.receive() & 0x7f);
  tm.minute     = bcdToDec(Wire.receive());
  tm.hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  tm.dayOfWeek  = bcdToDec(Wire.receive());
  tm.day = bcdToDec(Wire.receive());
  tm.month      = bcdToDec(Wire.receive());
  tm.year       = bcdToDec(Wire.receive());

  return tm;
}