Interacting with Your Project
You may have noticed that your microcontroller didn’t
come with a keyboard or a screen. As embedded system
developers, we need to get creative in how we interact
with our projects. In last month’s article, we touched on
digital and analog inputs, which require some thrifty
design if you plan to use them as a way to handle
complex interactions with your project. A menu displayed
over the serial port can be a great way to interact with
your project — whether you choose it as the only way, or
in combination with buttons, LEDs, and LCDs.
My first serious microcontroller-based project was the
irrigation controller I mentioned in the first article. While I
was developing it, I wanted to get the “core” functionality
working without spending time on the interface, so I
coded a simple serial menu. On startup, a list of
configuration options were sent over the serial port and
displayed in my terminal software. By pressing certain keys
on my PC keyboard (remember serial is a two-way
communication), I was able to select menu options, enter
irrigation schedules, set the time, and control various other
options. I eventually replaced most of the serial menu
functionality with an LCD, a couple of pushbuttons, and a
rotary encoder, but I left a few “behind-the-scenes”
configuration options on the serial menu in case I ever
needed them in the future.
I’m pretty sure that you want to get a serial
connection going as quickly as possible, so may not find
the thought of digging into theory very appealing. I hear
you, but I think that it’s useful to look very briefly at how
the UART transfers data.
The ATmega328 has a built-in device called a USART
(Universal Synchronous and Asynchronous
Receiver/Transmitter) that handles all the complexity of
the serial protocol — thankfully! The USART peripheral can
actually run in a number of modes to handle synchronous,
asynchronous, and SPI communication. In this article,
we’re focusing on asynchronous serial communication,
which is the mode that your computer operates in.
Without getting into the complexities, the very basic
difference between synchronous and asynchronous serial
communication is that the synchronous mode doesn’t
only send data — it also sends a clock signal to time
(synchronize) the data. The asynchronous mode — without
having a clock signal to regulate the timing of the data
transmission — needs to send the data at an agreed speed.
This is called a baud rate. We’re getting ahead of
ourselves, though, so let’s back-track a bit.
Connect Me Up (Scotty)
The UART uses two pins to transfer data: one for
incoming data (RXD, or receive); and one for outgoing
data (TXD, or transmit). On the ATmega328P, these are
pins 2 and 3, respectively. When you connect two
devices, remember that the transmit of each must be
connected to the receive of the other — a very common
error is to connect the two TXD pins and the two RXD
pins. It’s no good having two ears listening to each other,
and two mouths talking at each other!
Additionally, the devices communicating need to
share a common ground. In other words, the GND from
both devices needs to be connected. This, simplistically,
provides a common reference point for the voltages to be
measured off. A high voltage is high relative to ground,
and a low voltage is low relative to ground.
Framing Your Data
In order to send data over a UART, you need to frame
By Andrew Retallack
June 2015 57
Post comments on this article and find any associated files and/or downloads
In order for your PC to be able to communicate with your
microcontroller over the (virtual) serial port, you’ll need to use
terminal software. This name comes from the old days of dumb
terminals that were effectively a screen and a keyboard into another
computer. The Arduino IDE comes with simple terminal functionality,
which they call the serial monitor. The serial monitor — as you’ve
probably found — gives you pretty much all you need to
communicate with your MCU.
Atmel Studio doesn’t include a terminal application out-the-box,
but you can install one as an extension (there are a number of useful
add-ons that we can use to extend Atmel Studio’s functionality). To
install the “Terminal for Atmel Studio“ (as it’s called), open the
Extension Manager (Figure A) by choosing it from the Tools menu.
You can either browse or search for the Terminal for Atmel Studio.
Once you’ve found it, highlight and then click Download. You’ll need
to sign into Atmel’s community called “myAtmel” before completing
the download and installation process. The terminal window should
be accessible under the View menu.
If you prefer not to work in a terminal window within Atmel
Studio (and I prefer not to), then any other terminal software should
do the trick. I usually use either an open source application called
“Tera Term” or Pu TTY (refer to the download links in Resources).
Figure A: Atmel Studio's Extension Manager
contains some useful add-ons.