0% found this document useful (0 votes)
23 views

8051IOPORT PROGRAMMING

Micro controller programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

8051IOPORT PROGRAMMING

Micro controller programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

INTRODUCTION YO I/O PORT PROGRAMMING:There

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.

Pin configuration of 8051/8031 microcontroller.

Pin configuration of 8951

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

MOV A,#0FFH ; A = FF hex


MOV P0,A ; make P0 an input port
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK

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

MOV A,#55H ; A = 55 hex


BACK: MOV P1,A ;send it to Port 1
ACALL DELAY ;call delay routine
CPL A ;make A=0
SJMP BACK

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.

MOV A,#0FFH ;A=FF HEX


MOV P1,A ;make P1 an input port by writing all 1’s to it
MOV A,P1 ;get data from P1
MOV R7,A ;save it in register R7
ACALL DELAY ;wait
MOV A,P1 ;get another data from P1
MOV R6,A ;save it in register R6
ACALL DELAY ;wait
MOV A,P1 ;get another data from P1
MOV R5,A ;save it in register 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

Port 2 as input : To make port 2 an input, it must programmed as such by writing 1 to


all its bits. In the following code, port 2 is configured first as an input port by writing
1’s to it. Then data is received from that port and is sent to P1 continuously.

MOV A,#0FFH ;A=FF hex


MOV P2,A ;make P2 an input port by writing all 1’s to it
BACK: MOV A,P2 ;get data from P2
MOV P1,A ;send it to Port1
SJMP BACK ;keep doing that

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.

MOV P1,#55H ;P1=01010101


AGAIN: XLR P1,#0FFH ;EX-OR P1 with 1111 1111
ACALL DELAY
SJMP AGAIN

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.

Ports in 8051 microcontroller and structures


To work with 8051 microcontroller we should have complete knowledge about ports in
8051 microcontroller. We have to clear some basic question like how can we use
different ports of 8051 microcontroller? How can we configure them? will we use every
ports for every works? What are the special functions we can done by using 8051
microcontroller specified port?
Ports in 8051 microcontroller
In this post i think you will find the answer of those questions. So to starts with ports
in 8051 microcontroller we should know it has four ports. Those are port0, port1, port2
and por3.
Port 0
Port-0 can be used 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 bidirectional
I/O port.
PORT 0 as an Input 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
have floats hence whatever data written on pin is directly read by read pin.
PORT 0 as an Output Port
Suppose we want to write 1 on pin of Port 0, a ‘1’ written to the latch which turns ‘off’
the lower FET while due to ‘0’ control signal upper FET also turns off as shown in fig.
above. Here we wants logic ‘1’ on pin but we getting floating value so to convert that
floating value into logic ‘1’ we need to connect the pull up resistor parallel to upper
FET . This is the reasonwhy we needed to connect pull up resistor to port 0 when
we want to initialize port 0 as an output port .
If we want to write ‘0’ on pin of port 0 , when ‘0’ is written to the latch, the pin is pulled
down by the lower FET. Hence the output becomes zero.
When the control is ‘1’, address/data bus controls the output driver FETs. If the
address/data bus (internal) is ‘0’, the upper FET is ‘off’ and the lower FET is ‘on’. The
output becomes ‘0’. If the address/data bus is ‘1’, the upper FET is ‘on’ and the lower
FET 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
Port-1 dedicated only for I/O interfacing. When used as output port, not needed to
connect additional pull-up resistor like port 0. It have provided internally pull-up
resistor as shown in fig. below. The pin is pulled up or down through internal pull-up
when we want to initialize as an output port. 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:
Port-2 we use 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:
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.

Bit Manipulation Instructions

Bit manipulation instructions nicroprocesadores 8051

CLR bit. Zero the specified bit.

SETB bit. Putting a specified bit.


CPL bit. Complement the bit indicated.

Bit_destino MOV, bit_procedencia. Transfer or move a bit.

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.

8051 I/O Ports Programming


BY KENENI BENTI · MAY 18, 2018
This tutorial helps you know when and how to use the four input/output ports and the
remaining 8 pins of the 8051.

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.

Figure-1: The 8051 Pin Diagram


