#define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> #include <stdlib.h> 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; } unsigned char USART_Receive(void) { while (!(UCSRA & (1 << RXC))); return UDR; } void USART_SendString(const char* s) { while (*s) USART_Transmit(*s++); } void ADC_Init(void) { ADMUX = (1 << REFS0); ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); } uint16_t ADC_Read(void) { ADCSRA |= (1 << ADSC); while (ADCSRA & (1 << ADSC)); return ADC; } int main(void) { char buffer[20]; uint16_t adc_value; uint32_t millivolts; USART_Init(51); ADC_Init(); while (1) { if (USART_Receive() == '*') { adc_value = ADC_Read(); millivolts = (adc_value * 5000UL) / 1024; itoa(millivolts, buffer, 10); USART_SendString("Voltage="); USART_SendString(buffer); USART_SendString("mV\r\n"); } } }
Standard input is empty
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
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;
}
unsigned char USART_Receive(void) {
while (!(UCSRA & (1 << RXC)));
return UDR;
}
void USART_SendString(const char* s) {
while (*s) USART_Transmit(*s++);
}
void ADC_Init(void) {
ADMUX = (1 << REFS0);
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}
uint16_t ADC_Read(void) {
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
return ADC;
}
int main(void) {
char buffer[20];
uint16_t adc_value;
uint32_t millivolts;
USART_Init(51);
ADC_Init();
while (1) {
if (USART_Receive() == '*') {
adc_value = ADC_Read();
millivolts = (adc_value * 5000UL) / 1024;
itoa(millivolts, buffer, 10);
USART_SendString("Voltage=");
USART_SendString(buffer);
USART_SendString("mV\r\n");
}
}
}