首先感谢老哥提供的代码
https://blog.csdn.net/qq_38203996/article/details/103328291?utm_medium=distribute.pc_relevant_download.none-task-blog-2defaultBlogCommendFromBaidudefault-2.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2defaultBlogCommendFromBaidudefault-2.test_version_
在以上的基础上用HAL库修改了代码,使用的测试开发板是正点原子阿波罗STM32F767IGT6。结果如下图所示: 话不多说,直接上代码
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
#include "DHT11.h"
#include "BH1750VI.h"
u8 temp = 0,humi=0;
u8 temp_dec=0,humi_dec=0;
int main(void)
{
Cache_Enable();
HAL_Init();
Stm32_Clock_Init(432,25,2,9);
delay_init(216);
uart_init(115200);
LED_Init();
TIM3_Init(10000-1,10800-1);
delay_ms(50);
printf("DHT11 Test Program!");
if(!DHT11_Init()){
printf("\r\n ERROR! THE DHT11 HAS NO RESPOND...");
}
printf("\r\n THE DHT11 HAS RESPOND");
delay_ms(10);
while(1)
{
if(DHT11_Read_Data(&temp,&temp_dec,&humi,&humi_dec))
{
printf("\r\n temp:%d.%d,humi:%d.%d",temp,temp_dec,humi,humi_dec);
}
else{
printf("\r\n ERROR THE DHT11 HAS NO RESPOND...");
}
delay_ms(2000);
}
}
DHT11.c
#include "DHT11.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
GPIO_InitTypeDef GPIO_InitStructure;
static void GPIO_SETOUT(void);
static void GPIO_SETIN(void);
static u8 DHT11_Check(void);
static void DHT11_Rst(void)
{
GPIO_SETOUT();
HAL_GPIO_WritePin(DHT11_IO,DHT11_PIN,GPIO_PIN_RESET);
delay_ms(20);
HAL_GPIO_WritePin(DHT11_IO,DHT11_PIN,GPIO_PIN_SET);
delay_us(30);
HAL_GPIO_WritePin(DHT11_IO,DHT11_PIN,GPIO_PIN_RESET);
}
u8 DHT11_Init(void){
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStructure.Pin = DHT11_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(DHT11_IO,&GPIO_InitStructure);
DHT11_Rst();
return DHT11_Check();
}
static void GPIO_SETOUT(void)
{
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(DHT11_IO,&GPIO_InitStructure);
}
static void GPIO_SETIN(void)
{
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(DHT11_IO,&GPIO_InitStructure);
}
static u8 DHT11_Check(void)
{
u8 retry=0;
GPIO_SETIN();
while (!HAL_GPIO_ReadPin(DHT11_IO,DHT11_PIN) && retry<100)
{
retry++;
delay_us(1);
}
if(retry >= 100)
return 0;
else
retry = 0;
while (HAL_GPIO_ReadPin(DHT11_IO,DHT11_PIN) && retry<100)
{
retry++;
delay_us(1);
}
if(retry>=100)
return 0;
return 1;
}
static u8 DHT11_Read_Bit(void)
{
u8 retry = 0;
while(HAL_GPIO_ReadPin(DHT11_IO,DHT11_PIN) && retry<100)
{
retry++;
delay_us(1);
}
retry = 0;
while(!HAL_GPIO_ReadPin(DHT11_IO,DHT11_PIN) && retry<100)
{
retry++;
delay_us(1);
}
delay_us(30);
if(HAL_GPIO_ReadPin(DHT11_IO,DHT11_PIN)) return 1;
else return 0;
}
static u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
u8 DHT11_Read_Data(u8 *temp,u8 *temp_dec,u8 *humi,u8 *humi_dec)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==1)
{
for(i=0;i<5;i++)
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi = buf[0];
*humi_dec = buf[1];
*temp = buf[2];
*temp_dec = buf[3];
}
}else return 0;
return 1;
}
DHT11.h
#ifndef __DHT11_H
#define __DHT11_H
#include "sys.h"
#include "delay.h"
#define DHT11_IO GPIOA
#define DHT11_PIN GPIO_PIN_0
u8 DHT11_Init(void);
u8 DHT11_Read_Data(u8 *temp,u8 *temp_dec,u8 *humi,u8 *humi_dec);
#endif
由于我的目的只是采集数据,相关传感器原理只是随便看了下。代码采集数据是比较稳定的,具体原理大家自行研究吧。TOT
|