uint8_t SHT1x::receiveByte() { // We should not be interrupted when talking to sensor taskENTER_CRITICAL(); // Receive data uint8_t data = 0; for(int i = 0; i < 8; ++i) { data <<= 1; if(sdaRead()) data |= 1; sclPulse(); delayTimer.uDelay(COM_DELAY); } // Acknowledge received data sdaLo(); delayTimer.uDelay(COM_DELAY); sclPulse(); sdaHi(); delayTimer.uDelay(COM_DELAY); taskEXIT_CRITICAL(); return data; }
/** * Send byte to sensor * * @param data Byte to send * @return True when successful, false otherwise */ bool SHT1x::sendByte(uint8_t data) { // We should not be interrupted when talking to sensor taskENTER_CRITICAL(); // Send command data for(int i = 0; i < 8; ++i) { if(data & 0b10000000) sdaHi(); else sdaLo(); data <<= 1; delayTimer.uDelay(COM_DELAY); sclPulse(); } // Read acknowledge sdaHi(); delayTimer.mDelay(5); uint8_t ack = sdaRead(); sclPulse(); taskEXIT_CRITICAL(); return !ack; }
static void i2cBitTx(uint8_t instance, char bit){ if(bit) sdaHi(instance); else sdaLo(instance); i2cDelay(instance); sclHi(instance); i2cDelay(instance); sclLo(instance); }
static void i2cStop(uint8_t instance){ i2cDelay(instance); sdaLo(instance); i2cDelay(instance); sclHi(instance); i2cDelay(instance); sdaHi(instance); }
static void i2cStart(uint8_t instance){ sdaHi(instance); sclHi(instance); i2cDelay(instance); sdaLo(instance); i2cDelay(instance); sclLo(instance); i2cDelay(instance); }
void SHT1x::startCommand() { // Command start sequence sdaHi(); sclLo(); delayTimer.uDelay(COM_DELAY); sclHi(); delayTimer.uDelay(COM_DELAY); sdaLo(); delayTimer.uDelay(COM_DELAY); sclLo(); delayTimer.uDelay(COM_DELAY); sclHi(); delayTimer.uDelay(COM_DELAY); sdaHi(); delayTimer.uDelay(COM_DELAY); sclLo(); }