Professional Documents
Culture Documents
ECP 2216
Chapter 2 – The MCS-51 μC
ECP 2216
2.2 8051 Memory Organization
• 8051 implements a separate address space for programs (code)
and data memory.
• Both code and data memory may be internal, however both may
be expand using external memory components to a maximum of
64K code memory and 64K data memory.
8051
Program Data
Memory Memory
[ROM] [RAM]
ECP 2216 3
2.2.1 8051 Memory Structure (internal)
Data Program
Memory Memory
(Read/Write) (Read Only)
On-chip On-chip
(Internal RAM) (Internal ROM)
FFH 0FFFH
Upper 128
part
SFR
bytes 4Kbytes
7FH
128 Lower
bytes part
00H 0000H
(EA = 1)
ECP 2216 4
2.2.1 8051 Memory Structure (external)
Data Program
FFFFH FFFFH
Memory Memory
(Read/Write) (Read Only)
External External
Data Code
Memory Memory
(RAM) (ROM)
64K bytes 64K bytes
Enabled via Enabled via
WR and RD PSEN
0000H
1000H
(EA = 0)
ECP 2216 5
• The on-chip RAM contains of arrangements of general-purpose storage, bit-
addressable storage, register banks, and special function register (SFR).
7F FF
General-purpose
RAM
FF
Upper
Lower
2F Register
00 (SFR)
Bit-addressable
Locations
20
1F Bank 3
Bank 2
Bank 1
Bank 0 80
00
ECP 2216 6
2.2.2 Register Banks
• The bottom 32 locations of internal memory contain the register banks.
• The instruction set supports 8 registers, R0 through R7.
• By default (after a system reset), Bank 0 at addresses 00H – 07H is
used.
1F
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
00
• The active register bank may be altered by changing the register bank
select bits in the program status word (PSW) in SFR.
• The advantage of having register banks is to permit fast and effective
context switching.
ECP 2216 7
2.2.3 Bit Addressable RAM
• 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.
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
2F
25 2F 2E 2D 2C 2B 2A 29 28 2D 6F 6E 6D 6C 6B 6A 69 68
Bit-addressable 24 27 26 25 24 23 22 21 20 2C 67 66 65 64 63 62 61 60
Locations 23 1F 2E 2D 1C 1B 1A 19 18 2B 5F 5E 5D 5C 5B 5A 59 58
22 17 16 15 14 13 12 11 10 2A 57 56 55 54 53 52 51 50
20
21 0F 0E 0D 0C 0B 0A 09 08 29 4F 4E 4D 4C 4B 4A 49 48
20 07 06 05 04 03 02 01 00 28 47 46 45 44 43 42 41 40
SETB 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
2F
25 2F 2E 2D 2C 2B 2A 29 28 2D 6F 6E 6D 6C 6B 6A 69 68
Bit-addressable 24 27 26 25 24 23 22 21 20 2C 67 66 65 64 63 62 61 60
Locations 23 1F 2E 2D 1C 1B 1A 19 18 2B 5F 5E 5D 5C 5B 5A 59 58
22 17 16 15 14 13 12 11 10 2A 57 56 55 54 53 52 51 50
20
21 0F 0E 0D 0C 0B 0A 09 08 29 4F 4E 4D 4C 4B 4A 49 48
20 07 06 05 04 03 02 01 00 28 47 46 45 44 43 42 41 40
ECP 2216 9
2.2.4 General-purpose RAM
• General-purpose RAM occupies 80 bytes of RAM location from
addresses 30H to 7FH.
• Any location in this area can be accessed freely using direct or
indirect addressing modes (discussion on addressing mode will be on
Chapter 3).
7F
30
ECP 2216 10
2.2.5 Special Function Registers (SFR)
ECP 2216 11
Following sections will discuss the functions of common SFRs.
Ex:
Result:
MOV R5, #55H
ACC = FFH
MOV A, #0AAH
ADD A, R5 C=0
ACC = 00H
ADD A, #1 C=1
ORL C, 24H
ECP 2216 13
• Auxiliary Carry Flag (AC) is set in BCD addition when:
o there’s a carry out of bit 3 into bit 4, or
o the result in the lower nibble is in the range of 0AH – 0FH.
Ex:
Result:
MOV R5, #1
MOV A, #9 ACC = 0AH (0000 1010)
ADD A, R5 AC = 1
ECP 2216 14
• Register Bank Select Bits (RS1 and RS0) select the active register
bank.
• Default register bank used is Bank 0, where RS1 and RS0 are cleared
after a system reset.
RS1 RS0
Bank 0 0 0
Bank 1 0 1
Bank 2 1 0
Bank 3 1 1
POP ACC
Ex: Result
SETB P1.7 [97H ] = 1
MOV P2, #01H [A0H] = 00000001
ECP 2216 19
2.2.5.6 Timer Registers (TMOD, TCON, TH0, TL0, TH1, TL1)
• The 8051 contains two 16-bit timer registers for timing intervals or
counting events.
• Timer 0 is at addresses 8AH (low-byte) and 8CH (high-byte)
• Timer 1 is at addresses 8BH (low-byte) and 8DH (high-byte)
• Timer operation is set by the timer mode register (TMOD) at address
89H and the timer control register (TCON) at address 88H.
ECP 2216 20
2.2.5.8 Interrupt Registers
• Interrupts are disabled after a system reset and then enabled by
writing to the interrupt enable register (IE) at address A8H.
• Priority level for each interrupts can be set through interrupt priority
register (IP) at address B8H. Both registers are bit-addressable.
• The SMOD bit doubles the serial port baud rate when set
• GF1 and GF0 are available for general-purpose usage.
• 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.
ECP 2216 21
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 Port 0 is unavailable as an I/O port. It becomes a multiplexed
address (A0 – A7) and data (D0 – D7) bus.
o Port 2 is usually employed for the high-byte of address bus.
Port 0 D0 – D7
74HC373
EA D G A0 – A7
ALE G
Port 2 A8 – A15
PSEN OE
ECP 2216 23
Read Timing Diagram [From External ROM]
Oscillator
ALE
PSEN
Op code
Port 0 PCL PCL Byte 2
ECP 2216 24
24
Connections to be remembered:
i) PSEN OE
ii) EA Grounded
iii) ALE G of Latch
iv) Port 0 Lower Address Bus (A0 – A7)
v) Port 2 Upper Address Bus (A8 – A15)
ECP 2216 25
8051 RAM
Port 0 D0 – D7
VCC 74HC373
D Q A0 – A7
EA
ALE
G
Port 2
A8 – A15
RD
OE
WR WE
ECP 2216 26
MOVX A, @DPTR [Reading from External Data Memory]
State 1 State 2 State 3 State 4 State 5 State 6 State 1 State 2 State 3 State 4 State 5 State 6 State 1
ALE
PSEN
RD
Op code Data in
Port 0 PCL DPL
ECP 2216 27
MOVX @DPTR, A [Writing onto External Data Memory]
State 1 State 2 State 3 State 4 State 5 State 6 State 1 State 2 State 3 State 4 State 5 State 6 State 1
ALE
PSEN
WR
Op code
Port 0 PCL DPL Data out
ECP 2216 28
2.3.3 Address Decoding
• If multiple ROMs and/or RAMs are interfaced to an 8051, address
decoding is required.
• A decoder IC, such as 74HC138 is used with its outputs connected
to the chip select (CS) inputs on the memory ICs. The 8051 can
accommodate up to 64K each of ROM and RAM.
A15-A13 (CS)
74HC138
A15 C 0 000 RAM #1
A14 B 1
001 RAM #2
A13 A 2 010 RAM #3
3
4 100 ROM #1
101 ROM #2
5
110 ROM #3
6
7
ECP 2216 29
Address bus
Data bus
ROM RAM
(8K bytes) (8K bytes)
D0 – D8 D0 – D8
PSEN RD OE
OE
WR WE
A0 – A12 A0 – A12
CS CS
CS CS
CS CS
74HC138
0
C 1
B 2
A
3
4
5
6
7
ECP 2216 30
2.3.5 Design Problem 1
Design an 8051-based system with 32Kbytes of ROM
(32Kb ROM & RAM ICs are available)
Solution
ECP 2216 32
8051
32 K ROM
P0.0 – P0.7 D0 – D7
74373
EA D Q
A0 – A7
ALE G
A8 – A14
P2.0 – P2.6
PSEN OE
CS
ECP 2216 33
2.3.4 Design Problem 2
Design an 8051-based system with 8Kbytes of external data RAM.
(4K*8 bit Memory IC’s are available. Assume internal ROM is used)
Solution
Step 1- Find number of memory blocks needed..
• Target = 8 Kbytes
• Available = 4KBytes
• Needed = 2 blocks of 4KBytes* 8 bits
Step 2- Find the total address line needed to accommodate memory
space given.. 2n = Memory Space
2n = 4K
log2 2n = log2 4K
n log2 2 = log2 4K
n = log2 4K
log2 2
= 12 address lines (A0-A11)
P0 to accommodate 8 lines P2 to accommodate another 4 lines
ECP 2216 (P0.0 – P0.7) (P2.0 – P2.3)
34
Step 3- Determine number of data bus
• 8 bit= 8 data buses.
8051
Port 0
VCC 74373
EA D Q
ALE G
4 K RAM
P2.0 – P2.3
A12
D0 – D 7
P2.4
RD A0 – A 7
WR
A8 – A11
PSEN
CS
Not connected
OE
WE
ECP 2216 36
2.3.6 Design Exercise
You are given 32Kb ROM & RAM ICs. Design an 8051-
based system with 32Kbytes of ROM and 32Kbytes of RAM
Solution
8051 32 K x 8 ROM
D0 – D7
74373
EA D Q
A0 – A7
ALE G
A8 – A14 32 K x 8 RAM
P2.0 – P2.6
D0 – D7
PSEN OE
A15 A0 – A7
P2.7 CS
A8 – A14
CS
OE
RD
WE
WR
ECP 2216 37
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.
• The state of all the 8051 registers after a system reset summarized as:
REGISTER(S) CONTENTS
Program Counter (PC) 0000H
Accumulator (ACC) 00H
B Register 00H
PSW 00H
Stack Pointer (SP) 07H
Data Pointer (DPTR) 0000H
P0, P1, P2 and P3 FFH
Interrupt Priority (IP) XXX00000B
Interrupt Enable (IE) 0XX00000B
Timer Registers 00H
SCON 00H
SBUF 00H
PCON (CMOS) 0XXX0000B
ECP 2216 38