Go to the documentation of this file.
01 1 Copyright (c) 2007 Cliff Lawson
022Copyright (c) 2007 Carlos Lamas
033All rights reserved.
044
055Redistribution and use in source and binary forms, with or without
066modification, are permitted provided that the following conditions are met:
077
088* Redistributions of source code must retain the above copyright
099notice, this list of conditions and the following disclaimer.
1010
1111* Redistributions in binary form must reproduce the above copyright
1212notice, this list of conditions and the following disclaimer in
1313the documentation and/or other materials provided with the
1414distribution.
1515
1616* Neither the name of the copyright holders nor the names of
1717contributors may be used to endorse or promote products derived
1818from this software without specific prior written permission.
1919
2020THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2121AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2222IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2323ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2424LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2525CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2626SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2727INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2828CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2929ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3030POSSIBILITY OF SUCH DAMAGE. */
3131
3232/* $Id: setbaud.h 2424 2014-04-29 13:08:15Z pitchumani $ */
3333
3434/**
3535\file
3636*/
3737
3838/**
3939\defgroup util_setbaud <util/setbaud.h>: Helper macros for baud rate calculations
4040\code
4141#define F_CPU 11059200
4242#define BAUD 38400
4343#include <util/setbaud.h>
4444\endcode
4545
4646This header file requires that on entry values are already defined
4747for F_CPU and BAUD. In addition, the macro BAUD_TOL will define
4848the baud rate tolerance (in percent) that is acceptable during
4949the calculations. The value of BAUD_TOL will default to 2 %.
5050
5151This header file defines macros suitable to setup the UART baud
5252rate prescaler registers of an AVR. All calculations are done
5353using the C preprocessor. Including this header file causes no
5454other side effects so it is possible to include this file more than
5555once (supposedly, with different values for the BAUD parameter),
5656possibly even within the same function.
5757
5858Assuming that the requested BAUD is valid for the given F_CPU then
5959the macro UBRR_VALUE is set to the required prescaler value. Two
6060additional macros are provided for the low and high bytes of the
6161prescaler, respectively: UBRRL_VALUE is set to the lower byte of
6262the UBRR_VALUE and UBRRH_VALUE is set to the upper byte. An
6363additional macro USE_2X will be defined. Its value is set to 1 if
6464the desired BAUD rate within the given tolerance could only be
6565achieved by setting the U2X bit in the UART configuration. It will
6666be defined to 0 if U2X is not needed.
6767
6868Example usage:
6969
7070\code
7171#include <avr/io.h>
7272
7373#define F_CPU 4000000
7474
7575static void
7676uart_9600(void)
7777{
7878#define BAUD 9600
7979#include <util/setbaud.h>
8080UBRRH = UBRRH_VALUE;
8181UBRRL = UBRRL_VALUE;
8282#if USE_2X
8383UCSRA |= (1 << U2X);
8484#else
8585UCSRA &= ~(1 << U2X);
8686#endif
8787}
8888
8989static void
9090uart_38400(void)
9191{
9292#undef BAUD // avoid compiler warning
9393#define BAUD 38400
9494#include <util/setbaud.h>
9595UBRRH = UBRRH_VALUE;
9696UBRRL = UBRRL_VALUE;
9797#if USE_2X
9898UCSRA |= (1 << U2X);
9999#else
100100UCSRA &= ~(1 << U2X);
101101#endif
102102}
103103\endcode
104104
105105In this example, two functions are defined to setup the UART
106106to run at 9600 Bd, and 38400 Bd, respectively. Using a CPU
107107clock of 4 MHz, 9600 Bd can be achieved with an acceptable
108108tolerance without setting U2X (prescaler 25), while 38400 Bd
109109require U2X to be set (prescaler 12).
110110*/
111111
112112#ifndef F_CPU
113113# error "setbaud.h requires F_CPU to be defined"
114114#endif
115115
116116#ifndef BAUD
117117# error "setbaud.h requires BAUD to be defined"
118118#endif
119119
120120#if !(F_CPU)
121121# error "F_CPU must be a constant value"
122122#endif
123123
124124#if !(BAUD)
125125# error "BAUD must be a constant value"
126126#endif
127127
128128#if defined(__DOXYGEN__)
129129/**
130130\def BAUD_TOL
131131\ingroup util_setbaud
132132
133133Input and output macro for <util/setbaud.h>
134134
135135Define the acceptable baud rate tolerance in percent. If not set
136136on entry, it will be set to its default value of 2.
137137*/
138