AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects

setbaud.h

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 138#define BAUD_TOL 2 139139 140140/** 141141\def UBRR_VALUE 142142\ingroup util_setbaud 143143 144144Output macro from <util/setbaud.h> 145145 146146Contains the calculated baud rate prescaler value for the UBRR 147147register. 148148*/ 149 149#define UBRR_VALUE 150150 151151/** 152152\def UBRRL_VALUE 153153\ingroup util_setbaud 154154 155155Output macro from <util/setbaud.h> 156156 157157Contains the lower byte of the calculated prescaler value 158158(UBRR_VALUE). 159159*/ 160 160#define UBRRL_VALUE 161161 162162/** 163163\def UBRRH_VALUE 164164\ingroup util_setbaud 165165 166166Output macro from <util/setbaud.h> 167167 168168Contains the upper byte of the calculated prescaler value 169169(UBRR_VALUE). 170170*/ 171 171#define UBRRH_VALUE 172172 173173/** 174174\def USE_2X 175175\ingroup util_setbaud 176176 177177Output macro from <util/setbaud.h> 178178 179179Contains the value 1 if the desired baud rate tolerance could only 180180be achieved by setting the U2X bit in the UART configuration. 181181Contains 0 otherwise. 182182*/ 183 183#define USE_2X 0 184184 185185#else /* !__DOXYGEN__ */ 186186 187187#undef USE_2X 188188 189189/* Baud rate tolerance is 2 % unless previously defined */ 190190#ifndef BAUD_TOL 191191# define BAUD_TOL 2 192192#endif 193193 194194#ifdef __ASSEMBLER__ 195195#define UBRR_VALUE (((F_CPU) + 8 * (BAUD)) / (16 * (BAUD)) -1) 196196#else 197197#define UBRR_VALUE (((F_CPU) + 8UL * (BAUD)) / (16UL * (BAUD)) -1UL) 198198#endif 199199 200200#if 100 * (F_CPU) >> \ 201201(16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL)) 202202# define USE_2X 1 203203#elif 100 * (F_CPU) < \ 204204(16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)) 205205# define USE_2X 1 206206#else 207207# define USE_2X 0 208208#endif 209209 210210#if USE_2X 211211/* U2X required, recalculate */ 212212#undef UBRR_VALUE 213213 214214#ifdef __ASSEMBLER__ 215215#define UBRR_VALUE (((F_CPU) + 4 * (BAUD)) / (8 * (BAUD)) -1) 216216#else 217217#define UBRR_VALUE (((F_CPU) + 4UL * (BAUD)) / (8UL * (BAUD)) -1UL) 218218#endif 219219 220220#if 100 * (F_CPU) >> \ 221221(8 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL)) 222222# warning "Baud rate achieved is higher than allowed" 223223#endif 224224 225225#if 100 * (F_CPU) < \ 226226(8 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)) 227227# warning "Baud rate achieved is lower than allowed" 228228#endif 229229 230230#endif /* USE_U2X */ 231231 232232#ifdef UBRR_VALUE 233233/* Check for overflow */ 234234# if UBRR_VALUE >> = (1 << 12) 235235# warning "UBRR value overflow" 236236# endif 237237 238238# define UBRRL_VALUE (UBRR_VALUE & 0xff) 239239# define UBRRH_VALUE (UBRR_VALUE >> 8) 240240#endif 241241 242242#endif /* __DOXYGEN__ */ 243243/* end of util/setbaud.h */