AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

Index

Loading...
Searching...
No Matches
setbaud.h
Go to the documentation of this file.
1/* Copyright (c) 2007 Cliff Lawson
2 Copyright (c) 2007 Carlos Lamas
3 Copyright (c) 2025 Ian Gregg
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 * Neither the name of the copyright holders nor the names of
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
32
33/**
34 \file
35*/
36
37/**
38 \defgroup util_setbaud <util/setbaud.h>: Helper macros for baud rate calculations
39 \code
40 #define F_CPU 11059200
41 #define BAUD 38400
42 #include <util/setbaud.h>
43 \endcode
44
45 This header file requires that on entry values are already defined
46 for F_CPU and BAUD. In addition, the macro BAUD_TOL will define
47 the baud rate tolerance (in percent) that is acceptable during
48 the calculations. The value of BAUD_TOL will default to 2 %.
49
50 This header file defines macros suitable to setup the UART baud
51 rate prescaler registers of an AVR. All calculations are done
52 using the C preprocessor. Including this header file causes no
53 other side effects so it is possible to include this file more than
54 once (supposedly, with different values for the BAUD parameter),
55 possibly even within the same function.
56
57 Assuming that the requested BAUD is valid for the given F_CPU then
58 the macro UBRR_VALUE is set to the required prescaler value. Two
59 additional macros are provided for the low and high bytes of the
60 prescaler, respectively: UBRRL_VALUE is set to the lower byte of
61 the UBRR_VALUE and UBRRH_VALUE is set to the upper byte. An
62 additional macro USE_2X will be defined. Its value is set to 1 if
63 the desired BAUD rate within the given tolerance could only be
64 achieved by setting the U2X bit in the UART configuration. It will
65 be defined to 0 if U2X is not needed.
66
67 Example usage:
68
69 \code
70 #include <avr/io.h>
71
72 #define F_CPU 4000000
73
74 static void
75 uart_9600(void)
76 {
77 #define BAUD 9600
78 #include <util/setbaud.h>
79 UBRRH = UBRRH_VALUE;
80 UBRRL = UBRRL_VALUE;
81 #if USE_2X
82 UCSRA |= (1 << U2X);
83 #else
84 UCSRA &= ~(1 << U2X);
85 #endif
86 }
87
88 static void
89 uart_38400(void)
90 {
91 #undef BAUD // avoid compiler warning
92 #define BAUD 38400
93 #include <util/setbaud.h>
94 UBRRH = UBRRH_VALUE;
95 UBRRL = UBRRL_VALUE;
96 #if USE_2X
97 UCSRA |= (1 << U2X);
98 #else
99 UCSRA &= ~(1 << U2X);
100 #endif
101 }
102 \endcode
103
104 In this example, two functions are defined to setup the UART
105 to run at 9600 Bd, and 38400 Bd, respectively. Using a CPU
106 clock of 4 MHz, 9600 Bd can be achieved with an acceptable
107 tolerance without setting U2X (prescaler 25), while 38400 Bd
108 require U2X to be set (prescaler 12).
109*/
110
111#ifndef F_CPU
112# error "setbaud.h requires F_CPU to be defined"
113#endif
114
115#ifndef BAUD
116# error "setbaud.h requires BAUD to be defined"
117#endif
118
119#if !(F_CPU)
120# error "F_CPU must be a constant value"
121#endif
122
123#if !(BAUD)
124# error "BAUD must be a constant value"
125#endif
126
127#if defined(__DOXYGEN__)
128/**
129 \def BAUD_TOL
130 \ingroup util_setbaud
131
132 Input and output macro for <util/setbaud.h>
133
134 Define the acceptable baud rate tolerance in percent. If not set
135 on entry, it will be set to its default value of 2.
136*/
137#define BAUD_TOL 2
138
139/**
140 \def UBRR_VALUE
141 \ingroup util_setbaud
142
143 Output macro from <util/setbaud.h>
144
145 Contains the calculated baud rate prescaler value for the UBRR
146 register.
147*/
148#define UBRR_VALUE
149
150/**
151 \def UBRRL_VALUE
152 \ingroup util_setbaud
153
154 Output macro from <util/setbaud.h>
155
156 Contains the lower byte of the calculated prescaler value
157 (UBRR_VALUE).
158*/
159#define UBRRL_VALUE
160
161/**
162 \def UBRRH_VALUE
163 \ingroup util_setbaud
164
165 Output macro from <util/setbaud.h>
166
167 Contains the upper byte of the calculated prescaler value
168 (UBRR_VALUE).
169*/
170#define UBRRH_VALUE
171
172/**
173 \def USE_2X
174 \ingroup util_setbaud
175
176 Output macro from <util/setbaud.h>
177
178 Contains the value 1 if the desired baud rate tolerance could only
179 be achieved by setting the U2X bit in the UART configuration.
180 Contains 0 otherwise.
181*/
182#define USE_2X 0
183
184#else /* !__DOXYGEN__ */
185
186#undef USE_2X
187
188/* Baud rate tolerance is 2 % unless previously defined */
189#ifndef BAUD_TOL
190# define BAUD_TOL 2
191#endif
192
193#ifdef __ASSEMBLER__
194#define UBRR_VALUE ((((F_CPU) - 16 * (BAUD)) * 100 / (16 * (BAUD)) + 50) / 100)
195#else
196#define UBRR_VALUE ((((F_CPU) - 16UL * (BAUD)) * 100UL / (16UL * (BAUD)) + 50UL) / 100UL)
197#endif
198
199#if 100 * (F_CPU) > \
200 (16 * ((UBRR_VALUE) + 1) * (100 + (BAUD_TOL)) * (BAUD))
201# define USE_2X 1
202#elif 100 * (F_CPU) < \
203 (16 * ((UBRR_VALUE) + 1) * (100 - (BAUD_TOL)) * (BAUD))
204# define USE_2X 1
205#else
206# define USE_2X 0
207#endif
208
209#if USE_2X
210/* U2X required, recalculate */
211#undef UBRR_VALUE
212
213#ifdef __ASSEMBLER__
214#define UBRR_VALUE ((((F_CPU) - 8 * (BAUD)) * 100 / (8 * (BAUD)) + 50) / 100)
215#else
216#define UBRR_VALUE ((((F_CPU) - 8UL * (BAUD)) * 100UL / (8UL * (BAUD)) + 50UL) / 100UL)
217#endif
218
219#if 100 * (F_CPU) > \
220 (8 * ((UBRR_VALUE) + 1)) * (100 + (BAUD_TOL)) * (BAUD))
221# warning "Baud rate achieved is higher than allowed"
222#endif
223
224#if 100 * (F_CPU) < \
225 (8 * ((UBRR_VALUE) + 1)) * (100 - (BAUD_TOL)) * (BAUD))
226# warning "Baud rate achieved is lower than allowed"
227#endif
228
229#endif /* USE_U2X */
230
231#ifdef UBRR_VALUE
232 /* Check for overflow */
233# if UBRR_VALUE >= (1 << 12)
234# warning "UBRR value overflow"
235# endif
236
237# define UBRRL_VALUE (UBRR_VALUE & 0xff)
238# define UBRRH_VALUE (UBRR_VALUE >> 8)
239#endif
240
241#endif /* __DOXYGEN__ */
242/* end of util/setbaud.h */