micro_climate/Drivers/Sht3x/sht30.c

207 lines
5.0 KiB
C
Raw Permalink Normal View History

2024-07-05 03:52:43 +00:00
#include "sht30.h"
#include "i2c.h"
#include "pdebug.h"
#include "assertions.h"
#include "filter.h"
#include "cmsis_os.h"
#if 0
stTempHumiSensor g_stTempHumiSensor={
.i2c_reg_addr = 0x44<<1,
.cmd_init_mode = 0x2320, //repeat medium_4
.cmd_read_data = 0xe000,
};
#endif
stTempHumiSensor g_stTempHumiSensor={
.i2c_reg_addr = 0x44<<1,
.cmd_init_mode = 0x2c06,
.cmd_read_data = 0x2c06,
};
stTempHumiData g_stTempHumiData;
/**
* @brief д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1ms<EFBFBD><EFBFBD>
* @param
* @retval
*/
static u_int8_t i2c_write_cmd(u_int8_t addr,u_int16_t cmd)
{
u_int8_t cmd_buff[2]={0x00};
cmd_buff[0] = cmd>>8;
cmd_buff[1] = cmd;
u_int8_t ret = HAL_I2C_Master_Transmit(&hi2c1,addr,cmd_buff,2,0xffff);
return ret;
}
/**
* @brief CRCУ<EFBFBD><EFBFBD>
* @param
* @retval
*/
#define CRC8_POLYNOMIAL 0x31
u_int8_t CheckCrc8(u_int8_t* message, u_int8_t initial_value)
{
u_int8_t remainder;
u_int8_t i = 0, j = 0;
remainder = initial_value;
for(j = 0; j < 2;j++){
remainder ^= message[j];
for (i = 0; i < 8; i++){
if (remainder & 0x80){
remainder = (remainder << 1)^CRC8_POLYNOMIAL;
} else {
remainder = (remainder << 1);
}
}
}
return remainder;
}
/**
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
u_int8_t sht30_init()
{
float temp,humi;
uint8_t ret;
2024-07-05 03:52:43 +00:00
// soft reset
// i2c_write_cmd(0x30a2);
// HAL_Delay(25);
HAL_GPIO_WritePin(GPIOB, GPIO_SHT_PWR_EN_Pin, GPIO_PIN_SET);
HAL_Delay(1);
//u_int8_t ret = i2c_write_cmd(g_stTempHumiSensor.i2c_reg_addr, g_stTempHumiSensor.cmd_init_mode);
ret = get_temp_humi_data(&temp, &humi);
if(ret == TRUE)
term_printf("Sensor Sht30 Check OK.\r\n Temp:%.2f,Humi:%.2f\r\n", temp,humi);
else
term_printf("Sensor Sht30 Check Err.\r\n");
2024-07-05 03:52:43 +00:00
return 0;
}
/**
* @brief <EFBFBD><EFBFBD>ʪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲɼ<EFBFBD>
* @param
* @retval
*/
u_int8_t sht30_collect_data(stTempHumiSensor stSensorDev, float *temp, float *humi)
{
u_int8_t read_buff[6] = {0};
u_int16_t temp_value;
u_int16_t humi_value;
i2c_write_cmd(stSensorDev.i2c_reg_addr,stSensorDev.cmd_read_data);
if(HAL_I2C_Master_Receive(&hi2c1,stSensorDev.i2c_reg_addr,read_buff,6,0xffff) != HAL_OK){
return HAL_ERROR;
}
if(CheckCrc8(read_buff, 0xFF) != read_buff[2] && CheckCrc8(&read_buff[3], 0xFF) != read_buff[5]){
return HAL_ERROR;
}
temp_value = ((u_int16_t)read_buff[0]<<8)|read_buff[1];
*temp = -45 + 175*((float)temp_value/65535);
humi_value = ((u_int16_t)read_buff[3]<<8)|read_buff[4];
*humi = 100 * ((float)humi_value / 65535);
return HAL_OK;
}
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ʪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
#define COLLECT_DATA_NUM 10
BOOL get_temp_humi_data(float* temdata, float* humidata)
{
U_DataType collect_temdata[COLLECT_DATA_NUM]={0x00};
U_DataType collect_humidata[COLLECT_DATA_NUM]={0x00};
2024-07-05 03:52:43 +00:00
U_DataType tmp_temdata,tmp_humidata;
for(int i=0; i<COLLECT_DATA_NUM; i++){
int ret = sht30_collect_data(g_stTempHumiSensor,&collect_temdata[i].fValue, &collect_humidata[i].fValue);
// AssertError(ret == HAL_OK, return FALSE, "sht30<33><30><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
if(ret == HAL_ERROR)
// return FALSE;
goto error_return;
2024-07-05 03:52:43 +00:00
osDelay(1);
}
tmp_temdata = filter_middle(collect_temdata,COLLECT_DATA_NUM, FILTER_DATA_TYPE_FLOAT);
tmp_humidata = filter_middle(collect_humidata,COLLECT_DATA_NUM, FILTER_DATA_TYPE_FLOAT);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// AssertError((tmp_temdata.fValue >= -40) && (tmp_temdata.fValue <= 85), return FALSE, "sht30<33><EFBFBD>ֵУ<D6B5><D0A3>ʧ<EFBFBD><CAA7>");
// AssertError((tmp_humidata.fValue >= 0) && (tmp_humidata.fValue <= 100), return FALSE, "sht30ʪ<30><CAAA>ֵУ<D6B5><D0A3>ʧ<EFBFBD><CAA7>");
if(tmp_temdata.fValue < -40)
{
tmp_temdata.fValue = -40;
// term_printf("sht30<33><EFBFBD>ֵУ<D6B5><D0A3>ʧ<EFBFBD><CAA7>");
// return FALSE;
goto error_return;
}
if(tmp_temdata.fValue > 125)
{
tmp_temdata.fValue = 125;
// term_printf("sht30<33><EFBFBD>ֵУ<D6B5><D0A3>ʧ<EFBFBD><CAA7>");
// return FALSE;
goto error_return;
}
if(tmp_humidata.fValue < 0)
{
tmp_humidata.fValue = 0;
// term_printf("sht30ʪ<30><CAAA>ֵУ<D6B5><D0A3>ʧ<EFBFBD><CAA7>");
// return FALSE;
goto error_return;
}
if(tmp_humidata.fValue > 100)
{
tmp_humidata.fValue = 100;
// term_printf("sht3ʪ<33><CAAA>ֵУ<D6B5><D0A3>ʧ<EFBFBD><CAA7>");
// return FALSE;
goto error_return;
}
2024-07-05 03:52:43 +00:00
*temdata = tmp_temdata.fValue;
*humidata = tmp_humidata.fValue;
//g_stTempHumiData.temp = tmp_temdata.fValue;
//g_stTempHumiData.humi = tmp_humidata.fValue;
return TRUE;
error_return:
*temdata = 0;
*humidata = 0;
return FALSE;
2024-07-05 03:52:43 +00:00
}
#if 0
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
void TEST_read_sht30_value()
{
float temp,humi;
AssertError(get_temp_humi_data(&temp, &humi),return,"<EFBFBD>ɼ<EFBFBD>sht30<EFBFBD><EFBFBD>ʪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>" );
term_printf("temp:%.2f, humi:%.2f\r\n", temp, humi);
}
#endif