Applied Robotics/Microcontrollers/Serial Communication

Serial Communication Basics
Serial communication is a means of sending data between devices one bit at a time. This method only requires a few wires and I/O pins per device to facilitate communication. Typically, most serial devices send and receive byte-oriented data (8-bits per transaction), and have a dedicated transmit and receive line (full-duplex). Serial communication is typically handled by specialized hardware peripherals known as USARTs (Universal Synchronous-Asynchronous Receiver-Transmitters) that handle low level formatting, shifting, and recovery of data.

Most ATmega series AVR microcontrollers have 1-2 USARTs available. Some modern microcontrollers have 5-7 separate USARTs.

Serial Frame Structure
Serial transactions are broken up into what are called frames. Each frame consists of the payload data byte being sent along with a set number of bits that "frame" or format the data. Most USARTs, including the AVR USARTs, are compatible with a frame structure such as this one.

The serial line idles at a logic high level until the transmission starts. Each transmission is started with a single logic-low bit known as a start bit. After the start bit, the payload data is shifted out, typically with the least-significant bit first. After the last data bit is shifted out, an optional parity bit is sent. The parity bit takes on a value of 0 or 1 depending on the number of logic 1 bits in the data and can be used by the receiver to detect an erroneous transmissions in which one of the bits are flipped. Finally, the serial line returns to logic high for a configurable period of 1 or 2 bit-times; these are known as stop bits. AVR USARTs are capable of sending frames with 5-9 data bits, with even, odd, or no parity, and 1 or 2 stop bits. In the frame above, a logic-high signal corresponds to a '0' while a logic-low signal corresponds to a '1'.

Synchronous vs. Asynchronous Serial
Serial communication with a USART can be done both synchronously (data with a dedicated clock signal) or asynchronously (data without a dedicated clock signal). Using a synchronous serial line requires that extra wiring is included for the clock signals, and is often times unnecessary at typical serial data rates. Typically, most standard serial communication, especially between a PC and a microcontroller, is done asynchronously.

Baud Rate
The baud rate specifies the frequency at which bits are sent. The baud rate can be calculated by dividing 1 over the bit time in seconds. Setting the baud rate correctly for asynchronous serial is very important as the receiver must recover a clock signal from the incoming data based on a predetermined clock frequency. Common baud rates available on a PC are 1.2k, 2.4k, 4.8k, 9.6k, 19.2k, 38.4k, 57.6k, 115.2k, 230.4k, 460.8k, and 921.6k.

Logic Levels
There are many different standard serial logic levels used. Most PC interfaces use RS-232 serial, that specifies logic levels between -15V and -3V and +3 to +15V with idle-low lines. AVR USARTs use CMOS logic levels between 0-5V and have idle-high serial lines that are typically compatible with standard 3.3V or 5V logic outputs. The appropriate serial logic levels must be used between devices, and for that purpose, special level shifters may be required.

Texas Instruments manufactures RS-232 to CMOS/TTL level shifters for RS-232 use. These devices have a built in charge pump for providing the RS-232 levels and require only a few external components. An example of these chips is the MAX232 http://www.ti.com/product/max232.

Setting up a USART on an ATmega Microcontroller
The following example shows how to set up a serial port on an ATmega128 using USART0 as an asynchronous serial device operating at 115200 baud. The ATmega128 is using a 16 MHz clock in this example. The program should initialize USART0, and send out the byte 0xAA on its transmitter. It will continue to check its receiver for new data and will set PB0 high if it receives the 0xAA byte. This allows for a quick loopback test and demonstrates basic USART concepts.

USB - Serial Bridges
Typically most modern PCs do not have a dedicated RS-232 serial port available, so an alternative must be used. Many vendors sell both RS-232 and TTL or LVTTL level adapters for a low cost. Many of these devices typically require drivers, so follow all manufacturer setup instructions.

Plugable USB to RS-232 Adapter

FTDI UMFT230XB USB to 3.3V Serial Adapter

FTDI TTL-232R-5V-PCB USB to 5V (or 3.3V) Serial Adapter

On Windows machines
Serial adapters typically enumerate as a COMx port. Check your device manager to determine the COM port number.

On Linux machines
Serial adapters typically enumerate as ttyUSBx devices. Check your /dev/ directory for ttyUSB devices to determine the tty device number.

RS-232 Pinouts
The following pinouts are commonly found standard RS-232 DE-9 connectors. The male DTE pinout corresponds to the pins on the PC or serial adapter side. The female DCE pinout corresponds to the end device, such as a microcontroller board with an RS-232 port. RS-232 also specifies a number of other flow control lines that were commonly used in old serial modems, however for most cases, just the TxD/RxD and ground lines are needed.

Common Errors
Common errors include improperly wiring the DCE side connector Tx/Rx pins or accidentally using an RS-232 crossover cable rather than a straight cable. It's strongly advised to check these factors if your RS-232 communication does not work.

Serial Terminal Software
A variety of serial terminal programs exist that allow for quick sending and receiving of serial data. These are useful for debugging and testingrial devices.

Windows Serial Programs
Realterm - A very capable GUI based serial terminal. - http://realterm.sourceforge.net/ Linux Serial Programs: Minicom: A very popular terminal based serial interface program - Debian package name "minicom" Cutecom: A GUI based serial terminal program - Debian package name "cutecom"

Python Serial Module (PySerial)
An easy cross platform library for PC serial communication is PySerial. Python version 2.7 is recommended over Python 3.x. Pyserial allows for easy serial port management and manages sending and receiving data over a COM or tty port in Windows or Linux respectively. Pyserial and the accompanying documentation can be found at - http://pyserial.sourceforge.net/