#ifndef F_CPU #define F_CPU 16000000UL #endif #include #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) | (1U << RXCIE0); } void uart_tx(char c) { while (!(UCSR0A & (1U << UDRE0))) { } UDR0 = (uint8_t)c; } ISR(USART_RX_vect) { char c = (char)UDR0; if (c == '1') { PORTB |= (1U << PB5); } else if (c == '0') { PORTB &= (uint8_t)~(1U << PB5); } uart_tx(c); } static void uart_puts(const char *text) { while (*text) { uart_tx(*text++); } } int main(void) { cli(); PORTB &= (uint8_t)~(1U << PB5); DDRB |= (1U << PB5); uart_init(); uart_puts("ATmega328P ready\r\n"); sei(); while (1) { } }