• 树莓派使用DHT11温湿度传感器(C语言)


    硬件:

    树莓派 2.0

    DHT模块  接树莓派5V GND GPIO1

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

    [cpp] view plain copy
     
     print?
    1. //  
    2. //mydht11.c  
    3. //  
    4. #include <wiringPi.h>  
    5. #include <stdio.h>  
    6. #include <stdlib.h>  
    7.   
    8. typedef unsigned char uint8;  
    9. typedef unsigned int  uint16;  
    10. typedef unsigned long uint32;  
    11.   
    12. #define HIGH_TIME 32  
    13.   
    14. int pinNumber =1;  //use gpio1 to read data  
    15. uint32 databuf;  
    16.    
    17.    
    18.   
    19. uint8 readSensorData(void)  
    20. {  
    21.     uint8 crc;   
    22.     uint8 i;  
    23.    
    24.     pinMode(pinNumber,OUTPUT); // set mode to output  
    25.     digitalWrite(pinNumber, 0); // output a high level   
    26.     delay(25);  
    27.     digitalWrite(pinNumber, 1); // output a low level   
    28.     pinMode(pinNumber, INPUT); // set mode to input  
    29.     pullUpDnControl(pinNumber,PUD_UP);  
    30.   
    31.     delayMicroseconds(27);  
    32.     if(digitalRead(pinNumber)==0) //SENSOR ANS  
    33.        {  
    34.          while(!digitalRead(pinNumber)); //wait to high  
    35.   
    36.       for(i=0;i<32;i++)  
    37.        {  
    38.        while(digitalRead(pinNumber)); //data clock start  
    39.        while(!digitalRead(pinNumber)); //data start  
    40.           delayMicroseconds(HIGH_TIME);  
    41.           databuf*=2;  
    42.            if(digitalRead(pinNumber)==1) //1  
    43.            {  
    44.                 databuf++;  
    45.            }  
    46.         }  
    47.   
    48.       for(i=0;i<8;i++)  
    49.        {  
    50.        while(digitalRead(pinNumber)); //data clock start  
    51.        while(!digitalRead(pinNumber)); //data start  
    52.           delayMicroseconds(HIGH_TIME);  
    53.           crc*=2;    
    54.           if(digitalRead(pinNumber)==1) //1  
    55.            {  
    56.                 crc++;  
    57.            }  
    58.         }  
    59.     return 1;  
    60.        }  
    61.    else  
    62.         {  
    63.         return 0;  
    64.          }  
    65. }  
    66.    
    67. int main (void)  
    68. {  
    69.   
    70.   printf("Use GPIO1 to read data! ");  
    71.   
    72.   if (-1 == wiringPiSetup()) {  
    73.     printf("Setup wiringPi failed!");  
    74.     return 1;  
    75.   }  
    76.    
    77.   pinMode(pinNumber, OUTPUT); // set mode to output  
    78.   digitalWrite(pinNumber, 1); // output a high level   
    79.   
    80.   printf("Enter OS------- ");  
    81.   while(1) {  
    82.     pinMode(pinNumber,OUTPUT); // set mode to output  
    83.     digitalWrite(pinNumber, 1); // output a high level   
    84.     delay(3000);  
    85.     if(readSensorData())  
    86.     {  
    87.        printf("Congratulations ! Sensor data read ok! ");  
    88.        printf("RH:%d.%d ",(databuf>>24)&0xff,(databuf>>16)&0xff);   
    89.        printf("TMP:%d.%d ",(databuf>>8)&0xff,databuf&0xff);  
    90.        databuf=0;  
    91.      }  
    92.     else  
    93.      {  
    94.         printf("Sorry! Sensor dosent ans! ");  
    95.        databuf=0;  
    96.       }  
    97.   }  
    98.   return 0;  
    99. }  
  • 相关阅读:
    不能执行已释放的Script的代码(ie错误)
    javascript数组
    Jquery遍历方法
    Jquery选择器汇总
    使用xmlHttprequest 发送异步请求(Ajax核心对象)
    不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串
    解决Js跨域访问的问题
    Oracle 第一天
    计算机图形学1——绪论
    数据库
  • 原文地址:https://www.cnblogs.com/Pond-ZZC/p/6680443.html
Copyright © 2020-2023  润新知