帮我写dht11温湿度传感器的stm32程序
时间: 2023-10-31 16:05:46 浏览: 50
当然,我可以帮您编写dht11温湿度传感器的stm32程序。您需要先安装好Keil软件和stm32的库文件,并将dht11传感器连接至stm32单片机上,然后将下面的代码拷贝至Keil软件中进行编译、下载即可!
#include "dht11.h"
#include "delay.h"
#include "stm32f10x.h"
#include "stdio.h"
u8 dht11_buffer[5]; //DHT11数据缓存区
//DHT11初始化
void DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PORTB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //配置PB10为输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_10);
Delay_ms(100); //DHT11上电延时至少18ms
}
//获取DHT11数据
void DHT11_ReadData(u8 *dht11_data)
{
u8 i, j;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PORTB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //配置PB10为输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_10); //拉高总线
Delay_us(40); //上拉40us
GPIO_ResetBits(GPIOB, GPIO_Pin_10); //拉低总线
Delay_ms(20); //等待20ms确保DHT11开始工作
GPIO_SetBits(GPIOB, GPIO_Pin_10); //再次拉高总线
Delay_us(40); //上拉40us
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //配置PB10为输入模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
i = 0;
while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == Bit_SET) //等待DHT11响应
{
i++;
if (i > 100)
{
dht11_data[0] = 0xff;
dht11_data[1] = 0xff;
dht11_data[2] = 0xff;
dht11_data[3] = 0xff;
dht11_data[4] = 0xff;
return;
}
Delay_us(1);
}
i = 0;
while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == Bit_RESET) //数据开始位(拉低80us)
{
i++;
if (i > 100)
{
dht11_data[0] = 0xff;
dht11_data[1] = 0xff;
dht11_data[2] = 0xff;
dht11_data[3] = 0xff;
dht11_data[4] = 0xff;
return;
}
Delay_us(1);
}
for (j = 0; j < 5; j++)
{
for (i = 0; i < 8; i++)
{
while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == Bit_SET); //高50us 1
Delay_us(30);
dht11_buffer[j] <<= 1;
if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == Bit_RESET) //低26~28us 0
{
dht11_buffer[j] &= 0xfe;
}
else //高70us 1
{
dht11_buffer[j] |= 0x01;
}
while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == Bit_RESET); //等待下一个bit的到来
}
}
if (dht11_buffer[4] == (dht11_buffer[0] + dht11_buffer[1] + dht11_buffer[2] + dht11_buffer[3]))
{
dht11_data[0] = dht11_buffer[0];
dht11_data[1] = dht11_buffer[2];
}
else
{
dht11_data[0] = 0xff;
dht11_data[1] = 0xff;
}
}
int main(void)
{
u8 temper, humi;
Delay_Init();
DHT11_Init();
while(1)
{
DHT11_ReadData(dht11_buffer);
if (dht11_buffer[0] != 0xff)
{
temper = dht11_buffer[0];
humi = dht11_buffer[1];
printf("Temperature is %d.%d Celsius degree.\n", temper/10, temper%10);
printf("Humidity is %d.%d%%.\n\n", humi/10, humi%10);
}
else
{
printf("DHT11 communication error!\n\n");
}
Delay_ms(2000); //每隔2s获取一次数据
}
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)