util_setbaud(3avr)
π util_setbaud β bestie edition π
okay so this is util_setbaud from the 3avr manual, and it lives in <util/setbaud.h> β it's a little header of helper macros that do your baud rate math for you so you don't have to sit there with a calculator crying. it's basically the preprocessor doing your homework. periodt. β¨
πΈ NAME πΈ
util_setbaud β <util/setbaud.h>: helper macros for baud rate calculations π
β¨ SYNOPSIS β¨
π Macros
#define BAUD_TOL 2
#define UBRR_VALUE
#define UBRRL_VALUE
#define UBRRH_VALUE
#define USE_2X 0
Detailed Description
#define F_CPU 11059200
#define BAUD 38400
#include <util/setbaud.h>
so the tea is: this header file requires that on entry, values are already defined for F_CPU and BAUD. like, it needs those. no skipping.
on top of that, the macro BAUD_TOL defines the baud rate tolerance (in percent) that's acceptable during the calculations. and if you don't set it yourself? it defaults to 2 %. lowkey generous of it π«Ά
this header file defines macros suitable to set up the UART baud rate prescaler registers of an AVR. all the calculations happen using the C preprocessor β no runtime cost, no drama. including this header file causes no other side effects, so it's totally fine to include it more than once (supposedly with different values for the BAUD parameter), possibly even within the same function. iconic behavior honestly π
assuming that the requested BAUD is valid for the given F_CPU, then the macro UBRR_VALUE gets set to the required prescaler value. two additional macros are provided for the low and high bytes of the prescaler, respectively: UBRRL_VALUE is set to the lower byte of the UBRR_VALUE and UBRRH_VALUE is set to the upper byte. and an additional macro USE_2X will be defined β its value is set to 1 if the desired BAUD rate within the given tolerance could only be achieved by setting the U2X bit in the UART configuration, and it'll be defined to 0 if U2X is not needed. it's giving "i did the math so you don't have to" π
example usage, bestie, look at this gorgeous code:
#include <avr/io.h>
#define F_CPU 4000000
static void
uart_9600(void)
{
#define BAUD 9600
#include <util/setbaud.h>
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
#if USE_2X
UCSRA |= (1 << U2X);
#else
UCSRA &= ~(1 << U2X);
#endif
}
static void
uart_38400(void)
{
#undef BAUD // avoid compiler warning
#define BAUD 38400
#include <util/setbaud.h>
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
#if USE_2X
UCSRA |= (1 << U2X);
#else
UCSRA &= ~(1 << U2X);
#endif
}
in this example, two functions are defined to set up the UART to run at 9600 Bd, and 38400 Bd, respectively. using a CPU clock of 4 MHz, 9600 Bd can be achieved with an acceptable tolerance without setting U2X (prescaler 25), while 38400 Bd requires U2X to be set (prescaler 12). see? the vibes are immaculate π¦
π Macro Definition Documentation π
#define BAUD_TOL 2
input and output macro for <util/setbaud.h> π
this one defines the acceptable baud rate tolerance in percent. if it's not set on entry, it will be set to its default value of 2. that's the default, no cap.
#define UBRR_VALUE
output macro from <util/setbaud.h> π
contains the calculated baud rate prescaler value for the UBRR register. the main character of the prescaler situation.
#define UBRRH_VALUE
output macro from <util/setbaud.h> π
contains the upper byte of the calculated prescaler value (UBRR_VALUE).
#define UBRRL_VALUE
output macro from <util/setbaud.h> π
contains the lower byte of the calculated prescaler value (UBRR_VALUE).
#define USE_2X 0
output macro from <util/setbaud.h> π
contains the value 1 if the desired baud rate tolerance could only be achieved by setting the U2X bit in the UART configuration. contains 0 otherwise. it's a binary baddie, she only knows two moods π₯
π Author π
generated automatically by Doxygen for AVR-LibC from the source code. (not me personally, i just did the rewrite π )
aaaaand that's the whole page, bestie!! go set some baud rates, you're gonna slay πβ¨π¦
AVR-LibC β Version 2.2.1 β util_setbaud(3avr) π«ΆππΈ
π¦ where this page comes from
- ubuntu resolute: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- ubuntu stonking: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms