|
【Nucleo G071评测】检测HTS221温湿度传感器数据
[复制链接]
庆科的扩展板上带有HTS221温湿度传感器,传感器走I2C总线,与之前的OLED屏幕共用总线,这个传感器啥都好,精度和采集速率要比DHT11快得多,就是工作方式是插值型,写程序计算数值有一丁点的繁杂:
#define HTS221_ADDRESS 0xBE
#define HTS221_RES_CONF_ADDR 0x10
#define HTS221_CTRL_REG1_ADDR 0x20
#define HTS221_CTRL_REG2_ADDR 0x21
#define HTS221_HUMIDITY_OUT_L_ADDR 0x28
#define HTS221_H0_RH_X2_ADDR 0x30
#define HTS221_H1_RH_X2_ADDR 0x31
#define HTS221_T0_degC_X8_ADDR 0x32
#define HTS221_T1_degC_X8_ADDR 0x33
#define HTS221_T1_T0_MSB_X8_ADDR 0x35
#define HTS221_H0_T0_OUT_L_ADDR 0x36
#define HTS221_H1_T0_OUT_L_ADDR 0x3A
#define HTS221_T0_OUT_L_ADDR 0x3C
#define HTS221_T1_OUT_L_ADDR 0x3E
unsigned char buffer[4];
float t0,t1,h0,h1,humi_value=0,temper_value=0;
short t0out,t1out,h0out,h1out,humi_temp=0,temper_temp=0;
int HTS221_Init()
{
I2C_GPIO_Init();
if(I2C_ReadData(0xbe,0x0f)==0xbc)
{
I2C_WriteData(0xbe,0x80,HTS221_CTRL_REG1_ADDR);//启动
I2C_WriteData(0xbe,0x03,HTS221_RES_CONF_ADDR);
return 0;
}
return -1;
}
float mapFloat(int x,int in_min,int in_max,float out_min,float out_max)
{
return (x-in_min)*(out_max-out_min)/(in_max-in_min)+out_min;
}
void HTS221_Read()
{
I2C_WriteData(HTS221_ADDRESS,0x01,HTS221_CTRL_REG2_ADDR);
buffer[0]=I2C_ReadData(HTS221_ADDRESS,HTS221_HUMIDITY_OUT_L_ADDR);
buffer[1]=I2C_ReadData(HTS221_ADDRESS,HTS221_HUMIDITY_OUT_L_ADDR+1);
buffer[2]=I2C_ReadData(HTS221_ADDRESS,HTS221_HUMIDITY_OUT_L_ADDR+2);
buffer[3]=I2C_ReadData(HTS221_ADDRESS,HTS221_HUMIDITY_OUT_L_ADDR+3);
temper_temp=((int)(buffer[3]<<8)|buffer[2]);
humi_temp=((int)(buffer[1]<<8)|buffer[0]);
temper_value=mapFloat(temper_temp,t0out,t1out,t0,t1);
humi_value=mapFloat(humi_temp,h0out,h1out,h0,h1);
}
void HTS221_Calibration()
{
buffer[0]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_degC_X8_ADDR);
buffer[1]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_degC_X8_ADDR+1);
buffer[2]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_degC_X8_ADDR+2);
buffer[3]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_degC_X8_ADDR+3);
int t0degCx8 = ((int)(buffer[3]&0x03)<<8)|buffer[0];
int t1degCx8 = ((int)(buffer[3]&0x0c)<<6)|buffer[1];
t0=t0degCx8/8.0;
t1=t1degCx8/8.0;
buffer[0]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_OUT_L_ADDR);
buffer[1]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_OUT_L_ADDR+1);
buffer[2]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_OUT_L_ADDR+2);
buffer[3]=I2C_ReadData(HTS221_ADDRESS,HTS221_T0_OUT_L_ADDR+3);
t0out=((int)(buffer[1]<<8)|buffer[0]);
t1out=((int)(buffer[3]<<8)|buffer[2]);
buffer[0]=I2C_ReadData(HTS221_ADDRESS,HTS221_H0_RH_X2_ADDR);
buffer[1]=I2C_ReadData(HTS221_ADDRESS,HTS221_H0_RH_X2_ADDR+1);
unsigned char h0RHx2 = buffer[0];
unsigned char h1RHx2 = buffer[1];
h0 = h0RHx2/2.0;
h1 = h1RHx2/2.0;
buffer[0]=I2C_ReadData(HTS221_ADDRESS,HTS221_H0_T0_OUT_L_ADDR);
buffer[1]=I2C_ReadData(HTS221_ADDRESS,HTS221_H0_T0_OUT_L_ADDR+1);
h0out=((int)(buffer[1]<<8)|buffer[0]);
buffer[0]=I2C_ReadData(HTS221_ADDRESS,HTS221_H1_T0_OUT_L_ADDR);
buffer[1]=I2C_ReadData(HTS221_ADDRESS,HTS221_H1_T0_OUT_L_ADDR+1);
h1out = ((int)(buffer[1]<<8)|buffer[0]);
}
这里需要注意,HTS221的out数值和temp临时数值是两个八位寄存器的数值拼在一起的,所以必须使用short型数据类型,不然正负符号位会被当成数
据位,即原本是负数的数值成了整数。HTS221需要在进行插值计算中间值完毕之后才可以进行最终温湿度数据的计算。
观察效果:
|
|