\hypertarget{group__util__setbaud}{}\doxysubsection{$<$util/setbaud.h$>$\+: Helper macros for baud rate calculations}
\label{group__util__setbaud}\index{$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}}
\doxysubsubsection*{Macros}
\begin{DoxyCompactItemize}
\item 
\#define \mbox{\hyperlink{group__util__setbaud_gaddaa61b72aca51ed4835978d500f8755}{B\+A\+U\+D\+\_\+\+T\+OL}}~2
\item 
\#define \mbox{\hyperlink{group__util__setbaud_ga0b029ff580b042a27aaac4bd2ec925e2}{U\+B\+R\+R\+\_\+\+V\+A\+L\+UE}}
\item 
\#define \mbox{\hyperlink{group__util__setbaud_ga39ebec5d04e582b7b6ed9a72b973983c}{U\+B\+R\+R\+L\+\_\+\+V\+A\+L\+UE}}
\item 
\#define \mbox{\hyperlink{group__util__setbaud_ga8188daef772f5eb3fc81dfee168905e2}{U\+B\+R\+R\+H\+\_\+\+V\+A\+L\+UE}}
\item 
\#define \mbox{\hyperlink{group__util__setbaud_ga6977ce48ae3197f3f016b85d98380509}{U\+S\+E\+\_\+2X}}~0
\end{DoxyCompactItemize}


\doxysubsubsection{Detailed Description}

