1
/* Copyright (c) 2007 Cliff Lawson
2
Copyright (c) 2007 Carlos Lamas
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
8
* Redistributions of source code must retain the above copyright
9
notice, this list of conditions and the following disclaimer.
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
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.
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. */
32
/* $Id: setbaud.h,v 1.1 2007/10/28 23:01:09 joerg_wunsch Exp $ */
39
\defgroup util_setbaud <util/setbaud.h>: Helper macros for baud rate calculations
41
#define F_CPU 11059200
43
#include <util/setbaud.h>
46
This header file requires that on entry values are already defined
47
for F_CPU and BAUD. In addition, the macro BAUD_TOL will define
48
the baud rate tolerance (in percent) that is acceptable during
49
the calculations. The value of BAUD_TOL will default to 2 %.
51
This header file defines macros suitable to setup the UART baud
52
rate prescaler registers of an AVR. All calculations are done
53
using the C preprocessor. Including this header file causes no
54
other side effects so it is possible to include this file more than
55
once (supposedly, with different values for the BAUD parameter),
56
possibly even within the same function.
58
Assuming that the requested BAUD is valid for the given F_CPU then
59
the macro UBRR_VALUE is set to the required prescaler value. Two
60
additional macros are provided for the low and high bytes of the
61
prescaler, respectively: UBRRL_VALUE is set to the lower byte of
62
the UBRR_VALUE and UBRRH_VALUE is set to the upper byte. An
63
additional macro USE_2X will be defined. Its value is set to 1 if
64
the desired BAUD rate within the given tolerance could only be
65
achieved by setting the U2X bit in the UART configuration. It will
66
be defined to 0 if U2X is not needed.
79
#include <util/setbaud.h>
92
#undef BAUD // avoid compiler warning
94
#include <util/setbaud.h>
100
UCSRA &= ~(1 << U2X);
105
In this example, two functions are defined to setup the UART
106
to run at 9600 Bd, and 38400 Bd, respectively. Using a CPU
107
clock of 4 MHz, 9600 Bd can be achieved with an acceptable
108
tolerance without setting U2X (prescaler 25), while 38400 Bd
109
require U2X to be set (prescaler 12).
113
# error "setbaud.h requires F_CPU to be defined"
117
# error "setbaud.h requires BAUD to be defined"
121
# error "F_CPU must be a constant value"
125
# error "BAUD must be a constant value"
128
#if defined(__DOXYGEN__)
131
\ingroup util_setbaud
133
Input and output macro for <util/setbaud.h>
135
Define the acceptable baud rate tolerance in percent. If not set
136
on entry, it will be set to its default value of 2.
142
\ingroup util_setbaud
144
Output macro from <util/setbaud.h>
146
Contains the calculated baud rate prescaler value for the UBRR
153
\ingroup util_setbaud
155
Output macro from <util/setbaud.h>
157
Contains the lower byte of the calculated prescaler value
164
\ingroup util_setbaud
166
Output macro from <util/setbaud.h>
168
Contains the upper byte of the calculated prescaler value
175
\ingroup util_setbaud
177
Output bacro from <util/setbaud.h>
179
Contains the value 1 if the desired baud rate tolerance could only
180
be achieved by setting the U2X bit in the UART configuration.
181
Contains 0 otherwise.
185
#else /* !__DOXYGEN__ */
189
/* Baud rate tolerance is 2 % unless previously defined */
194
#define UBRR_VALUE (((F_CPU) + 8UL * (BAUD)) / (16UL * (BAUD)) -1UL)
196
#if 100 * (F_CPU) > \
197
(16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL))
199
#elif 100 * (F_CPU) < \
200
(16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL))
207
/* U2X required, recalculate */
209
#define UBRR_VALUE (((F_CPU) + 4UL * (BAUD)) / (8UL * (BAUD)) -1UL)
211
#if 100 * (F_CPU) > \
212
(8 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL))
213
# warning "Baud rate achieved is higher than allowed"
216
#if 100 * (F_CPU) < \
217
(8 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL))
218
# warning "Baud rate achieved is lower than allowed"
224
# define UBRRL_VALUE (UBRR_VALUE & 0xff)
225
# define UBRRH_VALUE (UBRR_VALUE >> 8)
228
#endif /* __DOXYGEN__ */
229
/* end of util/setbaud.h */