fork download
  1. #define F_CPU 8000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdlib.h>
  5.  
  6. void USART_Init(unsigned int ubrr) {
  7. UBRRH = (unsigned char)(ubrr >> 8);
  8. UBRRL = (unsigned char)ubrr;
  9. UCSRB = (1 << RXEN) | (1 << TXEN);
  10. UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0);
  11. }
  12.  
  13. void USART_Transmit(unsigned char data) {
  14. while (!(UCSRA & (1 << UDRE)));
  15. UDR = data;
  16. }
  17.  
  18. unsigned char USART_Receive(void) {
  19. while (!(UCSRA & (1 << RXC)));
  20. return UDR;
  21. }
  22.  
  23. void USART_SendString(const char* s) {
  24. while (*s) USART_Transmit(*s++);
  25. }
  26.  
  27. void ADC_Init(void) {
  28. ADMUX = (1 << REFS0);
  29. ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  30. }
  31.  
  32. uint16_t ADC_Read(void) {
  33. ADCSRA |= (1 << ADSC);
  34. while (ADCSRA & (1 << ADSC));
  35. return ADC;
  36. }
  37.  
  38. int main(void) {
  39. char buffer[20];
  40. uint16_t adc_value;
  41. uint32_t millivolts;
  42.  
  43. USART_Init(51);
  44. ADC_Init();
  45.  
  46. while (1) {
  47. if (USART_Receive() == '*') {
  48. adc_value = ADC_Read();
  49. millivolts = (adc_value * 5000UL) / 1024;
  50. itoa(millivolts, buffer, 10);
  51. USART_SendString("Voltage=");
  52. USART_SendString(buffer);
  53. USART_SendString("mV\r\n");
  54. }
  55. }
  56. }
Success #stdin #stdout 0.04s 25964KB
stdin
Standard input is empty
stdout
#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");
        }
    }
}