Figure 1: Structure of a typical serial data frame.
The final parameter to set for
your asynchronous serial
communication is the baud rate. The
baud rate is effectively the speed at
which data is transferred, and is
measured in bits per second. Of
course, there are limits to how fast
data can reliably be transmitted. A common baud rate is
9600 bps, but speeds of up to 115200 bps are obtainable.
So, what baud rate should I operate at? The answer is
that — just like driving your car — slower is safer, but not
always practical. Like many things in the world of
microcontrollers, trade-offs exist: In this case, speed vs.
error rate. The speed that your microcontroller is running
at has a large influence on the baud rates that you can
reliably operate at. A faster crystal means the MCU can
process at a higher baud rate.
To answer the speed question with empirical data,
open up your ATmega328 datasheet and refer to section
20. 10 “Examples of Baud Rate Setting.” The table in this
section shows common baud rates, and the error precent
you can expect with common crystal speeds. Take a look
at Figure 2 which shows this information for the 16 MHz
crystal that we’re using. From the table, you’ll see that we
can reliably run at baud rates of up to 76800 bps with a
less than ± 1% error rate (although I’d probably avoid
57600 bps with its 2.1% error rate). Speed question
answered! This table is a great resource, and I refer to it
often. As an aside, take note of the
“UBRRn” column — we’ll need this
it. (No, not set it up to take a fall, but set it up to
succeed!) But, what is a frame?
Many years ago, I used to program in a language
called GW-BASIC that ran on the DOS operating system
on an IBM PC. When I wanted to open a serial port, I had
to pass a whole host of parameters that I didn’t fully
understand at the time. Does this look at all familiar:
The parity, data, and stop parameters in the above
command help to define the data frame. Figure 1 shows
how these fit together. Every frame begins with a start bit.
When a serial line is in an idle state, it is high (in other
words, a “1”). The start bit drops this to a low state (“0”)
to signal the start of a frame.
Following the start bit, the data bits are transmitted:
low for a 0 and high for a 1. While the data section of the
frame can contain between five and nine bits, they
commonly carry eight bits — which works out really nicely
as eight bits are one byte.
At the end of the data section,
you can choose to include a parity
bit. A parity bit acts as a very
rudimentary form of error checking,
helping to flag if the data wasn’t
correctly transmitted. It isn’t used
very often, and if used, then the
devices need to have a mechanism
to handle the re-transmission of
Where has My PC
Serial Port Gone?
Finally, either one or two stop
bits are transmitted. These are high,
and signal the end of the frame.
Usually, one stop bit is enough but if
extra processing time is needed,
then a second stop bit gives the
receiving device a little more
breathing space between data
How Fast Should I Go?
Figure 2: Common baud rates for
a 16 MHz crystal.
Of course, it’s very rare to find a
PC with a serial port these days —
the standard communication port is
a USB. The USB standard is quite
different to a straight serial one, so
we need a mechanism to translate
the serial protocol to a USB one.
We’ve already used a USB-to-serial
converter in the first article — an
FTDI breakout board — so we’ll stick
with that in this article. Remember
that the Arduino UNO, for example,
had an additional chip on the board
to handle this translation, so this was
all transparent to you. When using a
USB-to-serial converter, you’ll usually
install a driver that creates a virtual
COM port on your PC. This should
be familiar from the Arduino IDE, as
you needed to select this virtual
COM port when uploading sketches
or using the serial monitor.
58 June 2015