Steven Cooreman
/
i2c-rht
Basic version of reading the temperature with a Si7021 sensor on the Happy Gecko STK board.
Revision 1:cc6bfdd667ea, committed 2016-03-17
- Comitter:
- stevew817
- Date:
- Thu Mar 17 11:33:02 2016 +0000
- Parent:
- 0:2629b8684f00
- Commit message:
- Update measurement algo
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Mar 17 08:44:50 2016 +0000 +++ b/main.cpp Thu Mar 17 11:33:02 2016 +0000 @@ -7,8 +7,10 @@ /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ /** Si7013 Read Temperature Command */ -#define SI7013_READ_TEMP 0xE0 /* Read previous T data from RH measurement +#define SI7013_READ_TEMP_POST 0xE0 /* Read previous T data from RH measurement command*/ +#define SI7013_READ_TEMP 0xE3 /* Stand-alone read temperature command */ + /** Si7013 Read RH Command */ #define SI7013_READ_RH 0xE5 /* Perform RH (and T) measurement. */ /** Si7013 Read ID */ @@ -57,20 +59,32 @@ ******************************* FUNC *********************************** ******************************************************************************/ -int32_t readTemperature(void) { +void readSensor(void) { int temp = 0; + unsigned int humidity = 0; + + //send humidity command + _tx_buf[0] = SI7013_READ_RH; + tempSensor.write(_address, (char*)_tx_buf, 1); + tempSensor.read(_address, (char*)_rx_buf, 2); + + /* Store raw RH info */ + humidity = ((uint32_t)_rx_buf[0] << 8) + (_rx_buf[1] & 0xFC); + /* Convert value to milli-percent */ + humidity = (((humidity) * 15625L) >> 13) - 6000; //send temperature command - _tx_buf[0] = SI7013_READ_TEMP; + _tx_buf[0] = SI7013_READ_TEMP_POST; tempSensor.write(_address, (char*)_tx_buf, 1); tempSensor.read(_address, (char*)_rx_buf, 2); /* Store raw temperature info */ temp = ((uint32_t)_rx_buf[0] << 8) + (_rx_buf[1] & 0xFC); /* Convert to milli-degC */ - temp = (((_tData) * 21965L) >> 13) - 46850; + temp = (((temp) * 21965L) >> 13) - 46850; - return temp; + _tData = temp; + _rhData = humidity; } int main() { @@ -93,7 +107,7 @@ while(1) { wait(1); - _tData = readTemperature(); + readSensor(); printf("Temperature: %d,%3d\r\n", _tData / 1000, _tData % 1000); } } \ No newline at end of file