Global Sources
EE Times-India
Stay in touch with EE Times India
 
EE Times-India > Processors/DSPs
 
 
Processors/DSPs  

STR71x UART communication with a PC using RS232

Posted: 08 Dec 2004     Print Version  Bookmark and Share

Keywords:STR71x UART 

COMMUNICATION WITH A PC USING RS232

INTRODUCTION

This document presents a standard communications interface between a STR71x microcon-

troller and a PC. This communication is done through the STR71x UART peripheral and a se-

rial port of the PC using the RS 232 protocol.

1

/PDF document

AN1797/0104 1/9

Rev. 1.0

AN1797

APPLICATION NOTE

STR71x UART

COMMUNICATION WITH A PC USING RS232

INTRODUCTION

This document presents a standard communications interface between a STR71x microcon-

troller and a PC. This communication is done through the STR71x UART peripheral and a se-

rial port of the PC using the RS 232 protocol.

1

2/9

STR71x UART COMMUNICATION WITH A PC USING RS232

1 SERIAL COMMUNICATION

The main features of the serial communication are summarized below.

1.1 MAIN FEATURES

The serial interface described here, allows two digital systems to communicate using only a

few wires. It is asynchronous, so it doesn't need a clock.

The serial interface requires at least three wires:

- a ground pin

- a transmitter wire (Tx)

- a receiver wire (Rx)

The interface is full-duplex, i.e. it can transmit and receive at the same time.

Figure 1. STR71x and UART interface Setup

1.2 BAUD RATES

Transmission and reception are driven by a baud rate generator. However, be aware that to

communicate correctly, the receiver must have exactly the same baud rate as the transmitter

baud rate, otherwise the communication will be corrupted. If this condition is met, a wide range

of baudrates is possible.

1.3 FRAMES

Communication frames begin with a start bit and end with a stop bit. Stop bits can be 0.5, 1,

1.5 or 2 bits long. Frame words can be 8 or 9 bits long. The least significant bit is sent first (see

Figure 2). In 8-bit word mode, the eighth bit can be a data bit or a parity bit, but in 9-bit word

mode, the ninth bit is a parity bit. Parity can be odd or even. Receiver and transmitter devices

must have the same word length, the same parity and the same number of stop bits.

UART Tx

Rx

Tx

Rx

STR71x DEVICE

2

3/9

STR71x UART COMMUNICATION WITH A PC USING RS232

Figure 2. Frames and word length

Bit0

Start

Bit Bit2Bit1 Bit3 Bit5Bit4 Bit7Bit6

Stop

Bit

Next

start

Bit

Next data frame

Bit0

Start

Bit Bit2Bit1 Bit3 Bit5Bit4 Bit8Bit6

Stop

Bit

Next

start

Bit

Next data frame

Bit7

8 bit word length

9 bit word length

4/9

STR71x UART COMMUNICATION WITH A PC USING RS232

2 RS232 COMMUNICATION WITH A PC

2.1 MAIN FEATURES

The electrical and protocol characteristics of the STR71x UART peripheral are different from

RS232. In RS232 communication, high level is typically +12 V and low level is typically -12 V,

while the UART peripheral works with TTL level (0, +5V).

The adaptation is done using a line adapter circuit (ex: MAX232), which transforms the logic

levels output by the digital system into RS232-compatible levels and vice versa.

2.2 PC CONFIGURATION

In this example, the PC will act as a terminal. The description below refers to the Windows en-

vironment. The Windows terminal application is called hyperterminal.

Under Windows, open the +hyperterminal; application. To configure it, go to the communica-

tion parameters menu. The options of you set in this menu must be the same as the ones de-

fined in your STR71x.

After selecting the right serial communication port, select the same baud rate as the one con-

figured in the microcontroller. Data word length will be 8 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.

2.3 GENERAL INITIALIZATION

The external clock works at 16 MHz.

In the STR71x, four UARTs are available. In this application only UART0 is used. Two pins of

the STR71x are used:

- the TX0 pin (Transmit Data Output, alternate function of P0.9).

- the RX0 pin (Receive Data Input, alternate function of P0.8).

The P0.8 and P0.9 pins must be initialized as alternate function (refer to the STR71x Refer-

ence Manual), the value of the Port 0 Data Register does not matter.

2.4 UART CONFIGURATION

The peripheral is configured using the UART Control Registers (CR), and the Baud Rate Reg-