\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#define F\_CPU 11059200}}
\DoxyCodeLine{\textcolor{preprocessor}{\#define BAUD 38400}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <util/setbaud.h>}}
\end{DoxyCode}


This header file requires that on entry values are already defined for F\+\_\+\+C\+PU and B\+A\+UD. In addition, the macro B\+A\+U\+D\+\_\+\+T\+OL will define the baud rate tolerance (in percent) that is acceptable during the calculations. The value of B\+A\+U\+D\+\_\+\+T\+OL will default to 2 \%.

This header file defines macros suitable to setup the U\+A\+RT baud rate prescaler registers of an A\+VR. All calculations are done using the C preprocessor. Including this header file causes no other side effects so it is possible to include this file more than once (supposedly, with different values for the B\+A\+UD parameter), possibly even within the same function.

Assuming that the requested B\+A\+UD is valid for the given F\+\_\+\+C\+PU then the macro U\+B\+R\+R\+\_\+\+V\+A\+L\+UE is set to the required prescaler value. Two additional macros are provided for the low and high bytes of the prescaler, respectively\+: U\+B\+R\+R\+L\+\_\+\+V\+A\+L\+UE is set to the lower byte of the U\+B\+R\+R\+\_\+\+V\+A\+L\+UE and U\+B\+R\+R\+H\+\_\+\+V\+A\+L\+UE is set to the upper byte. An additional macro U\+S\+E\+\_\+2X will be defined. Its value is set to 1 if the desired B\+A\+UD rate within the given tolerance could only be achieved by setting the U2X bit in the U\+A\+RT configuration. It will be defined to 0 if U2X is not needed.

Example usage\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{io_8h}{avr/io.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#define F\_CPU 4000000}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{uart\_9600(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\textcolor{preprocessor}{\#define BAUD 9600}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{setbaud_8h}{util/setbaud.h}}>}}
\DoxyCodeLine{UBRRH = \mbox{\hyperlink{group__util__setbaud_ga8188daef772f5eb3fc81dfee168905e2}{UBRRH\_VALUE}};}
\DoxyCodeLine{UBRRL = \mbox{\hyperlink{group__util__setbaud_ga39ebec5d04e582b7b6ed9a72b973983c}{UBRRL\_VALUE}};}
\DoxyCodeLine{\textcolor{preprocessor}{\#if USE\_2X}}
\DoxyCodeLine{UCSRA |= (1 << U2X);}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{UCSRA \&= ~(1 << U2X);}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{uart\_38400(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\textcolor{preprocessor}{\#undef BAUD  // avoid compiler warning}}
\DoxyCodeLine{\textcolor{preprocessor}{\#define BAUD 38400}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{setbaud_8h}{util/setbaud.h}}>}}
\DoxyCodeLine{UBRRH = \mbox{\hyperlink{group__util__setbaud_ga8188daef772f5eb3fc81dfee168905e2}{UBRRH\_VALUE}};}
\DoxyCodeLine{UBRRL = \mbox{\hyperlink{group__util__setbaud_ga39ebec5d04e582b7b6ed9a72b973983c}{UBRRL\_VALUE}};}
\DoxyCodeLine{\textcolor{preprocessor}{\#if USE\_2X}}
\DoxyCodeLine{UCSRA |= (1 << U2X);}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{UCSRA \&= ~(1 << U2X);}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\textcolor{preprocessor}{\}}}
\end{DoxyCode}


In this example, two functions are defined to setup the U\+A\+RT to run at 9600 Bd, and 38400 Bd, respectively. Using a C\+PU clock of 4 M\+Hz, 9600 Bd can be achieved with an acceptable tolerance without setting U2X (prescaler 25), while 38400 Bd require U2X to be set (prescaler 12). 

\doxysubsubsection{Macro Definition Documentation}
\mbox{\Hypertarget{group__util__setbaud_gaddaa61b72aca51ed4835978d500f8755}\label{group__util__setbaud_gaddaa61b72aca51ed4835978d500f8755}} 
\index{$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}!BAUD\_TOL@{BAUD\_TOL}}
\index{BAUD\_TOL@{BAUD\_TOL}!$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}}
\doxyparagraph{\texorpdfstring{BAUD\_TOL}{BAUD\_TOL}}
{\footnotesize\ttfamily \#define B\+A\+U\+D\+\_\+\+T\+OL~2}

Input and output macro for $<$\mbox{\hyperlink{setbaud_8h}{util/setbaud.\+h}}$>$

Define the acceptable baud rate tolerance in percent. If not set on entry, it will be set to its default value of 2. \mbox{\Hypertarget{group__util__setbaud_ga0b029ff580b042a27aaac4bd2ec925e2}\label{group__util__setbaud_ga0b029ff580b042a27aaac4bd2ec925e2}} 
\index{$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}!UBRR\_VALUE@{UBRR\_VALUE}}
\index{UBRR\_VALUE@{UBRR\_VALUE}!$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}}
\doxyparagraph{\texorpdfstring{UBRR\_VALUE}{UBRR\_VALUE}}
{\footnotesize\ttfamily \#define U\+B\+R\+R\+\_\+\+V\+A\+L\+UE}

Output macro from $<$\mbox{\hyperlink{setbaud_8h}{util/setbaud.\+h}}$>$

Contains the calculated baud rate prescaler value for the U\+B\+RR register. \mbox{\Hypertarget{group__util__setbaud_ga8188daef772f5eb3fc81dfee168905e2}\label{group__util__setbaud_ga8188daef772f5eb3fc81dfee168905e2}} 
\index{$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}!UBRRH\_VALUE@{UBRRH\_VALUE}}
\index{UBRRH\_VALUE@{UBRRH\_VALUE}!$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}}
\doxyparagraph{\texorpdfstring{UBRRH\_VALUE}{UBRRH\_VALUE}}
{\footnotesize\ttfamily \#define U\+B\+R\+R\+H\+\_\+\+V\+A\+L\+UE}

Output macro from $<$\mbox{\hyperlink{setbaud_8h}{util/setbaud.\+h}}$>$

Contains the upper byte of the calculated prescaler value (U\+B\+R\+R\+\_\+\+V\+A\+L\+UE). \mbox{\Hypertarget{group__util__setbaud_ga39ebec5d04e582b7b6ed9a72b973983c}\label{group__util__setbaud_ga39ebec5d04e582b7b6ed9a72b973983c}} 
\index{$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}!UBRRL\_VALUE@{UBRRL\_VALUE}}
\index{UBRRL\_VALUE@{UBRRL\_VALUE}!$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}}
\doxyparagraph{\texorpdfstring{UBRRL\_VALUE}{UBRRL\_VALUE}}
{\footnotesize\ttfamily \#define U\+B\+R\+R\+L\+\_\+\+V\+A\+L\+UE}

Output macro from $<$\mbox{\hyperlink{setbaud_8h}{util/setbaud.\+h}}$>$

Contains the lower byte of the calculated prescaler value (U\+B\+R\+R\+\_\+\+V\+A\+L\+UE). \mbox{\Hypertarget{group__util__setbaud_ga6977ce48ae3197f3f016b85d98380509}\label{group__util__setbaud_ga6977ce48ae3197f3f016b85d98380509}} 
\index{$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}!USE\_2X@{USE\_2X}}
\index{USE\_2X@{USE\_2X}!$<$util/setbaud.h$>$: Helper macros for baud rate calculations@{$<$util/setbaud.h$>$: Helper macros for baud rate calculations}}
\doxyparagraph{\texorpdfstring{USE\_2X}{USE\_2X}}
{\footnotesize\ttfamily \#define U\+S\+E\+\_\+2X~0}

Output macro from $<$\mbox{\hyperlink{setbaud_8h}{util/setbaud.\+h}}$>$

Contains the value 1 if the desired baud rate tolerance could only be achieved by setting the U2X bit in the U\+A\+RT configuration. Contains 0 otherwise. 