找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2112|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

基于STM32F103單片機(jī)的DHT11溫濕度檢測代碼 串口顯示

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:825202 發(fā)表于 2022-7-19 11:06 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
用STM32做的溫濕度簡單嘗試,將數(shù)據(jù)通過串口在上位機(jī)上顯示

單片機(jī)源程序如下:
  1. #include "dht11.h"

  2. //DHT11初始化
  3. //返回0:初始化成功,1:失敗

  4. u8 DHT11_Init(void)
  5. {
  6.         GPIO_InitTypeDef GPIO_InitStucture;
  7.         
  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
  9.         
  10.         GPIO_InitStucture.GPIO_Pin=GPIO_Pin_11;
  11.         GPIO_InitStucture.GPIO_Mode=GPIO_Mode_Out_PP;
  12.         GPIO_InitStucture.GPIO_Speed=GPIO_Speed_50MHz;
  13.         GPIO_Init(GPIOG,&GPIO_InitStucture);
  14.         GPIO_SetBits(GPIOG,GPIO_Pin_11);
  15.         
  16.         DHT11_Rst();
  17.         return DHT11_Check();
  18.         
  19. }
  20. //DHT11復(fù)位
  21. void DHT11_Rst()
  22. {
  23.         DHT11_IO_OUT(); //設(shè)置OUTPUT
  24.         DHT11_DQ_OUT=0; //拉低DQ
  25.         delay_ms(20);                //拉低至少18ms
  26.         DHT11_DQ_OUT=1;        //DQ=1
  27.         delay_us(20);                //主機(jī)拉高20~40us
  28. }
  29. //等待DHT11的回應(yīng)
  30. //返回1:未檢測到DHT11的存在
  31. //返回0:存在
  32. u8 DHT11_Check()
  33. {
  34.         u8 retry=0;
  35.         DHT11_IO_IN();        //設(shè)置INPUT
  36.         while(DHT11_DQ_IN&&retry<100)//DHT11拉低40~50us
  37.         {
  38.                 retry++;
  39.                 delay_us(1);
  40.         };
  41.         if(retry>=100)return 1;
  42.         else retry=0;
  43.         while(DHT11_DQ_IN&&retry<100)//DHT11拉低再拉高40~50us
  44.         {
  45.                 retry++;
  46.                 delay_ms(1);
  47.         };
  48.         if(retry>=100)return 1;
  49.         return  0;
  50.         
  51. }
  52. //從DHT11讀取一個位
  53. //返回值:1/0
  54. u8 DHT11_Read_Bit()
  55. {
  56.         u8  retry;
  57.         while(DHT11_DQ_IN&&retry<100)//等待變?yōu)榈碗娖?2~14us開始
  58.         {
  59.                 retry++;
  60.                 delay_us(1);
  61.                
  62.         }
  63.         retry=0;
  64.         while(!DHT11_DQ_IN&&retry<100)//等待變?yōu)楦唠娖?26~28us表示0,116~118us表示1
  65.         {
  66.                 retry++;
  67.                 delay_us(1);
  68.         }
  69.         delay_us(40);//等待40us
  70.         if(DHT11_DQ_IN)return 1;
  71.         return 0;
  72. }
  73. //從DHT11讀取一個字節(jié)
  74. //返回值:讀到的數(shù)據(jù)
  75. u8 DHT11_Read_Byte(void)
  76. {
  77.         u8 i,dat;
  78.         dat=0;
  79.         for(i=0;i<8;i++)
  80.         {
  81.                 dat<<=1;
  82.                 dat|=DHT11_Read_Bit();
  83.         }
  84.         return dat;
  85. }
  86. //從DHT11讀取一次數(shù)據(jù)
  87. //temp:溫度值(范圍:0-50)
  88. //hum1:濕度值(范圍:20%~90%)
  89. //返回值:0正常,1讀取失敗
  90. u8 DHT11_Read_Data(u8 *temp,u8 *hum1)
  91. {
  92.         u8 buf[5];
  93.         u8 i;
  94.         DHT11_Rst();
  95.         if(DHT11_Check()==0)
  96.         {
  97.                 for(i=0;i<5;i++)//讀取40位數(shù)據(jù)
  98.                 {
  99.                         buf[i]=DHT11_Read_Byte();
  100.                 }
  101.                 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]表示校驗位
  102.                 {
  103.                         *hum1=buf[0];
  104.                         *temp=buf[2];
  105.                         
  106.                         
  107.                         
  108.                 }
  109.                
  110.         }else return 1;
  111.         return 0;
  112. }
  113. //DHT11輸出模式配置
  114. void DHT11_IO_OUT()
  115. {
  116.         GPIO_InitTypeDef GPIO_InitStructure;
  117.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
  118.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  119.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  120.         GPIO_Init(GPIOG,&GPIO_InitStructure);
  121.         
  122. }

  123. //DHT11輸入模式配置
  124. void DHT11_IO_IN()
  125. {
  126.         GPIO_InitTypeDef GPIO_InitStructure;
  127.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
  128.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  129.         GPIO_Init(GPIOG,&GPIO_InitStructure);
  130. }
復(fù)制代碼
  1. #include "delay.h"
  2. #include "sys.h"
  3. #include "oled.h"
  4. #include "dht11.h"



  5. void LED_GPIO_Init(void)//GPIO配置
  6. {
  7.         GPIO_InitTypeDef        GPIO_InitStructure;
  8.         
  9.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  10.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
  11.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  12.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  13.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  14.         GPIO_SetBits(GPIOB,GPIO_Pin_0);
  15. }

  16. int main(void)
  17. {
  18.         u16 miao;
  19.         u8 temperature;
  20.         u8 humidity;
  21.         
  22.         delay_init();//延時函數(shù)初始化
  23.         NVIC_Configuration();//設(shè)置NVIC中斷分組為2:2位搶占優(yōu)先級,2位相應(yīng)優(yōu)先級,
  24.         OLED_Init();
  25.         
  26.         DHT11_Init();
  27.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  28.         OLED_Clear();
  29.         OLED_ShowString(0,0,"DHT11",8);
  30.         OLED_ShowCHinese(8,0,0);
  31.         OLED_ShowCHinese(16,0,1);
  32.         OLED_ShowString(0,2,"Temp:           C",16);
  33.         OLED_ShowString(0,4,"Humi:    %",16);
  34.         while(1)
  35.         {
  36.                
  37.                
  38.                 printf("溫度為:%d\n",temperature);
  39.                
  40. //                if(miao>=4)
  41. //                {
  42. //                                PBout(0)=!PBout(0);//LED閃爍
  43. //                                DHT11_Read_Data(&temperature,&humidity);//讀取溫濕度
  44. //                                OLED_ShowNum(0,0,temperature,8,8);//顯示溫度
  45. //                                OLED_ShowNum(0,2,humidity,16,16);//顯示濕度
  46. //                                miao=0;
  47. //        
  48. //                }
  49.         }
  50.         
  51. }
復(fù)制代碼

Keil代碼下載:
DHT11的Keil代碼.7z (191.88 KB, 下載次數(shù): 69)

評分

參與人數(shù) 1黑幣 +30 收起 理由
admin + 30 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏5 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表