码迷,mamicode.com
首页 > 其他好文 > 详细

[转载] 树莓派读取温湿度传感器DHT11

时间:2015-04-03 20:50:30      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

原文地址: http://blog.csdn.net/liang890319/article/details/8739683

 

硬件:

树莓派 2.0

DHT模块  接树莓派5V GND GPIO1

 

功能:读取传感器数据并打印出来

 

 

//
//mydht11.c
//
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

typedef unsigned char uint8;
typedef unsigned int  uint16;
typedef unsigned long uint32;

#define HIGH_TIME 32

int pinNumber =1;  //use gpio1 to read data
uint32 databuf;
 
 

uint8 readSensorData(void)
{
    uint8 crc; 
    uint8 i;
 
    pinMode(pinNumber,OUTPUT); // set mode to output
    digitalWrite(pinNumber, 0); // output a high level 
    delay(25);
    digitalWrite(pinNumber, 1); // output a low level 
    pinMode(pinNumber, INPUT); // set mode to input
    pullUpDnControl(pinNumber,PUD_UP);

    delayMicroseconds(27);
    if(digitalRead(pinNumber)==0) //SENSOR ANS
       {
         while(!digitalRead(pinNumber)); //wait to high

      for(i=0;i<32;i++)
       {
       while(digitalRead(pinNumber)); //data clock start
       while(!digitalRead(pinNumber)); //data start
          delayMicroseconds(HIGH_TIME);
          databuf*=2;
           if(digitalRead(pinNumber)==1) //1
            {
                databuf++;
            }
        }

      for(i=0;i<8;i++)
       {
       while(digitalRead(pinNumber)); //data clock start
       while(!digitalRead(pinNumber)); //data start
          delayMicroseconds(HIGH_TIME);
          crc*=2;  
          if(digitalRead(pinNumber)==1) //1
           {
                crc++;
           }
        }
    return 1;
       }
   else
        {
        return 0;
         }
}
 
int main (void)
{

  printf("Use GPIO1 to read data!\n");

  if (-1 == wiringPiSetup()) {
    printf("Setup wiringPi failed!");
    return 1;
  }
 
  pinMode(pinNumber, OUTPUT); // set mode to output
  digitalWrite(pinNumber, 1); // output a high level 

  printf("Enter OS-------\n");
  while(1) {
    pinMode(pinNumber,OUTPUT); // set mode to output
    digitalWrite(pinNumber, 1); // output a high level 
    delay(3000);
    if(readSensorData())
    {
       printf("Congratulations ! Sensor data read ok!\n");
       printf("RH:%d.%d\n",(databuf>>24)&0xff,(databuf>>16)&0xff); 
       printf("TMP:%d.%d\n",(databuf>>8)&0xff,databuf&0xff);
       databuf=0;
     }
    else
     {
        printf("Sorry! Sensor dosent ans!\n");
       databuf=0;
      }
  }
  return 0;
}

 

 

wiringpi是通过C语言控制树莓派GPIO口的头文件。在C语言中包含这个头文件之后可以很简单的调用已经封装好的方法来控制树莓派GPIO口。程序需要先安装。

wiringpi官网:http://wiringpi.com/

wiringpi下载和安装:http://wiringpi.com/download-and-install/

wiringpi文档:http://wiringpi.com/reference/

编译和运行:

写好C文件后,通过如下命令进行编译:

 

$ gcc -Wall -o dt11_test dt11-.c -lwiringPi     #连接动态库

 

gcc是编译器,-Wall是在编译时显示警告信息,-o executefilename cfilename.c是将cfilename.c文件编译成文件名为executefilename的可执行文件,-lwiringPi是将wiringPi头文件包含在可执行文件中。

编译完之后会生成文件名为executefilename的文件,使用root权限执行如下命令即可运行:

 

运行程序:
      
$ sudo ./dt_11_test 

 

#wiringPi 源代码,我已经同步到bitbucket.org了。 

   https://bitbucket.org/sndnvaps/wiringpi/

 

   编译wiringpi 动态库

使用git clone  git@bitbucket.org:sndnvaps/wiringpi.git       #下载源代码

   cd wiringpi 

   ./build  

    

 

[转载] 树莓派读取温湿度传感器DHT11

标签:

原文地址:http://www.cnblogs.com/sn-dnv-aps/p/4390899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!