【ESK32-360测评】+ DHT22与单总线检测温湿度
本帖最后由 jinglixixi 于 2020-8-29 08:53 编辑<p>DHT22是一种单总线的数字式温湿度传感器,它共有3个引脚,只需占用一个GPIO口接口工作。</p>
<p>通过这条总线既靠它发出指令,也靠它接收数据。此外,为了进行信号的识别,还需相应的时序配合。有了前面的I2C接口的基础,就可以探索一下DHT22的驱动及温湿度检测。</p>
<p>1. DHT22温湿度检测</p>
<p>DHT22与MCU的连接关系为:</p>
<p>DAT——PC12</p>
<p>其余两个引脚则接3.3V工作电源</p>
<p>定义GPIO口读取电位及输出高低电平的语句为:</p>
<p>#define DHT22_D0_H HT_GPIOC->SRR = GPIO_PIN_12</p>
<p>#define DHT22_D0_L HT_GPIOC->RR = GPIO_PIN_12</p>
<p>#define DHT22_D0_R GPIO_ReadInBit(HT_GPIOC, GPIO_PIN_12)</p>
<p> </p>
<p>设置GPIO为输入、输出的函数为:</p>
<pre>
<code class="language-cpp">void DHT22_IO_IN(void)
{
AFIO_GPxConfig(GPIO_PC, GPIO_PIN_12, 0);
GPIO_PullResistorConfig(HT_GPIOC, GPIO_PIN_12, GPIO_PR_DOWN);
GPIO_DirectionConfig(HT_GPIOC, GPIO_PIN_12, GPIO_DIR_IN);
GPIO_InputConfig(HT_GPIOC, GPIO_PIN_12, ENABLE);
}</code></pre>
<pre>
<code class="language-cpp">void DHT22_IO_OUT(void)
{
GPIO_PullResistorConfig(HT_GPIOC, GPIO_PIN_12, GPIO_PR_DISABLE);
GPIO_DriveConfig(HT_GPIOC, GPIO_PIN_12, GPIO_DV_8MA);
GPIO_DirectionConfig(HT_GPIOC, GPIO_PIN_12, GPIO_DIR_OUT);
GPIO_InputConfig(HT_GPIOC, GPIO_PIN_12, ENABLE);
}</code></pre>
<p> </p>
<p>DHT22读取数据的函数为:</p>
<pre>
<code class="language-cpp">u8 DHT22_Read_Data(u16 *temp,u16 *humi)
{
u8 buf;
u8 i;
DHT22_Rst();
if(DHT22_Check()==0)
{
for(i=0;i<5;i++)
{
buf<i>=DHT22_Read_Byte();
}
if((buf+buf+buf+buf)==buf)
{
*humi=(buf*256+buf);
*temp=(buf*256+buf);
}
} else return 1;
return 0;
}</i></code></pre>
<p><i>实现图1所示效果的主程序为:</i></p>
<pre>
<i>
<code class="language-cpp">int main(void)
{
u8 senflag;
SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK);
SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000000);
SYSTICK_IntConfig(ENABLE);
HT32F_DVB_OUT_Init();
HT_GPIOD->RR = GPIO_PIN_0;
Delayms(100);
UxART_Configuration();
senflag=DHT22_Init();
if(senflag) gHelloString = '1';
else gHelloString = '0';
UxART_TxTest();
while(1)
{
if(!senflag)
{
DHT11_Read_Data(&temperature,&humidity);
gHelloString = temperature/100+'0';
gHelloString = temperature%100/10+'0';
gHelloString = temperature%10+'0';
gHelloString = humidity/100+'0';
gHelloString = humidity%100/10+'0';
gHelloString = humidity%10+'0';
}
Delayus(2000000);
}
}</code></i></pre>
<p><i> </i></p>
<p><i>图1温湿度值输出显示</i></p>
<p><i> </i></p>
<p><i>2.LCD屏显示温湿度值</i></p>
<p><i>显示温湿度值得主程序为:</i></p>
<pre>
<i>
<code class="language-cpp">int main(void)
{
u8 senflag;
SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK);
SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000000);
SYSTICK_IntConfig(ENABLE);
HT32F_DVB_OUT_Init();
HT_GPIOD->RR = GPIO_PIN_0;
Delayms(100);
LCD_Init();
LCD_Config();
LCD_BackColorSet(Black);
LCD_TextColorSet(Yellow);
senflag=DHT22_Init();
if(senflag) LCD_StringLineDisplay8(Line0, " NO SEN!");
else LCD_StringLineDisplay8(Line0, " SEN OK!");
LCD_StringLineDisplay8(Line2, " Holtek");
LCD_StringLineDisplay8(Line3, " HT32 Series F1654 ");
LCD_StringLineDisplay8(Line4, " LCD DHT22 Example ");
LCD_StringLineDisplay8(Line6, " T= . C");
LCD_StringLineDisplay8(Line7, " H= . %");
while(1)
{
if(!senflag)
{
DHT11_Read_Data(&temperature,&humidity);
LCD_ShowNum(Line6,100,temperature/10,3);
LCD_ShowNum(Line6,140,temperature%10,1);
LCD_ShowNum(Line7,100,humidity/10,3);
LCD_ShowNum(Line7,140,humidity%10,1);
}
Delayus(2000000);
}
}</code></i></pre>
<p><i>下载后,其执行效果如图2所示。</i></p>
<p><i></i></p>
<p><i>图2 温湿度值显示</i></p>
<p>评测的好详细</p>
freebsder 发表于 2020-8-29 22:22
评测的好详细
<p>哈哈,能在开发板上实现的就与大家分享一下,以后用的时候知道该功能能实现,而有些功能是不能随便添加的。</p>
页:
[1]