main.c
/*
* dht11.c
*
* Created: 2021-02-04 오전 10:30:42
* Author : User
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "dht11.h"
int main(void)
{
initDHT11(DDRB, PORTB, PINB, PB7);
while (1)
{
int err;
float temp, humi;
if((err = read(&humi, &temp))==0)
{
Serial.print("temperature:");
Serial.print(temp);
Serial.print(" humidity:");
Serial.print(humi);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err);
Serial.println();
}
//delay(DHT11_RETRY_DELAY); //delay for reread
}
}
dht11.h
/*
* dht11.h
*
* Created: 2021-02-04 오전 11:18:57
* Author: User
*/
#ifndef DHT11_H_
#define DHT11_H_
#define HIGH 1
#define LOW 0
#define DHT11_RETRY_DELAY 1000 //1000ms
typedef unsigned char uint8_t;
typedef unsigned long uint32_t;
//전역변수화 시킴 g_(global 변수를 의미)
int g_pin;
unsigned long last_read_time;
int g_input; //PINx & (1<<PINxn)
int g_direction; //DDRx
int g_port;
//함수 prototype
unsigned char readByte();
unsigned long waitFor2(uint8_t target, uint32_t time_out_us);
void waitFor1(uint8_t target);
int read(float* humidity, float* temperature);
void initDHT11(int direction, int port, int input, int pin_number);
unsigned long millis();
unsigned long micros();
#endif /* DHT11_H_ */
dht11.c
/*
* dht11.c
*
* Created: 2021-02-04 오전 11:20:41
* Author: User
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "dht11.h"
void initDHT11(int direction, int port, int input, int pin_number){
g_pin = pin_number;
g_direction = direction;
g_port = port;
g_input = input;
last_read_time=0;
g_direction |= 0 << pin_number;
g_port |= 1 << pin_number;
}
//wait for target status
//parameters
// target : target status waiting for
// time_out_us : time out in microsecond.
uint32_t waitFor2(uint8_t target, uint32_t time_out_us) {
uint32_t start = micros();
uint32_t time_out = start+time_out_us;
while((g_input & (1 << g_pin)) !=target)
{
if(time_out < micros()) return -1;
}
return micros()-start;
}
//wait for target status.
void waitFor1(uint8_t target) {
while((g_input & (1 << g_pin)) !=target);
}
//read one bye
uint8_t readByte() {
int i=0;
uint8_t ret = 0;
for(i=7;i>=0;i--)
{
waitFor1(HIGH); //wait for 50us in LOW status
_delay_us(30); //wait for 30us
if((g_input & (1 << g_pin))==HIGH) //if HIGH status lasts for 30us, the bit is 1;
{
ret|=1<<(i);
waitFor1(LOW); //wait for rest time in HIGH status.
}
}
return ret;
}
int read (float* humidity, float* temperature) {
if((millis() - last_read_time < DHT11_RETRY_DELAY) && last_read_time !=0) {
return -1;
}
g_direction |= 1 << g_pin;
g_port |= 0 << g_pin;
_delay_ms(18);
g_port |= 1 << g_pin;
g_direction |= 0 << g_pin;
if(waitFor2(LOW, 40)<0) return 1; //waiting for DH11 ready
if(waitFor2(HIGH, 90)<0) return 1; //waiting for first LOW signal(80us)
if(waitFor2(LOW, 90)<0) return 1; //waiting for first HIGH signal(80us)
uint8_t hI = readByte();
uint8_t hF = readByte();
uint8_t tI = readByte();
uint8_t tF = readByte();
uint8_t cksum = readByte();
if(hI + hF + tI + tF != cksum){
return 4;
}
*humidity = (float)hI+(((float)hF)/100.0F);
*temperature = (float)tI+(((float)tF)/100.0F);
last_read_time = millis();
return 0;
}
unsigned long micros() {
return 0;
}
unsigned long millis(){
return 0;
}
#include "" 로 쓰는것과 #include <> 의 차이
""는 내가 만든 헤더파일. 같은 경로에 있음
<>는 이미 선언 되어있는 ? 것
<> 를 먼저 쓰고
""를 쓰는것이 원칙이긴 하다
.c 안에는 헤더파일 여러개 넣어도 충돌위험을 크게 걱정하지 않아도 된다 ..
.h 안에는 헤더파일 여러개 넣을 때 충돌을 주의하여야 한다
dht11 아두이노 라이브러리
DHT11.h
/*
* DH11.h
*
* Created on: 2012. 12. 12.
* Author: dalxx
* Version : 0.8
*/
#ifndef DHT11_H_
#define DHT11_H_
#include <Arduino.h>
#define DHT11_RETRY_DELAY 1000 // 1000ms
class DHT11 {
int pin;
unsigned long last_read_time;
protected:
byte readByte();
unsigned long waitFor(uint8_t target, unsigned long time_out_us);
void waitFor(uint8_t target);
public:
DHT11(int pin_number);
~DHT11();
int read( float& humidity, float& temperature);
};
#endif /* DHT11_H_ */
unsigned long waitFor(uint8_t target, unsigned long time_out_us);
void waitFor(uint8_t target);
c언어에서는 똑같은 함수 두번 선언하면 오류남
객체지향에서는 가능 : 함수 오버로딩 이라고 함
DHT11.cpp
/*
* DH11.cpp
*
* Created on: 2012. 12. 12.
* Author: dalxx
*/
#include "DHT11.h"
#include <Arduino.h>
DHT11::DHT11(int pin_number) {
pin=pin_number;
this->last_read_time=0;
pinMode(pin,INPUT);
digitalWrite(pin, HIGH);
}
//wait for target status
//parameters
// target : target status waiting for
// time_out_us : time out in microsecond.
unsigned long DHT11::waitFor(uint8_t target, unsigned long time_out_us) {
unsigned long start=micros();
unsigned long time_out=start+time_out_us;
while(digitalRead(this->pin)!=target)
{
if(time_out<micros()) return -1;
}
return micros()-start;
}
//wait for target status.
void DHT11::waitFor(uint8_t target) {
while(digitalRead(this->pin)!=target);
}
//read one bye
byte DHT11::readByte() {
int i=0;
byte ret=0;
for(i=7;i>=0;i--)
{
waitFor(HIGH); //wait for 50us in LOW status
delayMicroseconds(30); //wait for 30us
if(digitalRead(this->pin)==HIGH) //if HIGH status lasts for 30us, the bit is 1;
{
ret|=1<<(i);
waitFor(LOW); //wait for rest time in HIGH status.
}
}
return ret;
}
DHT11::~DHT11() {
// TODO Auto-generated destructor stub
}
//parameters
// temperature : temperature to read.
// humidity : humidity to read.
//return -1 : read too shortly. retry latter .
// 0 : read successfully
// 1 : DHT11 not ready.
// 4 : Checksum Error
int DHT11::read(float& humidity, float& temperature) {
if((millis()-this->last_read_time<DHT11_RETRY_DELAY)&&this->last_read_time!=0) return -1;
pinMode(pin,OUTPUT);
digitalWrite(pin, LOW);
delay(18);
digitalWrite(pin, HIGH);
pinMode(pin,INPUT);
if(waitFor(LOW, 40)<0) return 1; //waiting for DH11 ready
if(waitFor(HIGH, 90)<0) return 1; //waiting for first LOW signal(80us)
if(waitFor(LOW, 90)<0) return 1; //waiting for first HIGH signal(80us)
byte hI=this->readByte();
byte hF=this->readByte();
byte tI=this->readByte();
byte tF=this->readByte();
byte cksum=this->readByte();
if(hI+hF+tI+tF!=cksum)
return 4;
humidity=(float)hI+(((float)hF)/100.0F);
temperature=(float)tI+(((float)tF)/100.0F);
this->last_read_time=millis();
return 0;
}
참고
'임베디드 > Atmega128' 카테고리의 다른 글
(안됨)atmega128 dht11 + usart 시리얼통신 합치기 (0) | 2021.02.05 |
---|---|
atmega128 시리얼통신 (0) | 2021.02.04 |
AVR 내부 flash memory 구조, atmega128 bootsz 퓨즈상태와 부트로더 섹션 크기 조정, AVR 부트로더 동작 (0) | 2021.01.29 |
analog (0) | 2021.01.29 |
atmega128 (timer/counter0 normal) + (timer/counter2 fast pwm) LED깜빡이기 (0) | 2021.01.29 |