Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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)
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)
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*/
MOV R4,#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:
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.
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
MOVX A,@DPTR
CJNE A,30H,DN
SJMP NEXT
DN: JC NEXT
MOV 30H,A
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
MOVX A,@DPTR
CJNE A,30H,DN
SJMP NEXT
MOV 30H,A
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.
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.
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.
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.
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.
Notice in Examples 4-6 and 4-7 how the carry flag is used to get a bit of data from the port.
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.