c code:
#include <reg51.h> // Includes 8051 microcontroller definitions
// Function declarations
void delay(unsigned int time); // Generates a delay (approximately 1ms per iteration)
void lcd_cmd(unsigned char a); // Sends a command to the LCD
void lcd_data(unsigned char b); // Sends data (characters) to the LCD
void lcd_init(void); // Initializes the LCD
void lcd_str(unsigned char *str); // Displays a string on the LCD
void display_temperature(unsigned char temp); // Displays the temperature on the LCD
void start_dht11(void); // Initializes communication with the DHT11 sensor
unsigned char read_dht11_data(void); // Reads data from the DHT11 sensor
// Pin definitions
sbit rs = P2^0; // Register select pin for LCD
sbit en = P2^1; // Enable pin for LCD
sbit buz = P2^5; // Buzzer control pin
sbit dht11_pin = P2^2; // Data pin for DHT11 sensor (connected to P2^2)
sfr ldata = 0xB0; // Data port for LCD (Port 3)
// Variables
unsigned char temperature; // Variable to hold temperature value
// Main function
void main ()
{
lcd_init(); // Initialize LCD
buz = 0; // Ensure the buzzer is off at the start
// Display welcome message
lcd_str(" WELCOME ");
lcd_cmd(0xc0);
lcd_str(" TO PROJECT ");
delay(200);
lcd_cmd(0x01);
lcd_cmd(0x81); // Display project title
lcd_str(" TEMPERATURE ");
lcd_cmd(0xc2);
lcd_str(" ALERT ");
delay(200);
lcd_cmd(0x01);
lcd_cmd(0x80); // Display temperature label
lcd_str(" Temp =");
lcd_cmd(0x8b);
lcd_data((char)223); // Display degree symbol
lcd_str("C");
while (1)
{
// Read temperature from DHT11
start_dht11();
temperature = read_dht11_data();
// Display temperature
lcd_cmd(0x89);
display_temperature(temperature);
// Check if temperature exceeds threshold of 35°C
if (temperature > 35)
{
lcd_cmd(0xC0);
lcd_str("HIGH TEMP ALERT!");
buz = 1; // Turn buzzer on
}
else
{
lcd_cmd(0xC0);
lcd_str(" ");
buz = 0; // Turn buzzer off
}
delay(1000); // 1-second delay before the next reading
}
}
// Function to display temperature on LCD
void display_temperature(unsigned char temp)
{
unsigned char d1, d2;
d1 = temp / 10; // Tens digit
d2 = temp % 10; // Ones digit
lcd_data(d1 + 0x30); // Convert to ASCII and display tens digit
lcd_data(d2 + 0x30); // Convert to ASCII and display ones digit
}
// Function to initialize DHT11 communication
void start_dht11(void)
{
dht11_pin = 0; // Pull data pin low
delay(18); // Wait for at least 18ms
dht11_pin = 1; // Pull data pin high
delay(20); // Wait for DHT11 to respond
}
// Function to read 40 bits from DHT11 and extract temperature
unsigned char read_dht11_data(void)
{
unsigned char i, j, result = 0, dht11_data[5] = {0, 0, 0, 0, 0};
for (i = 0; i < 5; i++)
{
for (j = 0; j < 8; j++)
{
while (!dht11_pin); // Wait for DHT11 to pull data pin low
delay(30); // Wait for 30us
// If data pin is high, set the appropriate bit in the result
if (dht11_pin == 1)
dht11_data[i] |= (1 << (7 - j));
while (dht11_pin); // Wait for data pin to go low again
}
}
// The temperature integral value is stored in dht11_data[2]
return dht11_data[2]; // Return temperature byte
}
// LCD initialization function
void lcd_init()
{
lcd_cmd(0x38); // Initialize LCD for 8-bit mode
lcd_cmd(0x0C); // Display on, cursor off
lcd_cmd(0x01); // Clear display
lcd_cmd(0x80); // Set cursor to beginning of first line
}
// Updated delay function for 89S52 microcontroller
void delay(unsigned int time)
{
unsigned int i, j;
for (i = 0; i < time; i++)
{
for (j = 0; j < 1275; j++) // Approx 1ms delay for 89S52
{
// Do nothing, just waste time
}
}
}
// Function to send command to LCD
void lcd_cmd(unsigned char a)
{
rs = 0; // Set for command mode
ldata = a; // Load command to data port
en = 1; // Enable LCD
delay(2); // Short delay
en = 0; // Disable LCD
delay(2); // Short delay
}
// Function to send data (characters) to LCD
void lcd_data(unsigned char b)
{
rs = 1; // Set for data mode
ldata = b; // Load data to data port
en = 1; // Enable LCD
delay(2); // Short delay
en = 0; // Disable LCD
delay(2); // Short delay
}
// Function to display string on LCD
void lcd_str(unsigned char *str)
{
while (*str) // Loop until the end of the string
{
lcd_data(*str++); // Send each character to the LCD
}
}
Attachments
-
34.5 KB Views: 2
-
2.4 KB Views: 0
-
4.8 KB Views: 3