1st module except architecture
1st module except architecture
CPU
PO P2 P1 P3 TXD RXD
ADDRESS/DATA
Registers
In CPU, registers used to store information temporarily. That information could be byte of data toh
the are a
processed, or an address pointing to the data to be fetched. The vast majority of 8051 registers are 8-bit registers. Inthe
8051 there is only one data type: 8 bits. The 8 bits of a
register are shown in the diagram from the MSB (most significant
bit) D7 to the LSB (least significant bit) DO. With an 8-bit data type, any data larger than 8 bits must be broken into 8-bit
chunks before it is processed. Since there are a
large number of registers in the 8051, we will concentrate on some of the
widely used general-purpose registers and cover special registers in future chapters. See Appendix A.2 for a complete
list of 8051 registers.
D7 D6 D5 D4 D3 D2 D DO
7F34
0002 MOV A, #0
7400 ADD A, R5
0004
2D
0006 ADD A, R7
2F
0007 ADD A, # 12H
2412 HERE: SJMP HERE
0008
80FE
0 0A and address-
a value
into register R5,
for moving #25H" has a
contains 7D, which
is the opcode instruction "MOV R5,
that address 0000 Therefore, the
The list shows moved to R5. machine code "7F34"
is located
(in this case 25H) to be Similarly, the
0001 contains the operand and 25 is the operand. "MOV R7, #34H
instruction
where 7D is the opcode the for the
machine code of "7D25", and operand and
and represents the opcode 0005 and represents the opcode
locations 0002 and 0003 locations 0004 and
in memory located in memory of 2D, which is the opcode
machine code "7400" is location 0006 has the opcode
In the s a m e way, #0". The memory the opcode for the "ADD
instruction "MOV A, content 2F, which is
the operand for the location 0007 has the and the operand 12H at
instruction "ADD A,
R5" and memory is located at address 0008
for the "ADD A, #12H
instruction. The opcode
for the instruction
A, R7"
at location 0
ORG OH ; start
1 0000
MOV R5, #25H ;load 25H into R5
2 0000 7D25 34H into R7
MOV R7, #34H ;load
3 0002 7F34
MOV A, #0 ;load 0 into A
4 0004 7400 R5 to A
ADD A, R5 add c o n t e n t s of
5 0006 2D =
now A A + R5
;
DED SYSTEMS
REGISTER
BITS AND THE PSW conditions such as
FLAG arithmetic the c
SECTION 2.6: 8051 to indicate
8051 has a flag register this section we discuss varioe
the various bits
other microprocessor, register. In
Like any in the 8051 is called the program status word (PSW)
The flag register altered.
some examples
of how it is
register and provide
P
RSO OV
CY AC FO RS1
18H - 1FH
3
MUL 0
carry flag
AC, the auxiliary ADD or SUB operation, this bit DIV
an 0 X
from D3 to D4 during
If there is a carry that perform BCD
is used by instructions DA
it is cleared. This flag information.
is set; otherwise, more
arithmetic. See Chapter 6 for
(binary coded decimal) RRC
RLC
P, the parity flag SETBC
number of 1s in the A (accumulator)
register only.
The parity flag reflects the number of then P = 1. Therefore, P = 0 if A has CLR C
contains an odd 1s,
If the A register CPLC X
an even number of 1s.
ANLC, bit X
ANLC, /bit X
OV, the overflow flag
result of a signed number operation
is too large, ORL C, bit X
This flag is set whenever the
In general, the carry flag is ORL C, /bit X
the high-order bit to overflow into the sign bit.
causing arithmetic operations. The
overflow flag is only
used to detect errors in unsigned discussed in detail in MOV C, bit X
arithmetic operations and is
used to detect errors in signed X
Chapter 6.
CINE
Note: X can be 0 or 1.
Example 2-2
"SAM" in consecutive
a) Use assembler directives to place constants OFCH, 05H, 76H, 28D and character string
from location 0050H.
program memory locations beginning
Add the numbers 56H and 95H, and show how the CY, AC,
and P flags are affected.
b)
Solution:
a) ORG 050H
DB OFCH, 05H, 76H, 28
DB "SAM"
The program memory location will contain data as follows.
Address Data
0050 FC
0051 05
0052 76
0053 1C Hex equivalent of 28D
1. A total of 32 bytes from locations00 to 1F hex are set aside for register-banks-andthe stack.
2. A total of16 bytes from locations 20H to 2FHare set aside for bit-addressable read/write memory. A detailed dis-
cussion ofbit-addressable memory and instructions is given in Chapter 8.
3. A total of 80 bytes from locations 30H to 7FH are used for read and writestorage,or what is normally called a
scratch pad. These 80 locations of RAM are widely used for the purpose of storing data and parameters by 8051
programmers. We will use them in future chapters to store data brought into the CPU via I/O ports.
Example 2-7.
Bank 1 0
Bank 2
Stack in the 8051
store temporarily This information could he data.
hack is a section of RAM used by the CPU to a r e intormation or
Since there only a limited number of registers
an address. The
CPU needs this storage area
PROGRAMMING
SECTION 4.1:
In the 8051
8051 VO
there are a total of four ports for 1/O operations. Examining Figure 4-1, note that ofthethe 40
pinsniate
takes 8 pins. The rest of the pins
the four P0, P1, P2, and P3, where each port
of 32 pins are set aside for ports discussed in Chapter 8.
RST, EA, ALE/PROG and PSEN are
nated as VccGND, XTAL1, XTAL2,
Port 0
or To use the pins of port 0 as
Port 0 occupies atotal of 8 pins (pins 32 39). It can be used for input output.
to a 10K-ohm pull-up resistor.
This is due to the
and each must be connected externally
input output ports, pin the
and as we will soon see. Open
drain is a term used for MOS chips in
that PO is an open drain, unlike P1, P2, P3, PO to pu
the 8051/52 chip, we normally connect
used for TTL chips. In any system using
way that open collector is
PDIP/Cerdip
P1.0 1 40 Vcc
(RXD)P3.0 1 0 31 EA/VPP
30
ALE/PROG d s la t
(TXD)P3.1 11
INTO) P3.2 12 29 PSEN PJm
(TNTI)P3.3 13 28 P2.7 (A15)
(TO)P3.4 14 P2.6 (A14)
(T1)P3.5 1 5 26 P2.5 (A13)
WR) P3.6 16 P2.4 (A12)
(RD)P3.7 17 24 P2.3 (A11)
XTAL2 18 23 P2.2 (A10)
XTALI 19 22 P2.1 (A9)
GND 20 21 P2.0(A8)
YSTE
76 THE 8051 MICROCONTROLLER AND EMBEDDED "
resistors. See Figure 4-2. In this way we take advantage of
and output. For example, the follow
port 0 for both input Vcc
ing code will continuously send out to port 0 the alternat 10 K
55FH and AAH.
ing values of
b i t s of Po
Toggle a l l
MOV A, #55H
BACK:
MOV
PO.0
PO,A PO.1
ACALL DELAY
P0.2
MOV A, # 0AAH 8051 PO.3
MOV PO,,A PO.4
ACALL DELAY PO.5
PO.6
SJMP BACK
PO.7
It must be noted that complementing 55H (01010101)
55H and AAH
turns it into AAH (10101010). By sending
to a given port continuously,we toggle all the bits of that
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 followingcode, 0 is configured first as an input port by writing 1s to it, and then data is received from
port
that port and sent to P1.
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
since it has as
does not need anypul-up resistors already pull-up resistors internally, Uponreset port 1is configured
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 writ
ing 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.
/O PORT PROGRAMMING 77
MOV A, #OFFH ;A=FF hex
MOV P1,A make P1 an input port
MOV
by writing al1l 1s to it
A, P1 get data from P1
MOV R7,A ;save it in reg R7
ACALL DELAY wait
MOV A, P1
get another data from P1
MOV R6,A save it in reg R6
ACALL DELAY
wait
MOV A, P1 get another data from P1
MOV R5,A save it in reg R5
Port 2
Port 2
need any
occupies a
total of 8 pins (pins 21
28). It can be used as input or output. Just like P1,
pull-up resistors since it already hasthrough port 2
pull-up resistors internally. Upon reset, port 2 is configured
port. The following code will send out
all
of P2 toggle continuously to port 2 the alternating values 55H and AAH. That is,asa
continuously. t
MOV A, #55H
BACK MOV P2,A
ACALL DELAY
CPL A
Complement reg. A
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, port2
figured first as an input port by writing 1s to it. Then data is received from that port and is sent to Pl continuo
Port 3 eedan
17. It can be used as noti
input or output. P3 doestnsisnotthe
Port 3 occupies a total of 8 pins, pins 10 through
as
reset, i
3 is configured an input port upon
resistors, just as Pl and P2 did not. Although port
DEDS