ister (BRR).

As in this application a polling strategy is used, UART interrupts are disabled. Also FIFOs and

loop back mode are disabled.

5/9

STR71x UART COMMUNICATION WITH A PC USING RS232

In the application, data length can be 8 bits or 9 bits (parity bit included). Parity must be odd or

even parity if you select 7 bit mode or 8 bit mode with parity is selected. Select No Parity if 8

bit mode is selected, stop bits can be 1, 1.5 or 2 stop bits.

2.4.1 Baud rate selection

Then you must select the baud rates. We should always have the same baud rates in the PC

and in the UART.

The baud rates are calculated as follows:

Baudrate = fPCLK1 / (16*BRR), BRR = fPCLK1 / (16*Baudrate)

For example, in our application, we will use 9600 as baud rate, and the fPCLK1 is 8 MHz.

6/9

STR71x UART COMMUNICATION WITH A PC USING RS232

3 SERIAL COMMUNICATION BETWEEN STR71x AND PC VIA RS232

The software for this application is the UART driver. The complete software can be found in

the ST internet website. It is of course only an example. It is up to you to adapt it to your spe-

cific application.

3.1 GENERAL DESCRIPTION

In this application, an STR71x is connected to a PC. The communication is performed using

the Windows +hyperterminal; application.

Characters are sent by the PC in ASCII code to the STR71x. Each time 4 characters are re-

ceived by the STR71x, they are sent back to the PC and can be read on its screen.

The software is a polling serial communication between an STR71x and a PC via RS232 using

the UART0. The communication works at 9600bps and has 1 stop bit.

The first part of the program performs the initialization of the STR71x, core and UART periph-

eral. Then reception mode is disabled and the program sends the content of a 4-byte buffer to

the PC. The initialized value of the buffer is +test; in ASCII code. When the 4 characters are

sent, transmission mode is ended and the reception is enabled. In reception mode, the soft-

ware waits for the user to send 4 ASCII characters from the PC. Each character is put in the

buffer until the fourth is received. Then it goes back to transmission mode which will send the

new contents of the buffer and so on.

As the STR71x sends the same characters as received, it is very easy to check if the commu-

nication is correct. Note that this program does not manage communication errors.

7/9

STR71x UART COMMUNICATION WITH A PC USING RS232

3.2 4.2 HARDWARE

The STR71x UART peripheral cannot be directly connected to a PC, as it uses the RS232 pro-

tocol. The conversion between UART and RS232 can be done using a MAX232. An overview

schematic is presented below (Figure 3).

Figure 3. Hardware Overview

Be sure that the three main devices (PC, STR71x, MAX232) have the same electrical refer-

ence (GND).

The Receive Data pin (RD) of the serial port of the PC must correspond to the TX pin of the

STR71x, and the Transmit Data pin (TD) to the RX pin.

HOST PC

STR71x

GND

TX

RX

GND

OUT

IN

IN

OUT

GROUND

DB9 connector

RS232 cable MAX232

STR71x BOARD

8/9

STR71x UART COMMUNICATION WITH A PC USING RS232

3.3 FLOWCHARTS

The flowcharts of the application are presented below.

Figure 4. Transmission mode, Reception mode

3.4 SOFTWARE

All the source files in C code are given in the zip file with this application note.

The source files are for guidance only. STMicroelectronics shall not be held liable for any di-

rect, indirect or consequential damages with respect to any claims arising from use of this soft-

ware.

BEGIN

STR71x

initialization

UART0

initialization

NB = 0

send the byte

(NB) from buffer

byte sentNO

YES

NB = NB +1

NB = 4NO

YES

new byte

received

NO

YES

put new byte into buffer

at address (NB)

NB = 4NO

YES

NB = 0

enable the UART

0 reception

NB = NB +1

disable the UART

0 reception

9/9

STR71x UART COMMUNICATION WITH A PC USING RS232

"THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION

REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS

SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO

ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF

THE INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS."

Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences

of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted

by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject

to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not

authorized for use as critical components in life support devices or systems without express written approval of STMicroelectronics.

The ST logo is a registered trademark of STMicroelectronics.

All other names are the property of their respective owners

) 2004 STMicroelectronics - All rights reserved

STMicroelectronics GROUP OF COMPANIES

Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan -

Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States

www.st.com





Comment on "STR71x UART communication with a PC ..."
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top