8051IOPORT PROGRAMMING
8051IOPORT PROGRAMMING
are four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports. All the
ports upon RESET are configured as output, ready to be used as output ports. To use
any of these ports as an input port, it must be programmed.
Port 0: Port 0 occupies a total of 8 pins (pins 32-39) .It can be used for input or output.
To use the pins of port 0 as both input and output ports, each pin must be connected
externally to a 10K ohm pull-up resistor. This is due to the fact that P0 is an open drain,
unlike P1, P2, and P3.Open drain is a term used for MOS chips in the same way that
open collector is used for TTL chips. With external pull-up resistors connected upon
reset, port 0 is configured as an output port. For example, the following code will
continuously send out to port 0 the alternating values 55H and AAH
MOV A,#55H
BACK: MOV P0,A
ACALL DELAY
CPL A
SJMP BACK
Port 0 as Input : With resistors connected to port 0, in order to make it an input, the
port must be programmed by writing 1 to all the bits. In the following code, port 0 is
configured first as an input port by writing 1’s to it, and then data is received from the
port and sent to P1.
8051 I/O Ports
Dual role of port 0: Port 0 is also designated as AD0-AD7, allowing it to be used for
both address and data. When connecting an 8051/31 to an external memory, port 0
provides both address and data. The 8051 multiplexes address and data through port 0
to save pins. ALE indicates if P0 has address or data. When ALE = 0, it provides data
D0-D7, but when ALE =1 it has address and data with the help of a 74LS373 latch.
Port 1: Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or
output. In contrast to port 0, this port does not need any pull-up resistors since it already
has pull-up resistors internally. Upon reset, Port 1 is configured as an output port. For
example, the following code will continuously send out to port1 the alternating values
55h & AAh
Port 1 as input: To make port1 an input port, it must programmed as such by writing
1 to all its bits. In the following code port1 is configured first as an input port by writing
1’s to it, then data is received from the port and saved in R7 ,R6 & R5.
Port 2 : Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or output.
Just like P1, P2 does not need any pull-up resistors since it already has pull-up resistors
internally. Upon reset,Port 2 is configured as an output port. For example, the following
code will send out continuously to port 2 the alternating values 55h and AAH. That is
all the bits of port 2 toggle continuously.
MOV A,#55H ; A = 55 hex
BACK: MOV P2,A ;send it to Port 2
ACALL DELAY ;call delay routine
CPL A ;make A=0
SJMP BACK
Dual role of port 2 : In systems based on the 8751, 8951, and DS5000, P2 is used as
simple I/O. However, in 8031-based systems, port 2 must be used along with P0 to
provide the 16-bit address for the external memory. As shown in pin configuration
8051, port 2 is also designed as A8-A15, indicating the dual function. Since an 8031 is
capable of accessing 64K bytes of external memory, it needs a path for the 16 bits of
the address. While P0 provides the lower 8 bits via A0-A7, it is the job of P2 to provide
bits A8-A15 of the address. In other words, when 8031 is connected to external
memory, P2 is used for the upper 8 bits of the 16 bit address, and it cannot be used for
I/O.
Port 3 : Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or
output. P3 does not need any pull-up resistors, the same as P1 and P2 did not. Although
port 3 is configured as an output port upon reset. Port 3 has the additional function of
providing some extremely important signals such as interrupts. This information
applies both 8051 and 8031 chips.
Functions of port 3:P3.0 and P3.1 are used for the RxD and TxD serial communications
signals. Bits P3.2 and P3.3 are set aside for external interrupts. Bits P3.4 and P3.5 are
used for timers 0 and 1. Finally P3.6 and P3.7 are used to provide the WR and RD
signals of external memories connected in 8031 based systems.
Read-modify-write feature : The ports in the 8051 can be accessed by the read-
modify-write technique. This feature saves many lines of code by combining in a single
instruction all three action of (1) reading the port, (2) modifying it, and (3) writing to
the port. The following code first places 01010101 (binary) into port 1. Next, the
instruction “XLR P1,#0FFH” performs an XOR logic operation on P1 with 1111 1111
(binary), and then writes the result back into P1.
Notice that XOR of 55H and FFH gives AAH. Likewise, the XOR of AAH and FFH
gives 55H.
Single bit addressability of ports: There are times that we need to access only 1 or 2
bits of the port instead of the entire 8 bits. A powerful feature of 8051 I/O ports is their
capability to access individual bits of the port without altering the rest of the bits in that
port.
For example, the following code toggles the bit p1.2 continuously.
BACK: CPL P1.2 ;complement p1.2 only
ACALL DELAY
SJMP BACK
Notice that P1.2 is the third bit of P1, since the first bit is P1.0, the second bit is P1.1,
and so on. Notices in example of those unused portions of port1 are undisturbed. Table
bellow shows the bits of 8051 I/O ports. This single bit addressability of I/O ports is
one of the features of the 8051 microcontroller.
PORT 3:
Following are the alternate functions of port 3:
P3.0—–RXD
P3.1—– TXD
P3.2—– INT0 BAR
P3.3—– INT1 BAR
P3.4—– T0
P3.5—– T1
P3.6—– WR BAR
P3.7—– RD BAR
It works as an IO port same like Port 2 as well as it can do lots of alternate work which
are discuss above. Only alternate function of port 3 makes its architecture different than
other ports.
ANL C, bit_procedencia. AND (Y) between the carry logic and the bit
indicated.
ORL C, bit_procedencia. OR (O) and carry logic between the specified bit.
Basically, the commonly used 8051 microcontroller has 40 pins 32 of which are
grouped into four 8-bits I/O ports which can be used as input or output. The other eight
pins are used for different purposes and all are presented below in a very easily
understandable way.
The pin diagram of 8051 is added below to help you see where each pin is located while
reading their purpose simultaneously.
Pin-29 (PSEN)
This is PSEN pin which stands for Program Store Enable. It is used to read a signal
from the external program memory.
Pin 30 (EA)
This is EA pin which stands for External Access input. It is used to enable/disable the
external memory interfacing.
Pin 31 (ALE)
This is ALE pin which stands for Address Latch Enable. It is used to demultiplex the
address-data signal of port.
Pin-40(power pin)
This port is used to provide power supply to the chip. The voltage supply is usually 5V.
The remaining pins of the 8051 are arranged into four 8-bits input/output ports and are
presented below. The four ports can be used as input or output. Upon resetting the
microcontroller, all the ports’ pins’ value are set to high and ready to be used as input
pins. To use it as output, we must assign them low values.
Input Configuration
If any pin of this port is configured as an input, then it acts as if it “floats”, i.e. the input
has unlimited input resistance and in-determined potential.
Output Configuration
When the pin is configured as an output, then it acts as an “open drain”. By applying
logic 0 to a port bit, the appropriate pin will be connected to ground (0V), and applying
logic 1, the external output will keep on “floating”.
In order to apply logic 1 (5V) on this output pin, it is necessary to build an external
pull-up resistor.
In addition, if we want to access only specific pins of these ports, we make use of their
features of bit addressability using the following addresses:
0x80 to 0x87 for pin 0th to pin 7th of port 0 (P0.0 to P0.7),
0x90 to 0x97 for pin 0th to pin 7th of port 1 (P1.0 to P1.7),
0xA0 to 0xA7 for pin 0th to pin 7th of port 2 (P2.0 to P2.7, and
0xB0 to 0xB7 for pin 0th to pin 7th of port 3 (P3.0 to P3.7).
Arithmetic Instructions
ADD
ADDC
SUBB
INC
DEC
MUL
DIV
DA A
The arithmetic instructions has no knowledge about the data format i.e. signed,
unsigned, ASCII, BCD, etc. Also, the operations performed by the arithmetic
instructions affect flags like carry, overflow, zero, etc. in the PSW Register.
All the possible Mnemonics associated with Arithmetic Instructions are mentioned in
the following table.
Logical Instructions
The next group of instructions are the Logical Instructions, which perform logical
operations like AND, OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are
performed on Bytes of data on a bit-by-bit basis.
ANL
ORL
XRL
CLR
CPL
RL
RLC
RR
RRC
SWAP
The following table shows all the possible Mnemonics of the Logical Instructions.
PROGRAMMING 8051 TIMERS
The 8051 has two timers/counters. They can be used either as timers to generate a time
delay or as counters to count events happening outside the microcontroller. In Section
9.1 \ve see how these timers are used to generate time delays. In Section 9.2 we show
how they are used as event counters. In Section 9.3 we use C language to program the
8051 timers.
SECTION 9.1: PROGRAMMING 8051 TIMERS
The 8051 has two timers: Timer 0 and Timer 1. They can be used either as timers or as
event counters. In this section we first discuss the timers’ registers and then show how
to program the timers to generate time delays.
Basic registers of the timer
Both Timer 0 and Timer 1 are 16 bits wide. Since the 8051 has an 8-bit architecture,
each 16-bit timer is accessed as two separate registers of low byte and high byte. Each
timer is discussed separately.
Timer 0 registers
The 16-bit register of Timer 0 is accessed as low byte and high byte. The low byte
register is called TLO (Timer 0 low byte) and the high byte register is referred to as
THO (Timer 0 high byte). These registers can be accessed like any other register, such
as A, B, RO, Rl, R2, etc. For example, the instruction “MOV TLO , #4FH” moves the
value 4FH into TLO, the low byte of Timer 0. These registers can also be read like any
other register. For example, “MOV R5 , THO” saves THO (high byte of Timer 0) in
R5.
Both timers 0 and 1 use the same register, called TMOD, to set the various timer
operation modes. TMOD is an 8-bit register in which the lower 4 bits are set aside for
Timer 0 and the upper 4 bits for Timer 1. In each case, the lower 2 bits are used to set
the timer mode and the upper 2 bits to specify the operation. These options are discussed
next.
Example 9-1
Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD,#01H (b) MOV TMOD,#20H (c) MOV TMOD,#12H
Solution:
We convert the values from hex to binary. From Figure 9-3 we have:
1. TMOD = 00000001, mode 1 of Timer 0 is selected.
2. TMOD = 00100000, mode 2 of Timer 1 is selected.
1. TMOD = 00010010, mode 2 of Timer 0, and mode 1 of
Timer 1 are selected.
Clock source for timer
As you know, every timer needs a clock pulse to tick. What is the source of the clock
pulse for the 8051 timers? If C T = 0. the crystal frequency attached to the 8051 is the
source of the clock for the timer. This means that the size of the crystal frequency
attached to the 8051 also decides the speed at which the 8051 timer ticks. The frequency
for the timer is always 1 12th the frequency of the crystal attached to the 8051. See
Example 9-2.
Example 9-2
Now that we have this basic understanding of the role of the TMOD register, we will
look at the timer’s modes and how they are programmed to create a time delay. Because
modes 1 and 2 are so widely used, we describe each of them in detail.
Mode 1 programming
The following are the characteristics and operations of mode 1:
1. It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be loaded
into the timer’s registers TL and TH.
2. After TH and TL are loaded with a 16-bit initial value, the timer must be start
ed. This is done by “SETB TRO” for Timer 0 and “SETB TR1″ for Timer 1.
1. After the timer is started, it starts to count up. It counts up until it reaches its
limit of FFFFH. When it rolls over from FFFFH to 0000, it sets high a flag bit called
TF (timer flag). This timer flag can be monitored. When this timer flag is raised, one
option would be to stop the timer with the instructions “CLR TRO” or “CLR TR1″, for
Timer 0 and Timer 1, respectively. Again, it must be noted that each timer has its own
timer flag: TFO for Timer 0, and TF1 for Timer 1.
4. After the timer reaches its limit and rolls over, in order to repeat the process the
registers TH and TL must be reloaded with the original value, and TF must be reset to
0.
Introduction
An interrupt is an event that occurs randomly in the flow of continuity. It is just like a
call you have when you are busy with some work and depending upon call priority you
decide whether to attend or neglect it.
Vector Address: This is the address where controller jumps after the interrupt to serve
the ISR (interrupt service routine).
Reset - 0000H
Reset
Reset is the highest priority interrupt, upon reset 8051 microcontroller start executing
code from 0x0000 address.
Serial interrupt
8051 has serial communication port and have related serial interrupt flags (TI/RI).
When the last bit (stop bit) of a byte is transmitted, TI serial interrupt flag is set and
when last bit (stop bit) of receiving data byte is received, RI flag get set.
IE register: Interrupt Enable Register
IE register is used to enable/disable interrupt sources.
Interrupt priority
Priority to the interrupt can be assigned by using interrupt priority register (IP)
In the table, interrupts priorities upon reset are shown. As per 8051 interrupt priorities,
lowest priority interrupts are not served until microcontroller is finished with higher
priority ones. In a case when two or more interrupts arrives microcontroller queues
them according to priority.
Whenever a low level is detected on the INT0/INT1 pin while global and external
interrupts are enabled, the controller jumps to interrupt service routine (ISR) to serve
interrupt.
Whenever falling edge is detected on the INT0/INT1 pin while global and ext.
interrupts are enabled, the controller jumps to interrupt service routine (ISR) to serve
interrupt.
There are lower four flag bits in TCON register required to select and monitor the
external interrupt type and ISR status.
TCON: Timer/ counter Register
Bit 3- IE1:
External Interrupt 1 edge flag, set by hardware when interrupt on INT1 pin occurred
and cleared by hardware when interrupt get processed.
Bit 2- IT1:
This bit selects external interrupt event type on INT1 pin,
1= sets interrupt on falling edge
Bit 1- IE0:
Interrupt0 edge flag, set by hardware when interrupt on INT0 pin occurred and
cleared by hardware when an interrupt is processed.
Bit 0 - IT0:
This bit selects external interrupt event type on INT0 pin.
Example
Let’s program the external interrupt of AT89C51 such that, when falling edge is
detected on INT0 pin then the microcontroller will toggle the P1.0 pin.
Programming steps