- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #include "uart.h"
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- uint8_t c=0, I_RH, D_RH, I_Temp, D_Temp, CheckSum;
- #define setbit(port, bit) ((port) |= (1<<bit));
- #define clearbit(port, bit) ((port) &= ~(1<<bit));
- #define togglebit(port, bit) ((port) ^= (1<<bit));
- #define STATUS_LED_PORT PORTC
- #define STATUS_LED_PIN PORTC3
- char porty[8]="00000000";
- #define LED_PIN (1<<PB5)
- #define LED_MIG PORTB ^= LED_PIN
- #define LED_OFF PORTB &= ~LED_PIN
- #define LED_ON PORTB |= LED_PIN
- #define GUZIK_PIN (1<<PB7)
- #define GUZIK2_PIN (1<<PD0)
- #define GUZIK3_PIN (1<<PD2)
- #define WEN_PIN (1<<PD4)
- #define WEN_ON PORTD |= WEN_PIN
- #define WEN_OFF PORTD &= ~WEN_PIN
- #define HEA_PIN (1<<PD6)
- #define HEA_ON PORTD |= HEA_PIN
- #define HEA_OFF PORTD &= ~HEA_PIN
- #define DHT11_PIN PIND7
- 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 */
- {
- for (int 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];
- UartInit();
- DDRB |=LED_PIN;
- DDRB &= ~GUZIK_PIN;
- PORTB |= GUZIK_PIN;
- DDRD &= ~GUZIK2_PIN;
- PORTD |= GUZIK2_PIN;
- DDRD &= ~GUZIK3_PIN;
- PORTD |= GUZIK3_PIN;
- DDRD |= WEN_PIN;
- while (1)
- {
- porty[0]='1';
- UartTransmitString("Rozpoczecie pomiaru: \r\n");
- 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 */
- _delay_ms(200);
- _delay_ms(1800);
- if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
- {
- //LED_OFF;
- UartTransmitString("Blad pomiaru!!! \r\n");
- }
- else {
- UartTransmitString("Temperatura powietrza wynosi: ");
- _delay_ms(500);
- UartTransmitInt(I_Temp);
- UartTransmitString(".");
- UartTransmitInt(D_Temp);
- UartTransmitString("C \r\n");
- //UartTransmitString("\r\n");
- _delay_ms(1000);
- //int temperatura = I_Temp;
- //if(temperatura >= 22)
- //{
- // LED_ON;
- //}
- // else
- //{
- // LED_OFF;
- //}
- if( !(PIND & GUZIK2_PIN) )
- {
- LED_ON;
- WEN_OFF;
- //HEA_ON;
- }
- if( !(PIND & GUZIK3_PIN) )
- {
- LED_OFF;
- WEN_ON;
- //HEA_OFF;
- }
- }
- }
- ISR(USART_RX_vect)
- {
- RX_data=UDR0;
- }