질문게시판 > 답변 : 아두이노 질문이요!

TODAY213 TOTAL2,696,092
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


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_TIMEOUT
int dht:: read11(uint8_t pin){
  // READ VALUES
  int rv=read(pin);
  if(rv!=DHTLIB_OK){
    humidity   =DHTLIB_INVALID_VALUE// invalid value,or is NaN prefered?
    temperature=DHTLIB_INVALID_VALUE// invalid value
    return rv;
  }
  // CONVERT AND STORE
  humidity   =bits[0]; // bit[1]==0;
  temperature=bits[2]; // bits[3]==0;
  // TEST CHECKSUM
  // bits[1]&&bits[3]both 0
  uint8_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_TIMEOUT
int dht:: read21(uint8_t pin){
  return read22(pin); // dataformat identical to DHT22
}
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht:: read22(uint8_t pin){
  // READ VALUES
  int rv=read(pin);
  if(rv!=DHTLIB_OK){
    humidity   =DHTLIB_INVALID_VALUE// invalid value,or is NaN prefered?
    temperature=DHTLIB_INVALID_VALUE// invalid value
    return rv;                        // propagate error value
  }
  // CONVERT AND STORE
  humidity=word(bits[0],bits[1])*0.1;
  if(bits[2]&0x80){ temperature=-0.1*word(bits[2]&0x7F,bits[3]); } // negative temperature
  else            { temperature0.1*word(bits[2],bits[3]); }
  // TEST CHECKSUM
  uint8_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_TIMEOUT
int dht:: read(uint8_t pin){
  // INIT BUFFERVAR TO RECEIVE DATA
  uint8_t cnt=7;
  uint8_t idx=0;
  // EMPTY BUFFER
  for(uint8_t i=0i<5i++)bits[i]=0;
  // REQUEST SAMPLE
  pinMode(pin,OUTPUT);
  digitalWrite(pin,LOW ); delay(20);
  digitalWrite(pin,HIGH); delayMicroseconds(40);
  pinMode(pin,INPUT);
  // GET ACKNOWLEDGE or TIMEOUT
  unsigned 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 BYTES
  for(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=7idx++; } // next byte?
    else      { cnt--; }
  }
  return DHTLIB_OK;
}
// END OF FILE

 

dht 라이브러리 질문이군요

 

  // READ THE OUTPUT-40 BITS=>5 BYTES
  for(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=7idx++; } // next byte?
    else      { cnt--; }
  }
  return DHTLIB_OK;

 

주석에 40비트를 읽어서 5바이트로 저장하는 코드라고 적혀있습니다.

 

 

  // READ THE OUTPUT-40 BITS=>5 BYTES
  for(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 이상이면 비트 set  
    if(--cnt<0){ cnt=7idx++; }            // next byte?
  }
  return DHTLIB_OK;

조금 수정하면 이렇게 됩니다.

 

 

 

[PDF] 

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 40bitand 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 rightthe 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 BUFFER

  for(uint8_t i=0i<5i++)bits[i]=0; 

 

  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 0

조회수 6,566

등록된 댓글이 없습니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.
ATMEGA128PWMLED초음파
아두이노AVR블루투스LCD
UART모터적외선ATMEGA
전체 스위치 센서
질문게시판 목록
제목 작성자 작성일 조회
공지 MCU, AVR, 아두이노 등 전자공학에 관련된 질문은 질문게시판에서만 작성 가능합니다. 스태프 19-01-15 19665
공지 사이트 이용 안내댓글[28] master 17-10-29 34155
질문 아두이노 리니어 제어 모듈 설계중입니다. 도와주세요 새글 갓비타 23-06-06 15
질문 dc모터 제어 관련 질문 드려요 ㅠㅠ!! 새글 dpwl 23-06-06 15
질문 pixy2 cam 을 이용한 색상인식 모터 제어 새글 가나다라 23-06-05 13
질문 안녕하세요 제품 품목 이름에 대해서 궁금합니다. 이미지새글첨부파일 알려주시면감사합니다 23-06-05 18
질문 Atmega128 온도센서로 led제어 질문드려요 이미지새글첨부파일 얍얍 23-06-05 23
질문 아구이노 코드를 atmega 128 코드로 변환 하고 싶습니다 ㅠㅠ 새글 기로롱 23-06-05 22
질문 atmega128 uart 질문입니다. bme12 23-06-05 23
질문 라즈베리파이에 풀 프레임 이미지센서 활용에 대한 질문이 있습니다. 이미지첨부파일 KYLO 23-06-04 19
질문 아두이노 시리얼 번호를 이용해 led 제어 wnion 23-06-04 21
질문 ATMEGA128 혹시 여기서 왜 인터럽트 기능이 안되는지 알 수 있나요 IEEE 23-06-04 33
질문 stm32f767을 이용해서 자이로가속도 센서의 값 받아오기 rlchwjswk 23-06-03 22
질문 아두이노 모터제어 관련해서 질문드립니다!댓글[1] 이미지첨부파일 아두이노어렵잖아 23-06-03 46
질문 atmega128 디지털조도센서 코드오류댓글[1] 이미지 까미 23-06-02 41
질문 atmega128 디지털 조도 센서댓글[1] 까미 23-06-02 41
질문 적외선리모콘으로 부저를제어 하는방법 질문입니다.댓글[4] Tell 23-06-02 24
질문 lora 무선 모듈에 관한 질문입니다.댓글[1] 로이스10 23-06-01 22
질문 적외선 송수신기 DC모터2개 제어 질문입니다.댓글[5] Tell 23-06-01 40
질문 스텝모터 제어 코드 질문댓글[5] pmh11 23-05-31 46
질문 초음파 센서를 이용한 인원 카운팅댓글[1] 초음파야 23-05-31 38
질문 모터 Hall 스위치 연결 문의댓글[1] 오후 23-05-31 26
질문 아두이노 lcd 문자 스크롤디스플레이 wnion 23-05-31 34
답변 답변글 답변 : 아두이노 lcd 문자 스크롤디스플레이댓글[1] master 23-05-31 34
질문 아두이노 타이머 인터럽트 미ㅏㄴㅇ 23-05-30 48
답변 답변글 답변 : 아두이노 타이머 인터럽트댓글[7] master 23-05-30 55
질문 THC-Soil Sensor with TTL 모듈 아두이노 센서값 받아오기댓글[1] ppiickle 23-05-30 34
질문 stm32 psd센서구동 질문댓글[2] 수포자 23-05-29 31
질문 앱인벤터 아두이노 보드 LCD 글씨 나타내기 질문댓글[7] 이미지 당찬병아리 23-05-29 49
질문 atmega128 led와 fan댓글[3] 이라 23-05-28 56
게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기