HTS221 is a temperature and humidity sensor. It still uses IIC operation.
The schematic diagram is as follows, IIC address 0xBE:
register:
The chip operation register is relatively simple, but the chip needs to be set up at the beginning.
AV_CONF register : This can be set according to your needs. I set it to 0x09:
CTRL1 register : Set the power on. I set it to 0x81.
The above two registers are more important and need to be set.
Humidity reads register , 16-bit data :
Temperature read register , 16-bit data:
:
Chip logo: 0xB3.
There are also temperature and humidity calculation compensation registers, which are very important for calculating temperature and humidity:
I took a look at this and didn't quite understand it, so I only read the output temperature and humidity registers, which displayed hexadecimal data without performing temperature and humidity conversion. If conversion is required, the last registers need to be used together, and the operation is relatively complicated, so I haven't performed any calculations for now. I will slowly work on it when I have time later. The following is a description of compensation.
Code:
initialization:
//初始化传感器LPS22HH
if(0 == Set_Reg_HTS221(HTS221_ADDR_W_1, HTS221_AV_CONF, 0x09u)) //0x09
{
send_str((unsigned char *)"IF_CTRL set successful\r\n");
}
else
{
send_str((unsigned char *)"IF_CTRL set fail\r\n");
}
if(0 == Set_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_CTRL_1, 0x81u))
{
send_str((unsigned char *)"CTRL_1 set successful\r\n");
}
else
{
send_str((unsigned char *)"CTRL_1 set fail\r\n");
}
//HTS221写
static int IIC_WrDat_HTS221(uint8_t IIC_Add, uint8_t IIC_Reg, uint8_t IIC_Data)
{
uint8_t ret_suc = 1;
IIC_Start();
IIC_Send_Byte(IIC_Add & 0xfe);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Reg); //write data
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Data);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Stop();
return 0;
}
//HTS221读
static int IIC_Read_HTS221(uint8_t IIC_Add, uint8_t IIC_Reg)
{
int ret;
uint8_t ret_suc = 1;
IIC_Start();
IIC_Send_Byte(IIC_Add & 0xfe);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Reg);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Start();
IIC_Send_Byte(IIC_Add | 0x01);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
ret=IIC_Read_Byte(1);
IIC_Stop();
return ret;
}
//设置寄存器
int Set_Reg_HTS221(uint8_t add, uint8_t reg, uint8_t dat)
{
int ret_succ = -1;
uint8_t i = 20u; //失败之后尝试的次数
do{
if(0 == IIC_WrDat_HTS221(add, reg, dat))
{
ret_succ = 0; //设置成功
break;
}
else
{
i--; //获取失败,再次尝试
}
}while(i>0u);
return ret_succ;
}
//读取寄存器
int Get_Reg_HTS221(uint8_t add, uint8_t reg)
{
uint8_t i = 20u;
int ret_get = -1;
do{
ret_get = IIC_Read_HTS221(add, reg);
if(-1 != ret_get)
{
break; //获取数据成功
}
else
{
i--; //获取失败,再次尝试
}
}while(i>0u);
return ret_get;
}
//读取我是谁
int Get_WHO_AM_I_HTS221(void)
{
return Get_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_WHO_AM_I);
}
//获取状态寄存器
int Get_Status_HTS221(void)
{
return Get_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_STATUS);
}
//获取压力值L
int Get_Humidity_L_HTS221(void)
{
return Get_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_HUMI_OUT_L);
}
//获取压力值H
int Get_Humidity_H_HTS221(void)
{
return Get_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_HUMI_OUT_H);
}
float Get_Humidity_HTS221(void)
{
static float Last_return_p = 0.0f;
int get_p;
if((Get_Humidity_H_HTS221() != -1)
&&(Get_Humidity_L_HTS221() != -1))
{
get_p = (Get_Humidity_H_HTS221()<<8)|(Get_Humidity_L_HTS221());
//Last_return_p = ((float)get_p / 4096.0f);
Last_return_p = get_p;
}
else
{
}
return Last_return_p;
}
//获取温度值H
int Get_Temp_L_HTS221(void)
{
return Get_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_TEMP_OUT_L);
}
//获取温度值H
int Get_Temp_H_HTS221(void)
{
return Get_Reg_HTS221(HTS221_ADDR_W_1, HTS221_REG_TEMP_OUT_H);
}
float Get_Temp_HTS221(void)
{
static float Last_return_t = 0.0f;
int get_t;
if((Get_Temp_H_HTS221() != -1)
&&(Get_Temp_L_HTS221() != -1))
{
get_t = (Get_Temp_H_HTS221()<<8)|(Get_Temp_L_HTS221());
// Last_return_t = ((float) get_t / 100.0f);
Last_return_t = get_t;
}
else
{
}
return Last_return_t;
}
//发送数据到串口
void Send_Uart_HTS221(void)
{
char str[35] = {0};
send_str((uint8_t *)"<-----------START--------->\r\n");
sprintf(str,"Humidity:%X\r\n", (int)Get_Humidity_HTS221());
send_str((uint8_t *)str);
sprintf(str,"Temp:%X\r\n", (int)Get_Temp_HTS221());
send_str((uint8_t *)str);
sprintf(str,"Who_am_i:%X\r\n",Get_WHO_AM_I_HTS221());
send_str((uint8_t *)str);
send_str((uint8_t *)"<------------END---------->\r\n\n");
}
//显示数据到OLED
void OLED_Display_HTS221(void)
{
char str_dis1[20], str_dis2[20], str_dis3[20];
OLED_Clear();
sprintf(str_dis1,"Pressure:%.2f\r\n", Get_Humidity_HTS221());
sprintf(str_dis2,"Temp:%.2f\r\n", Get_Temp_HTS221());
sprintf(str_dis3,"Who_am_i:%X\r\n",Get_WHO_AM_I_HTS221());
OLED_ShowString(0,16,(uint8_t *)str_dis1,16,1);
OLED_ShowString(0,32,(uint8_t *)str_dis2,16,1);
OLED_ShowString(0,48,(uint8_t *)str_dis3,16,1);
OLED_Refresh_Gram();
}
Macro definition:
/*register address*/
#define HTS221_REG_WHO_AM_I (0x0FU)
#define HTS221_AV_CONF (0x10U)
#define HTS221_REG_CTRL_1 (0x20U)
#define HTS221_REG_CTRL_2 (0x21U)
#define HTS221_REG_CTRL_3 (0x22U)
#define HTS221_REG_STATUS (0x27U)
#define HTS221_REG_HUMI_OUT_L (0x28U)
#define HTS221_REG_HUMI_OUT_H (0x29U)
#define HTS221_REG_TEMP_OUT_L (0x2AU)
#define HTS221_REG_TEMP_OUT_H (0x2BU)
#define HTS221_REG_H0_rH_x2 (0x30U)
#define HTS221_REG_H1_rH_x2 (0x31U)
#define HTS221_REG_T0_degC_x8 (0x32U)
#define HTS221_REG_T1_degC_x8 (0x33U)
#define HTS221_REG_T1_T0 (0x35U)
#define HTS221_REG_H0_T0_OUT1 (0x36U)
#define HTS221_REG_H0_T0_OUT2 (0x37U)
#define HTS221_REG_H1_T0_OUT1 (0x3AU)
#define HTS221_REG_H1_T0_OUT2 (0x3BU)
#define HTS221_REG_T0_OUT1 (0x3CU)
#define HTS221_REG_T0_OUT2 (0x3DU)
#define HTS221_REG_T1_OUT1 (0x3EU)
#define HTS221_REG_T1_OUT2 (0x3FU)
/*IIC slave address write and read*/
#define HTS221_ADDR_W_1 0xBEU
#define HTS221_ADDR_R_1 0xBFU
Effect display:
As mentioned above, I did not perform any calculations on the collected data, but only displayed the information of the acquired registers:
|