jinglixixi 发表于 2020-10-15 21:03

【AT-START-F403A测评】+DHT22温湿度检测及显示

<p>除了显示器件的驱动,对各种数字传感器的驱动也是一件十分有益的事情。</p>

<p>这里就介绍一款单总线数字式温湿度传感器的驱动方法,该器件为DHT22,它较之于DHT11的精度更高,可达小数点后1位。</p>

<p>使用该器件时,其数据引脚与PB11相连接,故有如下的输出高低电平及读取引脚状态的定义引脚:</p>

<p>#define DHT22_D0_H&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIOB-&gt;BSRE = GPIO_Pins_15</p>

<p>#define DHT22_D0_L&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIOB-&gt;BRE = GPIO_Pins_15</p>

<p>#define DHT22_D0_R&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIO_ReadInputDataBit(GPIOB, GPIO_Pins_15)</p>

<p>&nbsp;</p>

<p>对DHT22初始化的函数为:</p>

<pre>
<code class="language-cpp">uint8_t DHT22_Init(void)
{
      GPIO_InitType GPIO_InitStructure;
      RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
      GPIO_StructInit(&amp;GPIO_InitStructure);
      GPIO_InitStructure.GPIO_Pins = GPIO_Pins_15;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
      GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
      GPIO_Init(GPIOB, &amp;GPIO_InitStructure);
      DHT22_Rst();
      return DHT22_Check();
}</code></pre>

<p>&nbsp;</p>

<p>读取DHT22数据的函数为:</p>

<pre>
<code class="language-cpp">uint8_t DHT22_Read_Data(uint8_t *temp,uint8_t *humi)   
{       
         uint8_t buf;
         uint8_t i;
         DHT22_Rst();
         if(DHT22_Check()==0)
         {
                   for(i=0;i&lt;5;i++)
                   {
                            buf=DHT22_Read_Byte();
                   }
                   if((buf+buf+buf+buf)==buf)
                   {
                            *humi=(buf*256+buf)/10;
                            *temp=(buf*256+buf)/10;
                   }
         }else return 1;
         return 0; 
}</code></pre>

<p>实现温湿度检测功能程序如下:</p>

<pre>
<code class="language-cpp">if(i==4)
{
           showhanzi16h(80,100,5,1);
           showhanzi16h(80,84,6,1);
           showhanzi16h(180,280,13,0);
           showhanzi16h(180,264,14,0);        
           while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pins_0)==0);
           LCD_Clear(WHITE);         
           show_imageh(10,300,0);  //200*200 * 1     
           BACK_COLOR=WHITE;
           POINT_COLOR=RED;
           LCD_ShowStringH(60,90,"DHT22 TEST");
           senflag=DHT22_Init();    
           if(senflag) LCD_ShowStringH(80,90,"NO");
           else   LCD_ShowStringH(80,90,"OK");
           while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pins_0)==0)
           {
                  if(!senflag)
                  {
                   DHT22_Read_Data(&amp;temperature,&amp;humidity);
                     LCD_ShowCharh(100,90,'T',1);
                     LCD_ShowCharh(100,80,'e',1);
                     LCD_ShowCharh(100,70,'m',1);
                     LCD_ShowCharh(100,60,'p',1);
                     LCD_ShowCharh(100,50,':',1);
                     LCD_ShowCharh(100,40,temperature/10+'0',0);
                     LCD_ShowCharh(100,30,temperature%10+'0',0);
                     LCD_ShowCharh(120,90,'H',1);
                     LCD_ShowCharh(120,80,'u',1);
                     LCD_ShowCharh(120,70,'m',1);
                     LCD_ShowCharh(120,60,'i',1);
                     LCD_ShowCharh(120,50,':',1);
                     LCD_ShowCharh(120,40,humidity/10+'0',0);
                     LCD_ShowCharh(120,30,humidity%10+'0',0);
                }
                  Delay_ms(200u);
          }
          LCD_Clear(WHITE);
          AT32_jm();
          showhanzi16h(80,100,5,1);
          showhanzi16h(80,84,6,1);
          showhanzi16h(180,280,13,0);
        showhanzi16h(180,264,14,0);
}</code></pre>

<p>该程序的运行效果如图1和图2所示,其中图1为选取环境检测功能,图2位温湿度显示界面。</p>

<p></p>

<p>图1 功能选择</p>

<p></p>

<p>图2 温湿度显示</p>

w494143467 发表于 2020-10-15 21:53

<p>感谢分享,可以把温度小数点后一位也显示出来哦~这样会更能体现DHT22呢!</p>

jinglixixi 发表于 2020-10-16 10:11

<div class="quote">
<blockquote><font size="2"><a href="forum.php?mod=redirect&amp;goto=findpost&amp;pid=3017107&amp;ptid=1144652" target="_blank"><font color="#999999">w494143467 发表于 2020-10-15 21:53</font></a></font> 感谢分享,可以把温度小数点后一位也显示出来哦~这样会更能体现DHT22呢!</blockquote>
</div>

<p>当时图省事,没加否则需再添加一条显示小数的语句。</p>

okhxyyo 发表于 2020-10-19 09:46

<p><a href="https://bbs.eeworld.com.cn/thread-1143018-1-1.html" target="_blank">雅特力AT-START-F403A测评汇总</a></p>

<p>汇总贴:<a href="https://bbs.eeworld.com.cn/thread-1143018-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1143018-1-1.html</a></p>
页: [1]
查看完整版本: 【AT-START-F403A测评】+DHT22温湿度检测及显示