Building a parallel interface::
Itshouldbesimpletocreateasetofoutputpinstocreatean‘output’port(Figure3.1).Letus
assign an address in the memory map to the port. Whenever that address is selected by an
instructionintheprogram,itactivatesalinecalled‘PortSelect’.Afurtherline,‘Read/Write’,
indicates whether the CPU is undertaking a Read (line is high) or Write (line is low) oper-
ation. This is gated with the Port Select line. Each line of the data bus is connected to
abistable,andalloftheseareclockedbythePortSelectline.Thenthevalueofthedatabusis
latchedintothebistablewhenevertheportmemorylocationisaddressed,inWritemode.The
outputs of the bistables are made available for connection to the outside world.
It is equally simple to create a set of input pins (Figure 3.2). All that is needed is a tristate
buffer gate connected between an external pin and a line of the data bus. When the buffer is
enabled, again by a logical combination of Port Select line and Read/Write control, the
logic value of the external pin is briefly connected to the data bus line, and can be read
by the CPU. Note that in this design the external data is not latched by the port; it must be
held at a stable value by the external source.
These ideas are quite attractive, but the reality is that it is inflexible to limit an external pin of
an IC to just one function, whether input or output. It would be much neater to combine
somehowthetwocircuitsusedforinputandoutput,andlettheuserdecideinwhichdirection
he/she wants the data to move. The diagram of Figure 3.3 does just that. It shows
apossible‘pindriver’circuitforonebitofaparallelport.Itiseasytopickoutinitthecircuits
of Figures 3.1 and 3.2. What must be added, however, is a further flip-flop (‘Direction’),
which is set to determinewhether this microcontroller pin is to act as an input or output. The
state of this flip-flop is set by the program. It controls the ‘Output buffer’, which is enabled
when the port bit is in output mode.
This circuit forms the basis for a very useful bi-directional input/output pin driver, and it is
easy to find versions of it in many popular microcontrollers. Sets of I/O pins are grouped
together to form a parallel I/O port. Each ‘Data’ flip-flop then forms one bit of a ‘Data’ SFR
(Special Function register), and each ‘Direction’ flip-flop forms one bit of a ‘Direction’ SFR,
asseeninFigure3.3.EachSFRismemorymapped,withitsownuniqueaddress.Derivedfrom
that address is its select line, which goes high when that location is addressed. ‘Port Select’
selects the Data SFR and ‘Direction Select’selects the Direction SFR.
By writing to the Direction SFR the user can determine which bits are to be input and which
are to be output. By writing to the Data SFR he/she can set the value of all Data flip-flops,
whether that pin is actually set as an output or not. This value is transferred to the I/O pin
throughthebufferforthosepinswhichareenabledasoutputs.ByreadingfromtheDataSFR
theprogramcanacquirethelogicalvalueoftheI/Opin.Ifthepinissetasoutput,thisvalueis
simply the value held by the Data flip-flop and asserted on the I/O pin through the Output
buffer. If the pin is set as an input, then an external signal should be connected to the pin, and
the controller will read its value.