util_setbaud(3)

πŸ“¦ version 2.2.1, in fedora 43, fedora 44, opensuse-leap 16.0

πŸ’– util_setbaud πŸ’–

okay bestie, welcome to util_setbaud from the avr-libc manual (that's section 3, the library-calls girlies). this is the header that does your UART baud rate math at preprocessor time and honestly? iconic. no runtime cost, no cap. πŸ’…


✨ NAME ✨

util_setbaud β€” the header that precomputes your AVR UART baud rate prescaler for you πŸŽ€


🌸 SYNOPSIS 🌸

πŸ¦‹ Macros πŸ¦‹

#define BAUD_TOL   2
#define UBRR_VALUE
#define UBRRL_VALUE
#define UBRRH_VALUE
#define USE_2X   0

πŸ’œ Detailed Description πŸ’œ

so here's the whole vibe:

#define F_CPU 11059200
#define BAUD 38400
#include <util/setbaud.h>

okay so first thing first, bestie: this header file requires that on entry, values are already defined for F_CPU and BAUD. like, it's not gonna guess your clock speed for you, that's your job. πŸ’€

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? it defaults to 2 %. she has a default, she's self-sufficient. 🫢

this header file defines macros suitable to setup the UART baud rate prescaler registers of an AVR. all calculations are done using the C preprocessor β€” like, at compile time, zero runtime energy. including this header file causes no other side effects so it's possible to include this file more than once (supposedly, with different values for the BAUD parameter), possibly even within the same function. multiple inclusions? within one function? she's flexible, she's low-maintenance, we love her. ✨

assuming that the requested BAUD is valid for the given F_CPU then the macro UBRR_VALUE is 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. 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. it will be defined to 0 if U2X is not needed. basically she tells you whether you gotta flip the 2X switch or not. so considerate. πŸ’–


🌈 Example usage 🌈

here's the moment you've been waiting for, the actual glow-up:

#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 setup 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 require U2X to be set (prescaler 12). see? she did the math for you AND left receipts. the range? she's serving it. πŸ‘‘


πŸ‘‘ Macro Definition Documentation πŸ‘‘

πŸ’… #define BAUD_TOL 2

input and output macro for <util/setbaud.h> πŸŽ€

define the acceptable baud rate tolerance in percent. if not set on entry, it will be set to its default value of 2.

bestie if you want tighter or looser tolerance, this is your knob. she reads it if you give it, defaults to 2 if you don't. πŸ’œ

⭐ #define UBRR_VALUE

output macro from <util/setbaud.h> ✨

contains the calculated baud rate prescaler value for the UBRR register.

πŸ¦‹ #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.


🧚 Author 🧚

Generated automatically by Doxygen for avr-libc from the source code. (not a human author, just doxygen doing her little documentation thing πŸ™ˆ)


and that's the whole page, bestie πŸ’– this was Version 2.1.0, dated Sat Jan 29 2022 β€” so if you're reading this in a terminal, go set your F_CPU and BAUD, hit that #include <util/setbaud.h>, and slay your serial connection. periodt. πŸ’…βœ¨πŸŒˆ

πŸ“¦ where this page comes from