Lecture #05
Lecture #05
Microprocessors and
Microcontrollers E1328
#Lecture_5
Dr. Mahmoud Adel Hassan,
Ph.D. Degree in Engineering Sciences in
Electrical Engineering,
Email: [email protected]
# Second Semester 2022/2023
Outline
➢Memory Organization
▪ General Purpose RAM
▪ Bit-Addressable RAM
▪ Register Banks
➢Special Function Registers
▪ Program Status Word
▪ B Register
▪ Stack Pointer
▪ Data Pointer
▪ Port Registers
Memory Organization
• Figure gives the details of the on-chip
data memory. As shown, the internal
data memory space is divided between
internal RAM (00H-7FH) and special
function registers (80H-0FFH).
MOV A,5FH
General Purpose RAM
• This instruction moves a byte of data using direct addressing to specify the "source location“
(i.e., address 5FH). The destination for the data is implicitly specified in the instruction opcode
as the A accumulator. Internal RAM can also be accessed using indirect addressing through R0
or R1. For example, the following two instructions perform the same operation as the single
instruction above:
MOV R0,#5FH
MOV A,@R0
• The first instruction uses immediate addressing to move the value 5FH into register R0, and
the second instruction uses indirect addressing to move the data "pointed at by R0“ into the
accumulator.
Bit-Addressable RAM
• The 8051 contains 210 bit-addressable locations, of which 128 are at byte addresses 20H
through 2FH.
• The idea of individually accessing bits through software is a powerful feature of most
microcontrollers. Bits can be set, cleared, ANDed, ORed, etc., with a single instruction.
SETB 67H
Referring to internal RAM Figure, note that "bit address 67H" is the most-significant bit at "byte
address 2CH." The instruction above has no effect on the other bits at this address. Most
microprocessors would perform the same operation as follows:
MOV A,R5
This instruction is a 1-byte instruction using register addressing. Of course, the same operation
could be performed in a 2-byte instruction, using the direct address as byte 2:
MOV A,05H
Instructions using registers R0 to R7 are shorter than the equivalent instructions using direct
addressing. Data values used frequently should use one of these registers. The active register
bank may be altered by changing the register bank select bits in the program status word .
Register Banks
Assuming, then, that register bank 3 is active, the following instruction writes the contents of the
accumulator into location 18H:
MOV R0,A
The idea of "register banks" permits fast and effective "context switching," whereby separate
sections of software use a private set of registers independent of other sections of software.
ADD A,#1
• The DIV AB instruction divides A by B, leaving the integer result in A and the remainder in
B. The B register can also be treated as a general-purpose scratch-pad register. It is bit-
addressable through bit addresses 0F0H to 0F7H.
Stack Pointer
The stack pointer (SP) is an 8-bit register at address 81H. It contains the address of the data item
currently on the top of the stack. Stack operations include "pushing" data on the stack and "popping"
data off the stack. Pushing on the stack increments the SP before writing data, and popping from the
stack reads data and then decrements the SP. The 8051 stack is kept in internal RAM and is limited to
addresses accessible by indirect addressing. These are the first 128 bytes on the 8031/8051 or the full
256 bytes of on-chip RAM on the 8032/8052.
To reinitialize the SP with the stack beginning at 60H, the following instruction is used:
MOV SP,#5FH
On the 8031/8051 this would limit the stack to 32 bytes, since the uppermost address of on-chip RAM is
7FH. The value 5FH is used, since the SP increments to 60H before the first push operation.
Data Pointer
The data pointer (DPTR), used to access external code or data memory, is a 16-bit register at addresses
82H (DPL, low-byte) and 83H (DPH, high-byte). The following three instructions write 55H into
external RAM location 1000H:
MOV A,#55H
MOV DPTR,#1000H
MOVX @DPTR,A
The first instruction uses immediate addressing to load the data constant 55H into the accumulator.
The second instruction also uses immediate addressing, this time to load the 16-bit address constant
1000H into the data pointer. The third instruction uses indirect addressing to move the value in A (55H)
to the external RAM location whose address is in the DPTR (1000H). The "X" in the mnemonic
"MOVX" indicates that the move instruction accesses external data memory.
Port Registers
The 8051 I/O ports consist of Port 0 at address 80H, Port 1 at address 90H, Port 2 at address
0A0H, and Port 3 at address 0B0H. Ports 0, 2, and 3 may not be available for I/O if external
memory is used or if some of the 8051 special features are used (interrupts, serial port, etc.).
Nevertheless, P1.2 to P1.7 are always available as general purpose I/O lines.
All ports are bit-addressable. This capability provides powerful interfacing possibilities. If a
motor is connected through a solenoid and transistor driver to Port 1 bit 7, for example, it could
be turned on and off using a single 8051 instruction:
SETB P1.7
might turn the motor on, and
CLR P1.7
might turn it off.
Port Registers
The instructions above use the dot operator to address a bit within a bit-addressable byte
location. The assembler performs the necessary conversion; thus, the following two
instructions are the same:
CLR P1.7
CLR 97H
In another example, consider the interface to a device with a status bit called BUSY, which is
set when the device is busy and clear when it is ready. If BUSY connects to, say, Port 1 bit 5,
the following loop could be used to wait for the device to become ready:
WAIT: JB P1.5,WAIT
This instruction means "if the bit P1.5 is set, jump to the label WAIT." In other words "jump
back and check it again."