user42283: I am using an Arduino Uno, and I need to create a program that calculates the actual baud rate and the percent error for all standard baud rate in Arduino monitor program.

user7508: First, look at the code used by the Arduino core library to configure
the USART baud rate register: it's in
HardwareSerial::begin(). The general formula is
uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;

but beware of the hard-coded exception. You can replicate that part of
the code in your own.
Then, look at the ATmega328P datasheet. More specifically, at section
24.4.1. Internal Clock Generation – The Baud Rate
Generator. Here you find the formula
BAUD = F_CPU / (8*(UBRR0 + 1))

assuming the U2X0 bit has been set (again, beware of the hard-coded
exception). Combine both formulas and you get the actual baud rate.

42283: <p>I am using an Arduino Uno, and I need to create a program that calculates the actual baud rate and the percent error for all standard baud rate in Arduino monitor program.</p>
 7508: <p>First, look at the code used by the Arduino core library to configure
the USART baud rate register: it's in
<a href="https://github.com/arduino/Arduino/blob/1.8.5/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp#L103" rel="nofollow noreferrer"><code>HardwareSerial::begin()</code></a>. The general formula is</p>
<pre class="lang-c++ prettyprint-override"><code>uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
</code></pre>
<p>but beware of the hard-coded exception. You can replicate that part of
the code in your own.</p>
<p>Then, look at the ATmega328P datasheet. More specifically, at section
<a href="http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf#page=227" rel="nofollow noreferrer">24.4.1. Internal Clock Generation – The Baud Rate
Generator</a>. Here you find the formula</p>
<pre><code>BAUD = F_CPU / (8*(UBRR0 + 1))
</code></pre>
<p>assuming the <code>U2X0</code> bit has been set (again, beware of the hard-coded
exception). Combine both formulas and you get the actual baud rate.</p>
