#include "stm8s.h" #include "htu21.h" #include "i2c.h" #include "i2c_master_poll.h" #include "delay.h" #define HTU_T_MEASURE 0xF3 #define HTU_H_MEASURE 0xF5 static uint8_t buf[3]; static const uint8_t addr = 0x40; static t_i2c_status i2cs = I2C_SUCCESS; static uint32_t val; static uint8_t CalcSht21Crc(uint8_t *data, uint8_t nbrOfBytes); htu_err_t htu_GetTemperature(int16_t * temperature) { buf[0] = HTU_T_MEASURE; i2cs = i2c_wr_data(addr, 1, buf); //I2C_WriteBytes(addr, 1, buf); if (i2cs == 0) { Delay(100); i2c_rd_data(addr, 3, buf); //I2C_ReadBytes(addr, 3, buf); if(buf[2] == CalcSht21Crc(buf,2)) { val = buf[0]; val <<= 8; val |= (buf[1] & 0xfc); // Temp = val * 175.72 / 2^16 - 46.85 val *= 4393; // 175.72/4*100 val *= 100; // for .xx degree val += 819200; // for round val /= 1638400; // 2^16/4*100 *temperature = val - 4685; return HTU_OK; } else { *temperature = 1100; return HTU_CRC_Temp; } } else { *temperature = 9900; return HTU_I2C; } } htu_err_t htu_GetHumidity(uint16_t * humidity) { buf[0] = HTU_H_MEASURE; i2cs = i2c_wr_data(addr, 1, buf); //I2C_WriteBytes(addr, 1, buf); if (i2cs == 0) { Delay(100); i2c_rd_data(addr, 3, buf); //I2C_ReadBytes(addr, 3, buf); if(buf[2] == CalcSht21Crc(buf,2)) { val = buf[0]; val <<= 8; val |= (buf[1] & 0xfc); // Humidity = val * 125 / 2^16 - 6 val *= 125; //val *= 10; // for .x %H val += 32768; // for round val /= 65536; *humidity = val - 6; return HTU_OK; } else { *humidity = 1100; return HTU_CRC_Humidity; } } else { *humidity = 9900; return HTU_I2C; } } static uint8_t CalcSht21Crc(uint8_t *data, const uint8_t nbrOfBytes) { // CRC //const u16t POLYNOMIAL = 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001 uint8_t byteCtr,bit,crc; crc = 0; //calculates 8-Bit checksum with given polynomial for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) { crc ^= (data[byteCtr]); for (bit = 8; bit > 0; --bit) { if (crc & 0x80) crc = (crc << 1) ^ 0x131; else crc = (crc << 1); } } return(crc); }