#define F_CPU 8000000UL /* Define CPU Frequency e.g. here 8MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
// LCD bağlantı pinleri
#define LCD_Data_Dir DDRB /* Define LCD data port direction */
#define LCD_Command_Dir DDRC /* Define LCD command port direction register */
#define LCD_Data_Port PORTB /* Define LCD data port */
#define LCD_Command_Port PORTC /* Define LCD data port */
#define RS PC0 /* Define Register Select (data/command reg.)pin */
#define RW PC1 /* Define Read/Write signal pin */
#define EN PC2 /* Define Enable signal pin */
#define DHT22_PIN PD6
// Load cell kalibrasyon faktörleri
float calibration_factor1 = 1.0; // Load cell 1 için kalibrasyon faktörü
float calibration_factor2 = 1.0; // Load cell 2 için kalibrasyon faktörü
void dht22_request() {
DDRD |= (1 << DHT22_PIN); // Çıkış modu
PORTD &= ~(1 << DHT22_PIN); // Düşük sinyal gönder
_delay_ms(20); // 20ms bekle
PORTD |= (1 << DHT22_PIN); // Yüksek sinyal gönder
_delay_us(40); // 40us bekle
DDRD &= ~(1 << DHT22_PIN); // Giriş modu
}
uint8_t dht22_response() {
uint8_t response = 0;
DDRD &= ~(1 << DHT22_PIN); // Giriş modu
_delay_us(40);
if (!(PIND & (1 << DHT22_PIN))) {
_delay_us(80);
if ((PIND & (1 << DHT22_PIN))) {
response = 1;
}
_delay_us(80);
}
return response;
}
uint8_t dht22_receive_data() {
uint8_t data = 0;
for (int i = 0; i < 8; i++) {
while (!(PIND & (1 << DHT22_PIN))); // Yüksek seviye bekleme
_delay_us(30);
if (PIND & (1 << DHT22_PIN)) {
data = (data << 1) | 1; // 1 oku
} else {
data = (data << 1); // 0 oku
}
while (PIND & (1 << DHT22_PIN)); // Yüksek seviye bekleme
}
return data;
}
uint8_t read_dht22_humidity(uint8_t pin) {
uint8_t humidity_high, humidity_low, checksum;
dht22_request();
if (dht22_response()) {
humidity_high = dht22_receive_data();
humidity_low = dht22_receive_data();
checksum = dht22_receive_data();
// Checksum kontrolü
if ((humidity_high + humidity_low) == checksum) {
return humidity_high;
} else {
return 0; // Hata durumunda 0 dön
}
} else {
return 0; // Sensör yanıt vermediğinde 0 dön
}
}
uint8_t read_dht22_temperature(uint8_t pin) {
uint8_t temp_high, temp_low, checksum;
dht22_request();
if (dht22_response()) {
temp_high = dht22_receive_data();
temp_low = dht22_receive_data();
checksum = dht22_receive_data();
// Checksum kontrolü
if ((temp_high + temp_low) == checksum) {
return temp_high;
} else {
return 0; // Hata durumunda 0 dön
}
} else {
return 0; // Sensör yanıt vermediğinde 0 dön
}
}
void LCD_Command(unsigned char cmnd) {
LCD_Data_Port = cmnd;
LCD_Command_Port &= ~(1 << RS); /* RS=0 command reg. */
LCD_Command_Port &= ~(1 << RW); /* RW=0 Write operation */
LCD_Command_Port |= (1 << EN); /* Enable pulse */
_delay_us(1);
LCD_Command_Port &= ~(1 << EN);
_delay_ms(3);
}
void LCD_Char(unsigned char char_data) {
LCD_Data_Port = char_data;
LCD_Command_Port |= (1 << RS); /* RS=1 Data reg. */
LCD_Command_Port &= ~(1 << RW); /* RW=0 write operation */
LCD_Command_Port |= (1 << EN); /* Enable Pulse */
_delay_us(1);
LCD_Command_Port &= ~(1 << EN);
_delay_ms(1);
}
void LCD_Init(void) {
LCD_Command_Dir = 0xFF; /* Make LCD command port direction as o/p */
LCD_Data_Dir = 0xFF; /* Make LCD data port direction as o/p */
_delay_ms(20); /* LCD Power ON delay always >15ms */
LCD_Command(0x38); /* Initialization of 16X2 LCD in 8bit mode */
LCD_Command(0x0C); /* Display ON Cursor OFF */
LCD_Command(0x06); /* Auto Increment cursor */
LCD_Command(0x01); /* Clear display */
LCD_Command(0x80); /* Cursor at home position */
}
void LCD_String(char *str) {
int i;
for (i = 0; str[i] != 0; i++) { /* Send each char of string till the NULL */
LCD_Char(str[i]);
}
}
void LCD_String_xy(char row, char pos, char *str) {
if (row == 0 && pos < 16) {
LCD_Command((pos & 0x0F) | 0x80);
} else if (row == 1 && pos < 16) {
LCD_Command((pos & 0x0F) | 0xC0);
}
LCD_String(str);
}
void LCD_Clear() {
LCD_Command(0x01);
LCD_Command(0x80);
}
/*
int main() {
LCD_Init();
while (1) {
// DHT22'den sıcaklık ve nem değerlerini okuma
int h = read_dht22_humidity(6);
int t = read_dht22_temperature(6);
// Load cell'den ağırlık değerlerini okuma
//int water_weight = hx711_get_units(2, 3, calibration_factor1);
//int food_weight = hx711_get_units(4, 5, calibration_factor2);
// Verileri LCD'de gösterme
char buffer[16];
// Sıcaklık ve nem değerlerini LCD'de gösterme
LCD_Clear();
snprintf(buffer, sizeof(buffer), "Nem: %d %%", h);
LCD_String_xy(0, 0, buffer);
snprintf(buffer, sizeof(buffer), "Sicaklik: %d C", t);
LCD_String_xy(1, 0, buffer);
_delay_ms(2000);
LCD_Clear();
// Load cell'den su ve mama miktarlarını LCD'de gösterme
//snprintf(buffer, sizeof(buffer), "Su: %d g", water_weight);
//LCD_String_xy(0, 0, buffer);
//snprintf(buffer, sizeof(buffer), "Mama: %d g", food_weight);
//LCD_String_xy(1, 0, buffer);
// _delay_ms(2000);
//LCD_Clear();
}
return 0;
}
*/
void setup() {
LCD_Init();
}
void loop() {
char buffer[80];
uint8_t humidity_high, humidity_low, temp_high, temp_low, checksum;
dht22_request();
if (dht22_response()) {
humidity_high = dht22_receive_data();
humidity_low = dht22_receive_data();
temp_high = dht22_receive_data();
temp_low = dht22_receive_data();
checksum = dht22_receive_data();
// Debug için okunan değerleri UART üzerinden yazdıralım
snprintf(buffer, sizeof(buffer), "Humidity High: %d, Low: %d\r\n", humidity_high, humidity_low);
LCD_String(buffer);
snprintf(buffer, sizeof(buffer), "Temp High: %d, Low: %d\r\n", temp_high, temp_low);
LCD_String(buffer);
snprintf(buffer, sizeof(buffer), "Checksum: %d\r\n", checksum);
LCD_String(buffer);
int humidity = (humidity_high << 8) | humidity_low;
int temperature = (temp_high << 8) | temp_low;
// Checksum kontrolü
if ((humidity_high + humidity_low + temp_high + temp_low) == checksum) {
snprintf(buffer, sizeof(buffer), "Humidity: %d.%d%%, Temperature: %d.%dC\r\n", humidity / 10, humidity % 10, temperature / 10, temperature % 10);
LCD_String(buffer);
} else {
LCD_String("Checksum error\r\n");
}
} else {
LCD_String("DHT22 not responding\r\n");
}
_delay_ms(2000);
}