You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
13 KiB
HTML

<!-- HTML header for doxygen 1.8.7-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.6.3"/>
<!--BEGIN PROJECT_NAME--><title>avr-libc: avr-libc: &lt;util/setbaud.h&gt;: Helper macros for baud rate calculations</title><!--END PROJECT_NAME-->
<!--BEGIN !PROJECT_NAME--><title>avr-libc: &lt;util/setbaud.h&gt;: Helper macros for baud rate calculations</title><!--END !PROJECT_NAME-->
<link href="$relpath^tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="$relpath^jquery.js"></script>
<script type="text/javascript" src="$relpath^dynsections.js"></script>
$treeview
$search
$mathjax
<link href="$relpath^$stylesheet" rel="stylesheet" type="text/css" />
$extrastylesheet
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<!--BEGIN TITLEAREA-->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<!--BEGIN PROJECT_LOGO-->
<td id="projectlogo"><img alt="Logo" src="$relpath^$projectlogo"/></td>
<!--END PROJECT_LOGO-->
<!--BEGIN PROJECT_NAME-->
<td style="padding-left: 0.5em;">
<div id="projectname">avr-libc
<!--BEGIN PROJECT_NUMBER-->&#160;<span id="projectnumber">2.0.0</span><!--END PROJECT_NUMBER-->
</div>
<!--BEGIN PROJECT_BRIEF--><div id="projectbrief">$projectbrief</div><!--END PROJECT_BRIEF-->
</td>
<!--END PROJECT_NAME-->
<!--BEGIN !PROJECT_NAME-->
<!--BEGIN PROJECT_BRIEF-->
<td style="padding-left: 0.5em;">
<div id="projectbrief">$projectbrief</div>
</td>
<!--END PROJECT_BRIEF-->
<!--END !PROJECT_NAME-->
<!--BEGIN DISABLE_INDEX-->
<!--BEGIN SEARCHENGINE-->
<td>$searchbox</td>
<!--END SEARCHENGINE-->
<!--END DISABLE_INDEX-->
</tr>
</tbody>
</table>
<table>
<tr>
<td align="left"><a href="http://www.nongnu.org/avr-libc/"><h2>AVR Libc Home Page</h2></a></td>
<td align="center" colspan=4><img src="avrs.png" alt="AVRs" align="middle" border="0"></td>
<td align="right"><a href="https://savannah.nongnu.org/projects/avr-libc/"><h2>AVR Libc Development Pages</h2></a></td>
</tr>
<tr>
<td align="center" width="20%"><a href="index.html"><h2>Main Page</h2></a></td>
<td align="center" width="20%"><a href="pages.html"><h2>User Manual</h2></a></td>
<td align="center" width="20%"><a href="modules.html"><h2>Library Reference</h2></a></td>
<td align="center" width="20%"><a href="FAQ.html"><h2>FAQ</h2></a></td>
<td align="center" width="20%"><a href="group__demos.html"><h2>Example Projects</h2></a></td>
</tr>
</table>
</div>
<!--END TITLEAREA-->
<!-- end header part -->
<!-- Generated by Doxygen 1.6.3 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div class="contents">
<h1>&lt;util/setbaud.h&gt;: Helper macros for baud rate calculations</h1><table border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2"><h2>Defines</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__setbaud.html#gaddaa61b72aca51ed4835978d500f8755">BAUD_TOL</a>&nbsp;&nbsp;&nbsp;2</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__setbaud.html#ga0b029ff580b042a27aaac4bd2ec925e2">UBRR_VALUE</a></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__setbaud.html#ga39ebec5d04e582b7b6ed9a72b973983c">UBRRL_VALUE</a></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__setbaud.html#ga8188daef772f5eb3fc81dfee168905e2">UBRRH_VALUE</a></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__setbaud.html#ga6977ce48ae3197f3f016b85d98380509">USE_2X</a>&nbsp;&nbsp;&nbsp;0</td></tr>
</table>
<hr/><a name="_details"></a><h2>Detailed Description</h2>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #define F_CPU 11059200</span>
<span class="preprocessor"></span><span class="preprocessor"> #define BAUD 38400</span>
<span class="preprocessor"> #include &lt;util/setbaud.h&gt;</span>
</pre></div><p>This header file requires that on entry values are already defined for F_CPU and BAUD. In addition, the macro BAUD_TOL will define the baud rate tolerance (in percent) that is acceptable during the calculations. The value of BAUD_TOL will default to 2 %.</p>
<p>This header file defines macros suitable to setup the UART baud rate prescaler registers of an AVR. 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 BAUD parameter), possibly even within the same function.</p>
<p>Assuming that the requested BAUD is valid for the given F_CPU then the macro UBRR_VALUE is set to the required prescaler value. Two additional macros are provided for the low and high bytes of the prescaler, respectively: UBRRL_VALUE is set to the lower byte of the UBRR_VALUE and UBRRH_VALUE is set to the upper byte. An additional macro USE_2X will be defined. Its value is set to 1 if the desired BAUD rate within the given tolerance could only be achieved by setting the U2X bit in the UART configuration. It will be defined to 0 if U2X is not needed.</p>
<p>Example usage:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include &lt;<a class="code" href="io_8h.html">avr/io.h</a>&gt;</span>
<span class="preprocessor"> #define F_CPU 4000000</span>
<span class="preprocessor"></span>
<span class="keyword">static</span> <span class="keywordtype">void</span>
uart_9600(<span class="keywordtype">void</span>)
{
<span class="preprocessor"> #define BAUD 9600</span>
<span class="preprocessor"></span><span class="preprocessor"> #include &lt;<a class="code" href="setbaud_8h.html">util/setbaud.h</a>&gt;</span>
UBRRH = <a class="code" href="group__util__setbaud.html#ga8188daef772f5eb3fc81dfee168905e2">UBRRH_VALUE</a>;
UBRRL = <a class="code" href="group__util__setbaud.html#ga39ebec5d04e582b7b6ed9a72b973983c">UBRRL_VALUE</a>;
<span class="preprocessor"> #if USE_2X</span>
<span class="preprocessor"></span> UCSRA |= (1 &lt;&lt; U2X);
<span class="preprocessor"> #else</span>
<span class="preprocessor"></span> UCSRA &amp;= ~(1 &lt;&lt; U2X);
<span class="preprocessor"> #endif</span>
<span class="preprocessor"></span> }
<span class="keyword">static</span> <span class="keywordtype">void</span>
uart_38400(<span class="keywordtype">void</span>)
{
<span class="preprocessor"> #undef BAUD // avoid compiler warning</span>
<span class="preprocessor"></span><span class="preprocessor"> #define BAUD 38400</span>
<span class="preprocessor"></span><span class="preprocessor"> #include &lt;<a class="code" href="setbaud_8h.html">util/setbaud.h</a>&gt;</span>
UBRRH = <a class="code" href="group__util__setbaud.html#ga8188daef772f5eb3fc81dfee168905e2">UBRRH_VALUE</a>;
UBRRL = <a class="code" href="group__util__setbaud.html#ga39ebec5d04e582b7b6ed9a72b973983c">UBRRL_VALUE</a>;
<span class="preprocessor"> #if USE_2X</span>
<span class="preprocessor"></span> UCSRA |= (1 &lt;&lt; U2X);
<span class="preprocessor"> #else</span>
<span class="preprocessor"></span> UCSRA &amp;= ~(1 &lt;&lt; U2X);
<span class="preprocessor"> #endif</span>
<span class="preprocessor"> }</span>
</pre></div><p>In this example, two functions are defined to setup the UART to run at 9600 Bd, and 38400 Bd, respectively. Using a CPU clock of 4 MHz, 9600 Bd can be achieved with an acceptable tolerance without setting U2X (prescaler 25), while 38400 Bd require U2X to be set (prescaler 12). </p>
<hr/><h2>Define Documentation</h2>
<a class="anchor" id="gaddaa61b72aca51ed4835978d500f8755"></a><!-- doxytag: member="setbaud.h::BAUD_TOL" ref="gaddaa61b72aca51ed4835978d500f8755" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define BAUD_TOL&nbsp;&nbsp;&nbsp;2</td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Input and output macro for &lt;<a class="el" href="setbaud_8h.html">util/setbaud.h</a>&gt;</p>
<p>Define the acceptable baud rate tolerance in percent. If not set on entry, it will be set to its default value of 2. </p>
</div>
</div>
<a class="anchor" id="ga0b029ff580b042a27aaac4bd2ec925e2"></a><!-- doxytag: member="setbaud.h::UBRR_VALUE" ref="ga0b029ff580b042a27aaac4bd2ec925e2" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define UBRR_VALUE</td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Output macro from &lt;<a class="el" href="setbaud_8h.html">util/setbaud.h</a>&gt;</p>
<p>Contains the calculated baud rate prescaler value for the UBRR register. </p>
</div>
</div>
<a class="anchor" id="ga8188daef772f5eb3fc81dfee168905e2"></a><!-- doxytag: member="setbaud.h::UBRRH_VALUE" ref="ga8188daef772f5eb3fc81dfee168905e2" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define UBRRH_VALUE</td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Output macro from &lt;<a class="el" href="setbaud_8h.html">util/setbaud.h</a>&gt;</p>
<p>Contains the upper byte of the calculated prescaler value (UBRR_VALUE). </p>
</div>
</div>
<a class="anchor" id="ga39ebec5d04e582b7b6ed9a72b973983c"></a><!-- doxytag: member="setbaud.h::UBRRL_VALUE" ref="ga39ebec5d04e582b7b6ed9a72b973983c" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define UBRRL_VALUE</td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Output macro from &lt;<a class="el" href="setbaud_8h.html">util/setbaud.h</a>&gt;</p>
<p>Contains the lower byte of the calculated prescaler value (UBRR_VALUE). </p>
</div>
</div>
<a class="anchor" id="ga6977ce48ae3197f3f016b85d98380509"></a><!-- doxytag: member="setbaud.h::USE_2X" ref="ga6977ce48ae3197f3f016b85d98380509" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define USE_2X&nbsp;&nbsp;&nbsp;0</td>
</tr>
</table>
</div>
<div class="memdoc">
<p>Output macro from &lt;<a class="el" href="setbaud_8h.html">util/setbaud.h</a>&gt;</p>
<p>Contains the value 1 if the desired baud rate tolerance could only be achieved by setting the U2X bit in the UART configuration. Contains 0 otherwise. </p>
</div>
</div>
</div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&nbsp;</span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<!-- HTML footer for doxygen 1.8.7-->
<!-- start footer part -->
<!--BEGIN GENERATE_TREEVIEW-->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
$navpath
<li class="footer">$generatedby
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="$relpath^doxygen.png" alt="doxygen"/></a> 1.6.3 </li>
</ul>
</div>
<!--END GENERATE_TREEVIEW-->
<!--BEGIN !GENERATE_TREEVIEW-->
<hr class="footer"/><address class="footer"><small>
$generatedby &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="$relpath^doxygen.png" alt="doxygen"/>
</a> 1.6.3
</small></address>
<!--END !GENERATE_TREEVIEW-->
</body>
</html>