|
楼主 |
发表于 2017-4-27 15:10:07
|
显示全部楼层
- bool SHT30_Get_Temp_And_Humi(int16_t *temperature, uint8_t *humiditiy)
- {
- uint8_t command[2] = {0};
- uint8_t data[6] = {0};
- uint16_t t_tmp = 0; /* temperature tmp */
- uint16_t h_tmp = 0; /* humiditiy tmp */
- uint16_t num_of_bytes = 0;
- init_I2C(SHT30_SLAVE_ADDRESS,I2C_400K_BAUDRATE);
- /* soft reset sht30 */
- command[0] = 0x30;
- command[1] = 0xA2;
- if(I2C_send_bytes(command, 2, &num_of_bytes) != I2C_SUCCESS)
- {
- /* soft reset error */
- return false;
- }
- if(I2C_stop() != I2C_SUCCESS)
- {
- /* stop I2C error */
- return false;
- }
- /* between soft reset command and sensor entering idle state : 0.5S(MAX) */
- DELAY_MS(1);
- /* start measurement in clock stretching mode(high repeatability) */
- command[0] = 0x2C;
- command[1] = 0x06;
- if(I2C_send_bytes(command, 2, &num_of_bytes) != I2C_SUCCESS)
- {
- /* measurement error */
- return false;
- }
- if(I2C_stop() != I2C_SUCCESS)
- {
- /* stop I2C error */
- return false;
- }
- /* Duration for a humidity and temperature measurement : 13.5S(MAX) */
- DELAY_MS(15);
- if(I2C_read_bytes(data, 6, &num_of_bytes) == I2C_SUCCESS)
- {
- if(SHT30_Calc_CRC(data, 2) != data[2])
- {
- /* temperature data CRC error */
- return false;
- }
- if(SHT30_Calc_CRC((data + 3), 2) != data[5])
- {
- I2C_stop();
- /* humidity data CRC error */
- return false;
- }
- /* combine the two bytes to a 16-bit value */
- t_tmp = (data[0] << 8) | data[1];
- h_tmp = (data[3] << 8) | data[4];
- /* calculate temperature [°C] T*10 = -45 + 175 * tmp / (2^16-1) */
- *temperature = (int16_t)(1750.0f * (float)t_tmp / 65535.0f - 450.0f);
- /* calculate relative humidity [%RH] RH = tmp / (2^16-1) * 100*/
- *humiditiy = (uint8_t)(100.0f * (float)h_tmp / 65535.0f);
- }
- else
- {
- /* read data error */
- return false;
- }
- if(I2C_stop() == I2C_SUCCESS)
- {
- /* read success */
- return true;
- }
- else
- {
- /* stop I2C error */
- return false;
- }
- }
复制代码 |
|