아래에 첨부파일 있음. 폴더 안에 다 들어가 있다
main.c
/*
* dht11_0205_final.c
*
* Created: 2021-02-05 오후 2:37:53
* Author : User
*/
#define F_CPU 16000000UL
#include <stdio.h>
#include <string.h>
#include <avr/io.h>
#include <util/delay.h>
#include "dht11.h"
#include "uart.h"
int main(){
uint8_t data = 0;
char buf[40] = {0,};
USART_Init();
while (1){
dht11_getdata(0, &data);
sprintf(buf, "Temp:%d", data);
strTransmit(buf);
memset(buf, 0x00, 40);
_delay_ms(1000);
dht11_getdata(1, &data);
sprintf(buf, "//Humi:%d", data);
strTransmit(buf);
memset(buf, 0x00, 40);
_delay_ms(1000);
}
}
dht11.h
/*
* dht11.h
*
* Created: 2021-02-05 오후 2:53:57
* Author: User
*/
#ifndef DHT11_H_
#define DHT11_H_
#define DHT11_ERROR 255
#define DHT11_DDR DDRF
#define DHT11_PORT PORTF
#define DHT11_PIN PINF
#define DHT11_INPUTPIN PF1
void dht11_getdata(uint8_t num, uint8_t *data);
uint8_t getdata(uint8_t select);
#endif /* DHT11_H_ */
dht11.c
/*
* dht11.c
*
* Created: 2021-02-05 오후 3:01:48
* Author: User
*/
#define F_CPU 16000000UL
#include <stdio.h>
#include <string.h>
#include <avr/io.h>
#include <util/delay.h>
#include "dht11.h"
/* get data from dht11 */
uint8_t getdata(uint8_t select) {
uint8_t bits[5];
uint8_t i,j = 0;
memset(bits, 0, sizeof(bits));
//reset port
DHT11_DDR |= (1<<DHT11_INPUTPIN); //output
DHT11_PORT |= (1<<DHT11_INPUTPIN); //high
_delay_ms(100);
//send request
DHT11_PORT &= ~(1<<DHT11_INPUTPIN); //low
_delay_ms(18);
//-- MCU pulls up voltage and waits for DHT response (20-40us)
DHT11_PORT |= (1<<DHT11_INPUTPIN); //high
_delay_us(1);
DHT11_DDR &= ~(1<<DHT11_INPUTPIN); //input
_delay_us(39);
//--
//check start condition 1 (low)
if((DHT11_PIN & (1<<DHT11_INPUTPIN))) {
return DHT11_ERROR;
}
_delay_us(80);
//check start condition 2 (high)
if(!(DHT11_PIN & (1<<DHT11_INPUTPIN))) {
return DHT11_ERROR;
}
_delay_us(80);
//read the data
for (j=0; j<5; j++) { //read 5 byte
uint8_t result=0;
for(i=0; i<8; i++) {//read every bit
while(!(DHT11_PIN & (1<<DHT11_INPUTPIN))); //wait for an high input
_delay_us(30);
if(DHT11_PIN & (1<<DHT11_INPUTPIN)) //if input is high after 30 us, get result
result |= (1<<(7-i));
while(DHT11_PIN & (1<<DHT11_INPUTPIN)); //wait until input get low
}
bits[j] = result;
}
//reset port
DHT11_DDR |= (1<<DHT11_INPUTPIN); //output
DHT11_PORT |= (1<<DHT11_INPUTPIN); //low
_delay_ms(100);
//check checksum
if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
if (select == 0) { //return temperature
return(bits[2]);
} else if(select == 1){ //return humidity
return(bits[0]);
}
}
return DHT11_ERROR;
}
void dht11_getdata(uint8_t num, uint8_t *data){
uint8_t buf = getdata(num);
if(buf == DHT11_ERROR){
;
}
else{
*data = buf;
}
}
uart.h
/*
* uart.h
*
* Created: 2021-02-05 오전 10:23:17
* Author: User
*/
#ifndef UART_H_
#define UART_H_
void USART_Init(void);
unsigned char USART_Receive(void);
void USART_Transmit(unsigned char data);
void strTransmit(char *str);
#endif /* UART_H_ */
uart.c
/*
* uart.c
*
* Created: 2021-02-05 오전 10:33:46
* Author: User
*/
#include <avr/io.h>
#include "uart.h"
void USART_Init(void)
{
UCSR0A=0x00; // clear
UCSR0B=(1<<RXEN0) | (1<<TXEN0); // Rx, Tx Enable
UCSR0C=(0<<UCSZ02) | (1<<UCSZ01) | (1<<UCSZ00); // Tx data len : 8-bit
UBRR0H=0;
UBRR0L=103; // baudrate 9600bps
}
unsigned char USART_Receive(void)
{
while(!(UCSR0A & (1<<RXC0))); // Wait for data to be received
return UDR0; // Get and return received data form buffer
}
void USART_Transmit(unsigned char data)
{
while(!(UCSR0A & (1<<UDRE))); // Wait for empty transmit buffer
UDR0=data; // Put data into buffer, sends the data
}
void strTransmit(char *str) {
while (*str) {
USART_Transmit(*str);
str++;
}
USART_Transmit(10); // 개행문자
}
https://jdselectron.tistory.com/69
dht11은 위의 블로그 참고함
'임베디드 > Atmega128' 카테고리의 다른 글
millis() 와 micros() atmega128 에서 사용하기 (0) | 2021.02.05 |
---|---|
(안됨)atmega128 dht11 + usart 시리얼통신 합치기 (0) | 2021.02.05 |
atmega128 시리얼통신 (0) | 2021.02.04 |
atmega128 dht11 코드 + 코드 작성 위한 아두이노 라이브러리 찾기 (0) | 2021.02.04 |
AVR 내부 flash memory 구조, atmega128 bootsz 퓨즈상태와 부트로더 섹션 크기 조정, AVR 부트로더 동작 (0) | 2021.01.29 |