Você está na página 1de 20

Unit-IV : Programming Examples:Addition of 8 bit numbers using 8051

To perform addition of two 8 bit numbers using 8051 instruction set.


ALGORITHM:
Start at Location 4100 (origin)
1. Clear C register for Carry
2. Get the data immediately .
3. Add the two data
4. Store the result in memory pointed by DPTR

PROGRAM:
ORG 4100
CLR C
MOV A,#data1
ADD A,#data2
MOV DPTR,#4500
MOVX @DPTR,A
HERE: SJMP HERE

OBSERVATION:
Input: 66
23
Output: 89 (4500)

Multiplication of two 8 bit numbers using 8051


To perform multiplication of two 8 bit numbers using 8051 instruction set.

ALGORITHM:
1. Get the data in A reg.
2. Get the value to be multiplied in B reg.
3. Multiply the two data
4. The higher order of the result is in B reg.
5. The lower order of the result is in A reg.
6. Store the results.

PROGRAM:
ORG 4100
CLR C
MOV A,#data1
MOV B,#data2
MUL AB
MOV DPTR,#4500
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
HERE: SJMP HERE

OBSERVATION:
Input: 80
80

Output: 00 (4500)
19 (4501)
Subtraction of two 8 bit numbers using 8051
To perform Subtraction of two 8 bit numbers using 8051 instruction set.

ALGORITHM:
1. Clear C register for Carry
2. Get the data immediately .
3. Subtract the two data
4. Store the result in memory pointed by DPTR

PROGRAM:
ORG 4100
CLR C
MOV A,#data1
SUBB A,#data2
MOV DPTR,#4500
MOVX @DPTR,A
HERE: SJMP HERE

OBSERVATION:
Input: 66
23
Output: 43 (4500)

division of two 8 bit numbers using 8051


To perform division of two 8 bit numbers using 8051 instruction set.

ALGORITHM:
1. Get the data in A reg.
2. Get the value to be divided in B reg.
3. Divide the two data
4. The quotient is in A reg.
5. The remainder is in B reg.
6. Store the results.

PROGRAM:
ORG 4100
CLR C
MOV A,#data1
MOV B,#data2
DIV AB
MOV DPTR,#4500
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
HERE: SJMP HERE

OBSERVATION:
Input: 05
03
Output: 01 (4500)
02 (4501)
Program for arranging numbers in ascending order using 8051?

in ascending order
ORG 000H
SJMP START
START : MOV R1,#05H /* R1 declared as a pass counter*/

AGAIN : MOV A,R1


MOV R2,A
MOV R0,#30H
MOV A,@R0
UP : INC R0
MOV B,@R0
CLR C
SUBB A,B
JC SKIP
MOV B,@R0
DEC R0
MOV A,@R0
MOV @R0,B
INC R0
MOV @R0,A
SKIP : DJNZ R2,UP
DJNZ R1,AGAIN
STOP : SJMP STOP
8051 Program to arrange numbers in Descending Order:

8051 Program to arrange numbers in Descending Order is as follows:

MOV R4,#0AH

AGAIN: MOV R3,#0AH

MOV R0,#20H

CLR C

UP:MOV A,@R0

MOV R1,A

INC R0

MOV A,@R0

SUBB A,R1

JC SKIP

MOV A,@R0

DEC R0

MOV @R0,A

MOV A,R1

INC R0

MOV @R0,A

SKIP:DJNZ R3,UP

DJNZ R4,AGAIN

RET
Logic for 8051 Program to arrange numbers in Descending order:

For this program, we need two registers as counters. In order to arrange 10


numbers in descending order we need count value 10 x 10 ie 100. Similarly for n
numbers count required will be n x n= n^2. Reason for that is, in one cycle only
two adjacent numbers are compared. After comparison larger number is stored at
higher memory location while small number is stored at lower memory location.
Thus arranging adjacent numbers in descending order. But our goal is to arrange
entire block in descending order so in order to compare every number with
another it requires n x n cycles or comparison loops.

In above program a block of numbers is stored at location 20H-29H. First number


is taken into Accumulator. Then the number is moved to register R1, this is done
to fetch next number into accumulator. Second number is fetched into
accumulator. Comparison is carried out between accumulator and contents of R1
(ie. first number). If first number is small it is stored in second location while the
larger number is transferred at first location.

