標題:
STM32F407單片機DHT11溫度采集源程序
[打印本頁]
作者:
接電話很大聲
時間:
2022-7-7 09:19
標題:
STM32F407單片機DHT11溫度采集源程序
DTH11測量實時濕度 天馬F407插上就能用DHT11 是一款濕溫度一體化的數(shù)字傳感器。該傳感器包括一個電阻式測濕元件和一個 NTC測溫元件,并與一個高性能 8 位單片機相連接。通過單片機等微處理器簡單的電路連接就能夠?qū)崟r的采集本地濕度和溫度。DHT11 與單片機之間能采用簡單的單總線進行通信,僅僅需要一個 I/O 口。傳感器內(nèi)部濕度和溫度數(shù)據(jù) 40Bit 的數(shù)據(jù)一次性傳給單片機,數(shù)據(jù)采用校驗和方式進行校驗,有效的保證數(shù)據(jù)傳輸?shù)臏蚀_性。DHT11 功耗很低,5V 電源電壓下,工作平均最大電流 0.5mA。此例中DATA口接PG9
將DHT11傳感器數(shù)據(jù)口連接PG9。通過LCD或串口助手均可顯示檢測數(shù)據(jù)。
單片機源程序如下:
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "usart.h"
#include "tftlcd.h"
#include "dht11.h"
void data_pros() //數(shù)據(jù)處理函數(shù)
{
u8 temp=0;
u8 humi=0;
u8 temp_buf[3],humi_buf[3];
DHT11_Read_Data(&temp,&humi);
temp_buf[0]=temp/10+0x30;
temp_buf[1]=temp%10+0x30;
temp_buf[2]='\0';
LCD_ShowString(55,100,tftlcd_data.width,tftlcd_data.height,16,temp_buf);
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
humi_buf[2]='\0';
LCD_ShowString(55,130,tftlcd_data.width,tftlcd_data.height,16,humi_buf);
printf("溫度=%d°C 濕度=%d%%RH\r\n",temp,humi);
}
int main()
{
u8 i=0;
SysTick_Init(168);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優(yōu)先級分組 分2組
LED_Init();
USART1_Init(115200);
TFTLCD_Init(); //LCD初始化
FRONT_COLOR=BLACK;
LCD_ShowString(10,10,tftlcd_data.width,tftlcd_data.height,16," ");
LCD_ShowString(10,30,tftlcd_data.width,tftlcd_data.height,16," ");
LCD_ShowString(10,50,tftlcd_data.width,tftlcd_data.height,16,"DHT11 Test");
LCD_ShowString(10,100,tftlcd_data.width,tftlcd_data.height,16,"Temp: C");
LCD_ShowString(10,130,tftlcd_data.width,tftlcd_data.height,16,"Humi: %RH");
FRONT_COLOR=RED;
while(DHT11_Init()) //檢測DHT11
{
LCD_ShowString(130,50,tftlcd_data.width,tftlcd_data.height,16,"Error ");
printf("DHT11 Check Error!\r\n");
delay_ms(500);
}
LCD_ShowString(130,50,tftlcd_data.width,tftlcd_data.height,16,"Success");
printf("DHT11 Check OK!\r\n");
while(1)
{
i++;
if(i%20==0)
{
LED1=!LED1;
data_pros(); //讀取一次DHT11數(shù)據(jù)最少要大于100ms
}
delay_ms(100);
}
}
復制代碼
#include "dht11.h"
//DHT11初始化
//返回0:初始化成功,1:失敗
u8 DHT11_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT; //輸出模式
GPIO_InitStructure.GPIO_Pin=DHT11;//管腳設置
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//速度為100M
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽輸出
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//上拉
GPIO_Init(GPIO_DHT11,&GPIO_InitStructure); //初始化結(jié)構體
GPIO_SetBits(GPIO_DHT11,DHT11); //拉高
DHT11_Rst();
return DHT11_Check();
}
//復位DHT11
void DHT11_Rst()
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_OUT=0; //拉低DQ
delay_ms(20); //拉低至少18ms
DHT11_DQ_OUT=1; //DQ=1
delay_us(30); //主機拉高20~40us
}
//等待DHT11的回應
//返回1:未檢測到DHT11的存在
//返回0:存在
u8 DHT11_Check()
{
u8 retry=0;
DHT11_IO_IN();//SET INPUT
while (DHT11_DQ_IN&&retry<100)//DHT11會拉低40~50us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100)//DHT11拉低后會再次拉高40~50us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
return 0;
}
//從DHT11讀取一個位
//返回值:1/0
u8 DHT11_Read_Bit(void)
{
u8 retry=0;
while(DHT11_DQ_IN&&retry<100)//等待變?yōu)榈碗娖?12-14us 開始
{
retry++;
delay_us(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100)//等待變高電平 26-28us表示0,116-118us表示1
{
retry++;
delay_us(1);
}
delay_us(40);//等待40us
if(DHT11_DQ_IN)return 1;
else return 0;
}
//從DHT11讀取一個字節(jié)
//返回值:讀到的數(shù)據(jù)
u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
//從DHT11讀取一次數(shù)據(jù)
//temp:溫度值(范圍:0~50°)
//humi:濕度值(范圍:20%~90%)
//返回值:0,正常;1,讀取失敗
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)//讀取40位數(shù)據(jù)
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}
}else return 1;
return 0;
}
//DHT11輸出模式配置
void DHT11_IO_OUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT; //輸出模式
GPIO_InitStructure.GPIO_Pin=DHT11;//管腳設置
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//速度為100M
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽輸出
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//上拉
GPIO_Init(GPIO_DHT11,&GPIO_InitStructure); //初始化結(jié)構體
}
//DHT11輸入模式配置
void DHT11_IO_IN()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN; //輸入模式
GPIO_InitStructure.GPIO_Pin=DHT11;//管腳設置
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//上拉
GPIO_Init(GPIO_DHT11,&GPIO_InitStructure); //初始化結(jié)構體
}
復制代碼
Keil代碼下載:
Keil代碼.7z
(388.4 KB, 下載次數(shù): 39)
2022-7-10 14:52 上傳
點擊文件名下載附件
無
下載積分: 黑幣 -5
作者:
bnls023
時間:
2023-12-13 15:42
怎么不能load
作者:
bnls023
時間:
2023-12-13 15:45
為何不能LOAD呢?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1