/*----------------------------------------------------------------------* * Write multiple bytes to RTC RAM. * * Valid address range is 0x00 - 0xFF, no checking. * * Number of bytes (nBytes) must be between 1 and 31 (Wire library * * limitation). * * Returns the I2C status (zero if successful). * *----------------------------------------------------------------------*/ byte DS3232RTC::writeRTC(byte addr, byte *values, byte nBytes) { i2cBeginTransmission(RTC_ADDR); i2cWrite(addr); for (byte i=0; i<nBytes; i++) i2cWrite(values[i]); return i2cEndTransmission(); }
/*----------------------------------------------------------------------* * Helper method to read a 8 bit value from the given register * *----------------------------------------------------------------------*/ uint8_t I2CSoilMoistureSensor::readI2CRegister8bit(int addr, int reg) { i2cBeginTransmission(addr); i2cWrite(reg); i2cEndTransmission(); delay(20); i2cRequestFrom(addr, 1); return i2cRead(); }
/*----------------------------------------------------------------------* * Read multiple bytes from RTC RAM. * * Valid address range is 0x00 - 0xFF, no checking. * * Number of bytes (nBytes) must be between 1 and 32 (Wire library * * limitation). * * Returns the I2C status (zero if successful). * *----------------------------------------------------------------------*/ byte DS3232RTC::readRTC(byte addr, byte *values, byte nBytes) { i2cBeginTransmission(RTC_ADDR); i2cWrite(addr); if ( byte e = i2cEndTransmission() ) return e; i2cRequestFrom( (uint8_t)RTC_ADDR, nBytes ); for (byte i=0; i<nBytes; i++) values[i] = i2cRead(); return 0; }
/*----------------------------------------------------------------------* * Helper method to read a 16 bit signed value from the given register* *----------------------------------------------------------------------*/ int I2CSoilMoistureSensor::readI2CRegister16bitSigned(int addr, int reg) { i2cBeginTransmission(addr); i2cWrite(reg); i2cEndTransmission(); delay(20); i2cRequestFrom(addr, 2); int t = i2cRead() << 8; t = t | i2cRead(); return t; }
/*----------------------------------------------------------------------* * Set the RTC's time from a tmElements_t structure. * * Returns the I2C status (zero if successful). * *----------------------------------------------------------------------*/ byte DS3232RTC::write(tmElements_t &tm) { i2cBeginTransmission(RTC_ADDR); i2cWrite((uint8_t)RTC_SECONDS); i2cWrite(dec2bcd(tm.Second)); i2cWrite(dec2bcd(tm.Minute)); i2cWrite(dec2bcd(tm.Hour)); //sets 24 hour format (Bit 6 == 0) i2cWrite(tm.Wday); i2cWrite(dec2bcd(tm.Day)); i2cWrite(dec2bcd(tm.Month)); i2cWrite(dec2bcd(tmYearToY2k(tm.Year))); return i2cEndTransmission(); }
/*----------------------------------------------------------------------* * Read the current time from the RTC and return it in a tmElements_t * * structure. Returns the I2C status (zero if successful). * *----------------------------------------------------------------------*/ byte DS3232RTC::read(tmElements_t &tm) { i2cBeginTransmission(RTC_ADDR); i2cWrite((uint8_t)RTC_SECONDS); if ( byte e = i2cEndTransmission() ) return e; //request 7 bytes (secs, min, hr, dow, date, mth, yr) i2cRequestFrom(RTC_ADDR, tmNbrFields); tm.Second = bcd2dec(i2cRead() & ~_BV(DS1307_CH)); tm.Minute = bcd2dec(i2cRead()); tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock tm.Wday = i2cRead(); tm.Day = bcd2dec(i2cRead()); tm.Month = bcd2dec(i2cRead() & ~_BV(CENTURY)); //don't use the Century bit tm.Year = y2kYearToTm(bcd2dec(i2cRead())); return 0; }
/*----------------------------------------------------------------------* * Sets the RTC's time from a tmElements_t structure and clears the * * oscillator stop flag (OSF) in the Control/Status register. * * Returns the I2C status (zero if successful). * *----------------------------------------------------------------------*/ byte DS3232RTC::write(tmElements_t &tm) { i2cBeginTransmission(RTC_ADDR); i2cWrite((uint8_t)RTC_SECONDS); i2cWrite(dec2bcd(tm.Second)); i2cWrite(dec2bcd(tm.Minute)); i2cWrite(dec2bcd(tm.Hour)); //sets 24 hour format (Bit 6 == 0) i2cWrite(tm.Wday); i2cWrite(dec2bcd(tm.Day)); i2cWrite(dec2bcd(tm.Month)); i2cWrite(dec2bcd(tmYearToY2k(tm.Year))); byte ret = i2cEndTransmission(); uint8_t s = readRTC(RTC_STATUS); //read the status register writeRTC( RTC_STATUS, s & ~_BV(OSF) ); //clear the Oscillator Stop Flag return ret; }
/*----------------------------------------------------------------------* * Helper method to write an 8 bit value to the sensor via I2C to the * * given register * *----------------------------------------------------------------------*/ void I2CSoilMoistureSensor::writeI2CRegister8bit(int addr, int reg, int value) { i2cBeginTransmission(addr); i2cWrite(reg); i2cWrite(value); i2cEndTransmission(); }