0% found this document useful (0 votes)
15 views

btvn4 (1)

Uploaded by

Vương Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

btvn4 (1)

Uploaded by

Vương Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <avr/io.

h>
#include <util/delay.h>
#include <stdlib.h> // For converting numbers to strings

#define F_CPU 16000000UL // Define CPU frequency for delay calculations

// LCD Port and Pin Configuration


#define LCD_PORT PORTD
#define LCD_DDR DDRD
#define RS PD0
#define EN PD1

void ADC_Init();
uint16_t ADC_Read(uint8_t channel);
void LCD_Command(unsigned char cmd);
void LCD_Char(unsigned char data);
void LCD_Init();
void LCD_String(char *str);

int main(void) {
uint16_t adc_value;
float temperature;
char temp_str[10];

// Initialize LCD and ADC


LCD_DDR = 0xFF; // Configure all pins of PORTD as output
ADC_Init();
LCD_Init();

// Configure PB0 (LED pin) as output


DDRB |= (1 << PB0); // Set PB0 as output
while (1) {
adc_value = ADC_Read(0); // Read ADC value from channel 0 (LM35 connected)
temperature = (adc_value * 5.0) / 1024.0 * 100.0; // Convert ADC value to temperature
in Celsius

// Display temperature on LCD


LCD_Command(0x80); // Move cursor to the first line
LCD_String("Temp: ");
dtostrf(temperature, 5, 2, temp_str); // Convert float to string
LCD_String(temp_str);
LCD_String(" C");

// Control LED based on temperature


if (temperature > 40.0) {
PORTB |= (1 << PB0); // Turn LED ON
} else {
PORTB &= ~(1 << PB0); // Turn LED OFF
}

_delay_ms(500); // Refresh every 500ms


}
}

void ADC_Init() {
ADMUX = (1 << REFS0); // Reference voltage = AVcc
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // Enable ADC, prescaler = 64
}

uint16_t ADC_Read(uint8_t channel) {


channel &= 0x07; // Ensure the channel is between 0 and 7
ADMUX = (ADMUX & 0xF8) | channel; // Select ADC channel
ADCSRA |= (1 << ADSC); // Start ADC conversion
while (ADCSRA & (1 << ADSC)); // Wait for conversion to finish
return ADC; // Return ADC value
}

void LCD_Command(unsigned char cmd) {


LCD_PORT = (LCD_PORT & 0x0F) | (cmd & 0xF0); // Send higher nibble
LCD_PORT &= ~(1 << RS); // RS = 0 (command)
LCD_PORT |= (1 << EN); // Enable pulse
_delay_us(1);
LCD_PORT &= ~(1 << EN);

_delay_us(200);

LCD_PORT = (LCD_PORT & 0x0F) | (cmd << 4); // Send lower nibble
LCD_PORT |= (1 << EN); // Enable pulse
_delay_us(1);
LCD_PORT &= ~(1 << EN);
_delay_ms(2);
}

void LCD_Char(unsigned char data) {


LCD_PORT = (LCD_PORT & 0x0F) | (data & 0xF0); // Send higher nibble
LCD_PORT |= (1 << RS); // RS = 1 (data)
LCD_PORT |= (1 << EN); // Enable pulse
_delay_us(1);
LCD_PORT &= ~(1 << EN);

_delay_us(200);

LCD_PORT = (LCD_PORT & 0x0F) | (data << 4); // Send lower nibble
LCD_PORT |= (1 << EN); // Enable pulse
_delay_us(1);
LCD_PORT &= ~(1 << EN);
_delay_ms(2);
}

void LCD_Init() {
LCD_Command(0x02); // Initialize LCD in 4-bit mode
LCD_Command(0x28); // 2 lines, 5x8 matrix
LCD_Command(0x0C); // Display on, cursor off
LCD_Command(0x06); // Increment cursor
LCD_Command(0x01); // Clear display
_delay_ms(2);
}

void LCD_String(char *str) {


while (*str) {
LCD_Char(*str);
str++;
}
}

You might also like