BASIC4MCU | 질문게시판 | 답변 : 아두이노 질문이요!
페이지 정보
작성자 master 작성일2018-12-09 20:32 조회6,566회 댓글0건본문
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}humidity = bits[0];
temperature = bits[2];
제가 이걸 봤는데 여기보니 여기서 온도값이랑 습도값을 받더라구요.
근데 궁금한게..
bits[0]이 1<<7 ~ 1<<0 값을 다 더해서 습도값이고
bits[2]이 1<<7 ~ 1<<0 값을 다 더해서 온도값이잖아요.
근데 이러면 값이 똑같아지지않나요? 온도 습도 채널 잡은 것도 아니고...
왜 어떻게 온도 습도 값이 정해지나요 ㅠㅠ
//
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-12-09 오후 8:25:23// by Ok-Hyun Park//// FILE: dht.cpp// AUTHOR: Rob Tillaart// VERSION: 0.1.07// PURPOSE: DHT Temperature&Humidity Sensor library for Arduino// URL: http: //arduino.cc/playground/Main/DHTLib// HISTORY:// 0.1.07 added support for DHT21// 0.1.06 minimize footprint(2012-12-27)// 0.1.05 fixed negative temperature bug(thanks to Roseman)// 0.1.04 improved readability of code using DHTLIB_OK in code// 0.1.03 added error values for temp and humidity when read failed// 0.1.02 added error codes// 0.1.01 added support for Arduino 1.0,fixed typos(31/12/2011)// 0.1.0 by Rob Tillaart(01/04/2011)// inspired by DHT11 library// Released to the public domain//#include "dht.h"#define TIMEOUT 10000// // // // // // // // // // // // // // // // // // // // // // // // // ///// PUBLIC// return values:// DHTLIB_OK// DHTLIB_ERROR_CHECKSUM// DHTLIB_ERROR_TIMEOUTint dht:: read11(uint8_t pin){// READ VALUESint rv=read(pin);if(rv!=DHTLIB_OK){humidity =DHTLIB_INVALID_VALUE; // invalid value,or is NaN prefered?temperature=DHTLIB_INVALID_VALUE; // invalid valuereturn rv;}// CONVERT AND STOREhumidity =bits[0]; // bit[1]==0;temperature=bits[2]; // bits[3]==0;// TEST CHECKSUM// bits[1]&&bits[3]both 0uint8_t sum=bits[0]+bits[2];if(bits[4]!=sum)return DHTLIB_ERROR_CHECKSUM;return DHTLIB_OK;}// return values:// DHTLIB_OK// DHTLIB_ERROR_CHECKSUM// DHTLIB_ERROR_TIMEOUTint dht:: read21(uint8_t pin){return read22(pin); // dataformat identical to DHT22}// return values:// DHTLIB_OK// DHTLIB_ERROR_CHECKSUM// DHTLIB_ERROR_TIMEOUTint dht:: read22(uint8_t pin){// READ VALUESint rv=read(pin);if(rv!=DHTLIB_OK){humidity =DHTLIB_INVALID_VALUE; // invalid value,or is NaN prefered?temperature=DHTLIB_INVALID_VALUE; // invalid valuereturn rv; // propagate error value}// CONVERT AND STOREhumidity=word(bits[0],bits[1])*0.1;if(bits[2]&0x80){ temperature=-0.1*word(bits[2]&0x7F,bits[3]); } // negative temperatureelse { temperature= 0.1*word(bits[2],bits[3]); }// TEST CHECKSUMuint8_t sum=bits[0]+bits[1]+bits[2]+bits[3];if(bits[4]!=sum)return DHTLIB_ERROR_CHECKSUM;return DHTLIB_OK;}// // // // // // // // // // // // // // // // // // // // // // // // // ///// PRIVATE// return values:// DHTLIB_OK// DHTLIB_ERROR_TIMEOUTint dht:: read(uint8_t pin){// INIT BUFFERVAR TO RECEIVE DATAuint8_t cnt=7;uint8_t idx=0;// EMPTY BUFFERfor(uint8_t i=0; i<5; i++)bits[i]=0;// REQUEST SAMPLEpinMode(pin,OUTPUT);digitalWrite(pin,LOW ); delay(20);digitalWrite(pin,HIGH); delayMicroseconds(40);pinMode(pin,INPUT);// GET ACKNOWLEDGE or TIMEOUTunsigned int loopCnt=TIMEOUT;while(digitalRead(pin)==LOW){ if(loopCnt--==0)return DHTLIB_ERROR_TIMEOUT; }loopCnt=TIMEOUT;while(digitalRead(pin)==HIGH){ if(loopCnt--==0)return DHTLIB_ERROR_TIMEOUT; }// READ THE OUTPUT-40 BITS=>5 BYTESfor(uint8_t i=0;i<40;i++){loopCnt=TIMEOUT;while(digitalRead(pin)==LOW){ if(loopCnt--==0)return DHTLIB_ERROR_TIMEOUT; }unsigned long t=micros();loopCnt=TIMEOUT;while(digitalRead(pin)==HIGH){ if(loopCnt--==0)return DHTLIB_ERROR_TIMEOUT; }if((micros()-t)>40)bits[idx]|=(1<<cnt);if(cnt==0){ cnt=7; idx++; } // next byte?else { cnt--; }}return DHTLIB_OK;}// END OF FILE
dht 라이브러리 질문이군요
// READ THE OUTPUT-40 BITS=>5 BYTESfor(uint8_t i=0;i<40;i++){loopCnt=TIMEOUT;while(digitalRead(pin)==LOW){ if(loopCnt--==0)return DHTLIB_ERROR_TIMEOUT; }unsigned long t=micros();loopCnt=TIMEOUT;while(digitalRead(pin)==HIGH){ if(loopCnt--==0)return DHTLIB_ERROR_TIMEOUT; }if((micros()-t)>40)bits[idx]|=(1<<cnt);if(cnt==0){ cnt=7; idx++; } // next byte?else { cnt--; }}return DHTLIB_OK;
주석에 40비트를 읽어서 5바이트로 저장하는 코드라고 적혀있습니다.
// READ THE OUTPUT-40 BITS=>5 BYTESfor(uint8_t i=0;i<40;i++){while(digitalRead(pin)==LOW); // 0인 동안 기다리고unsigned long t=micros(); // high가 된 순간loopCnt=TIMEOUT;while(digitalRead(pin)==HIGH); // HIGH인 동안 기다린 후if((micros()-t)>40)bits[idx]|=(1<<cnt); // HIGH가 된 순간부터 40ms 이상이면 비트 setif(--cnt<0){ cnt=7; idx++; } // next byte?}return DHTLIB_OK;조금 수정하면 이렇게 됩니다.
DHT11 Humidity & Temperature Sensor - Mouser Electronics
https://www.mouser.com/ds/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
데이터시트 5페이지에 보면 아래와 같은 내용이 나와 있습니다.
Data consists of decimal and integral parts.A complete data transmission is 40bit, and the sensor sends higher data bit first.//Data format:8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum.//If the data transmission is right, the check-sum should be the last 8bit of "8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data".40비트(5바이트) 구성은 습도 2바이트 + 온도 2바이트 + 첵섬 입니다.
//
if((micros()-t)>40)bits[idx]|=(1<<cnt); // 40ms 이상이면 cnt 비트에 1을 채움else bits[idx]&=~(1<<cnt); // 40ms 이하면 cnt 비트에 0을 채움else 문이 빠져있는 형태인데요
처음에 배열을 미리 0으로 clear 시켜놨기 때문에 else를 생략할 수 있습니다.
// EMPTY BUFFERfor(uint8_t i=0; i<5; i++)bits[i]=0;
댓글 0
조회수 6,566등록된 댓글이 없습니다.