After the above process completes, one cycle is completed. In second cycle,
number at second memory location and at third memory location are compared
and action is taken. In third cycle number at third memory location and fourth are
compared and action takes place. Similarly in further cycles the adjacent numbers
are compared and action takes place.

Completion of 10 such cycles completes one batch. 10 such batches will be


executed by the program. In case largest number is stored at 10th (last) memory
location. It will take 10 such batches for it to reach first memory location. So
execution of 10 batch cycles is essential.
8051 Program to find largest number in a block of numbers:

8051 Program to find largest number;

A program to find largest number among given block of numbers using 8051
microcontroller

MOV DPTR,#5000H

CLR C

MOV R2,#09H

MOVX A,@DPTR

MOV 30H,A

UP: INC DPTR

MOVX A,@DPTR

CJNE A,30H,DN

SJMP NEXT

DN: JC NEXT

MOV 30H,A

NEXT: DJNZ R2,UP

INC DPTR

MOV A,30H

MOVX @DPTR,A

RET
8051 Program to find smallest number in block:
In this article we will be designing 8051 Program to find smallest number in block.

MOV DPTR,#5000H

CLR C

MOV R2,#09H

MOVX A,@DPTR

MOV 30H,A

UP: INC DPTR

MOVX A,@DPTR

CJNE A,30H,DN

SJMP NEXT

DN: JNC NEXT

MOV 30H,A

NEXT: DJNZ R2,UP

INC DPTR

MOV A,30H

MOVX @DPTR,A

RET
8051 I/O PROGRAMMING
This chapter describes the I/O port programming of the 8051 with many examples. In Section 4.1, we
describe I/O access using byte-size data, and in Section 4.2, bit manipulation of the I/O ports is
discussed in detail.
SECTION 4.1: 8051 I/O PROGRAMMING
In the 8051 there are a total of four ports for I/O operations. Examining Figure 4-1, note that of the 40
pins, a total of 32 pins are set aside for the four ports PO, PL P2, and P3, where each port takes 8
pins. The rest of the pins are designated as V rt, GND, XTAL1, XTAL2. RST, EA, ALE/PROG and
PSEN are discussed in Chapter 8.

Figure 4-1. 8051 Pin Diagram


I/O port pins and their functions
The four ports PO, Pi, P2, and P3 each use 8 pins, making them 8-bit ports. All the ports upon
RESET are configured as inputs, ready to be used as input ports. When the first 0 is written to a port,
it becomes an output. To reconfigure it as an input, a 1 must be sent to the port. To use any of these
ports as an input port, it must be programmed, as we will explain throughout this section. First, we
describe each port.
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 lOK-ohm pull-up
resistor. This is due to the fact that PO is an open drain,
unlike PI, P2, and P3, as Figure 4-2. Port 0 with Pull-Up Resistors we will soon see. Open drain is
a term used for MOS chips in the same way that open collector is used for TTL chips. In any system
using the 8051/52 chip, we normally connect PO to pull-up resistors. See Figure 4-2. In this way we
take advantage of port 0 for both input and output. For example, the following code will continuously
send out to port 0 the alternating values of 55H and AAH.

It must be noted that complementing 55H (01010101) turns it into AAH (10101010). By sending 55H
and AAH to a given port continuously, we toggle all the bits of that port.
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 Is to
it, and then data is received from that port and sent to P1.

Dual role of port 0


As shown in Figure 4-1, port 0 is also designated as ADO 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. We discuss that in
Chapter 14.
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 input port. The following code will continuously send out to port
1 the alternating values 55H and AAH.
Port 1 as input
If port 1 has been configured as an output port, to make it an input port again, it must programmed as
such by writing 1 to all its bits. The reason for this is discussed in Appendix C.2. In the following code,
port 1 is configured first as an input port by writing Is to it, then data is received from that port and
saved in R7, R6, and R5.

