找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2239|回复: 0
打印 上一主题 下一主题
收起左侧

基于STM32F103单片机的DHT11温湿度检测代码 串口显示

[复制链接]
跳转到指定楼层
楼主
ID:825202 发表于 2022-7-19 11:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用STM32做的温湿度简单尝试,将数据通过串口在上位机上显示

单片机源程序如下:
  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复位
  21. void DHT11_Rst()
  22. {
  23.         DHT11_IO_OUT(); //设置OUTPUT
  24.         DHT11_DQ_OUT=0; //拉低DQ
  25.         delay_ms(20);                //拉低至少18ms
  26.         DHT11_DQ_OUT=1;        //DQ=1
  27.         delay_us(20);                //主机拉高20~40us
  28. }
  29. //等待DHT11的回应
  30. //返回1:未检测到DHT11的存在
  31. //返回0:存在
  32. u8 DHT11_Check()
  33. {
  34.         u8 retry=0;
  35.         DHT11_IO_IN();        //设置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)//等待变为低电平12~14us开始
  58.         {
  59.                 retry++;
  60.                 delay_us(1);
  61.                
  62.         }
  63.         retry=0;
  64.         while(!DHT11_DQ_IN&&retry<100)//等待变为高电平 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读取一个字节
  74. //返回值:读到的数据
  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读取一次数据
  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位数据
  98.                 {
  99.                         buf[i]=DHT11_Read_Byte();
  100.                 }
  101.                 if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])//接收到的40位数据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. }
复制代码
  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();//延时函数初始化
  23.         NVIC_Configuration();//设置NVIC中断分组为2:2位抢占优先级,2位相应优先级,
  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. }
复制代码

Keil代码下载:
DHT11的Keil代码.7z (191.88 KB, 下载次数: 69)

评分

参与人数 1黑币 +30 收起 理由
admin + 30 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏5 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表