12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #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);
-
- if (i2cs == 0) {
- Delay(100);
- i2c_rd_data(addr, 3, buf);
-
- if(buf[2] == CalcSht21Crc(buf,2)) {
- val = buf[0];
- val <<= 8;
- val |= (buf[1] & 0xfc);
-
- val *= 4393;
- val *= 100;
- val += 819200;
- val /= 1638400;
- *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);
-
- if (i2cs == 0) {
- Delay(100);
- i2c_rd_data(addr, 3, buf);
-
- if(buf[2] == CalcSht21Crc(buf,2)) {
- val = buf[0];
- val <<= 8;
- val |= (buf[1] & 0xfc);
-
- val *= 125;
-
- val += 32768;
- 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) {
-
-
-
- uint8_t byteCtr,bit,crc;
- crc = 0;
-
- 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);
- }
|