Port 2
Port 2 occupies a total of 8 pins (pins 21 through 28). It can be used as input or output. Just like PI,
port 2 does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset,
port 2 is configured as an input port. The following code will send out continuously to port 2 the
alternating values 55H and AAH. That is, all the bits of P2 toggle continuously.

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 PI continuously.
Dual role of port 2
In many systems based on the 8051, P2 is used as simple I/O. However, in 8031-based systems, port
2 must be used along with PO to provide the 16-bit address for external memory. As shown in Figure
4-1, port 2 is also designated as A8 A15, indicating its dual function. Since an 8051/31 is capable of
accessing 64K bytes of external memory, it needs a path for the 16 bits of the address. While PO
provides the lower 8 bits via AO A7, it is the job of P2 to provide bits A8 -A15 of the address. In
other words, when the 8051/31 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. This is discussed in detail in Chapter 14.
From the discussion so far, we conclude that in systems based on 8751, 89C51, or DS589C4xO
microcontrollers, we have three ports, PO, PI, and P2, for I/O operations. This should be enough for
most microcontroller applications. That leaves port 3 for interrupts as well as other signals, as we will
see next.
D^.4 1 Table 4-1: Port 3 Alternate
run o
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, just as PI and P2 did not. Although port 3 is configured as an input port
upon reset, this is not the way it is most commonly used. Port 3 has the additional function of
providing some extremely important signals such as interrupts. Table 4-1 provides these alternate
functions of P3. This information applies to both 8051 and
8031 chips.

Functions
P3.0 and P3.1 are used for the RxD and TxD serial communications signals. See Chapter 10 to see
how they are connected. Bits P3.2 and P3.3 are set aside for external interrupts, and are discussed in
Chapter 11. Bits P3.4 and P3.5 are used for timers 0 and 1, and are discussed in Chapter 9 where
timers are discussed. Finally, P3.6 and P3.7 are used to provide the WR and RD signals of external
memories connected in 8031-based systems. Chapter 14 discusses how they are used in 8031-
based systems. In systems based on the 8751, 89C51, or DS89C4xO, pins 3.6 and 3.7 are used for
I/O while the rest of the pins in port 3 are normally used in the alternate function role.
Different ways of accessing the entire 8 bits
In the following code, as in many previous I/O examples, the entire 8 bits of port 1 are accessed.
The above code toggles every bit of PI continuously. We have seen a variation of the above program
before. Now we can rewrite the above code in a more efficient manner by accessing the port directly
without going through the accumulator. This is shown next.

We can write another variation of Table 4-2: Reset Value of Some


the above code by using a technique called onci porj-s

read-modify-write. This is shown at the end of this chapter.


Ports status upon reset
Upon reset all ports have value FFH on them as shown in Table 4-2. This makes them input ports
upon reset.

8051 I/O BIT MANIPULATION


PROGRAMMING
SECTION 4.2: I/O BIT MANIPULATION PROGRAMMING
In this section we further examine 8051 I/O instructions. We pay special attention to I/O bit
manipulation since it is a powerful and widely used feature of the 8051 family.
I/O ports and bit-addressability
Sometimes 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. Of the four 8051 ports, we can access either the entire 8 bits or any single bit
without altering the rest. When accessing a port in single-bit manner, we use the syntax SETB X. Y
where X is the port number 0, 1,2. or 3, and Y is the desired bit number from 0 to 7 for data bits DO to
D7. For example, SETB PI. 5 sets high bit 5 of port 1. Remember that DO is the LSB and D7 is the
MSB. For example, the following code toggles bit PI .2 continuously.

Notice that PI.2 is the third bit of PI, since the first bit is Pl.O, the second bit is P1.1, and so on. Table
4-3 shows the bits of the 8051 I/O ports. See Example 4-2 for an example of bit manipulation of I/O
bits. Notice in Example 4-2 that unused portions of ports 1 and 2 are undisturbed. This single-bit
addressability of I/O ports is one of most powerful features of the 8051 microcontroller and is among
the reasons that many designers choose the 8051 over other microcontrollers. We will see the use of
the bit-addressability of I/O ports in future chapters.
Table 4-3: Sinele-Bit Addressability of Ports

Example 4-2
Table 4-4: Single-Bit Instructions
Table 4-4 lists the single-bit instructions for the 8051. We will see the use of these instructions
throughout future chapters.
Checking an input bit
The JNB (jump if no bit) and JB (jump if bit = 1) instructions are also widely used single-bit operations.
They allow you to monitor a bit and make a decision depending on whether it is 0 or 1. Instructions
JNB and JB can be used for any bits of I/O ports 0, 1,2, and 3, since all ports are bit-addressable.
However, most of port 3 is used for interrupts and serial communication signals, and typically is not
used for any I/O, either single-bit or byte-wise. This is discussed in Chapters 10 and 11. Table 4-5
shows a list of instructions for reading the ports.
Example 4-3

