Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fixed this code to show the correct persantage of water in solil of 5 V ref voltage #include #include #define F _ CPU 8 0

Fixed this code to show the correct persantage of water in solil of 5V ref voltage
#include
#include
#define F_CPU 8000000UL
#define BAUD 2400
#define MYUBRR F_CPU/16/BAUD-1
void USART_Init(unsigned int ubrr){
UBRRH =(unsigned char)(ubrr >>8);
UBRRL =(unsigned char)ubrr;
UCSRB =(1<< RXEN)|(1<< TXEN);
UCSRC =(1<< URSEL)|(1<< UCSZ1)|(1<< UCSZ0);
}
void USART_Transmit(unsigned char data){
while (!(UCSRA & (1<< UDRE)));
UDR = data;
}
void ADC_Init(){
ADCSRA |=(1<< ADEN)|(1<< ADPS2)|(1<< ADPS1)|(1<< ADPS0);
}
uint16_t ADC_Read(uint8_t adc_channel){
ADMUX =(1<< REFS0)| adc_channel;
ADCSRA |=(1<< ADSC);
while (ADCSRA & (1<< ADSC));
return ADC;
}
void LED_Init(){
DDRD |=(1<< PD2)|(1<< PD3)|(1<< PD4)|(1<< PD5); // Set PD2, PD3, PD4, PD5 as output
}
void LED_Control(uint16_t moisture_percentage){
if (moisture_percentage <20){
PORTD =(1<< PD2); // LED 1(connected to PD2) ON
} else if (moisture_percentage <40){
PORTD =(1<< PD3); // LED 2(connected to PD3) ON
} else if (moisture_percentage <60){
PORTD =(1<< PD4); // LED 3(connected to PD4) ON
} else {
PORTD =(1<< PD5); // LED 4(connected to PD5) ON
}
}
int main(void){
USART_Init(MYUBRR);
ADC_Init();
LED_Init();
uint16_t adc_value;
uint16_t moisture_percentage;
while (1){
adc_value = ADC_Read(0); // Assuming the soil sensor is connected to PC0
moisture_percentage =((adc_value *100)/255); // Scale to 100% moisture for 5V input
USART_Transmit((moisture_percentage /100)+'0');
USART_Transmit(((moisture_percentage /10)%10)+'0');
USART_Transmit((moisture_percentage %10)+'0');
USART_Transmit('%');
USART_Transmit('
');
LED_Control(moisture_percentage); // Control LEDs based on moisture percentage
_delay_ms(500); // Delay for stability
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions