AN1753
Application note
Software UART using ST7LITE0 12-bit autoreload timer
Introduction
This application note describes a software implementation of a Universal Asynchronous
Receiver/Transmitter (UART). This can be used on devices with no on-chip SCI peripheral.
In this example, a software UART is implemented for the ST7LITE0, using the 12-bit
Autoreload timer and two I/O ports for asynchronous receive and transmit. The UART
software provides the following features:
•
Half-duplex operation
•
Asynchronous operation
•
Flexible data formats (7 or 8 data bits, 1 or 2 stop bits)
•
Baud rate: 2400 to 19200 baud
To test this interrupt-driven software UART, you can use the “Hyperterminal” application
running on a Windows PC.
The program code is quite small (357 bytes) and can easily be adapted to specific
application requirements.
Table 1
lists the microcontrollers and software concerned by this application note.
Table 1. Applicable products and software
Type
Microcontrollers
Software
Part numbers or product categories
ST7LITE02Y0, ST7LITE05Y0, ST7LITE09Y0
STSW-ST7094
March 2013
DocID9986 Rev 2
1/22
www.st.com
Contents
AN1753
Contents
1
UART communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1
1.2
1.3
Main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Baud rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2
RS232 communication with a PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1
2.2
Main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
PC configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3
ST7FLITE0 configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1
3.2
3.3
Clock source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Input initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Auto-reload timer register configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4
UART implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.1
4.2
4.3
4.4
Baud rate definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Majority-voting system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Status handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Transmit and receive implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5
6
Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Functional software flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.1
6.2
Main software routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Software flow charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7
8
9
Test procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2/22
DocID9986 Rev 2
AN1753
List of figures
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
ST7 UART communication set-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Majority-voting system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Test Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Main flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Output compare interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Transmit routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Receive data routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Output compare transmit flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Output compare receive flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
EI1 interrupt routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
DocID9986 Rev 2
3/22
UART communication
AN1753
1
UART communication
The main features of a standard UART are summarized below.
1.1
Main features
The UART offers a flexible means of full-duplex data exchange with external equipment
requiring an industry standard NRZ asynchronous serial data format.
The UART allows a very wide range of baud rates and different baud rates for transmission
and reception.
In UART communication, a minimum of only two signals are needed, one for transmission
and one for reception. No clock signal is needed as it works in asynchronous mode. Each
device has to have a Transmit Data Output pin (the PA3 pin is used in our example for the
ST7FLITE0) and a Receive Data Input pin (PA7 pin in our example). (Refer to
Figure 1.)
Figure 1. ST7 UART communication set-up
ST7 system
(host)
ST7FLITE0
(TDO) PA3
(RDI) PA7
RDI
TDO
Terminal
device
You must be very careful to identify the use of each pin. A simple method is to put the device
in transmission and check with an oscilloscope if a transmission frame is present or not.
1.2
Baud rate
Transmission and reception can be driven by their own baud rate generator. However, be
aware that, for a correct communication, the receiver must have a reception baud rate
strictly equal to the transmission baud rate of the transmitter. If not, the communication will
be corrupted. As long as this condition is met, a wide range of baud rates is possible.
1.3
Frame
Any transmission is Least Significant Bit first. A data word is usually 8 bits long. A data
frame begins with a «start bit», which is a ‘0‘ bit and ends with a stop bit, which is a ‘1‘ bit
(see
Figure 2).
4/22
DocID9986 Rev 2
AN1753
Figure 2. Frames
8-bit Word length
Data Frame
Start
Bit
Bit0
Bit1
Bit2
Bit3
Bit4
Bit5
Bit6
Stop
Bit7 Bit
UART communication
Next Data Frame
Next
Start
Bit
In some cases, a 9th bit can be used, as a parity bit or as a second stop bit.
DocID9986 Rev 2
5/22