temperature LCD and DHT11
Code for temperature LCD and DHT11
/*
* temperature LCD and DHT11.c
*
* Created: 7/16/2020 7:41:41 PM
* Author : Daniel F Diaz Bayona
*/
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include "LCD16x2_4bit.h"
#define DHT11_PIN 8
uint8_t c=0,I_RH,D_RH,I_Temp,D_Temp,CheckSum;
//here we will set the pin to low and high
//first the pin will be low then after 200ms delay it will show or be high
void Request()
{
DDRD |= (1<<DHT11_PIN);
PORTD &= ~(1<<DHT11_PIN);
_delay_ms(200);
PORTD |= (1<<DHT11_PIN);
}
//the following function will show th response of the sensor DHT11
void Response()
{
DDRD &= ~(1<<DHT11_PIN);
while(PIND & (1<<DHT11_PIN));
while((PIND & (1<<DHT11_PIN))==0);
while(PIND & (1<<DHT11_PIN));
}
//this function will receive the data, check for received bit 0 or 1
//if the pulse is greater than 30ms
//then it will set the logic or the PIN to high or low
uint8_t Receive_data()
{
for (int q=0; q<8; q++)
{
while((PIND & (1<<DHT11_PIN)) == 0);
_delay_us(30);
if(PIND & (1<<DHT11_PIN))
c = (c<<1)|(0x01);
else
c = (c<<1);
while(PIND & (1<<DHT11_PIN));
}
return c;
}
int main(void)
{
char data[5];
//the following line will show the LCD initiating
lcdinit();
//the LCD will be clear
lcd_clear();
//here we will enter or set the cursor on the LCD
lcd_gotoxy(0,0);
lcd_print("Humidity =");
lcd_gotoxy(0,1);
lcd_print("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 */
//THIS IF STATEMENT WILL SHOW ON THE LCD SCREEN IF THE SENSOR IS NOT WPRKING
// The following message will show if the DATA THAT IS SUPOSED TO RECEVIE IS NOT CORRRECT
if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
lcd_gotoxy(0,0);
lcd_print("READ DHT11 FAILED");
}
//Here we will show the data of the sensor on the lcd screen
else
{
//we will delay reading the data for 2 second so th sensor has time to read the data
//after the data is readed it will sow on the screen
//if the data is not readed, then the screen will show a message for an error.
_delay_ms(2000);
itoa(I_RH,data,10);
lcd_gotoxy(11,0);
lcd_print(data);
lcd_print(".");
itoa(D_RH,data,10);
lcd_print(data);
lcd_print("%");
itoa(I_Temp,data,10);
lcd_gotoxy(6,1);
lcd_print(data);
lcd_print(".");
itoa(D_Temp,data,10);
lcd_print(data);
lcddata(0xDF);
lcd_print("C ");
itoa(CheckSum,data,10);
lcd_print(data);
lcd_print(" ");
}
_delay_ms(1000);
}
}
Comments
Post a Comment