/* * DHT22.c * * Created: 5/10/2016 5:15:19 am * Author: Dimitrios Porlidas */ #include "LCD_keyb_ROT.h" uint16_t hum; // Humidity register uint16_t tem; // Temperature register uint8_t cdig; // Control digit register uint8_t i4; uint8_t i5; char hum_reg[6]; // Humidity display register char tem_reg[6]; // Temperature display register uint8_t err[] = {'E','R','R','O','R',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; void timer1_init() // Initialize interrupts { TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode TCCR1B |= (5 << CS10); // Start timer at Fcpu/1024 (1:/1, 2:/8, 3:/64, 4:/256, 5:/1024) TIMSK |= (1 << OCIE1A); // Enable CTC interrupt OCR1A = 15624; // Set CTC compare value to 2 seconds at 8MHz AVR clock, with a prescaler of 1024 sei(); // Enable global interrupts } ISR(TIMER1_COMPA_vect) // Send "Error" message to display if DHT22 does not respond after 2 seconds { SeCu (L2R1); _delay_ms(5); for (uint8_t i3 = 0; i3 < 16; i3++) WrDa(err[i3]); } void GetDHT(void) { timer1_init(); _delay_ms(1000); while (rot1bin == 4) { _delay_ms (500); sensddr |= (1<<0); // Init DHT Port as input sensport = 1< 100) { SeCu (L2R1); _delay_ms(5); for (uint8_t i3 = 0; i3 < 16; i3++) WrDa(err[i3]); break; } } } // else // Read 0 (no need for this routine because bit is 0 due to shift function) // hum &= ~(1<<0); } tem = 0; for (uint8_t i1 = 0; i1 < 16; i1++) // Start reading temperature 16-bit register { while (!(bit_is_set(sensreg,0))); // Wait for 1 _delay_us(40); tem = (tem<<1); if (bit_is_set(sensreg,0)) // Read 1 { tem |= (1<<0); // while (bit_is_set(sensreg,0)); // Wait for 0 i5=0; while (bit_is_set(sensreg,0)) { i5++; _delay_us(5); if (i5 > 100) { SeCu (L2R1); _delay_ms(5); for (uint8_t i3 = 0; i3 < 16; i3++) WrDa(err[i3]); break; } } } // else // Read 0 (no need for this routine because bit is 0 due to shift function) // tem &= ~(1<<0); } for (uint8_t i1 = 0; i1 < 8; i1++) // Start reading control digit 8-bit register { while (!(bit_is_set(sensreg,0))); // wait for 1 _delay_us(40); cdig = (cdig<<1); if (bit_is_set(sensreg,0)) // Read 1 { cdig |= (1<<0); // while (bit_is_set(sensreg,0)); // Wait for 0 i5=0; while (bit_is_set(sensreg,0)) { i5++; _delay_us(5); if (i5 > 100) { SeCu (L2R1); _delay_ms(5); for (uint8_t i3 = 0; i3 < 16; i3++) WrDa(err[i3]); break; } } } // else // cdig &= ~(1<<0); // Read 0 (no need for this routine because bit is 0 due to shift function) } SeCu (L2R1); _delay_ms (5); WrDa('H'); WrDa('='); itoa (hum, hum_reg, 10); // integer to ASCII function // SeCu (L1R3); _delay_ms (5); // Send cursor to a specific address 0-80 (LCD) for (i4 = 0; i4 < ((unsigned)strlen(hum_reg)-1); i4++) // Send ASCII string to LCD, but not null characters, except the last digit (decimal) WrDa (hum_reg[i4]); WrDa('.'); // Send . to LCD for decimal digit WrDa (hum_reg[i4]); // Send the last digit (decimal) to LCD WrDa('%'); // Send % symbol for (i4 = (unsigned)strlen(hum_reg); i4 < 6; i4++) // Complete with blanks WrDa(' '); SeCu (L2R9); _delay_ms (5); WrDa('T'); WrDa('='); itoa (tem, tem_reg, 10); // integer to ASCII function // SeCu (L2R6); _delay_ms (5); // Send cursor to a specific address 0-80 (LCD) for (i4 = 0; i4 < ((unsigned)strlen(tem_reg)-1); i4++) // Send ASCII string to LCD, but not null characters, except the last digit (decimal) WrDa (tem_reg[i4]); WrDa('.'); // Send . to LCD for decimal digit WrDa (tem_reg[i4]); // Send the last digit (decimal) to LCD WrDa(223); // Send degree symbol WrDa('C'); // Write C at the end of number (for Celcius) for (i4 = (unsigned)strlen(tem_reg); i4 < 5; i4++) // Complete with blanks WrDa(' '); TCNT1 = 0; // Reset TCNT1 } }