[DemoCode下载] 使用GPIO操作DHT11

[复制链接]
1180|6
 楼主| a_ziliu 发表于 2016-12-26 10:39 | 显示全部楼层 |阅读模式
在NUC240上面使用GPIO操作DHT11
EC_NUC230_240_DHT11.zip (930.11 KB, 下载次数: 20)
643757107 发表于 2016-12-26 13:47 | 显示全部楼层
这个芯片的时序很简单,用IO来做确实很快。
huangcunxiake 发表于 2016-12-31 10:30 | 显示全部楼层
这个芯片应该算是SPI接口的,不过一般都是用IO来模拟操作
huangcunxiake 发表于 2016-12-31 10:31 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 3 $
  5. * $Date: 15/08/18 11:54a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC200 Series LED Controller Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "NUC230_240.h"
  14. #include "NuEdu-Basic01.h"


  15. void Timer_initial(void)
  16. {
  17.         /* Step 1. Enable and Select Timer clock source */         
  18.   CLK_EnableModuleClock(TMR0_MODULE);
  19.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);

  20.         /* Step 2. Select Operation mode */       
  21.         TIMER0->TCSR=TIMER_PERIODIC_MODE;                //Select periodic mode for operation mode

  22.         /* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
  23.         TIMER0->TCSR|=11;        // Set Prescale [0~255]
  24.         TIMER0->TCMPR  = 16777215;                // Set TICR(TCMP) [0~16777215]
  25.                
  26.           TIMER0->TCSR|=TIMER_TCSR_TDR_EN_Msk;                // Enable TDR function
  27.         TIMER_Start(TIMER0);                      //Enable Timer0
  28. }


  29. unsigned int  DHT11_RESET(void)
  30. {
  31.           unsigned int old_time,poll_time;
  32.             /* The Quasi-bidirection mode could be used as input with pull up enable */
  33.     GPIO_SetMode(PA, BIT0, GPIO_PMD_QUASI);
  34.          PA0=1;
  35.                 PA0=0;
  36.           CLK_SysTickDelay(18000);//delay 18ms
  37.            PA0=1;
  38.          
  39.         poll_time=TIMER0->TDR;
  40.         //wait ack
  41.          while(1)                 
  42.          {
  43.                         if (PA0 == 0)
  44.                         {                               
  45.                                 old_time=TIMER0->TDR;                                
  46.                                 break;
  47.                         }       
  48.                         if(TIMER0->TDR-poll_time>1000)
  49.                                 return 1;//DHT11 time out, no ack.
  50.                 }
  51.                
  52.             while(1)
  53.                   {                            
  54.                         if (PA0 == 1)
  55.                         {                                                       
  56.                                 //printf("0x%x\n\r",TIMER0->TDR-old_time);
  57.                                 break;
  58.                         }       
  59.                   }
  60.                         return 0;//pass
  61. }

  62. unsigned int temp1,temp2,temp3,temp4;
  63. unsigned int DHT11_Read(void)
  64. {
  65.   int i=0;
  66.         unsigned int old_time;
  67.         Timer_initial();
  68. if(DHT11_RESET()==0)
  69. {
  70.   while(PA0==1);
  71.                        
  72.         temp1=0;
  73.         for(i=0;i<8;i++)
  74.         {
  75.                 temp1=temp1<<1;
  76.           while(PA0==0);
  77.                 old_time=TIMER0->TDR;
  78.                 //GPIOC->DOUT=0;
  79.                
  80.                 while(PA0==1);
  81.                 //GPIOC->DOUT=1;
  82.                 if(TIMER0->TDR-old_time>40)
  83.                 temp1=temp1|0x01;               
  84.         }
  85.         temp2=0;
  86.         for(i=0;i<8;i++)
  87.         {
  88.                 temp2=temp2<<1;
  89.           while(PA0==0);
  90.                 old_time=TIMER0->TDR;
  91.                 //GPIOC->DOUT=0;
  92.                
  93.                 while(PA0==1);
  94.                 //GPIOC->DOUT=1;
  95.                 if(TIMER0->TDR-old_time>40)
  96.                 temp2=temp2|0x01;
  97.                
  98.         }
  99.        
  100.         temp3=0;
  101.         for(i=0;i<8;i++)
  102.         {
  103.                 temp3=temp3<<1;
  104.           while(PA0==0);
  105.                 old_time=TIMER0->TDR;
  106.                 //GPIOC->DOUT=0;
  107.                
  108.                 while(PA0==1);
  109.                 //GPIOC->DOUT=1;
  110.                 if(TIMER0->TDR-old_time>40)
  111.                 temp3=temp3|0x01;               
  112.         }       

  113.         temp4=0;
  114.         for(i=0;i<8;i++)
  115.         {
  116.                 temp4=temp4<<1;
  117.           while(PA0==0);
  118.                 old_time=TIMER0->TDR;
  119.                 //GPIOC->DOUT=0;
  120.                
  121.                 while(PA0==1);
  122.                 //GPIOC->DOUT=1;
  123.                 if(TIMER0->TDR-old_time>40)
  124.                 temp4=temp4|0x01;               
  125.         }       

  126.         return 0;
  127. }
  128. else
  129.         return 1;
  130.          
  131. }


  132. /*---------------------------------------------------------------------------------------------------------*/
  133. /*  MAIN function                                                                                          */
  134. /*---------------------------------------------------------------------------------------------------------*/
  135. int main(void)
  136. {

  137.     SYS_Init();
  138.         UART0_Init();
  139.         if(DHT11_Read()==0)
  140.         {
  141.                 printf("humidity: %d.%d\n\r",temp1, temp2);
  142.                 printf("temperature: %d.%d\n\r",temp3, temp4);
  143.         }
  144.         while(1);
  145. }


捉虫天师 发表于 2016-12-31 20:43 | 显示全部楼层
例程不错,收了。
598330983 发表于 2017-1-1 21:36 | 显示全部楼层
貌似定时器采用了数据结构体的方式存储相关信息。
mintspring 发表于 2017-1-2 15:03 | 显示全部楼层
一些初始化函数应该在头文件里吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

310

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部