Chapter 2 - The 8051 Microcontroller (Students)
Chapter 2 - The 8051 Microcontroller (Students)
Chapter 2 - The 8051 Microcontroller (Students)
2.1
2.2
Memory Organization
2.3
Memory Mapping
2.1
2.1.1
2.1.2
C1, C2 = 30 pF 10 pF
C2
XTAL1 [Pin 19]
2.1.3
Control Pins
Activated (held low) when executing code from the external program
memory.
2.1.4
I/O Pins
Port 0
Port 1
Port 2
Port 3
i)
Port 0
o
o
o
ii) Port 1
o
o
iii) Port 2
o
o
iv) Port 3
o
o
P3.1
Name
Bit Address
RXD
B0H
TXD
P3.2
INT0
P3.4
T0
P3.3
P3.5
P3.6
P3.7
INT1
T1
WR
RD
B1H
B2H
B3H
B4H
B5H
B6H
B7H
Alternate Function
Receive data for serial port
External interrupt 0
External interrupt 1
2.2
8051
Program
Memory
Data
Memory
[ROM]
[RAM]
10
2.2.1
Data
Memory
Program
Memory
FFFF
(Read/Write)
(Read Only)
On-chip
(Internal)
Data Memory
(RAM)
FF
128
bytes
7F
00
Upper
part
Lower
part
SFR
External
Data
Memory
(RAM)
External
Code
Memory
(ROM)
Enabled via
PSEN
Enabled via
WR and RD
1000
4Kb
On-chip
ROM
0FFF
(EA=1)
0000
64K bytes
FFFF
External
ROM
(EA=0)
0000
11
The on-chip RAM contains of arrangements of general-purpose storage, bitaddressable storage, register banks, and special function register (SFR).
7F
30
2F
20
1F
00
Bit-addressable
Locations
Bank 3
Bank 2
Bank 1
Bank 0
Special Function
Register
(SFR)
Lower
On-chip
RAM
Upper
General-purpose
RAM
FF
00
FF
80
12
2.2.2
Register Banks
00
Bank 3
R7 R6 R5 R4 R3 R2 R1 R0
Bank 2
R7 R6 R5 R4 R3 R2 R1 R0
Bank 1
R7 R6 R5 R4 R3 R2 R1 R0
Bank 0
R7 R6 R5 R4 R3 R2 R1 R0
The active register bank may be altered by changing the register bank
select bits in the program status word (PSW) in SFR.
13
2.2.3
The 8051 contains 210 bit-addressable locations, where 128 are at byte
addresses of 20H to 2FH, and the rest are in the SFR.
All bits in this area can be set (1), cleared (0), ANDed, ORed, and so on
with a single instruction.
2F
Bit-addressable
Locations
20
27
3F
3E 3D 3C 3B 3A 39 38
2F
7F
7E 7D 7C 7B 7A 79 78
26
37 36 35 34 33 32 31 30
2E
77 76 75 74 73 72 71 70
25
2F
2E 2D 2C 2B 2A 29 28
2D
6F
24
27 26 25 24 23 22 21 20
2C
67 66 65 64 63 62 61 60
23
1F
2E 2D 1C 1B 1A 19 18
2B
5F
22
17 16 15 14 13 12 11 10
2A
57 56 55 54 53 52 51 50
21
0F
0E 0D 0C 0B 0A 09 08
29
4F
20
07 06 05 04 03 02 01 00
28
47 46 45 44 43 42 41 40
6E 6D 6C 6B 6A 69 68
5E 5D 5C 5B 5A 59 58
4E 4D 4C 4B 4A 49 48
14
For example, to set bit 3 in byte address 25H, we use the instruction:
SETB
2F
Bit-addressable
Locations
20
2BH
27
3F
3E 3D 3C 3B 3A 39 38
2F
7F
7E 7D 7C 7B 7A 79 78
26
37 36 35 34 33 32 31 30
2E
77 76 75 74 73 72 71 70
25
2F
2E 2D 2C 2B 2A 29 28
2D
6F
24
27 26 25 24 23 22 21 20
2C
67 66 65 64 63 62 61 60
23
1F
2E 2D 1C 1B 1A 19 18
2B
5F
22
17 16 15 14 13 12 11 10
2A
57 56 55 54 53 52 51 50
21
0F
0E 0D 0C 0B 0A 09 08
29
4F
20
07 06 05 04 03 02 01 00
28
47 46 45 44 43 42 41 40
6E 6D 6C 6B 6A 69 68
5E 5D 5C 5B 5A 59 58
4E 4D 4C 4B 4A 49 48
2BH, #08H
15
2.2.4
General-purpose RAM
General-purpose RAM occupies 80 bytes of RAM location from
addresses 30H to 7FH.
MOV
General-purpose
RAM
30
A, 5FH
MOV
MOV
@R0, 5FH
34H, #R1
16
2.2.5
Some of the SFR may not be bitaddressable, while some are both byteaddressable and bit-addressable.
For example, the instruction
SETB 0E0H
17
Contains several status bits that reflect the current state of CPU as
summarized below:
BIT
PSW.7
PSW.6
PSW.5
PSW.4
PSW.3
PSW.2
PSW.1
PSW.0
SYMBOL
ADDRESS
CY
AC
-RS1
RS0
OV
-P
D7H
D6H
D5H
D4H
D3H
D2H
D1H
D0H
BIT DESCRIPTION
Carry Flag
Auxiliary Carry Flag
Undefined
Register Bank Select 1
Register Bank Select 0
Overflow Flag
Undefined
Parity Flag
18
Result:
ACC =
C=
ACC =
C=
ADD A, #1
For example, the following instruction ORs bit 24H with the CY and
places the result back in the CY flag.
ORL
C, 24H
19
If the values added are BCD, then the ADD instruction must be
followed by DA A (decimal adjust accumulator) to bring results
greater than 910 back into range.
For example, (with initial ACC = 0)
MOV R5, #1
MOV A, #9
ADD A, R5
Result:
ACC =
AC =
SETB, F0
PSW.5 =
20
Register Bank Select Bits (RS1 and RS0) determine the active
register bank.
Default register bank used is Bank 0, where RS1 and RS0 are cleared
after a system reset.
Bank
Bank
Bank
Bank
0
1
2
3
RS1
0
0
1
1
SETB RS1
SETB RS0
MOV R7, #1
Bank 3
SETB RS1
CLR RS0
MOV R7, #1
Bank 2
CLR RS1
SETB RS0
MOV R7, #1
Bank 1
Bank 0
RS0
0
1
0
1
1FH
18H
17H
10H
0FH
08H
07H
00H
21
Theres a carry out of bit 7 and no carry out from bit 6 to bit 7
Result:
ACC =
OV =
22
Result:
P=
MOV A, #55H
MOV B, #22H
MUL AB
ACC =
B
=
23
Stack operations include pushing data on the stack and popping data
off the stack.
o Pushing increments the SP before writing data.
o Popping reads data and decrement s the SP.
The default value of SP upon system reset is 07H. For example, to
reinitialize the SP with the stack begin at 60H:
MOV SP, #5FH
PUSH A
PUSH PSW
POP PSW
POP A
7F
.
.
.
.
.
.
62
61
60
5F
PSW
A
24
25
Example:
SETB P1.7
[97H ] = 1
[A0H] = 00000001
The 8051 contains two 16-bit timer registers for timing intervals or
counting events.
26
Serial data buffer (SBUF) at address 99H, holds both the transmit data
and receive data.
Priority level for each interrupts can be set through interrupt priority
register (IP) at address B8H.
Both register are bit-addressable.
27
--
--
--
GF1
GF0
PD
IDL
The SMOD bit doubles the serial port baud rate when set
GF1 and GF0 are available for general-purpose flag bits for user
applications.
The power control bits, power down (PD) and idle (IDL), were
originally available in all MCS-51 family ICs but are now
implemented only in CMOS versions.
PCON is not bit-addressable.
28
2.3
Memory Mapping
The MCS-51 architecture provides memory expansion capability, in the
form of 64K external code memory and 64K external data memory.
Extra ROM and RAM can be added as desired. Peripheral interface ICs
can also be added to expand the I/O capability.
When external memory is used:
o
o
2.3.1
The hardware connections for external ROM memory are shown in the
next slide.
29
8051
ROM
Port 0
D0 D7
74HC373
EA
ALE
A0 A7
Port 2
A8 A15
PSEN
OE
30
State 2
P1 P2
State 3
P1 P2
State 4
P1 P2
State 5
P1 P2
Op
code
PCL
State 6
P1 P2
State 1
P1 P2
Oscillator
ALE
PSEN
Port 0
Port 2
PCL
PCH
Byte 2
PCH
31
Connections to be remembered:
i) PSEN
ii) EA
iii) ALE
iv) Port 0
v) Port 2
2.3.2
OE
Grounded
G of Latch
Lower Address Bus (A0 A7)
Upper Address Bus (A8 A15)
The only access to external data memory is with the MOVX instruction,
either using the 16-bit DPTR, R0 or R1 as the address register.
RAMs maybe interfaced to the 8051 the same way as ROMs except:
o the RD line connects to the RAMs output enable, OE line
o the WR line connects to the RAMs write enable, WE line.
32
8051
RAM
Port 0
D0 D7
VCC
74HC373
D
A0 A7
EA
ALE
Port 2
RD
WR
G
A8 A15
OE
WE
33
Machine cycle 2
State State State State State State State State State State State State State
1
2
3
4
5
6
1
2
3
4
5
6
1
ALE
PSEN
RD
Port 0
Port 2
Op
code
PCL
PCH
Data
in
DPL
DPH
34
Machine cycle 2
State State State State State State State State State State State State State
1
2
3
4
5
6
1
2
3
4
5
6
1
ALE
PSEN
WR
Port 0
Port 2
Op
code
PCL
PCH
DPL
Data out
DPH
35
2.3.3
Address Decoding
If multiple ROMs and/or RAMs are interfaced to an 8051, address
decoding is required.
A15
A14
A13
2
3
4
5
6
7
A15-A13
(CS)
000
RAM #1
010
RAM #3
100
ROM #1
101
ROM #2
110
ROM #3
001
RAM #2
36
Address bus
Data bus
D0 D8
PSEN
OE
C
B
A
RD
OE
WR
D0 D8
A0 A12
A0 A12
CS
CS
CS
74HC138
ROM
(8K bytes)
RAM
(8K bytes)
CS
CS
CS
0
1
2
3
4
5
6
7
37
2.3.4
Design Problem 1
Solution
Step 1- Find number of memory blocks needed..
Target = 8 Kbytes
Available = 4KBytes
Needed = 2 blocks of 4KBytes* 8 bits
P0 to accommodate 8 lines
(P0.0 P0.7)
38
Chip Select
Upper Bytes Address Lower Bytes Address
(A12 <-> P2.4) (A8-A11 <-> P2.0-P2.3) (A0-A7 <-> P0.0-P0.7)
12
02
1111 2
1111 1111 2
0000 2
0000 0000 2
1111 2
1111 1111 2
0000 2
0000 0000 2
39
4 K RAM
Port 0
D0 D7
VCC
EA
ALE
74373
D Q
A0 A7
P2.0 P2.3
4 K RAM
A8 A11
A12
CS
D0 D7
RD
OE
WR
WE
A0 A7
P2.4
PSEN
Not connected
A8 A11
CS
OE
WE
40
2.3.5
Design Problem 2
Design an 8051-based system with 32Kbytes of ROM
(32Kb ROM & RAM ICs are available)
Solution
Step 1- Find number of memory blocks needed..
Target =32Kbytes
Available = 32KBytes
Needed =
Memory Space
32K
25 * 210
215
P0 to accommodate 8 lines
(P0.0 P0.7)
41
8051
32 K ROM
42
2.3.6
Design Problem 3
You are given 32Kb ROM & RAM ICs. Design an 8051based system with 32Kbytes of ROM and 32Kbytes of RAM
Solution
8051
32 K x 8 ROM
D0 D7
74373
D Q
A0 A7
A8 A14
P2.0 P2.6
PSEN
P2.7
OE
A15
CS
RD
WR
32 K x 8 RAM
D0 D7
A0 A7
A8 A14
CS
OE
WE
43
2.3.7
Reset Operation
The 8051 is reset by holding RST high for at least two machine cycles
and then returning it to low. RST may be activated by a switch, or it may
be activated upon power-up using RC (resistor-capacitor) network.
CONTENTS
0000H
00H
00H
00H
07H
0000H
FFH
XXX00000B
0XX00000B
00H
00H
00H
0XXX0000B
44