欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
STM32單片機溫濕度DHT11程序
[打印本頁]
作者:
114742221
時間:
2023-9-17 15:11
標題:
STM32單片機溫濕度DHT11程序
溫濕度DHT11
//////////////////////////////////////////////////////////////////////////////////
// 功能描述 : OLED I2C 顯示溫濕度的值
// 說明: 主控 stm32f103c8t6
// ----------------------------------------------------------------
// GND 電源地
// VCC 3.3v電源
// D0 PA0(SCL)
// D1 PA1(SDA)
// RES PA2(SPI模塊改成IIC模塊需要接此引腳,IIC模塊用戶請忽略)
// 溫濕度DHT11 信號線引腳接線 PB9
// 串口打印接口---- PA9 PA10
// ----------------------------------------------------------------
//******************************************************************************/
#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "bmp.h"
#include "DHT11.h"
#include "usart.h"
int main(void)
{
u8 wd=0;
u8 sd=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();
uart_init(115200);
DHT11_Init();
OLED_Init();
OLED_ColorTurn(0);//0正常顯示,1 反色顯示
OLED_DisplayTurn(0);//0正常顯示 1 屏幕翻轉顯示
while(1)
{
DHT11_Read_Data(&wd,&sd);//讀取溫濕度值
printf("當前環境溫度: %d ℃\r\n",wd); //串口打印溫濕度
printf("當前環境濕度: %d %%\r\n",sd);
OLED_ShowChinese(0,0,0,16,1); //溫 x,y
OLED_ShowChinese(18,0,1,16,1);//度
OLED_ShowNum(50,0,wd,2,16,1); //顯示溫度值
OLED_ShowString(36,0,":",16,1);
OLED_ShowChinese(0,16,2,16,1);//濕
OLED_ShowChinese(18,16,1,16,1);//度
OLED_ShowNum(50,16,sd,2,16,1); //顯示濕度值
OLED_ShowString(36,16,":",16,1);
OLED_ShowString(68,16,"%",16,1); //
delay_ms(1000);
OLED_Refresh();
}
}
復制代碼
#include "stm32f10x.h"
#include "DHT11.h"
void DHT11_IO_IN(void)//溫濕度模塊輸入函數
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = IO_DHT11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIO_DHT11, &GPIO_InitStructure);
}
void DHT11_IO_OUT(void)//溫濕度模塊輸出函數
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IO_DHT11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIO_DHT11, &GPIO_InitStructure);
}
//復位DHT11
void DHT11_Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_Low; //DQ=0
DelayXms(20); //拉低至少18ms
DHT11_DQ_High; //DQ=1
DelayUs(30); //主機拉高20~40us
}
//等待DHT11的回應
//返回1:未檢測到DHT11的存在
//返回0:存在
u8 DHT11_Check(void)
{
u8 retry = 0; //定義臨時變量
DHT11_IO_IN();//SET INPUT
while ((GPIO_ReadInputDataBit(GPIO_DHT11, IO_DHT11) == 1) && retry < 100) //DHT11會拉低40~80us
{
retry++;
DelayUs(1);
};
if(retry >= 100)return 1;
else retry = 0;
while ((GPIO_ReadInputDataBit(GPIO_DHT11, IO_DHT11) == 0) && retry < 100) //DHT11拉低后會再次拉高40~80us
{
retry++;
DelayUs(1);
};
if(retry >= 100)return 1;
return 0;
}
//從DHT11讀取一個位
//返回值:1/0
u8 DHT11_Read_Bit(void)
{
u8 retry = 0;
while((GPIO_ReadInputDataBit(GPIO_DHT11, IO_DHT11) == 1) && retry < 100) //等待變為低電平
{
retry++;
DelayUs(1);
}
retry = 0;
while((GPIO_ReadInputDataBit(GPIO_DHT11, IO_DHT11) == 0) && retry < 100) //等待變高電平
{
retry++;
DelayUs(1);
}
DelayUs(40);//等待40us
if(GPIO_ReadInputDataBit(GPIO_DHT11, IO_DHT11) == 1)
return 1;
else
return 0;
}
//從DHT11讀取一個字節
//返回值:讀到的數據
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讀取一次數據
//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位數據
{
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的IO口 DQ 同時檢測DHT11的存在
//返回1:不存在
//返回0:存在
void DHT11_Init(void)
{
DHT11_Rst(); //復位DHT11
DHT11_Check();//等待DHT11的回應
}
復制代碼
原理圖: 無
仿真: 無
代碼:
DHT11溫濕度程序.7z
(197.04 KB, 下載次數: 71)
2023-9-17 15:16 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
aide51
時間:
2024-2-18 12:13
應該有一個配套的原理圖吧
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1