ST AN1753 Application note

AN1753
APPLICATION NOTE
SOFTWARE UART USING
ST7 12-BIT AUTORELOAD TIMER
by Microcontroller Division Applications
INTRODUCTION
In this example, a software UART is implemented for the ST7FLITE0, using the 12-bit Autore­load timer and two I/O ports for asynchronous receive and transmit. The UART software pro­vides 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 run-
ning 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
1
Table of Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1 UART COMMUNICATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 MAIN FEA TU RES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 BAUD RATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 FRAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 RS232 COMMUNICATION WITH A PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 MAIN FEA TU RES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 PC CONFIGURATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 ST7FLITE0 CON FIGURATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1 CLOCK SOURCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 INPUT INITIALIZATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 AUTO-RELOAD TIMER REGISTER CONFIGURATION . . . . . . . . . . . . . . . . . . 6
4 UART IMPLEM ENTATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1 BAUD RATE DEFINITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2 MAJORITY VOTING SYSTEM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 STATUS HANDLING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.4 TRANSM IT & RECEIVE IMPLEMENTATION . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 HARDWARE SETUP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 FUNCTIONAL SOFTWARE FLOW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.1 MAIN SOFT W ARE ROUTINES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.2 SOFTWAR E FLOW CHARTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7 TEST PROCEDURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8 SOFTWARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2/22
1
22
SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER
1 UART COMM UNICATION
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 r ates 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 hav e a Transm it Data Ou tput pin (the PA3 pin is used in o ur 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 Device
(Host)
ST7FLITE0
(TDO) PA3
(RDI) PA7
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 r eception 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 cor-
rupted. A s long as this condition is met, a wide range of baud rates is possible.
RDI
TDO
3/22
2
SOFTWARE UART USING ST7 12-BIT AUTORELOAD TIMER
1.3 FRAME
Any transmission is Least Significant Bit first. A data word is us ually 8 bi ts long. A data frame begins with a «star t bit», which i s a ‘0‘ bit and ends wit h a «stop bit», wh ich is a ‘1‘ bit. See Figure 2.
Figure 2. Frames
8-bit Wo rd l ength
Next Data Frame
Data Frame
Start
Bit
Bit0 Bit1
Bit2
Bit3 Bit4
Bit5 Bit6
Bit7
Stop
Bit
Next Start
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 R S232 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 CMO S levels (0, +5V).
Furthermore, the polarities are different. A ‘1‘ bit coming from the UART corr esponds 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 appl ication, 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 environ-
ment is Windows 2000. Under Windows , open the Hy pertermin al applicatio n. To configur e it, go to the port s ettings
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 commun ication port, select the same baud rate as t he 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 y ou can choos e to use 1 or 2 s top 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 INP UT 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 transm ission. 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 s ignal generator & Output Com­pare 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. W hen the 12-bit upcount er (CNTR) re aches the value s tored in th e DCR0H and DCR0L reg isters, the CMPF 0 bit in th e PWM0 CSR register is set and an interrup t req uest is gen erated 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 STAT US RE GISTER (ATCSR):
CMPIEOVFIEOVFCK0CK1000
- 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):
0000 OP0
0
CMPF00
- 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):
000
0
0
00
OE0
- OE0: When set to 1, PWM0 output enabled
Timer Initialization:
Write to DCRx registers with required value. Then Output Compare functionality must be en­abled. 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