用STM32做的溫濕度簡單嘗試,將數(shù)據(jù)通過串口在上位機上顯示
單片機源程序如下:
- #include "dht11.h"
- //DHT11初始化
- //返回0:初始化成功,1:失敗
- u8 DHT11_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStucture;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
-
- GPIO_InitStucture.GPIO_Pin=GPIO_Pin_11;
- GPIO_InitStucture.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStucture.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOG,&GPIO_InitStucture);
- GPIO_SetBits(GPIOG,GPIO_Pin_11);
-
- DHT11_Rst();
- return DHT11_Check();
-
- }
- //DHT11復位
- void DHT11_Rst()
- {
- DHT11_IO_OUT(); //設置OUTPUT
- DHT11_DQ_OUT=0; //拉低DQ
- delay_ms(20); //拉低至少18ms
- DHT11_DQ_OUT=1; //DQ=1
- delay_us(20); //主機拉高20~40us
- }
- //等待DHT11的回應
- //返回1:未檢測到DHT11的存在
- //返回0:存在
- u8 DHT11_Check()
- {
- u8 retry=0;
- DHT11_IO_IN(); //設置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_ms(1);
- };
- if(retry>=100)return 1;
- return 0;
-
- }
- //從DHT11讀取一個位
- //返回值:1/0
- u8 DHT11_Read_Bit()
- {
- u8 retry;
- while(DHT11_DQ_IN&&retry<100)//等待變?yōu)榈碗娖?2~14us開始
- {
- retry++;
- delay_us(1);
-
- }
- retry=0;
- while(!DHT11_DQ_IN&&retry<100)//等待變?yōu)楦唠娖?26~28us表示0,116~118us表示1
- {
- retry++;
- delay_us(1);
- }
- delay_us(40);//等待40us
- if(DHT11_DQ_IN)return 1;
- 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)
- //hum1:濕度值(范圍:20%~90%)
- //返回值:0正常,1讀取失敗
- u8 DHT11_Read_Data(u8 *temp,u8 *hum1)
- {
- 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])//接收到的40位數(shù)據(jù)buf[0],buf[1],buf[2],buf[3],分別表示濕度高8位,低8位,溫度高8位,低8位,buf[4]表示校驗位
- {
- *hum1=buf[0];
- *temp=buf[2];
-
-
-
- }
-
- }else return 1;
- return 0;
- }
- //DHT11輸出模式配置
- void DHT11_IO_OUT()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOG,&GPIO_InitStructure);
-
- }
- //DHT11輸入模式配置
- void DHT11_IO_IN()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOG,&GPIO_InitStructure);
- }
復制代碼- #include "delay.h"
- #include "sys.h"
- #include "oled.h"
- #include "dht11.h"
- void LED_GPIO_Init(void)//GPIO配置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- }
- int main(void)
- {
- u16 miao;
- u8 temperature;
- u8 humidity;
-
- delay_init();//延時函數(shù)初始化
- NVIC_Configuration();//設置NVIC中斷分組為2:2位搶占優(yōu)先級,2位相應優(yōu)先級,
- OLED_Init();
-
- DHT11_Init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- OLED_Clear();
- OLED_ShowString(0,0,"DHT11",8);
- OLED_ShowCHinese(8,0,0);
- OLED_ShowCHinese(16,0,1);
- OLED_ShowString(0,2,"Temp: C",16);
- OLED_ShowString(0,4,"Humi: %",16);
- while(1)
- {
-
-
- printf("溫度為:%d\n",temperature);
-
- // if(miao>=4)
- // {
- // PBout(0)=!PBout(0);//LED閃爍
- // DHT11_Read_Data(&temperature,&humidity);//讀取溫濕度
- // OLED_ShowNum(0,0,temperature,8,8);//顯示溫度
- // OLED_ShowNum(0,2,humidity,16,16);//顯示濕度
- // miao=0;
- //
- // }
- }
-
- }
復制代碼
Keil代碼下載:
DHT11的Keil代碼.7z
(191.88 KB, 下載次數(shù): 69)
2022-7-19 17:12 上傳
點擊文件名下載附件
|