AVR C UART Echo
Configure an ATmega UART using avr-gcc registers.
Project files
Starter — main.c
#include <avr/io.h>
int main(void){ /* TODO UART init */ while(1){} }Completed — main.c
#include <avr/io.h>
#define F_CPU 16000000UL
void uart_init(void){ UBRR0L=103; UCSR0B=(1<<RXEN0)|(1<<TXEN0); UCSR0C=(1<<UCSZ01)|(1<<UCSZ00); }
int main(void){ uart_init(); while(1){ while(!(UCSR0A&(1<<RXC0))); char c=UDR0; while(!(UCSR0A&(1<<UDRE0))); UDR0=c; }}Toolchain and dependencies
IDE requirements
avr-gcc toolchain, Microchip Studio or PlatformIO
Compatible targets
ATmega328P at 16 MHz
Libraries/dependencies
- avr-libc
Serial output examples
Serial Monitor
Type text; each byte is echoed.
Compiler-error explanations
avr/io.h: No such file
Likely cause: AVR toolchain is not installed/selected
Fix: Install avr-gcc/avr-libc and choose the correct MCU
F_CPU not defined
Likely cause: Clock-dependent library cannot calculate timing
Fix: Define F_CPU before delay/UART calculations
Submit your code
Log in to edit the starter files and submit your solution for instructor review.
Log in to submit