void DS3231_get(struct ts *t) { uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year uint8_t century = 0; uint8_t i, n; uint16_t year_full; Wire.beginTransmission(DS3231_I2C_ADDR); Wire.write(DS3231_TIME_CAL_ADDR); Wire.endTransmission(); uint8_t gotData = false; uint32_t start = millis(); // start timeout while(millis()-start < DS3231_TRANSACTION_TIMEOUT){ if (Wire.requestFrom(DS3231_I2C_ADDR, 7) == 7) { gotData = true; break; } delay(2); } if (!gotData) return; // error timeout for (i = 0; i <= 6; i++) { n = Wire.read(); if (i == 5) { TimeDate[5] = bcdtodec(n & 0x1F); century = (n & 0x80) >> 7; } else
// type==0 returns hh:mm:ss YYMMDD // type==1 returns YYYYMMDD // type==2 returns D // type==3 returns YYYYMMDD hh:mm:ss void DS3234_get(uint8_t pin, unsigned char type, char *buf, size_t len) { uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year uint8_t century = 0; uint8_t i, n; uint16_t year_full; for (i = 0; i <= 6; i++) { digitalWrite(pin, LOW); SPI.transfer(i + 0x00); n = SPI.transfer(0x00); digitalWrite(pin, HIGH); if (i == 5) { // month address also contains the century on bit7 TimeDate[5] = bcdtodec(n & 0x1F); century = (n & B10000000) >> 7; } else {
void DS3234_get(const uint8_t pin, struct ts *t) { uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year uint8_t century = 0; uint8_t i, n; uint16_t year_full; SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1)); for (i = 0; i <= 6; i++) { digitalWrite(pin, LOW); SPI.transfer(i + 0x00); n = SPI.transfer(0x00); digitalWrite(pin, HIGH); if (i == 5) { // month address also contains the century on bit7 TimeDate[5] = bcdtodec(n & 0x1F); century = (n & 0x80) >> 7; } else {
void DS3231_get(struct ts *t) { uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year uint8_t century = 0; uint8_t i, n; uint16_t year_full; Wire.beginTransmission(DS3231_I2C_ADDR); Wire.write(DS3231_TIME_CAL_ADDR); Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDR, 7); for (i = 0; i <= 6; i++) { n = Wire.read(); if (i == 5) { TimeDate[5] = bcdtodec(n & 0x1F); century = (n & 0x80) >> 7; } else
{ uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year uint8_t century = 0; uint8_t i, n; uint16_t year_full; for (i = 0; i <= 6; i++) { digitalWrite(pin, LOW); SPI.transfer(i + 0x00); n = SPI.transfer(0x00); digitalWrite(pin, HIGH); if (i == 5) { // month address also contains the century on bit7 TimeDate[5] = bcdtodec(n & 0x1F); century = (n & B10000000) >> 7; } else { TimeDate[i] = bcdtodec(n); } } if (century == 1) year_full = 2000 + TimeDate[6]; else year_full = 1900 + TimeDate[6]; if (type == 0) { snprintf(buf, len, "%02d:%02d:%02d %d%02d%02d", TimeDate[2], TimeDate[1], TimeDate[0], year_full, TimeDate[5], TimeDate[4]); } else if (type == 1) { snprintf(buf, len, "%d%02d%02d", year_full, TimeDate[5], TimeDate[4]); } else if (type == 2) { snprintf(buf, len, "%d", TimeDate[4]);