In this program, instruction JNB PI . 2 , AGAIN (JNB means jump if no bit) stays in the loop as long
as P1.2 is low. When P1.2 becomes high, it gets out of the loop, writes the value 45H to port 0, and
creates an H-to-L pulse by the sequence of instructions SETB and CLR.

Table 4-5: Instructions For Reading an Input Port

Example 4-4
Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it means that
the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-to-low pulse to port
PI.5 to turn on a buzzer.
Example 4-5
A switch is connected to pin PI .7. Write a program to check the status of SW and perform the
following:
1. If SW=0, send letter N to P2.
2. If SW=1, send letter Y to P2.

Reading a single bit into the carry flag


We can also use the carry flag to save or examine the status of a single bit of the port. To do that, we
use the instruction MOV C, Px. y as shown in the next two examples.
Example 4-6
A switch is connected to pin P1.7. Write a program to check the status of the switch and perform the
following:
1. If switch = 0, send letter N to P2.
2. If switch = 1, send letter Y to P2.
Use the carry flag to check the switch status. This is a repeat of the last example. Solution:
Example 4-7
A switch is connected to pin PI .0 and an LED to pin P2.7. Write a program to get the status of the
switch and send it to the LED.

Notice in Examples 4-6 and 4-7 how the carry flag is used to get a bit of data from the port.

Reading input pins vs. port latch


In reading a port, some instructions read the status of port pins while others read the status of an
internal port latch. Therefore, when reading ports there are two possibilities:
1. Read the status of the input pin.
2. Read the internal latch of the output port.
We must make a distinction between these two categories of instructions since confusion between
them is a major source of errors in 8051 programming, especially where external hardware is
concerned. We discuss these instructions briefly. However, readers must study and understand the
material on this topic and on the internal working of ports that is given in Appendix C.2.
Instructions for reading input ports
As stated earlier, to make any bit of any 8051 port an input port, we must write 1 (logic high) to that
bit. After we configure the port bits as input, we can use only certain instructions in order to get the
external data present at the pins into the CPU. Table 4-6 shows the list of such instructions.
Reading latch for output port
Some instructions read the contents of an internal port latch instead of reading the status of an
external pin. Table 4-6 provides a list of these instructions. For example, look at the ANL PI, A
instruction. The sequence of actions taken when such an instruction is executed is as follows.
1. The instruction reads the internal latch of the port and brings that data into the
CPU.
1. This data is ANDed with the contents of register A.
2. The result is rewritten back to the port latch.
3. The port pin data is changed and now has the same value as the port latch.
Table 4-6: Instructions Reading a Latch (Read-Modify-Write)
From the above discussion, we conclude that the instructions that read the port latch normally read a
value, perform an operation (and possibly change it), then rewrite it back to the port latch. This is
often called Read-Modify-Write.
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 actions of (1) reading the port, (2) modifying
its value, and (3) writing to the port. The following code first places 01010101 (binary) into port 1.
Next, the instruction XLR Pi, #OFFH performs an XOR logic operation on PI with 1111 1111 (binary),
and then writes the result back into PI.

Notice that the XOR of 55H and FFH gives AAH. Likewise, the XOR of AAH and FFH gives 55H.
Logic instructions are discussed in Chapter 6.

caution
We strongly recommend that you study Section C.2 (Appendix C) if you are connecting any external
hardware to your 8051 system. Failure to use the right instruction or the right connection to port pins
can damage the ports of your 8051 system.
SUMMARY
This chapter focused on the I/O ports of the 8051. The four ports of the 8051, PO, PI, P2, and P3,
each use 8 pins, making them 8-bit ports. These ports can be used for input or output. Port 0 can be
used for either address or data. Port 3 can be used to provide interrupt and serial communication
signals. Then I/O instructions of the 8051 were explained, and numerous examples were given. We
also showed the bit-addressability of the 8051 ports.

Você também pode gostar