Pin-9 (Reset pin)
This pin is used to reset the chip. It is active high (it means normally low), so to reset
the chip, we need to give the pin logic high. Upon reset, all I/O ports of the
microcontroller are configured as input (having values of high). Therefore, to use those
ports as outputs, we must give values of low to each pin. In addition, when we reset the
chip, the program counter (a 16-bit register) goes to its initial default reset value of
0x0000 and the stack pointer (an 8-bit register) goes to its initial default value of 0x07
and they both are defined by the processor.
Pin-18 and Pin-19 (XTAL2 and XTAL1 pins)
Basically, 8051 has on chip clock whose frequency depends on the model of the chip
(in most case, it is 12MHz). This clock helps in synchronizing the tasks and operations
performed by the microcontroller. But, this built-in clock doesn’t work alone, so, to
make use of it, we need to connect external quartz crystal oscillator of the frequency
not more than the chip’s frequency. For example, for 12MHz microcontroller, we must
connect an external oscillator of not more than 12MHz. Actually, one machine cycle in
8051 takes 12 clock cycles. This means, 8051 has capable of executing one million
instructions in one second (performing one instruction cycle in 1 microsecond). In
summary, the pins (18 and 19) are used for the connection of external quartz crystal
oscillator. In fact, the crystal oscillator also needs to be connected to two capacitors of
22pF whose other ends are connected to ground.

Pin-20 (ground pin)


Pin-20 is always connected to the ground

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.

Port 0 (pin-32 to pin-39)


This is 8-bits I/O port whose pins can be used as input or output. Unlike other ports
(ports 1, 2, and 3), this port doesn’t have internal pull-up resistors. Therefore, to make
use of this port, we need to connect 10k pull-up resistors to them. When the external
memory is used then the lower address byte (addresses A0 to A7) is applied on it, else
all bits of this port are configured as input/output.

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.

Port 1 (pin-1 to pin-8)


P1 is a true I/O port as it doesn’t have any alternative functions as in P0, but this port
can be configured as general I/O only. It has a built-in pull-up resistor and is completely
compatible with TTL circuits.

Port 2 (pin-21 to pin-28)


P2 is similar to P0 when the external memory is used. Pins of this port occupy addresses
intended for the external memory chip. This port can be used for higher address byte
with addresses A8-A15. When no memory is added then this port can be used as a
general input/output port similar to Port 1.

Port 3 (pin-10 to 17)


In this port, functions are similar to other ports except that the logic 1 must be applied
to appropriate bit of the P3 register. Pins of this port can also be used for different
purposes such as serial communication (pins 10 and 11), external interrupts (pins 12
and 13), timers/counters (pins pins 14 and 15), and write/read (pins 16 and 17).

Bits and Bytes addressability


In some microcontrollers it’s impossible to access the registers, RAM or I/O in bits
unless it is in bytes. That means, if we want to use for example only some of a certain
port’s pins, we need to read the entire byte first and then manipulate the whole byte
with some logic instructions to get hold of the desired single bit. But, in 8051, you can
access I/O ports, RAM or registers in bits instead of bytes. This is one of the most
important features of 8051.

Accessing RAM in bits


As we have seen in the previous tutorial, 8051 has on-chip RAM of 128 bytes. These
bytes are divided into three: the first 32 bytes (from 00H to 1FH) are assigned for the
four register banks, the next bytes (from 20H to 2FH) are used for bit addressable
registers and the last bytes (from 30H to 7FH) are used for read and write storage. Now,
the second division (bit addressable registers location) is 16 bytes capable of being
accessed in bits. Therefore, we need to further expand them into 128 bits (00H to 7FH)
to access them individually (because 16 bytes = 16×8 bits = 128bits). In this way, the
8051 RAM can be accessed in bits.
Accessing the I/O ports
The 8051 I/O ports can be accessed either in byte of bits. When we want to access the
ports in byte we use the addresses: 0x80 for port 0, 0x90 for port 1, 0xA0 for port 2 and
0xB0 for port 3.

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

Using Arithmetic Instructions, you can perform addition, subtraction, multiplication


and division. The arithmetic instructions also include increment by one, decrement by
one and a special instruction called Decimal Adjust Accumulator.

The Mnemonics associated with the Arithmetic Instructions of the 8051


Microcontroller Instruction Set are:

 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.

Mnemonics associated with Logical Instructions are as follows:

 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.

Figure 9-1. Timer 0 Registers


Timer 1 registers

Figure 9-2. Timer 1 Registers


Timer I is also 16 bits, and its 16-bit register is split into two bytes, referred to as TLl
(Timer I low byte) and TH1 (Timer 1 high byte). These registers are accessible in the
same way as the registers of Timer 0.
TMOD (timer mode) register

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.

Figure 9-3. TMOD Register


M1, MO
MO and Ml select the timer mode. As shown in Figure 9-3, there are three modes: 0, 1,
and 2. Mode 0 is a 13-bit timer, mode 1 is a 16-bit timer, and mode 2 is an 8-bit timer.
We will concentrate on modes 1 and 2 since they are the ones used most widely. We
will soon describe the characteristics of these modes, after describing the rest of the
TMOD register.
C/T (clock/timer)
This bit in the TMOD register is used to decide whether the timer is used as a delay
generator or an event counter. If C/T = 0, it is used as a timer for time delay generation.
The clock source for the time delay is the crystal frequency of the 8051. This section is
concerned with this choice. The timer’s use as an event counter is discussed in the next
section.

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

