-
Notifications
You must be signed in to change notification settings - Fork 0
/
For Atmega.c
98 lines (83 loc) · 2.34 KB
/
For Atmega.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd_io.h"
#include "util/delay.h"
#define DHT11_PIN 6
uint8_t c=0,I_RH,D_RH,I_Temp,D_Temp,CheckSum;
void Request() /* Microcontroller send start pulse or request */
{
DDRD |= (1<<DHT11_PIN);
PORTD &= ~(1<<DHT11_PIN); /* set to low pin */
_delay_ms(20); /* wait for 20ms */
PORTD |= (1<<DHT11_PIN); /* set to high pin */
}
void Response() /* receive response from DHT11 */
{
DDRD &= ~(1<<DHT11_PIN);
while(PIND & (1<<DHT11_PIN));
while((PIND & (1<<DHT11_PIN))==0);
while(PIND & (1<<DHT11_PIN));
}
uint8_t Receive_data() /* receive data */
{
int q;
for ( q=0; q<8; q++)
{
while((PIND & (1<<DHT11_PIN)) == 0); /* check received bit 0 or 1 */
_delay_us(30);
if(PIND & (1<<DHT11_PIN)) /* if high pulse is greater than 30ms */
c = (c<<1)|(0x01); /* then its logic HIGH */
else /* otherwise its logic LOW */
c = (c<<1);
while(PIND & (1<<DHT11_PIN));
}
return c;
}
int main(void)
{
char data[5];
lcd_init(); /* initialize LCD */
lcd_clrscr(); /* clear LCD */
lcd_gotoxy(0,0); /* enter column and row position */
lcd_puts("Humidity:");
//lcd_gotoxy(1,0);
//lcd_puts("Temp = ");
while(1)
{
Request(); /* send start pulse */
Response(); /* receive response */
I_RH=Receive_data(); /* store first eight bit in I_RH */
D_RH=Receive_data(); /* store next eight bit in D_RH */
I_Temp=Receive_data(); /* store next eight bit in I_Temp */
D_Temp=Receive_data(); /* store next eight bit in D_Temp */
CheckSum=Receive_data(); /* store next eight bit in CheckSum */
if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
lcd_gotoxy(0,0);
lcd_puts("Error");
}
else
{
itoa(I_RH,data,10);
lcd_gotoxy(11,0);
lcd_puts(data);
lcd_puts(".");
itoa(D_RH,data,10);
lcd_puts(data);
//lcd_puts("\%");
//itoa(I_Temp,data,10);
//lcd_gotoxy(8,1);
//lcd_puts(data);
//lcd_putc('.');
//itoa(D_Temp,data,10);
//lcd_puts(data);
//lcd_putc(0xDF);
//lcd_putc('C');
//itoa(CheckSum,data,10);
//lcd_puts(data);
//lcd_putc(' ');
}
_delay_ms(10);
}
}