2

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.

jasmine
  • 21
  • 2
  • 4
    It's listed in datasheets. The table is called: "Equations for Calculating Baud Rate Register Setting" – KIIV Jan 30 '18 at 09:18
  • Welcome to Arduino:SE. Please be aware that this is not a free design house or an on-demand online technical encyclopedia. You might find it helpful to review the site [tour](https://arduino.stackexchange.com/tour) and [Help Centre](https://arduino.stackexchange.com/help) and, in particular, [ask]. – sempaiscuba Jan 30 '18 at 09:18
  • See http://wormfood.net/avrbaudcalc.php – Mikael Patel Mar 31 '18 at 12:02
  • Jasmine, if you found the below answer helpful, please upvote it and/or mark it as the answer. That's a good way of thanking @EdgarBonet for his hard work. See https://stackoverflow.com/help/privileges/vote-up to understand why voting is important. – MechtEngineer Mar 31 '18 at 23:53

1 Answers1

1

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.

Edgar Bonet
  • 39,449
  • 4
  • 36
  • 72