AN432
APPLICATION NOTE
USING ST62xx I/O PORTS SAFELY
by J.Stockinger
INTRODUCTION
All members of the ST62 Series of Microcontrollers from STMicroelectronics feature I/O ports
with configurable bit functions. In addition many I/O bits may be set as inputs to the on-chip
Analog to Digital Converter. This port bit function is in addition to the normal I/O functions of
input (with or without internal pull-up resistor), output (open drain or push-pull) or edge/level
selectable interrupt input (with pull-up). This flexibility makes the ST62 series suitable for
many industrial control applications (and for many other uses).
This application note explains the architecture of the I/O bit associated with these port func-
tions and provides some indications on the correct use of these features for functions such as
keyboard scanning and analog inputs. The correct manner to switch between these function is
also demonstrated in order to prevent potential malfunctions in operation.
Rev. 1.1
AN432/1203
1/12
1
USING ST62xx I/O PORTS SAFELY
1 I/O PORT STRUCTURE
The ST62 I/O Port with Analog Input (hereafter referred to as I/O port) is comprised of 8 iden-
tical bit structures as shown in
Figure 1.
These include the interface to the ST62 internal dat-
abus and the three registers selecting the programmable options. I/O Ports without the analog
capability are identical with the exception of the analog input buffer and control logic.
The control registers are named the Data Register (DR), the Data Direction Register (DDR)
and the Option Register (OR). The DDR and OR are accessed directly at their addresses for
read and write cycles i.e. data is written to the register and can be read from the register. The
OR Register has a different operation and care must be taken when reading and writing to this
register.
Figure 1. ST62XX I/O port structure with A/D input
Note: The control registers are located in the ST62 Dataspace, please refer to the individual device da-
tasheets for the specific addresses for the I/O ports with analog capability.
2/12
2
USING ST62xx I/O PORTS SAFELY
Figure 2. Data register access
Figure 3. Read multiplexing
Table 1. Input and output modes of an I/O cell
DDR
0
0
0
0
1
1
OR
0
0
1
1
0
1
DR
0
1
0
1
DR
DR
I/O pin input/output modes
input with pull-up
input no pull-up
interrupt input with pull-up
analog input
open drain output
push-pull output
For most microcontrollers which use programmable flexibility for I/O pins, it has been
common, to reduce the number of registers, to use the data register address for 3 purposes:
– writing to the DR
– reading the DR content
– reading the state of the I/O pin.
With this convention, the data read from the DR register may come from two different sources,
the DR output and the I/O pin. The source is selected by a multiplexer controlled by the state
of the DDR.
If the DDR bit corresponding to the I/O pin contains a “1", the port pin is set to output. Subse-
quent read accesses to DR will return the content of the DR.
3/12
USING ST62xx I/O PORTS SAFELY
If the DDR bit corresponding to the I/O pin contains a “0", the port pin is set to input. Subse-
quent read accesses to DR will return the state of the I/O pin. This means that the DR bit is ef-
fectively Read Only when the bit is in Output mode. To read the content of any DR bit set to
input mode a copy of the DR content previously written must be saved in Data RAM.
Writing to the DR bit is possible in both input and output modes. It is important to note this as
in input mode, DR is used to set the input pin characteristics (table 1). Due to the flexibility of
programming each I/O bit individually, some pins of a port may be configured to input mode
and others to output mode. Reading the DR will return some DR bit contents (from pins in
output mode) and some I/O pin states (from pins in input mode) combined in the one byte. As
will be shown in a following section this can create serious system malfunctions if care is not
taken.
4/12
USING ST62xx I/O PORTS SAFELY
2 I/O PIN OPTIONS
Each ST62 I/O pin with Analog capability can work as:
– a push-pull output
– an open drain output
– a schmitt trigger input with internal 100K pull-up
– a schmitt trigger input without pull-up
– an interrupt input with schmitt trigger and internal 100K pull-up
– an analog input (connected to the Analog to Digital converter)
As described in the previous section,
– each bit of the I/O port can have different characteristics
– each bit is completely independent of the others
– each bit has its own control bits defining its operation mode
– the bit operation modes can be changed by software at any time
Figure 4. I/O port with pins in different functions
An example of a potential I/O port configuration is shown in
Figure 4.
Pin 0 is in push pull
output mode, outputting a high level, pin 1 is in open drain output mode and presents a high
impedance (this is because DR is 1, causing the pull-down resistor to be turned off). Pin 2 is
in input mode with pull-up, pin 3 is in input mode without pull-up, while pins 4 and 5 are inter-
rupt inputs, pin 6 is analog input and pin 7 is an input without pull-up.
Only one pin of an ST62XX should be switched to analog input mode at any time. Otherwise,
all pins switched to analog input mode will be connected together via the analog switches
(RDSON = 1/4 kΩ)
5/12