FMM Unit-4 (8051 Microcontroller)
FMM Unit-4 (8051 Microcontroller)
The 8051 microcontroller is a widely used 8-bit microcontroller known for its
versatility and low power consumption. Its architecture is based on Harvard
architecture and includes a range of features such as timers, interrupt handling,
and serial communication interfaces.
It has two memory spaces, one for program memory and the other for data
memory. The program counter can access up to 64K of program memory, while
the data memory has 128 bytes of internal RAM and up to 64K of external RAM.
The microcontroller has four functional blocks: the CPU, memory, input/output
ports, and serial communication interface.
(or)
The Internal architecture is shown in Figure below and the various Registers and
units are described below.
Accumulator (Acc):
•Operand register
B Register:
Used to store one of the operands for multiplication and division, otherwise,
scratch pad considered as a SFR.
Stack Pointer (SP): 8 bit wide register. Incremented before data is stored on to
the stack using PUSH or CALL instructions. Stack defined anywhere on the 128
byte RAM.
Data Pointer (DPTR): 16 bit register contains DPH and DPL Pointer to external
RAM address. DPH and DPL allotted separate addresses in SFR bank
Port 0 To 3 Latches & Drivers: Each I/O port allotted a latch and a driver
Latches allotted address in SFR. User can communicate via these ports P0, P1, P2,
and P3.
Program Status Word (PSW): Set of flags contains status information as detailed
below in the Figure
Timer Registers: for Timer0 (16 bit register – TL0 & TH0) and for Timer1 (16 bit
register – TL1 & TH1) four addresses allotted in SFR
Control Registers: Control registers are IP, IE, TMOD, TCON, SCON, and
PCON. These registers contain control and status information for interrupts,
timers/counters and serial port. Allotted separate address in SFR.
Timing and Control Unit: This unit derives necessary timing and control signals
for internal circuit and external system bus.
Oscillator: generates basic timing clock signal using crystal oscillator.
Instruction Register: Decodes the opcode and gives information to timing and
control unit.
EPROM & program address Register: provide on chip EPROM and mechanism
to address it. All versions don’t have EPROM.
RAM & RAM Address Register: provide internal 128 bytes RAM and a
mechanism to address internally
ALU: Performs 8 bit arithmetic and logical operations over the operands held by
TEMP1 and TEMP 2.User cannot access temporary registers.
The 8051 microcontroller has a total of 40 pins arranged in four groups, namely
Port 0, Port 1, Port 2, and Port 3. Here’s a brief explanation of each group:
Port 0: Pins 32-39 are designated as Port 0 and are used for both input and
output operations. Port 0 can also be used for external memory interfacing.
Port 1: Pins 1-8 are designated as Port 1 and are used for both input and
output operations. Port 1 also has built-in pull-up resistors that can be
activated by writing a 1 to the corresponding bit.
Port 2: Pins 21-28 are designated as Port 2 and are used for both input and
output operations. Port 2 can also be used for external memory interfacing,
and some of its pins have alternate functions, such as being used as a serial
communication interface.
Port 3: Pins 10-17 are designated as Port 3 and are used for both input and
output operations. Port 3 can also be used for external memory interfacing,
and some of its pins have alternate functions, such as being used as a
timer/counter input.
Vcc: This pin is used to supply power to the microcontroller and is typically
connected to a +5V power source.
GND: This pin is used as the ground reference for the microcontroller and is
typically connected to the ground.
XTAL1 and XTAL2: These pins are used to connect an external crystal
oscillator that provides the clock signal for the microcontroller.
Reset: This pin is used to reset the microcontroller and is typically
connected to a push-button switch.
ALE: This pin is used as the Address Latch Enable signal and is used to
latch the address from the program counter onto the external memory bus.
PSEN1: This pin is used as the Program Store Enable signal and is used to
select the external ROM for program execution.
EA1: This pin is used to enable/disable external memory interfacing. If this
pin is connected to Vcc, the microcontroller will fetch the program from the
external memory. if it is connected to GND, the microcontroller will fetch
the program from the internal ROM.
(or)
Pins 1 to 8 − these pins are known as Port 1. This port doesn’t serve any
other functions. It is internally pulled up, bi-directional I/O port.
Pins 10 to 17 − These pins are known as Port 3. This port serves some
alternate functions like interrupts, timer input, control signals, serial
communication signals RxD and TxD, etc.
Pins 18 & 19 − These pins are used for interfacing an external crystal to get
the system clock.
Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher
order address bus signals are also multiplexed using this port.
Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used
to read a signal from the external program memory.
Pin 30 − This is EA pin which stands for External Access input. It is used to
enable/disable the external memory interfacing.
Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used
to demultiplex the address-data signal of port.
Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower
order address and data bus signals are multiplexed using this port.
Pin 40 − This pin is used to provide power supply to the circuit.
The 8051 microcontroller has128 bytes of Internal RAM and 4kB of on chip
ROM. The RAM is also known as Data memory and the ROM is known as
program memory. The program memory is also known as Code memory .This
Code memory holds the actual 8051 program that is to be executed. In 8051 this
memory is limited to 64K.Code memory may be found on-chip, as ROM or
EPROM. It may also be stored completely off-chip in an external ROM or, more
commonly, an external EPROM. The 8051 has only 128 bytes of Internal RAM
but it supports 64kB of external RAM. As the name suggests, external RAM is
any random access memory which is off-chip. Since the memory is off-chip it is
not as flexible in terms of accessing, and is also slower. For example, to
increment an Internal RAM location by1,it requires only 1 instruction and 1
instruction cycle but to increment a 1-byte value stored in External RAM
requires 4 instructions and 7 instruction cycles. So, here the external memory is 7
times slower.
8051 microcontrollers have 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.
Port 1
P1 is a true I/O port as it doesn’t have any alternative functions as in P0, but this
port can be configured as general I/O only. It has a built-in pull-up resistor and is
completely compatible with TTL circuits.
Port 2
P2 is similar to P0 when the external memory is used. Pins of this port occupy
addresses intended for the external memory chip. This port can be used for higher
address byte with addresses A8-A15. When no memory is added then this port can
be used as a general input/output port similar to Port 1.
Port 3
In this port, functions are similar to other ports except that the logic 1 must be
applied to appropriate bit of the P3 register. Port 3 Alternate Functions shown in
below table.
6. 8051 Interrupts
Interrupts are the events that temporarily suspend the main program, pass the
control to the external sources and execute their task. It then passes the control to
the main program where it had left off.
The priority of Interrupts is: INT0, TFO, INT1, TF1, RI/TI (Highest to Lowest).
The timer and serial interrupts are internally produced by the microcontroller,
whereas the external interrupts are produced by additional interfacing devices or
switches that are externally connected with the microcontroller. These external
interrupts can be level triggered or edge triggered.
Each interrupt can be enabled or disabled by setting bits of the IE register and the
whole interrupt system can be disabled by clearing the EA bit of the same register.
When interrupt occur then the microcontroller executes the interrupt service
routine. Therefore the memory location corresponds to interrupt enables it.
Consider the interrupt corresponding to the memory location is shown in the
interrupt vector table below.
7. Instruction Set of 8051:-
Based on the operation they perform, all the instructions in the 8051
Microcontroller Instruction Set are divided into five groups. They are:
Data Transfer Instructions
Arithmetic Instructions
Logical Instructions
Boolean or Bit Manipulation Instructions
Program Branching Instructions
Data Transfer Instructions
The Data Transfer Instructions are associated with transfer of data between
registers or external program memory or external data memory. The Mnemonics
associated with Data Transfer are given below.
MOV
MOVC
MOVX
PUSH
POP
XCH
XCHD
Arithmetic Instructions
Using Arithmetic Instructions, you can perform addition, subtraction,
multiplication and division. The arithmetic instructions also include increment by
one, decrement by one and a special instruction called Decimal Adjust
Accumulator.The Mnemonics associated with the Arithmetic Instructions of the
8051 Microcontroller Instruction Set are:
ADD
ADDC
SUBB
INC
DEC
MUL
DIV
DA A
Logical Instructions
The next group of instructions are the Logical Instructions, which perform
logical operations like AND, OR, XOR, NOT, Rotate, Clear and Swap. Logical
Instruction are performed on Bytes of data on a bit-by-bit basis.Mnemonics
associated with Logical Instructions are as follows:
ANL
ORL
XRL
CLR
CPL
RL
RLC
RR
RRC
SWAP
Boolean or Bit Manipulation Instructions
As the name suggests, Boolean or Bit Manipulation Instructions deal with bit
variables. We know that there is a special bit-addressable area in the RAM and
some of the Special Function Registers (SFRs) are also bit addressable.The
Mnemonics corresponding to the Boolean or Bit Manipulation instructions are:
CLR ,SETB ,MOV ,JC, JNC, JB, JNB ,JBC, ANL, ORL,CPL
Program Branching Instructions
The last group of instructions in the 8051 Microcontroller Instruction Set are the
Program Branching Instructions. These instructions control the flow of program
logic. The mnemonics of the Program Branching Instructions are as follows.
LJMP ,AJMP, SJMP, JZ, JNZ, CJNE, DJNZ, NOP, LCALL, ACALL, RET,
RETI, JMP
12 The device has 40 pins requires +5V Available in 40 pin, in which 32 pins are
single power supply, operate with a 3 reserved for 4 multiplexed address and
MHz single phase clock. All signals data ports (Port 0. Port 1, Port 2 and
classified in 6 groups(i) Address bus (ii) Control port) and 2 pins for power supply,
Multiplexed data bus (iii) Control and 2 pins for XTAL oscillator, 1 ALE pin to
status signals select address or data
(iv) Power supply&frequency signals (v) through ports, 𝐸̅ 𝐴̅ for external access, 𝑃̅ ̅𝑆̅𝐸̅
̅𝑁̅ for external program memory and RST
External initiated signals and (vi) Serial for resetting the device
input and output ports.
The Assembly language program for 16 bit addition and multiplication of two
hexa-decimal numbers for both 8085 and 8051 were given Table 4.
Table 4. The similarities and differences in assembly language programs of
8085 and 8051