IO Port Configuration 8051
IO Port Configuration 8051
Each port of 8051 has bidirectional capability. Port 0 is called 'true bidirectional port' as it
floats (tristated) when configured as input. Port-1, 2, 3 are called 'quasi bidirectional port'.
Port-0 Pin Structure
Port-0 can be configured as a normal bidirectional I/O port or it can be used for address/data
interfacing for accessing external memory. When control is '1', the port is used for
address/data interfacing. When the control is '0', the port can be used as a normal
bidirectional I/O port.
Let us assume that control is '0'. When the port is used as an input port, '1' is written to the
latch. In this situation both the output MOSFETs are 'off'. Hence the output pin floats. This
high impedance pin can be pulled up or low by an external source. When the port is used as
an output port, a '1' written to the latch again turns 'off' both the output MOSFETs and causes
the output pin to float. An external pull-up is required to output a '1'. But when '0' is written
to the latch, the pin is pulled down by the lower MOSFET. Hence the output becomes zero.
When the control is '1', address/data bus controls the output driver MOSFETs. If the
address/data bus (internal) is '0', the upper MOSFET is 'off' and the lower MOSFET is 'on'.
The output becomes '0'. If the address/data bus is '1', the upper transistor is 'on' and the lower
transistor is 'off'. Hence the output is '1'. Hence for normal address/data interfacing (for
external memory access) no pull-up resistors are required.
Port-0 latch is written to with 1's when used for external memory access. Port-1 Pin Structure
Port-1 has 8 pins (P1.1-P1.7) .The structure of a port-1 pin is shown in fig 4.11.
Port-1 does not have any alternate function i.e. it is dedicated solely for I/O interfacing.
When used as output port, the pin is pulled up or down through internal pull-up. To use port-
1 as input port, '1' has to be written to the latch. In this input mode when '1' is written to the
pin by the external device then it read fine. But when '0' is written to the pin by the external
device then the external source must sink current due to internal pull-up. If the external
device is not able to sink the current the pin voltage may rise, leading to a possible wrong
reading.
Port-2 has 8-pins (P2.0-P2.7) . The structure of a port-2 pin is shown in fig 4.12.
Port-2 is used for higher external address byte or a normal input/output port. The I/O
operation is similar to Port-1. Port-2 latch remains stable when Port-2 pin are used for
external memory access. Here again due to internal pull-up there is limited current driving
capability.
Port-3 has 8 pin (P3.0-P3.7) . Port-3 pins have alternate functions. The structure of a port-3
pin is shown in fig 4.13.
Each pin of Port-3 can be individually programmed for I/O operation or for alternate
function. The alternate function can be activated only if the corresponding latch has been
written to '1'. To use the port as input port, '1' should be written to the latch. This port also
has internal pull-up and limited current driving capability.
Note:
2. Port-0 can drive 8 LS TTL inputs in address /data mode. For digital output port, it
needs external pull-up resistors.
3. Ports-1,2and 3 pins can also be driven by open-collector or open-drain outputs.
4. Each Port 3 bit can be configured either as a normal I/O or as a special function bit.
Reading a port (port-pins) versus reading a latch
There is a subtle difference between reading a latch and reading the output port pin.
The status of the output port pin is sometimes dependant on the connected load. For instance
if a port is configured as an output port and a '1' is written to the latch, the output pin should
also show '1'. If the output is used to drive the base of a transistor, the transistor turns 'on'.
If the port pin is read, the value will be '0' which is corresponding to the base-emitter voltage
of the transistor.
Reading a latch: Usually the instructions that read the latch, read a value, possibly change it,
and then rewrite it to the latch. These are called "read-modify-write" instructions. Examples
of a few instructions are-
In this the latch value of P2 is read, is modified such that P2.1 is the same as Carry and is
then written back to P2 latch.
Reading a Pin: Examples of a few instructions that read port pin, are-
For external program memory, always 16 bit address is used. For example - MOVC A, @
A+DPTR
MOVC A, @ A+PC
Access to external data memory can be either 8-bit address or 16-bit address - 8-bit address-
MOVX A, @Rp where Rp is either R0 or R1
MOVX @Rp, A
16 bit address- MOVX A,@DPTR
MOV X @DPTR, A
The external memory access in 8051 can be shown by a schematic diagram as given in fig
4.14.
If an 8-bit external address is used for data memory (i.e. MOVX @Rp) then the content of
Port-2 SFR remains at Port-2 pins throughout the external memory cycle. This facilitates
memory paging as the upper 8 bit address remains fixed.
During any access to external memory, the CPU writes FFH to Port-0 latch (SFR). If the user
writes to Port-0 during an external memory fetch, the incoming byte is corrupted. External
program memory is accessed under the following condition.
External program memory can be not only used to store the code, but also for lookup table of
various functions required for a particular application. Mathematical functions such as Sine,
Square root, Exponential, etc. can be stored in the program memory (Internal or eternal) and
these functions can be accessed using MOVC instruction.
Timers / Counters
8051 has two 16-bit programmable UP timers/counters. They can be configured to operate
either as timers or as event counters. The names of the two counters are T0 and T1
respectively. The timer content is available in four 8-bit special function registers, viz,
TL0,TH0,TL1 and TH1 respectively.
In the "timer" function mode, the counter is incremented in every machine cycle. Thus, one
can think of it as counting machine cycles. Hence the clock rate is 1/12 th of the oscillator
frequency.
The operation of the timers/counters is controlled by two special function registers, TMOD
and TCON respectively.
TMOD
Address: 89 H
TCON is bit addressable. The address of TCON is 88H. It is partly related to Timer and
partly to interrupt.
TF1 : Timer1 overflow flag. It is set when timer rolls from all 1s to 0s. It is cleared when
processor vectors to execute ISR located at address 001BH.
TR1 : Timer1 run control bit. Set to 1 to start the timer / counter. TF0 : Timer0 overflow
flag. (Similar to TF1)
IE1 : Interrupt1 edge flag. Set by hardware when an external interrupt edge is detected. It is
cleared when interrupt is processed.
IE0 : Interrupt0 edge flag. (Similar to IE1)
IT1 : Interrupt1 type control bit. Set/ cleared by software to specify falling edge / low level
triggered external interrupt.
As mentioned earlier, Timers can operate in four different modes. They are as follows Timer
Mode-0:
This is a 8 bit counter/timer operation. Counting is performed in TLX while THX stores a
constant value. In this mode when the timer overflows i.e. TLX becomes FFH, it is fed with
the value stored in THX. For example if we load THX with 50H then the timer in mode 2
will count from 50H to FFH. After that 50H is again reloaded. This mode is useful in
applications like fixed time sampling.
Timer Mode-3:
Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0. Timer0 in
mode-3 establishes TL0 and TH0 as two separate counters.
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0
and TF0 are available to Timer-0 lower 8 bits(TL0).