找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1014|回复: 0
收起左侧

基于stm32单片机的dht11测量代码 串口显示

[复制链接]
ID:651755 发表于 2022-3-27 17:53 | 显示全部楼层 |阅读模式
单片机源程序如下:
  1. #include "dht11.h"
  2. #include "delay.h"
  3. #include "usart.h"       

  4.       
  5. //复位DHT11
  6. void DHT11_Rst(void)          
  7. {                 
  8.         DHT11_IO_OUT();         //SET OUTPUT
  9.     DHT11_DQ_OUT=0;         //拉低DQ
  10.     delay_ms(20);            //拉低至少18ms
  11.     DHT11_DQ_OUT=1;         //DQ=1
  12.         delay_us(30);             //主机拉高20~40us
  13. }
  14. //等待DHT11的回应
  15. //返回1:未检测到DHT11的存在
  16. //返回0:存在
  17. u8 DHT11_Check(void)           
  18. {   
  19.         u8 retry=0;
  20.         DHT11_IO_IN();//SET INPUT         
  21.     while (DHT11_DQ_IN&&retry<100)//DHT11会拉低40~80us
  22.         {
  23.                 retry++;
  24.                 delay_us(1);
  25.         };         
  26.         if(retry>=100)return 1;
  27.         else retry=0;
  28.     while (!DHT11_DQ_IN&&retry<100)//DHT11拉低后会再次拉高40~80us
  29.         {
  30.                 retry++;
  31.                 delay_us(1);
  32.         };
  33.         if(retry>=100)return 1;            
  34.         return 0;
  35. }
  36. //从DHT11读取一个位
  37. //返回值:1/0
  38. u8 DHT11_Read_Bit(void)                          
  39. {
  40.         u8 retry=0;
  41.         while(DHT11_DQ_IN&&retry<100)//等待变为低电平
  42.         {
  43.                 retry++;
  44.                 delay_us(1);
  45.         }
  46.         retry=0;
  47.         while(!DHT11_DQ_IN&&retry<100)//等待变高电平
  48.         {
  49.                 retry++;
  50.                 delay_us(1);
  51.         }
  52.         delay_us(40);//等待40us
  53.         if(DHT11_DQ_IN)return 1;
  54.         else return 0;                  
  55. }
  56. //从DHT11读取一个字节
  57. //返回值:读到的数据
  58. u8 DHT11_Read_Byte(void)   
  59. {        
  60.     u8 i,dat;
  61.     dat=0;
  62.         for (i=0;i<8;i++)
  63.         {
  64.                    dat<<=1;
  65.             dat|=DHT11_Read_Bit();
  66.     }                                                    
  67.     return dat;
  68. }
  69. //从DHT11读取一次数据
  70. //temp:温度值(范围:0~50°)
  71. //humi:湿度值(范围:20%~90%)
  72. //返回值:0,正常;1,读取失败
  73. u8 DHT11_Read_Data(u8 *temp,u8 *humi)   
  74. {        
  75.         u8 buf[5];
  76.         u8 i;
  77.         DHT11_Rst();
  78.         if(DHT11_Check()==0)
  79.         {
  80.                 for(i=0;i<5;i++)//读取40位数据
  81.                 {
  82.                         buf[i]=DHT11_Read_Byte();
  83.                 }
  84.                 if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
  85.                 {
  86.                         *humi=buf[0];
  87.                         *temp=buf[2];
  88.                 }
  89.         }else return 1;
  90.         return 0;            
  91. }

  92. void Dht11Show(void)
  93. {
  94.         u8 temp,humi;
  95.         if(DHT11_Read_Data(&temp,&humi))
  96.         {
  97.                 printf("DHT11 read failed\r\n");
  98.         }
  99.         else
  100.         {
  101.                 printf("温度 %d 湿度 %d \r\n",temp,humi);
  102.         }
  103. }
  104. //初始化DHT11的IO口 DQ 同时检测DHT11的存在
  105. //返回1:不存在
  106. //返回0:存在             
  107. u8 DHT11_Init(void)
  108. {         
  109.         GPIO_InitTypeDef  GPIO_InitStructure;
  110.        
  111.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);         //使能PG端口时钟
  112.        
  113.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                                 //PG11端口配置
  114.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
  115.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  116.         GPIO_Init(GPIOA, &GPIO_InitStructure);                                 //初始化IO口
  117.         GPIO_SetBits(GPIOA,GPIO_Pin_0);                                                 //PA0 输出高
  118.                             
  119.         DHT11_Rst();  //复位DHT11
  120.         return DHT11_Check();//等待DHT11的回应
  121. }
复制代码

  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"         
  6. #include "adc.h"
  7. #include "dht11.h"



  8. int main(void)
  9. {         

  10.         delay_init();                     //延时函数初始化          
  11.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  12.         uart_init(115200);                 //串口初始化为115200
  13.         LED_Init();                                  //初始化与LED连接的硬件接口
  14.         DHT11_Init();
  15.                    
  16.         while(1)
  17.         {
  18.                
  19.                 Dht11Show();
  20.                 delay_ms(500);       
  21.         }
  22. }
复制代码


Keil代码下载: STM32_dht11_串口显示.7z (215.57 KB, 下载次数: 27)

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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