基于stm32f103r6的dht11的温湿度检测的代码及头h文件和c文件
时间: 2023-08-24 20:08:21 浏览: 40
以下是基于STM32F103R6的DHT11温湿度检测的代码、头文件和C文件。请注意,这是一个简单的示例,您需要根据您的具体硬件和需求进行适当的修改。
main.c:
```
#include "stm32f10x.h"
#include "dht11.h"
int main(void)
{
uint8_t temp, humi;
SysTick_Init();
DHT11_Init();
while(1)
{
DHT11_Read_Data(&temp, &humi);
// 处理温湿度数据
}
}
void SysTick_Init(void)
{
if(SysTick_Config(SystemCoreClock / 1000))
{
while(1);
}
}
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
void Delay_ms(uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
```
dht11.h:
```
#ifndef __DHT11_H
#define __DHT11_H
#include "stm32f10x.h"
// DHT11引脚定义
#define DHT11_PIN GPIO_Pin_8
#define DHT11_PORT GPIOB
// 函数声明
void DHT11_Init(void);
void DHT11_Start(void);
uint8_t DHT11_Check_Response(void);
uint8_t DHT11_Read_Byte(void);
void DHT11_Read_Data(uint8_t *temp, uint8_t *humi);
#endif
```
dht11.c:
```
#include "dht11.h"
uint32_t TimingDelay;
void DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
GPIO_SetBits(DHT11_PORT, DHT11_PIN);
Delay_ms(1000); // 等待1秒钟
}
void DHT11_Start(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
GPIO_ResetBits(DHT11_PORT, DHT11_PIN);
Delay_ms(18); // 至少18ms
GPIO_SetBits(DHT11_PORT, DHT11_PIN);
}
uint8_t DHT11_Check_Response(void)
{
uint8_t Response = 0;
uint32_t TimeOut = 0;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
while(GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
TimeOut++;
Delay_ms(1);
if(TimeOut > 100)
{
Response = 1;
break;
}
}
TimeOut = 0;
while(!GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
TimeOut++;
Delay_ms(1);
if(TimeOut > 100)
{
Response = 1;
break;
}
}
TimeOut = 0;
while(GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
TimeOut++;
Delay_ms(1);
if(TimeOut > 100)
{
Response = 1;
break;
}
}
return Response;
}
uint8_t DHT11_Read_Byte(void)
{
uint8_t i, j, byte = 0;
for(j = 0; j < 8; j++)
{
while(!GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN)); // 等待高电平
Delay_ms(40); // 40us
if(GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN))
{
byte |= (1 << (7 - j));
}
while(GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN)); // 等待低电平
}
return byte;
}
void DHT11_Read_Data(uint8_t *temp, uint8_t *humi)
{
uint8_t i, data[5], check_sum;
DHT11_Start();
if(DHT11_Check_Response() == 1)
{
*temp = 0;
*humi = 0;
return;
}
for(i = 0; i < 5; i++)
{
data[i] = DHT11_Read_Byte();
}
check_sum = data[0] + data[1] + data[2] + data[3];
if(check_sum != data[4])
{
*temp = 0;
*humi = 0;
return;
}
*humi = data[0];
*temp = data[2];
}
```
希望这个示例能对您有所帮助!