dht11接PA15引脚STM32F103程序
时间: 2023-12-19 15:05:25 浏览: 28
以下是使用 STM32F103C8T6 板载的 PA15 引脚来读取 DHT11 温湿度传感器数据的程序示例:
```
#include "stm32f10x.h"
#include "delay.h"
#define DHT11_GPIO_PORT GPIOA
#define DHT11_GPIO_PIN GPIO_Pin_15
void DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能 GPIO 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* 配置 GPIO 为推挽输出 */
GPIO_InitStructure.GPIO_Pin = DHT11_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);
/* 默认拉高 DHT11 数据线 */
GPIO_SetBits(DHT11_GPIO_PORT, DHT11_GPIO_PIN);
/* 延时至少 18ms 以上 */
delay_ms(20);
}
uint8_t DHT11_ReadByte(void)
{
uint8_t i, dat = 0;
for (i = 0; i < 8; i++)
{
while (!GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
; /* 等待低电平结束 */
delay_us(30); /* 高电平持续 30us,用于判断数据位是 0 还是 1 */
if (GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
dat |= 1 << (7 - i);
while (GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
; /* 等待高电平结束 */
}
return dat;
}
void DHT11_ReadData(uint8_t *temp, uint8_t *hum)
{
uint8_t i, buf[5];
/* DHT11 数据线拉低至少 18ms */
GPIO_ResetBits(DHT11_GPIO_PORT, DHT11_GPIO_PIN);
delay_ms(20);
/* DHT11 数据线拉高 20-40us */
GPIO_SetBits(DHT11_GPIO_PORT, DHT11_GPIO_PIN);
delay_us(30);
/* 设置 GPIO 为输入模式 */
GPIO_InitStructure.GPIO_Pin = DHT11_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);
/* 等待 DHT11 响应信号,低电平持续 80us,高电平持续 80us */
if (!GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
{
while (!GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
; /* 等待低电平结束 */
while (GPIO_ReadInputDataBit(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
; /* 等待高电平结束 */
/* 读取 40 位数据 */
for (i = 0; i < 5; i++)
buf[i] = DHT11_ReadByte();
/* DHT11 数据线拉高 */
GPIO_SetBits(DHT11_GPIO_PORT, DHT11_GPIO_PIN);
/* 数据校验 */
if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*hum = buf[0];
*temp = buf[2];
}
}
/* 设置 GPIO 为推挽输出 */
GPIO_InitStructure.GPIO_Pin = DHT11_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStructure);
}
int main(void)
{
uint8_t temp, hum;
/* 初始化 DHT11 */
DHT11_Init();
while (1)
{
/* 读取温湿度数据 */
DHT11_ReadData(&temp, &hum);
/* 在此处添加处理温湿度数据的代码 */
/* 延时一段时间 */
delay_ms(1000);
}
}
```
注意:以上程序示例仅供参考,具体实现需要根据自己的实际情况进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)