AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

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