STM32F407-驱动SHT41采集温湿度
SHT41
SHT41通过I2C方式进行驱动
从机地址: 0x44
获取数据方式
1)先发送I2C写,写入特定指令
2)延时一段时间,等待SHT41处理
3)再进行I2C读,读数据即可
一些指令如下:
demo
STM32F407驱动
I2C初始化
void my_sht41_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_I2C1);
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 300000;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_OwnAddress1 = STM32_I2C_OWN_ADDR;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
读SHT41 芯片ID
void my_sht41_read_id(void)
{
uint8_t data[6] = {0};
uint8_t ret = 0;
uint16_t time = 0xFFF;
my_sht41_send_cmd(0x89);
printf("开始延迟\n");
while(time)
{
time--;
}
printf("延时结束\n");
ret = my_SHT41_buffer_read(data,6);
if(ret == 0)
{
printf("data[0] = 0x%x\n",data[0]);
printf("data[1] = 0x%x\n",data[1]);
printf("data[3] = 0x%x\n",data[3]);
printf("data[4] = 0x%x\n",data[4]);
}
else
{
printf("读取ID错误!\n");
}
}
读温度湿度数据
void my_sht41_read_data(void)
{
uint8_t data[6] = {0};
uint8_t ret = 0;
uint32_t time = 0xFFFFFF;
my_sht41_send_cmd(0xFD);
uint16_t recovery_temper = 0u;
uint16_t recovery_hum = 0u;
float temp = 0.f;
float hum = 0.f;
printf("开始延迟\n");
while(time)
{
time--;
}
printf("延时结束\n");
ret = my_SHT41_buffer_read(data,6);
if(ret == 0)
{
printf("data[0] = 0x%x\n",data[0]);
printf("data[1] = 0x%x\n",data[1]);
printf("data[3] = 0x%x\n",data[3]);
printf("data[4] = 0x%x\n",data[4]);
}
else
{
printf("读取ID错误!\n");
}
recovery_temper = ((uint16_t)data[0]<<8)|data[1];
temp = -45 + 175*((float)recovery_temper/65535);
/* 转换湿度数据 */
recovery_hum = ((uint16_t)data[3]<<8)|data[4];
hum = -6 + 125 * ((float)recovery_hum / 65535);
if(hum>=100) //根据数据手册编写
hum=100;
else if(hum<=0)
hum=0;
printf("温度为:%.2f",temp);
printf("湿度为:%.1f%%",hum);
}
I2C读写接口
static uint8_t my_sht41_send_cmd(uint8_t data)
{
uint32_t count_wait;
I2C_GenerateSTART(I2C1,ENABLE);
count_wait = TIME_OUT;
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS)
{
count_wait--;
if(count_wait == 0) {
return Error_CallBack(1);
}
}
I2C_Send7bitAddress(I2C1, SHT41_I2C_ADDR, I2C_Direction_Transmitter);
count_wait = TIME_OUT;
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) != SUCCESS)
{
count_wait--;
if(count_wait == 0) {
return Error_CallBack(2);
}
}
//发送数据
I2C_SendData(I2C1,data);
count_wait = TIME_OUT;
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS)
{
count_wait--;
if(count_wait == 0) {
return Error_CallBack(4);
}
}
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}
/*读六个字节数据 */
static uint8_t my_SHT41_buffer_read(uint8_t *data,uint16_t size)
{
uint32_t count_wait;
//开始进行I2C通讯
I2C_GenerateSTART(I2C1, ENABLE);
if(size > 6)
{
return 1;
}
count_wait = TIME_OUT; //等待拉低
while(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS)
{
count_wait--;
if(count_wait == 0 )
{
return Error_CallBack(16);
}
}
//读模式
I2C_Send7bitAddress(I2C1, SHT41_I2C_ADDR, I2C_Direction_Receiver);
count_wait = TIME_OUT;
while(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) != SUCCESS)
{
count_wait--;
if(count_wait == 0 )
{
return Error_CallBack(20);
}
}
while(size--)
{
if(size == 0)
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
}else {
I2C_AcknowledgeConfig(I2C1,ENABLE);
}
count_wait = TIME_OUT;
while(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) != SUCCESS)
{
count_wait--;
if(count_wait == 0 )
{
return Error_CallBack(21);
}
}
*data = I2C_ReceiveData(I2C1);
data++;
}
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}