Yup, this beast is like most peripherals. It can do so
many different things that the real hard part is
figuring out the limited subset of things that you
want it to do. We won’t be using 90% of what you
see in the datasheet, but the trick — as usual — is
figuring out which 10% we want to use. There are
things that it can do that I have never seen done in
any system I’ve used — like allowing five, six, seven,
eight, or nine data bits. I’ve only ever used eight bits
and have only seen seven and nine on other systems
(never five or six). Guess somebody somewhere in
some historical context used these so they are there
if you need them.
To simplify things, let’s keep this as simple as
possible by using only the most common modem
parameters: eight data bits, no parity, one stop bit,
and no handshaking. Yes, so the datasheet is hard.
So let’s try to simplify it a bit.
Set Up the USART
■ FIGURE 6. WAC teletype operators during World War II.
telephone line which ‘rings’ when a call comes in; that a
PC keyboard has a ‘Ctrl’ (control) key because it evolved
from Baudot’s original keyboard; and that any other weird
term you come across probably has a historic reason for
being used. Just as a fun factoid of interest, the Arduino
uses the DTR pin of the modem to reset the AVR so that
you can use the bootloader to upload applications.
So, what does this have to
do with USART?
Early in the development of digital systems, all the
above arcania was put into a single IC peripheral that
came to be known as a UART: Universal Asynchronous
Receiver Transimitter. Like many useful peripheral ICs
(memory, ADC, timers, etc.) the UART was soon put on
the same silicon as a microprocessor, like the AVR core.
The old-fashioned UART only did asynchronous (not
occurring at predetermined or regular intervals – no clock)
communication, but Atmel added some features that let
theirs do some synchronous (does occur at predetermined
regular intervals – clocked) things. They also added an S
to the name that has confused folks mightily. We won’t be
using the S other than in the name. So, just get used to
the fact that when we say USART we are actually dealing
with the venerable UART. However, since this is an Atmel
peripheral, we’ll use their name for it (grudgingly). Clear? I
didn’t think so.
Using Aliases for Comprehension and Portability
We have already discussed a bit about using
aliases for register and bit acronyms in earlier
You are likely to see both of these in some AVR
source code, so you might expect that you are supposed
to know what they mean. The UCSR0C is the USART
Control and Status Register C for USART0, and the
UPM01 is USART parity mode bit 1 for USART0. Now
don’t you feel like a real dummy for not knowing that?
Well, I don’t and personally I think folks who use the raw
acronyms in their code are foolish. Wouldn’t it be better if
they had used an alias in the first place that spelled it out?
For example say they used:
AVR USART Hardware
70 August 2011
I suggest that you get the Atmel ATmega328
datasheet and take a look at the section on the USART.
Then, when they look at their code and see
USART_PARITY_MODE_BIT_1 they will have a much
better chance of knowing what is going on without having
to dig out the datasheet to find the acronyms. The
compiler doesn’t care because it doesn’t see the long
name anyway since the preprocessor substitutes the
acronym. Of course, you do have the bother of all the
extra typing and I do hate typing the ‘_’ character, but I’ve
found in the long run doing things this way is just adding
more documentation for your code. But that is generally a
good thing — especially for educational code — so that’s
what we will do.