ST AN1753 Application note

AN1753

APPLICATION NOTE

SOFTWARE UART USING

ST7 12-BIT AUTORELOAD TIMER

by Microcontroller Division Applications

INTRODUCTION

This application note describes a software implementation of a Universal Asynchronous Receiver/Transmitter (UART). This can be used on devices, like the ST7LITE0, with no on-chip SCI peripheral.

In this example, a software UART is implemented for the ST7FLITE0, 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)

Baudrate: 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.

Rev. 1.0

AN1753/1103

1/22

Table of Contents

INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 UART COMMUNICATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 MAIN FEATURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 BAUD RATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 FRAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 RS232 COMMUNICATION WITH A PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.1 MAIN FEATURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2 PC CONFIGURATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3 ST7FLITE0 CONFIGURATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1 CLOCK SOURCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.2 INPUT INITIALIZATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.3 AUTO-RELOAD TIMER REGISTER CONFIGURATION . . . . . . . . . . . . . . . . . . 6 4 UART IMPLEMENTATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.1 BAUD RATE DEFINITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.2 MAJORITY VOTING SYSTEM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.3 STATUS HANDLING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.4 TRANSMIT & RECEIVE IMPLEMENTATION . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5 HARDWARE SETUP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 6 FUNCTIONAL SOFTWARE FLOW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 6.1 MAIN SOFTWARE ROUTINES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 6.2 SOFTWARE FLOW CHARTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 7 TEST PROCEDURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

8 SOFTWARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2/22

ST AN1753 Application note

SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER

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 other 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

Terminal

(Host)

Device

 

ST7FLITE0

 

(TDO) PA3

RDI

(RDI) PA7

TDO

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 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.

3/22

SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER

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.

Figure 2. Frames

8-bit Word length

 

 

 

Data Frame

 

 

 

Next Data Frame

 

Start

 

 

 

 

Next

 

 

 

 

 

 

 

 

 

 

 

Bit0

Bit1

Bit2

Bit3

Bit4

Bit5

Bit6

Bit7

Stop

Start

 

 

 

Bit

Bit

Bit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In some cases, a 9th bit can be used, as a parity bit or as a second stop bit.

4/22

SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER

2 RS232 COMMUNICATION WITH A PC

2.1 MAIN FEATURES

Electrical and protocol characteristics of RS232 are different from those used by the UART. In RS232 communication, high level is typically +7V and low level is typically -7V, while the ST7 I/Os work at CMOS levels (0, +5V).

Furthermore, the polarities are different. A ‘1‘ bit coming from the UART corresponds to a ‘0‘ bit

in RS232, and a ‘0‘ bit to a ‘1‘ bit. It is true for all bits including the START and STOP bits. So it is necessary to implement a conversion between the PC and the ST7. In the application, a MAX232 is used for this purpose.

2.2 PC CONFIGURATION

The PC will be used as a terminal interface. “Hyperteminal”, the terminal application software

is used as a interfacing software to test the functionality [.zip file attached]. The test environment is Windows 2000.

Under Windows, open the Hyperterminal application. To configure it, go to the port settings and set the parameters to your application requirements. The options in this window must be the same as the ones defined for your ST7FLITE0, communication device, except the port.

After selecting the right serial communication port, select the same baud rate as the one set for the ST7. As the PC accepts only one baud rate, transmission and reception baud rates will

have the same value. Data word can be 8/7 bits, but you can choose to use 1 or 2 stop bits. «Flow control» can be either Xon/Xoff or none. The PC is then correctly configured.

5/22

SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER

3 ST7FLITE0 CONFIGURATION

3.1 CLOCK SOURCE

This application is implemented using ST7FLITE0 device with an 8 MHz internal clock. PLL * 8 is used to generate this 8 MHz clock.

3.2 INPUT INITIALIZATION

Two pins of the ST7FLITE0 are used:

-PA3: Pin of PortA

-PA7: Pin of PortA with interrupt

Pin PA3 is normal Input/Output port pin with no alternate function, used for transmission.

During initialization it is configured as an output.

Pin PA7 is normal Input/Output port pin with no alternate function, used for data receive.

During initialization it is configured as an input. While in receive mode, at start, same pin is used with interrupt enabled (“ei1”) to sense “START” bit. So this pin is configured with “pull up

interrupt input” by setting PADDR to 0 & PAOR to 1. And to set interrupt sensitivity “Falling edge only” we set IS11=1 & IS10=0 in the EICR register.

Refer to the device datasheet for detailed description of the I/O and interrupt control registers.

3.3 AUTO-RELOAD TIMER REGISTER CONFIGURATION

The AT timer is based on free running 12-bit up counter with 12-bit auto reload register (ATR). Apart from this it also includes other functionality like, PWM signal generator & Output Compare Function & etc.

We are using “Output Compare” functionality for this application. To use this function, the OE bit must be 0, otherwise the compare is done with the shadow register instead of the DCRx register. Software must then write a 12-bit value in the DCR0H and DCR0L registers. When the 12-bit upcounter (CNTR) reaches the value stored in the DCR0H and DCR0L registers, the CMPF0 bit in the PWM0CSR register is set and an interrupt request is generated if the CMPIE bit is set.

6/22

SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER

The registers that are used in application note are:

TIMER CONTROL STATUS REGISTER (ATCSR):

0

0

0

CK1

CK0

OVF

OVFIE

CMPIE

 

 

 

 

 

 

 

 

- CK1 & CK0: They select the clock frequency of the counter. For fcounter = fcpu, set CK1=1 & CK0=0,

- CMPIE: It allows to mask the interrupt generation when CMPF bit is set. When it is 0: CMPF interrupt disabled

1: CMPF interrupt enabled

PWM0 CONTROL/STATUS REGISTER (PWM0CSR):

0

0

0

0

0

0

OP0

CMPF0

 

 

 

 

 

 

 

 

- CMPF0:

It indicates that the upcounter value matches the DCR register value. When it is,

0:Upcounter value does not match DCR value.

1:Upcounter value matches DCR value.

PWM OUTPUT CONTROL REGISTER (PWMCR):

0

0

0

0

0

0

0

OE0

 

 

 

 

 

 

 

 

- OE0: When set to 1, PWM0 output enabled

Timer Initialization:

Write to DCRx registers with required value. Then Output Compare functionality must be enabled. To do this, reset the OE bit in the PWMCR register. This will disable PWM output.

To configure fcounter = fcpu, set CK1=1 & CK0=0, and to enable the CMPF interrupt, set CMPIE=1 in the ATCSR register.

7/22

Loading...
+ 15 hidden pages