Update AHT10.cpp

This commit is contained in:
enjoyneering 2020-03-20 18:49:54 -04:00 committed by GitHub
parent 66f8802f8d
commit 22cd8087d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,9 +38,10 @@
Constructor Constructor
*/ */
/**************************************************************************/ /**************************************************************************/
AHT10::AHT10(uint8_t address) AHT10::AHT10(uint8_t address, ASAIR_I2C_SENSOR sensorName)
{ {
_address = address; _address = address;
_sensorName = sensorName;
} }
/**************************************************************************/ /**************************************************************************/
@ -72,8 +73,11 @@ bool AHT10::begin(void)
Wire.setClock(100000); //experimental! AVR I2C bus speed: 31kHz..400kHz/31000..400000, default 100000 Wire.setClock(100000); //experimental! AVR I2C bus speed: 31kHz..400kHz/31000..400000, default 100000
#endif #endif
/* sensor initialization, load factory calibration coeff */ delay(AHT10_POWER_ON_DELAY); //wait for sensor to initialize
return enableFactoryCalCoeff();
setNormalMode(); //one measurement+sleep mode
return enableFactoryCalCoeff(); //load factory calibration coeff
} }
@ -88,14 +92,19 @@ uint8_t AHT10::readRawData()
{ {
/* send measurment command */ /* send measurment command */
Wire.beginTransmission(_address); Wire.beginTransmission(_address);
#if ARDUINO >= 100 #if (ARDUINO) >= 100
Wire.write(AHT10_START_MEASURMENT_CMD); //send measurment command Wire.write(AHT10_START_MEASURMENT_CMD); //send measurment command
Wire.write(AHT10_DATA_MEASURMENT_CMD); //send measurment parameter
Wire.write(AHT10_DATA_NOP); //send measurment parameter
#else #else
Wire.send(AHT10_START_MEASURMENT_CMD); Wire.send(AHT10_START_MEASURMENT_CMD);
Wire.send(AHT10_DATA_MEASURMENT_CMD);
Wire.send(AHT10_DATA_NOP);
#endif #endif
if (Wire.endTransmission(true) != 0) return AHT10_ERROR; //error handler, collision on I2C bus if (Wire.endTransmission(true) != 0) return AHT10_ERROR; //error handler, collision on I2C bus
if (getBusyBit() != 0x00) delay(AHT10_MEASURMENT_DELAY); //measurement delay if (getCalibrationBit() != 0x01) return AHT10_ERROR; //error handler, calibration coefficient turned off
if (getBusyBit(AHT10_USE_READ_DATA) != 0x00) delay(AHT10_MEASURMENT_DELAY); //measurement delay
/* read 6-bytes from sensor */ /* read 6-bytes from sensor */
#if defined(_VARIANT_ARDUINO_STM32_) #if defined(_VARIANT_ARDUINO_STM32_)
@ -110,7 +119,7 @@ uint8_t AHT10::readRawData()
} }
/* read 6 bytes from "wire.h" rxBuffer */ /* read 6 bytes from "wire.h" rxBuffer */
#if ARDUINO >= 100 #if (ARDUINO) >= 100
for (uint8_t i = 0; i < 6 ; i++) for (uint8_t i = 0; i < 6 ; i++)
{ {
_rawDataBuffer[i] = Wire.read(); _rawDataBuffer[i] = Wire.read();
@ -149,7 +158,7 @@ float AHT10::readTemperature(bool readI2C)
uint32_t temperature = ((uint32_t)(_rawDataBuffer[3] & 0x0F) << 16) | ((uint16_t)_rawDataBuffer[4] << 8) | _rawDataBuffer[5]; //20-bit raw temperature data uint32_t temperature = ((uint32_t)(_rawDataBuffer[3] & 0x0F) << 16) | ((uint16_t)_rawDataBuffer[4] << 8) | _rawDataBuffer[5]; //20-bit raw temperature data
return (float)temperature * 200 / 1048576 - 50; return (float)temperature * 0.000191 - 50;
} }
@ -160,7 +169,7 @@ float AHT10::readTemperature(bool readI2C)
Read relative humidity, % Read relative humidity, %
NOTE: NOTE:
- Prolonged exposure for 60 hours at humidity > 80% can lead to a - prolonged exposure for 60 hours at humidity > 80% can lead to a
temporary drift of the signal +3%. Sensor slowly returns to the temporary drift of the signal +3%. Sensor slowly returns to the
calibrated state at normal operating conditions. calibrated state at normal operating conditions.
- relative humidity range 0%..100% - relative humidity range 0%..100%
@ -179,7 +188,7 @@ float AHT10::readHumidity(bool readI2C)
uint32_t rawData = (((uint32_t)_rawDataBuffer[1] << 16) | ((uint16_t)_rawDataBuffer[2] << 8) | (_rawDataBuffer[3])) >> 4; //20-bit raw humidity data uint32_t rawData = (((uint32_t)_rawDataBuffer[1] << 16) | ((uint16_t)_rawDataBuffer[2] << 8) | (_rawDataBuffer[3])) >> 4; //20-bit raw humidity data
float humidity = (float)rawData * 100 / 1048576; float humidity = (float)rawData * 0.000095;
if (humidity < 0) return 0; if (humidity < 0) return 0;
if (humidity > 100) return 100; if (humidity > 100) return 100;
@ -202,7 +211,7 @@ bool AHT10::softReset(void)
{ {
Wire.beginTransmission(_address); Wire.beginTransmission(_address);
#if ARDUINO >= 100 #if (ARDUINO) >= 100
Wire.write(AHT10_SOFT_RESET_CMD); Wire.write(AHT10_SOFT_RESET_CMD);
#else #else
Wire.send(AHT10_SOFT_RESET_CMD); Wire.send(AHT10_SOFT_RESET_CMD);
@ -212,6 +221,8 @@ bool AHT10::softReset(void)
delay(AHT10_SOFT_RESET_DELAY); delay(AHT10_SOFT_RESET_DELAY);
setNormalMode(); //reinitialize sensor registers after reset
return enableFactoryCalCoeff(); //reinitialize sensor registers after reset return enableFactoryCalCoeff(); //reinitialize sensor registers after reset
} }
@ -223,24 +234,26 @@ bool AHT10::softReset(void)
Set normal measurment mode Set normal measurment mode
NOTE: NOTE:
- one measurement & power down - one measurement & power down??? no info in datasheet!!!
*/ */
/**************************************************************************/ /**************************************************************************/
bool AHT10::setNormalMode(void) bool AHT10::setNormalMode(void)
{ {
Wire.beginTransmission(_address); Wire.beginTransmission(_address);
#if ARDUINO >= 100 #if (ARDUINO) >= 100
Wire.write(AHT10_INIT_CMD); //set command mode Wire.write(AHT10_NORMAL_CMD);
Wire.write(AHT10_INIT_CAL_ENABLE); //x,0,0,x,0,x,x,x Wire.write(AHT10_DATA_NOP);
Wire.write(AHT10_DATA_NOP);
#else #else
Wire.send(AHT10_INIT_CMD); Wire.send(AHT10_NORMAL_CMD);
Wire.send(AHT10_INIT_CAL_ENABLE); Wire.send(AHT10_DATA_NOP);
Wire.send(AHT10_DATA_NOP);
#endif #endif
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete
delay(AHT10_COMMAND_DELAY); delay(AHT10_CMD_DELAY);
return true; return true;
} }
@ -260,18 +273,19 @@ bool AHT10::setCycleMode(void)
{ {
Wire.beginTransmission(_address); Wire.beginTransmission(_address);
#if ARDUINO >= 100 #if (ARDUINO) >= 100
Wire.write(AHT10_INIT_CMD); //set command mode if (_sensorName != AHT20_SENSOR) Wire.write(AHT10_INIT_CMD); //set command mode
Wire.write(AHT10_INIT_CYCLE_MODE | AHT10_INIT_CAL_ENABLE); //x,0,1,x,0,x,x,x else Wire.write(AHT20_INIT_CMD);
Wire.write(AHT10_INIT_CYCLE_MODE | AHT10_INIT_CAL_ENABLE); //0,[0,1],0,[1],0,0,0
Wire.write(AHT10_DATA_NOP);
#else #else
Wire.send(AHT10_INIT_CMD); if (_sensorName != AHT20_SENSOR) Wire.send(AHT10_INIT_CMD);
else Wire.send(AHT20_INIT_CMD);
Wire.send(AHT10_INIT_CYCLE_MODE | AHT10_INIT_CAL_ENABLE); Wire.send(AHT10_INIT_CYCLE_MODE | AHT10_INIT_CAL_ENABLE);
Wire.send(AHT10_DATA_NOP);
#endif #endif
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete
delay(AHT10_COMMAND_DELAY);
return true; return true;
} }
@ -293,7 +307,7 @@ uint8_t AHT10::readStatusByte()
if (Wire.available() != 1) return AHT10_ERROR; //check rxBuffer & error handler, collision on I2C bus if (Wire.available() != 1) return AHT10_ERROR; //check rxBuffer & error handler, collision on I2C bus
/* read byte from "wire.h" rxBuffer */ /* read byte from "wire.h" rxBuffer */
#if ARDUINO >= 100 #if (ARDUINO) >= 100
return Wire.read(); return Wire.read();
#else #else
return Wire.receive(); return Wire.receive();
@ -333,17 +347,21 @@ bool AHT10::enableFactoryCalCoeff()
/* load factory calibration coeff */ /* load factory calibration coeff */
Wire.beginTransmission(_address); Wire.beginTransmission(_address);
#if ARDUINO >= 100 #if (ARDUINO) >= 100
Wire.write(AHT10_INIT_CMD); //set command mode if (_sensorName != AHT20_SENSOR) Wire.write(AHT10_INIT_CMD); //set command mode
Wire.write(AHT10_INIT_CAL_ENABLE); //x,x,x,x,0,x,x,x else Wire.write(AHT20_INIT_CMD);
Wire.write(AHT10_INIT_CAL_ENABLE); //0,0,0,0,[1],0,0,0
Wire.write(AHT10_DATA_NOP); //0,0,0,0,0,0,0,0
#else #else
Wire.send(AHT10_INIT_CMD); if (_sensorName != AHT20_SENSOR) Wire.send(AHT10_INIT_CMD);
else Wire.send(AHT20_INIT_CMD);
Wire.send(AHT10_INIT_CAL_ENABLE); Wire.send(AHT10_INIT_CAL_ENABLE);
Wire.send(AHT10_DATA_NOP);
#endif #endif
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete
delay(AHT10_COMMAND_DELAY); delay(AHT10_CMD_DELAY);
/*check calibration enable */ /*check calibration enable */
if (getCalibrationBit() == 0x01) return true; if (getCalibrationBit() == 0x01) return true;
@ -358,8 +376,8 @@ bool AHT10::enableFactoryCalCoeff()
Read busy bit from status byte Read busy bit from status byte
NOTE: NOTE:
- 0, sensor idle - 0, sensor idle & sleeping
- 1, sensor busy - 1, sensor busy & in measurement state
*/ */
/**************************************************************************/ /**************************************************************************/
uint8_t AHT10::getBusyBit(bool readI2C) uint8_t AHT10::getBusyBit(bool readI2C)