NOTE THAT 8051 TIMERS USE 1/12 OF XTAL FREQUENCY, REGARDLESS


OF MACHINE CYCLE TIME.
Although various 8051-based systems have an XTAL frequency of 10 MHz to 40 MHz,
we will concentrate on the XTAL frequency of 11.0592 MHz. The reason behind such
an odd number has to do with the baud rate for serial communication of the 8051. XTAL
= 11.0592 MHz allows the 8051 system to communicate with the IBM PC with no
errors, as we will see in Chapter 10.
GATE
The other bit of the TMOD register is the GATE bit. Notice in the TMOD register of
Figure 9-3 that both Timers 0 and 1 have the GATE bit. What is its purpose? Every
timer has a means of starting and stopping. Some timers do this by software, some by
hardware, and some have both software and hardware controls. The timers in the 8051
have both. The start and stop of the timer are controlled by way of software by the TR
(timer start) bits TRO and TR1. This is achieved by the instructions “SETB TR1″ and
“CLR TR1″ for Timer 1, and “SETB TRO” and “CLR TRO” for Timer 0. The SETB
instruction starts it, and it is stopped by the CLR instruction. These instructions start
and stop the timers as long as GATE = 0 in the TMOD register. The hardware way of
starting and stopping the timer by an external source is achieved by making GATE = 1
in the TMOD register. However, to avoid further confusion for now, we will make
GATE = 0, meaning that no external hardware is needed to start and stop the timers. In
using software to start and stop the timer where GATE = 0. all we need are the
instructions “SETB TRx” and “CLR TRx”. The use of external hardware to stop or start
the timer is discussed in Chapter 11 when interrupts are discussed.
Example 9-3
Find the value for TMOD if we want to program Timer 0 in mode 2, use 8051 XTAL
for the clock source, and use instructions to start and stop the timer.

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.

Steps to program in mode 1


To generate a time delay, using the timer’s mode 1, the following steps are taken. To
clarify these steps, see Example 9-4.
1. Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is
to be used and which timer mode (0 or 1) is selected.
1. Load registers TL and TH with initial count values.
2. Start the timer.
1. Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruc
tion to see if it is raised. Get out of the loop when TF becomes high.
3. Stop the timer.
4. Clear the TF flag for the next round.
5. Go back to Step 2 to load TH and TL again.
To calculate the exact time delay and the square wave frequency generated on pin
PI .5, we need to know the XTAL frequency. See Example 9-5.
From Example 9-6 we can develop a formula for delay calculations using mode 1 (16-
bit) of the timer for a crystal frequency of XTAL = 11.0592 MHz. This is given in
Figure 9-4. The scientific calculator in the Accessories directory of Microsoft Windows
can help you to find the TH, TL values. This calculator supports decimal, hex, and
binary calculations.
(a) in hex
(FFFF – YYXX + 1) X 1.085 us where YYXX are TH, TL initial values
respectively. Notice that values YYXX are in hex.
(b) in decimal
Convert YYXX values of the TH,TL register to decimal to get a NNNNN
decimal number, then (65536 – NNNNN) x 1.085 mircosec
Timer Delay Calculation for XTAL = 11.0592 MHz
COUNTER PROGRAMMING
In the last section we used the timer/counter of the 8051 to generate time delays. These
timers can also be used as counters counting events happening outside the 8051. The
use of the timer/counter as an event counter is covered in this section. As far as the use
of a timer as an event counter is concerned, everything that we have talked about in
programming the timer in the last section also applies to programming it as a counter,
except the source of the frequency. When the timer/counter is used as a timer, the 8051′s
crystal is used as the source of the frequency. When it is used as a counter, however, it
is a pulse outside the 8051 that increments the TH, TL registers. In counter mode, notice
that the TMOD and TH, TL registers are the same as for the timer discussed in the last
section; they even have the same names. The timer’s modes are the same as well.
C/T bit in TMOD register
Recall from the last section that the C/T bit in the TMOD register decides the source of
the clock for the timer. If C/T = 0, the timer gets pulses from the crystal. In contrast,
when C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051.
Therefore, when C/T = 1, the counter counts up as pulses are fed from pins 14 and 15.
These pins are called TO (Timer 0 input) and Tl (Timer 1 input). Notice that these two
pins belong to port 3. In the case of Timer 0, when C/T = ], pin P3.4 provides the clock
pulse and the counter counts up for each clock pulse coming from that pin. Similarly,
for Timer 1, when C/T = 1 each clock pulse coming in from pin P3.5 makes the counter
count up.
Table 9-1: Port 3 Pins Used For Timers 0 and 1
8051 Interrupts

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.

