Documente Academic
Documente Profesional
Documente Cultură
Introduction
1 Concept of microcontroller
2 Microprocessor and Microcontroller
3 Comparison of all 8- bit microcontrollers
4 8051 architecture
5 Pin Diagram
6 Memory Organization
7 Special Function Registers
8 Internal Structure of I/O ports
9 Interfacing of 8051 with external memory
1. Concept of Microcontroller
The MCU contains a flash memory where it stores its program . The flash
memory is easily erased and a new program can burned. This makes it very
flexible. MCUs can be programmed few thousand times before they die.
The features of 8051 microcontroller are :
8051 CPU consists of 8-bit ALU with associted registers like accumulator A ,B
register , PSW ,SP , 16-bit program counter, stack pointer.
ALU
Microprocessor Microcontroller
It has many instructions to move data It has few instructions to move data
between memory and CPU between memory and CPU.
Single memory map for data and Separate memory map for data and
code(program) code (program)
Access time for memory and I/O are It requires less additional hardware.
more.
Microcontrollers with small instruction set are called reduced instruction set
computer (RISC) machines and those with complex instruction set are called
complex instruction set computer (CISC). Intel 8051 is an example of CISC
machine whereas microchip PIC 18F87X is an example of RISC machine.
The Harvard architecture has separate program memory and data memory. The
instruction code and data can be fetched simultaneously.
The PIC16C5X/XX microcontrollers is attributed primarily to its RISC
architecture.
It employs Harvard architecture that is a separate program and memory and data
memory space.
It uses two stage pipelining instruction fetch and execution . All instructions are
executed in a single cycle.
In this example we take two bytes in RAM or registers, each containing a BCD
digit in the lower nibble and create a packed BCD data byte, which is stored
back in the register or RAM location holding the low BCD digit.
SWAP F REGHI,W 1 1
IORWF REG LO 1 1
0.4µs
ST 62
LD A,REGHI 2 4
RLC A 1 4
RLC A 1 4
RLC A 1 4
RLC A 1 4
ADD A,REGLO 1 4
LD REGLO,A 1 4
10 28
10 28
45.5µs
Z86CXX
SWAP REGHI 2 8
OR REGHI,REGLO 2 6
10 28
5.33 µs
COP 800
SWAP REGHI 1 2
OR REGHI,REGLO 1 1
OR A,[B] 1 1
X A,[B] 1 1
4 5
5 µs
MC68HC05
LDA REG HI 2 3
ROLA 1 3
ROLA 1 3
ROLA 1 3
ROLA 1 3
ADD REGLO 2 3
STA REGLO 2 4
10 22
10.5µsec
8051
MOV A,Rx 1 1
SWAP 1 1
ORL A,Ry 1 1
MOV Ry,A 1 1
4 4
2.4µs
LOOP CONTROL
This example is one of simple loop control where the register containing a loop
count is decremented, tested for zero, and if not zero, then branched back to the
beginning of the loop.
2 3/2 2 6
0.6 µs 6 µs
ST 62 MC68HC05
DEC X 1 4 DEC X 1 3
2 6 3 6
9.75 µs 2.86µs
Z86CXX 8051
1.67 µs 1.2µs
BIT TEST & BRANCH
This example tests a single bit in a register or a RAM location and makes a
conditional branch. We assume that the MSb is tested and a branch is to be taken if
the bit is set
2 3/2 2 6
0.6 µs 4 µs
ST 62 MC68HC05
8.125 µs 2.38µs
Z86CXX 8051
0.6 µs 4µs
MOV A,@Rx 1 1
JB A.7 , NEWADD 3 2
SOFTWARE TIMER
PIC 16C5X/XX
Byte/Word Cycles
MOVWF COUNT 2 1 1
CLRF COUNT1 1 1
GOTO LOOP 1 2
GOTO LOOP 1 1
RET 1 1
COP 800
Byte/Word Cycles
LD B, #0EH 1 1
DRSZ B 1 1
GOTO LOOP 1 2
DECFSZ COUNT2 1 2/1
GOTO LOOP 1 1
RET 1 1
ST62
Byte/Word Cycles
LDI A, #FF 2 4
LD X,A 1 4
LDI A,#04 2 4
LD Y,A 1 4
JRNZ LOOP 1 4
DEC Y 1 2
JRNZ LOOP 1 4
RET 1 2
10
4. 8051 architecture
It is an 8-bit microcontroller.
It is built with 40 pins DIP (dual inline package), 4kb of ROM storage and 128
bytes of RAM storage, 2 16-bit timers.
It consists of are four parallel 8-bit ports, which are programmable as well as
addressable depending on the requirement.
An on-chip crystal oscillator is integrated in the microcontroller having crystal
frequency of 12 MHz.
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If
the program size is more than 4 K Bytes 8051 will fetch the code automatically
from external memory.
Accumulator is an 8 bit register widely used for all arithmetic and logical
operations. Accumulator is also used to transfer data between external memory.
B register is used along with Accumulator for multiplication and division. A and B
registers together is also called MATH registers.
PSW (Program Status Word). This is an 8 bit register which contains the
arithmetic status of ALU and the bank select bits of register banks
CY AC F0 RS1 RS0 OV - P
CY - carry flag
OV – overflow
P - parity
It contains the address of the data item on the top of the stack. Stack may
reside anywhere on the internal RAM. On reset, SP is initialized to 07 so
that the default stack will start from address 08 onwards.
Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer
lower byte). This is a 16 bit register which is used to furnish address
information for internal and external program memory and for external
data memory.
8051 microcontrollers has 4 I/O ports each of 8-bit, which can be configured as
input or output. Hence, total 32 input/output pins allow the microcontroller to be
connected with the peripheral devices.
Pin configuration, i.e. the pin can be configured as 1 for input and 0 for
output as per the logic state.
o Input/Output (I/O) pin − All the circuits within the microcontroller
must be connected to one of its pins except P0 port because it does not
have pull-up resistors built-in.
Port 0 − The P0 (zero) port is characterized by two functions −
o When the external memory is used then the lower address byte
(addresses A0A7) is applied on it, else all bits of this port are
configured as input/output.
o When P0 port is configured as an output then other ports consisting of
pins with built-in pull-up resistor connected by its end to 5V power
supply, the pins of this port have this resistor left out.
The 8051 Microcontroller Memory is separated into Program Memory (ROM) and Data
Memory (RAM). The Program Memory of the 8051 Microcontroller is used for storing the
program to be executed, which means instructions. The Data Memory is used for storing
temporary variable data and intermediate results.
8051 Microcontroller has both Internal ROM and Internal RAM. If the internal memory is
inadequate, you can add external memory using suitable circuits.
o For External Access Pin (EA Pin) must be pulled HIGH i.e. when
the EA Pin is high, the CPU first fetches instructions from the
Internal Program Memory in the address range of 0000H to 0FFFFH
and if the memory addresses exceed the limit, then the
instructions are fetched from the external ROM in the address
range of 1000H to FFFFH.
There is also an alternative method to fetch the instructions that is ignore the Internal
ROM and fetch all the instructions only from External Program Memory(External ROM).
For this purpose EA pin must be connected to GND . In this case the memory addresses
of external ROM will be from 0000H to FFFFH.
Data Memory (RAM) of 8051 Microcontroller
Currently 8051 Microcontroller have 256B of RAM. Of this 256B, the first 128B
memory addresses from 00H to 7FH is divided in to Working Registers organized as
Register Banks, Bit – Addressable Area and General Purpose RAM known as
Scratchpad area.
In the first 128B of RAM (from 00H to 7FH), the first 32B that is memory from
addresses 00H to 1FH consists of 32 Working Registers that are organized as four
banks with 8 Registers in each Bank.
Lower 128B(00H to 07H) Upper 128B(80H-FFH)
(Direct and Indirect Addressing) (Direct Addressing) (Indirect Addressing)
The 4 banks are named as Bank0, Bank1, Bank2 and Bank3. Each Bank consists of 8
registers named as R0 – R7. Each Register can be addressed in two ways: by name
or by address.
To address the register by name, first the corresponding Bank has to be selected. In
order to select the bank, use RS0 and RS1 bits of the Program Status Word (PSW)
Register (RS0 and RS1 are 3rd and 4th bits in the PSW Register).
When addressing the Register using its address say 12H for example, the
corresponding Bank may or may not be selected. (12H corresponds to R2 in Bank2).
The next 16B of the RAM i.e. from 20H to 2FH are Bit – Addressable memory
locations. There are totally 128 bits that can be addressed individually using 00H to
7FH or the entire byte can be addressed as 20H to 2FH.
The final 80B of the internal RAM addresses from 30H to 7FH, is the general purpose
RAM area which are byte addressable.
o The 8051 Microcontroller Special Function Registers act as a control table that
monitor and control the operation of the 8051 Microcontroller.
o In Internal RAM Structure, the Address Space from 80H to FFH is allocated to
SFRs.
o Out of these 128 Memory Locations (80H to FFH), there are only 21 locations
that are actually assigned to SFRs.
o Each SFR has one Byte Address and also a unique name which specifies its
purpose.
They are:
Pointer Registers: DPTR (Data Pointer – DPL, DPH) and SP (Stack Pointer)
I/O Port Latches: P0 (Port 0), P1 (Port 1), P2 (Port 2) and P3 (Port 3)
A or Accumulator (ACC)
The Accumulator or Register A is the most important and most used 8051 Microcontroller
SFRs. The Register A is located at the address E0H in the SFR memory space. The
Accumulator is used to hold data for almost all ALU Operations.
0 0 0 0 0 0 0 0
MSB LSB
B (Register B)
The B Register is used along with the accumulator in Multiplication and Division operations.
These two operations are performed on data that are stored only in Registers A and B.
During Multiplication Operation, one of the operand either the multiplier or multiplicand is
stores in B Register and also the higher byte of the result.
In case of Division Operation, the B Register holds the divisor and also the remainder of the
result. It can be used as a General Purpose Register for normal operations and is often used
as an Auxiliary Register by Programmers to store temporary results.
0 0 0 0 0 0 0 0
The PSW or Program Status Word Register is called as Flag Register is one of the important
SFRs. The PSW Register consists of Flag Bits, which help the programmer in checking the
condition of the result and make decisions.
Flags are 1-bit storage elements that store and indicate the nature of the result that is
generated by execution of certain instructions. The following image shows the contents of
the PSW Register.
0 0 0 0 0 0 0 0
0 0 Bank 0
0 1 Bank1
1 0 Bank2
1 1 Bank3
Pointer Registers
The Data Pointer is a 16-bit Register and is physically the combination of DPL (Data Pointer
Low) and DPH (Data Pointer High) SFRs. The Data Pointer can be used as a single 16-bit
register (as DPTR) or two 8-bit registers (as DPL and DPH).
DPTR doesn’t have a physical Memory Address but the DPL (Lower Byte of DPTR) and DPH
(Higher Byte of DPTR) have separate addresses in the SFR Memory Space. DPL = 82H and
DPH = 83H.
The DPTR Register is used by the programmer addressing external memory (Program –
ROM or Data – RAM).
DPH DPL
DPTR
DPL
DPH
SP or Stack Pointer points to the top of the Stack and it indicates the next data to be
accessed. Stack Pointer can be accessed using PUSH, POP, CALL and RET Instructions. The
Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized with 07H.
When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1 and the new data is written at an address SP+1. When reading data from
stack, the data is retrieved from the Address in SP and after that the SP is decremented by 1
(SP-1).
0 0 0 0 0 0 0 0
The 8051 Microcontroller four Ports which can be used as Input and/or Output. These four
ports are P0, P1, P2 and P3. Each Port has a corresponding register with same names (the
Port Registers are also P0, P1, P2 and P3). The addresses of the Port Registers are as follows:
P0 – 80H, P1 – 90H, P2 – A0H and P2 – B0H.
Each bit in these SFRs corresponds to one physical Pin in the 8051 Microcontroller. All these
Port Registers are both Bit Addressable and Byte Addressable. Writing 1 or 0 on a Port
Register Bit will reflect as an appropriate voltage (5V and 0V) on the corresponding Pin.
If a Port Bit is SET (declared as 1), the corresponding Port Pin will be configured as Input and
similarly if a Port Bit is CLEARED (declared as 0), the corresponding Port Pin is configured
as Output. Upon reset, all the Port Bits are SET (1) and hence, all the Port Pins are configured
as Inputs.
1 1 1 1 1 1 1 1
P0
1 1 1 1 1 1 1 1
P1
1 1 1 1 1 1 1 1
P2
1 1 1 1 1 1 1 1
The PCON or Power Control register,is used to control the 8051 Microcontroller’s Power
Modes and is located at 87H of the SFR Memory Space. Using two bits in the PCON Register,
the microcontroller can be set to Idle Mode and Power Down Mode.
During Idle Mode, the Microcontroller will stop the Clock Signal to the ALU (CPU) but it is
given to other peripherals like Timer, Serial, Interrupts, etc. In order to terminate the Idle
Mode, use an Interrupt or Hardware Reset.
In the Power Down Mode, the oscillator will be stopped and the power will be reduced to
2V. To terminate the Power Down Mode, use the Hardware Reset.
Apart from these two, the PCON Register can also be used for few additional purposes. The
SMOD Bit in the PCON Register is used to control the Baud Rate of the Serial Port.
There are two general purpose Flag Bits in the PCON Register, which can be used by the
programmer during execution.
0 0 0 0 0 0 0 0
The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial Port. It is
located at an address of 98H. Using SCON, helps in the control the Operation Modes of the
Serial Port, Baud Rate of the Serial Port and Send or Receive Data using Serial Port.
SCON Register also consists of bits that are automatically SET when a byte of data is
transmitted or received.
0 0 0 0 0 0 0 0
MSB LSB
SCON
Timer Control or TCON Register is used to start or stop the Timers of 8051 Microcontroller. It
also contains bits to indicate if the Timers has overflowed. The TCON SFR also consists of
Interrupt related bits.
0 0 0 0 0 0 0 0
MSB LSB
The TMOD or Timer Mode register or SFR is used to set the Operating Modes of the Timers
T0 and T1. The lower four bits are used to configure Timer0 and the higher four bits are used
to configure Timer1.
0 0 0 0 0 0 0 0
The Gatex bit is used to operate the Timerx with respect to the INTx pin or regardless of the
INTx pin.
The C/Tx bit is used selects the source of pulses for the Timer to count.
C/T1 = 1 ==> Timer1 counts pulses from Pin T1 (P3.5) (Counter Mode)
C/T1 = 0 ==> Timer1 counts pulses from internal oscillator (Timer Mode)
C/T0 = 1 ==> Timer0 counts pulses from Pin T0 (P3.4) (Counter Mode)
C/T0 = 0 ==> Timer0 counts pulses from internal oscillator (Timer Mode)
The IE or Interrupt Enable Register is used to enable or disable individual interrupts. If a bit
is SET, the corresponding interrupt is enabled and if the bit is cleared, the interrupt is
disabled. The Bit7 of the IE register that is EA bit is used to enable or disable all the
interrupts.
0 0 0 0 0 0 0 0
MSB LSB
IP (Interrupt Priority)
The IP or Interrupt Priority Register is used to set the priority of the interrupt as High or Low.
If a bit is CLEARED, the corresponding interrupt is assigned low priority and if the bit is SET,
the interrupt is assigned high priority.
0 0 0 0 0 0 0 0
MSB LSB
The Serial Buffer or SBUF register is used to hold the serial data while transmission or
reception.
MSB LSB
TL0/TH0 (Timer 0 Low/High)
The Timer 0 consists of two SFRs: TL0 and TH0. The TL0 is the lower byte and the TH0 is the
higher byte and together they form a 16-bit Timer0 Register.
TH0 TL0
TIMER 0
TL0
TH0
The TL1 and TH1 are the lower and higher bytes of the Timer 0.
TH1 TL1
TIMER 1
TL1
TH1
All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured
as inputs or outputs. Accordingly, in total of 32 input/output pins enabling the
microcontroller to be connected to peripheral devices are available for use.
The figure above illustrates a simplified schematic of all circuits within the
microcontroller connected to one of its pins. It refers to all the pins except those of
the P0 port which do not have pull-up resistors built-in.
Output pin A logic zero (0) is applied to a bit of the P register. The output FE transistor is
turned on, thus connecting the appropriate pin to ground.
Input pin A logic one (1) is applied to a bit of the P register. The output FE transistor is
turned off and the appropriate pin remains connected to the power supply voltage over a
pull-up resistor of high resistance.
Logic state (voltage) of any pin can be changed or read at any moment. A logic zero (0) and
logic one (1) are not equal. A logic one (0) represents a short circuit to ground. Such a pin
acts as an output. A logic one (1) is “loosely” connected to the power supply voltage over a
resistor of high resistance. Since this voltage can be easily “reduced” by an external signal,
such a pin acts as an input.
Port 0
The P0 port is characterized by two functions. If external memory is used then the lower
address byte (addresses A0-A7) is applied on it. Otherwise, all bits of this port are
configured as inputs/outputs.
If any pin of this port is configured as an input then it acts as if it “floats”. Such an input has
unlimited input resistance and in determined potential.
When the pin is configured as an output, it acts as an “open drain”. By applying logic 0 to a
port bit, the appropriate pin will be connected to ground (0V). By applying logic 1, the
external output will keep on “floating”. In order to apply logic 1 (5V) on this output pin, it is
necessary to built in an external pull-up resistor.
Port 1
P1 is a true I/O port, because it doesn't have any alternative functions as is the case with P0,
but can be configured as general I/O only. It has a pull-up resistor built-in and is completely
compatible with TTL circuits.
Port 2
P2 acts similar to P0 when external memory is used. Pins of this port occupy addresses
intended for external memory chip. This time it is about the higher address byte with
addresses A8-A15. When no memory is added, this port can be used as a general
input/output port showing features similar to P1.
Port 3
All port pins can be used as general I/O, but they also have an alternative function. In order
to use these alternative functions, a logic one (1) must be applied to appropriate bit of the P3
register. In terms of hardware, this port is similar to P0, with the difference that its pins have
a pull-up resistor built-in.
The design for 8051 Microcontroller based system is not limited to the internal RAM and
ROM present in the 8051 Microcontroller. There is also a provision of connecting both
external RAM and ROM i.e. Data Memory and Program.
The reason for interfacing external Program Memory or ROM is that complex programs
written in high – level languages often tend to be larger and occupy more memory.
Another important reason is that chips like 8031 or 8032, do not have any internal ROM but
have to be interfaced with external ROM.
A maximum of 64B of Program Memory (ROM) and Data Memory (RAM) each can be
interfaced with the 8051 Microcontroller.
Consider the following block diagram of interfacing 64KB of External RAM and 64KB of
External ROM with the 8051 Microcontroller.
References: