#ifndef F_CPU #define F_CPU 16000000UL #endif #include #include #define UART_BAUD 9600UL #define UART_UBRR (F_CPU / (16UL * UART_BAUD) - 1UL) void uart_init(void) { UCSR0B = 0; UCSR0A = 0; UBRR0H = (uint8_t)(UART_UBRR >> 8); UBRR0L = (uint8_t)UART_UBRR; UCSR0C = (1U << UCSZ01) | (1U << UCSZ00); UCSR0B = (1U << RXEN0) | (1U << TXEN0); } void uart_tx(char c) { while (!(UCSR0A & (1U << UDRE0))) { } UDR0 = (uint8_t)c; } char uart_rx(void) { while (!(UCSR0A & (1U << RXC0))) { } return (char)UDR0; } static void uart_puts(const char *text) { while (*text) { uart_tx(*text++); } } int main(void) { uart_init(); uart_puts("ATmega328P ready\r\n"); while (1) { uart_tx(uart_rx()); } }