Same thing happens in microcontrollers. 8051 architecture handles 5 interrupt sources,


out of which two are internal (Timer Interrupts), two are external and one is a serial
interrupt. Each of these interrupts has their interrupt vector address. Highest priority
interrupt is the Reset, with vector address 0x0000.

Vector Address: This is the address where controller jumps after the interrupt to serve
the ISR (interrupt service routine).

Interrupt Flag Interrupt vector address

Reset - 0000H

INT0 (Ext. int. 0) IE0 0003H

Timer 0 TF0 000BH

INT1 (Ext. int. 1) IE1 0013H

Timer 1 TF1 001BH

Serial TI/RI 0023H

Reset
Reset is the highest priority interrupt, upon reset 8051 microcontroller start executing
code from 0x0000 address.

Internal interrupt (Timer Interrupt)


8051 has two internal interrupts namely timer0 and timer1. Whenever timer overflows,
timer overflow flags (TF0/TF1) are set. Then the microcontroller jumps to their vector
address to serve the interrupt. For this, global and timer interrupt should be enabled.

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.

Bit 7 – EA: Enable All Bit

1 = Enable all interrupts

0 = Disable all interrupts

Bit 6,5 – Reserved bits

Bit 4 – ES: Enable Serial Interrupt Bit

1 = Enable serial interrupt

0 = Disable serial interrupt

Bit 3 – ET1: Enable Timer1 Interrupt Bit

1 = Enable Timer1 interrupt

0 = Disable Timer1 interrupt

Bit 2 – EX1: Enable External1 Interrupt Bit

1 = Enable External1 interrupt

0 = Disable External1 interrupt

Bit 1 – ET0: Enable Timer0 Interrupt Bit


1 = Enable Timer0 interrupt

0 = Disable Timer0 interrupt

Bit 0 – EX0: Enable External0 Interrupt Bit

1 = Enable External0 interrupt

0 = Disable External0 interrupt

Interrupt priority
Priority to the interrupt can be assigned by using interrupt priority register (IP)

Interrupt priority after Reset:


Priority Interrupt source Intr. bit / flag

1 External Interrupt 0 INT0

2 Timer Interrupt 0 TF0

3 External Interrupt 1 INT1

4 Timer Interrupt 1 TF1

5 Serial interrupt (TI/RI)

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.

IP Register: Interrupt priority register


8051 has interrupt priority register to assign priority to interrupts.

Bit 7,6,5 – Reserved bits.

Bit 4 – PS: Serial Interrupt Priority Bit

1 = Assign high priority to serial interrupt.

0 = Assign low priority to serial interrupt.

Bit 3 – PT1: Timer1 Interrupt Priority Bit

1 = Assign high priority to Timer1 interrupt.

0 = Assign low priority to Timer1 interrupt.

Bit 2 – PX1: External Interrupt 1 Priority Bit

1 = Assign high priority to External1 interrupt.

0 = Assign low priority to External1 interrupt.


Bit 1 – PT0: Timer0 Interrupt Priority Bit

1 = Assign high priority to Timer0 interrupt.


0 = Assign low priority to Timer0 interrupt.

Bit 0 – PX0: External0 Interrupt Priority Bit

1 = Assign high priority to External0 interrupt.

0 = Assign low priority to External0 interrupt.

External interrupts in 8051

 8051 has two external interrupt INT0 and INT1.


 8051 controller can be interrupted by external Interrupt, by providing level or
edge on external interrupt pins PORT3.2, PORT3.3.
 External peripherals can interrupt the microcontroller through these external
interrupts if global and external interrupts are enabled.
 Then the microcontroller will execute current instruction and jump to the
Interrupt Service Routine (ISR) to serve to interrupt.
 In polling method microcontroller has to continuously check for a pulse by
monitoring pin, whereas, in interrupt method, the microcontroller does not need
to poll. Whenever an interrupt occurs microcontroller serves the interrupt
request.

External interrupt has two types of activation level

1. Edge triggered (Interrupt occur on rising/falling edge detection)


2. Level triggered (Interrupt occur on high/low-level detection)

In 8051, two types of activation level are used. These are,

Low level triggered

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.

Falling edge triggered

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

0= sets interrupt on low level

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.

1= sets interrupt on falling edge

0= sets interrupt on low level

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

1. Enable global interrupt i.e. EA = 1


2. Enable external interrupt i.e. EX0 = 1
3. Enable interrupt trigger mode i.e. whether interrupt is edge triggered or level
triggered, here we will use falling edge trigger interrupt, so make IT0 = 1.

You might also like