Introduction
Baud rate configuration sets how fast data is sent or received over serial communication. It helps devices talk to each other at the same speed.
void UART_Init(unsigned int baudrate) { unsigned int ubrr_value = (F_CPU / (16UL * baudrate)) - 1; UBRR0H = (unsigned char)(ubrr_value >> 8); UBRR0L = (unsigned char)ubrr_value; UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable receiver and transmitter UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // Set frame format: 8 data bits, 1 stop bit }
UART_Init(9600);UART_Init(115200);#include <avr/io.h> #define F_CPU 16000000UL void UART_Init(unsigned int baudrate) { unsigned int ubrr_value = (F_CPU / (16UL * baudrate)) - 1; UBRR0H = (unsigned char)(ubrr_value >> 8); UBRR0L = (unsigned char)ubrr_value; UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable receiver and transmitter UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, 1 stop bit } int main(void) { UART_Init(9600); while (1) { // Main loop } return 0; }
unsigned int ubrr_value = (8000000 / (16 * 115200)) - 1; UBRR0H = (ubrr_value >> 8); UBRR0L = ubrr_value;
#define F_CPU 16000000 #define BAUD 9600 unsigned int ubrr = F_CPU / 16 / BAUD; UBRR0H = (ubrr >> 8); UBRR0L = ubrr;
Pass the test and this pattern moves unknown