8051 Microcontroller

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

UNIT-V

8051 MICRO CONTROLLER


Differences between Micro-processor and
Micro-controller
SINO Micro-processor Micro-controller

1 Micro-processor contains ALU, GPRs, PC, Micro-controller contains the


SP, Control and Timing circuit, and microprocessor, in built ROM, RAM, I/O
interrupts circuit. ports, and Timer & Counter circuits.
2 It has many instructions to move data It has few instructions to move data
between Memory and CPU. between Memory and CPU.
3 Access time for memory and I/O devices Less access time for built-in memory and
are more. I/O devices.
4 Microprocessor based system requires Microcontroller based system requires less
more hardware. hardware.
5 Microprocessor based system is more Microcontroller based system is less
flexible in design point of view flexible in design point of view
6 Less number of pins are multi- functioned. More number of pins are multi- functioned.

7 A single board CPU is called A single board computer is called


Microprocessor. Microcontroller.
Features of 8051 Micro-controller
1. It is an 8-bit Micro-controller.
2. It has 8 data lines (D7-D0) and 16 address lines (A15-A0). All data lines
are multiplexed with the lower order 8- address lines (A7-A0), and then
the multiplexed bus is (AD7-AD0).
3. It has 34 eight-bit GPRS, and four 16-bit registers (PC, DPTR, T0, & T1).
4. It has four I/O ports, and each port contains eight I/O pins.
5. It has 128 bytes of Internal RAM and 4Kbytes of internal ROM and it
supports 64KB of External RAM and another 64KB of external ROM.
6. Its operating clock frequency of 1MHz -16 MHz, but for commercial
applications frequency is 12MHz theoretically but practically it is
11.0592MHz.
7. It supports serial communication.
8. It supports five interrupts in that two are external interrupts and three
are internal interrupts. All are vectored interrupts.
9. It can have four mathematical flags and three user defined flags.
10. It can have two 16-bit Timer/Counter circuits.
Architecture of 8051 Micro-controller
• A Register (Accumulator)
• A register is a general-purpose register used for storing intermediate results
obtained during operation. Prior to execution any instruction by ALU one operand
provided by Accumulator and after completion of operation the result is stored in
the accumulator.
• If the micro-controller is communicated with external world entire communication
is done through accumulator register (A Register).
• B Register
• This register also treated as accumulator for some operations. This register
function is appears along with A register.
• Multiplication and division can be performed only upon numbers stored in the
A and B registers and the result of that operation also stored in those registers
only.
• All other instructions in the program can use this register as a spare
accumulator (A).
• Program Status Word Register (PSW)
D7 D6 D5 D4 D3 D2 D1 D0

CY AC F0 RS1 RS0 OV ---- PF


• P (Parity bit)
• If a number stored in the accumulator A contains even number of 1’s then this bit will be
automatically set (1), otherwise it will be cleared (0).
• It is mainly used during data transmit and receive via serial communication.
• OV (Overflow)
• Overflow condition causes the OV bit to be set (1). Otherwise, it will be cleared (0).
• RS0 & RS1 (Register Bank Select Bits)
• These two bits are used to select one of four register banks of RAM. By setting and clearing these
bits, registers R0-R7 are stored in one of four banks of RAM.

RS1 RS0 Space in RAM


0 0 Bank0 (00H-07H)

0 1 Bank1 (08H-0FH)

1 0 Bank2 (10H-17H)

1 1 Bank3 (18H-1FH)
• F0 (Flag 0)
• This is a general-purpose user defined flag the use of this flag is decided by the user.
• AC (Auxiliary Carry Flag)
• It is used for BCD operations only. This flag is set to ‘1’ when in the addition the carry is
transferred from bit B3 to B4 and in subtraction the borrow was transferred from bit B4 to
B3.
• CY (Carry Flag)
• This flag is set to ‘1’ when in the addition operation the final carry is generated or in
subtraction operation the Minuend is less than the Subtrahend.
• PC (Program Counter)
• It holds the next instruction byte address of the program memory
• DPTR (Data Pointer)
• It is made up of two 8-bit registers those are DPH & DPL. This register gives the addresses of
internal and external memories for data access. The DPTR has two independent internal
addresses, one for DPL and another for DPH.
• Internal Memory
• The 8051 Microcontroller has internal program memory (PROM) and internal data memory
(RAM). Due to this 8051 has a Harvard architecture, which uses a same address in different
memories, for code and data.
• Internal RAM
• The 8051 microcontroller has 128 bytes of internal RAM, its address range from 00H to
07FH. From 80H to 0FFH addresses are assigned to SFRs (Special Function Registers).
• The internal RAM 128Bytes can divide into three parts.
Those are
• Register Banks – 32 Bytes (00H – 1FH)
• Bit/Byte addressable memory – 16 Bytes (20H – 2FH)
• User memory or General purpose memory—80 Bytes (30H – 7FH)
INTERNAL RAM
• Bit / Byte Addressable Memory General Purpose Memory

Register Banks Memory


A. Thirty two bytes from addresses 00H to 1FH that make up 32
working registers organized as four register banks of eight
registers each. The register banks are numbered from 0 to 3 and
are made up of eight registers named R0 to R7. Each register can
be called in to the program by its name or by its RAM address. The
register bank selection bits RS1 & RS0 are used for selection of
specific register bank for controller operation. The register bank-0
is selected when the microcontroller is reset.
B. The Bit / Byte addressable area of 16-Bytes occupies RAM
addresses from 20H to 2FH forming a total of 128 addressable bits. A
bit addressable bit can be specified by its bit addresses from 00H to
7FH or 8- bits may form a byte address from 20H to 2FH. This area bits
useful when the controller performs operations on bits.

C. A general purpose RAM area starts from 30H to 7FH. This is


addressable as bytes
INTERNAL ROM
• The 8051 microcontroller has an internal ROM of
4Kbytes, its addresses from 0000H to 0FFFH. This is used
to store the system files or system code information.
• The PC is ordinarily used to address the program code
bytes from addresses 0000H to 0FFFH. The program
addresses higher than the 0FFFH, which exceeds the
internal ROM capacity, will cause the 8051 to
automatically fetch the program codes from external
program memory.
• Code bytes can also be fetched exclusively from the
external program memory addresses from 0000H to
0FFFFH, by connecting the External Active or External
Enable pin to the ground.
• The PC does not care the code where it is, the
manufacture decides the code entirely found in the
internal ROM, external ROM, or the combination of both
internal and external program memories.
• Special Function Registers (SFR)
• 8051 Microcontroller contains several special
function registers, which can perform different
functions in the controller.
• In these SFRs some registers are Bit Addressable
Registers and some are Byte Addressable Registers.
• Bit Addressable Registers means these registers a
byte address is allotted for the entire register and
each and every individual bit also have their own
address.
• We can call these register bits individually by their bit
addresses as well as register byte addresses.
Special Function Registers (SFR)
Register Name Function Internal Address in Hex
A Accumulator * 0E0H
B Arithmetic * 0F0H
DPL Data Pointer Lower 82H
DPH Data Pointer Higher 83H
SP Stack Pointer 81H
TCON Timer Control Register * 88H
TMOD Timer Mode Register 89H
SCON Serial Control Register * 98H
SBUF Serial Buffer Register 99H
PCON Power Control Register 87H
IP Interrupt Priority Register * 0A8H
IE Interrupt Enable Register * 0B8H
PORT 0 Input/output Port Latch – 0 * 80H
PORT 1 Input/output Port Latch – 1 * 90H
PORT 2 Input/output Port Latch – 2 * 0A0H
PORT 3 Input/output Port Latch – 3 * 0B0H
TL0 Timer - 0 Lower 8AH
TH0 Timer - 0 Higher 8CH
TL1 Timer - 1 Lower 8BH
TH0 Timer - 1 Higher 8DH
PSW Program Status Word * 0D0H
Pin Diagram of 8051 Microcontroller
External Memory Interfacing to 8051
Microcontroller
• Timer/Counter in 8051
• 8051 has two 16-bit UP Timer/counter registers those are T0 and
T1. These can be implemented with two 8- bit registers TLX and THX
(X = 0 or 1).
• To control the operation of these timers 8051 have two control
registers TCON and TMOD.
• T0 can be formed by the combination of TH0 and TL0. Similarly T1 is
the combination of TH1 and TH0.
• TH0, TL0, TH1, TL1, TCON, and TMOD registers are byte addressable
registers of 8051, and each one size is 8-bit.
• The timers T0 and T1 main purpose is to measure time and count
external events.
• T0 and T1 act as timers for internal operation of 8051 and they
accept the input clock pulse from internal oscillator. T0 and T1 act
as event counters for external operation of 8051 and they accept
the input clock pulse from external through T0 and T1 port 3
signals.
The control logic circuitry timer/
counter operation

• Timer Mode Register (TMOD)


• It is not bit addressable register. The timers T0
and T1 can operate in different modes.
• By using this register we can program the T0
and T1 individually as timers or counter and
mode operation of T0 and T1. Its internal
address is 89H.
Timer control Register (TCON)
• It is an 8-bit register. Its internal address is 88H. It is bit addressable
register.
• The bits in this registers controls the timers operation as well as
related to interrupts of 8051.

• Timer’s modes of operation


• Timers can operate in four different modes. They are as follows
• Mode-0 Operation
• In this mode, the timer is used as a 13-bit UP counter as follows
• Mode-1 Operation
• In this mode, the timer is used as a 16-bit UP counter as follows
• This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit
mode.

• Mode-2 Operation
• This is an 8-bit counter/timer operation. Counting is performed in TLX while THX
store a constant value.

• Mode-3 Operation
• Timer 1 in Mode-3 simply holds its count. The effect is same as setting TR1=0.
Timer0 in mode-3 establishes TL0 and TH0 as two separate counters.
• Control bits TR1 and TF1 are used by Timer-0 (TH0) and TR0 and TF0 are available
to Timer-0 (TL0).
Interrupts in 8051
• 8051 provides 5 vectored interrupts. They are –
• INTO’
• TF0
• TF1
• INT1’
• RI/TI
• INTO’ and INT1’ are external interrupts whereas Timer and Serial port
interrupts are generated internally.
• Interrupt Enable register (IE)
• Its internal address is A8H. It is bit addressable register
.
PRIORITY STRUCTURE
• Each interrupt source can be programmed to have one of the two
priority levels by setting (high priority) or clearing (low priority) a bit
in the IP (Interrupt Priority) Register.
• A low priority interrupt can itself be interrupted by a high priority
interrupt, but not by another low priority interrupt.

• Interrupt Priority register (IP)


• Serial Communication in 8051
• The serial port of 8051 is full duplex, i.e., it can transmit and receive
simultaneously.
• The register SBUF is used to hold the data. The special function register SBUF is
physically two registers. One is, write-only and is used to hold data to be
transmitted out of the 8051 via TXD.
• The other is, read-only and holds the received data from external sources via RXD.
Both mutually exclusive registers have the same address 099H.
• Serial Control register (SCON)
• It controls the serial communication. Its internal address is 98H. It is bit
addressable register.

• Serial mode selection bits SM0 & SM1


SM0 SM1 Mode of Operation Baud Rate
0 0 Mode-0 (Shift Register Mode) Fosc/12
0 1 Mode-1 (8-bit UART) Variable
1 0 Mode-2 (9-bit UART) Fosc/32 or Fosc/64
1 1 Mode-3 (9-bit UART) Variable
• Power Control Register (PCON)
Register PCON controls processor power-down, sleep modes and serial data baud rate. Only one bit of
PCON is used with respect to serial communication. The seventh bit (b7) (SMOD) is used to generate the
baud rate of serial communication. Its internal address is 87H.

SMOD = 1 Serial Communication baud rate is double

SMOD = 0 Normal baud rate

SMOD: Serial Communication baud rate modify bit


GF1 & GF0: User defined flags
PD: Power Down mode bit
IDL: Idle mode bit
Addressing Modes of 8051
Microcontroller
• An Addressing Mode indicates how the data is
represented in the instruction. 8051 supports 6
types of Addressing Modes, those are
1. Immediate Addressing mode
2. Register Addressing Mode
3. Register Indirect Addressing Mode
4. Direct Addressing Mode
5. Indexed Addressing Mode
6. Implicit Addressing Mode
1. Immediate Addressing Mode
• In these addressing mode instructions the data is directly
placed in the source operand field of the instruction, and a
‘#’ symbol must prefix for the data.
• Ex: - MOV A, #38H ADD A, #67H
2. Register Addressing Mode
• In these addressing mode instructions the data is directly
placed in the operand field of the instruction through a
GPR (General Purpose Register).
• EX: - MOV A, B ADD A, R0
3. Register Indirect Addressing Mode
• In these addressing mode instructions the address of the
data is indirectly placed in the operand field of the
instruction through a GPR (General Purpose Register) R0
or R1.
• Ex: - MOV A, @R0 ADD A, @R1
4. Direct Addressing Mode
• In these addressing mode instructions the address of the
data is directly placed in the operand field of the
instruction. The address is the internal RAM or internal
SFR (Special Function Register).
• Ex: - MOV A, 20H MOV R1, 70H
5. Indexed Addressing Mode
• In these addressing mode instructions the address of the
data is indirectly placed in the operand field of the
instruction through combination ‘A’ register & PC or
DPTR.
• Ex: - MOVC A, @A+DPTR MOVC A, @A+PC
6. Implied Addressing Mode
• In this addressing mode instruction the operand is
implicitly represented in the operation code of the
instruction.
• Ex: - NOP, RET, RETI
INSTRUCTION SET OF 8051 MICROCONTROLLER
• All instructions of 8051 can be divided in to five different groups, those are
• 1. Data Transfer Instructions
• 2. Arithmetic Instructions
• 3. Logical Instructions
• 4. Bit Manipulation Instructions
• 5. Branching Instructions
• 1. Data Transfer Instructions
Different mnemonics used in this group. Those are;
MOV MOVC MOVX PUSH POP
XCH XCHD
• 2. Arithmetic Instructions
ADD ADDC SUBB MUL DIV
INC DEC DA
• 3. Logical Instructions
ANL ORL XRL CPL CLR
RL RLC RR RRC SWAP
• 4. Bit Manipulation Instructions
For all these instructions Carry flag (CY) is act as Accumulator.
CLR SETB CPL ANL ORL
MOV
External Addressing Using MOVX and
MOVC Instructions
• B. Stack Related Instructions
• There are two instructions are there in this group. These instructions are used
for data transfer between the internal RAM and the specified Direct Address in
the instruction.
• The internal RAM can be used as Stack. The instructions are
• i. PUSH Instruction
• Ex: - PUSH 76H
• ii. POP Instruction
• Ex: - POP 86H
• C. Exchange Related Instructions
• In this group there are two instructions. In these instructions
one operand is accumulator register ‘A’. These instructions don’t
support Immediate Addressing Mode.
• i. XCH
• Ex: - XCH A, R0 XCH A, @R1 XCH A, 46H
• ii. XCHD
• Ex: - XCHD A, R0 XCHD A, @R1 XCHD A, 46H
• 2. Arithmetic Instructions
• The microcontroller 8051 supports the following arithmetic operations.
• If any arithmetic instruction is executed by controller based on the result
the content of mathematical flags are modified.
• For these arithmetic instructions one operand must be the content of
accumulator ‘A’ and after completion of operation the result is stored in
register ‘A’.
• i. Addition a. ADD b. ADDC
ii. Subtraction Ex: - SUBB A, R0 SUBB A, #24H SUBB A, @R0
SUBB A, 68H
iii. Multiplication Ex: - MUL AB
iv. Division Ex: - DIV AB
v. Increment Ex: - INC R0 INC 70H INC @R0 INC DPTR
INC A
vi. Decrement Ex: - DEC R0 DEC 70H DEC @RO DEC DPTR DEC A

vii. Decimal Adjust Ex: - DA A


• 3. Logical Instructions
• The 8051 support two types of logical operations based on the size of
applied data.
• Those are Byte level Logical Instructions and Bit Level Logical Instructions.
8051 support the following logical operations,
• i. AND ii. OR iii. XOR iv. NOT

• 4. Branching Instructions
• These instructions are also called as Transfer of control Instructions, or
Program flow control instructions.
• By using these instructions the program flow control is transferred from
one location to another location conditionally or unconditionally.
• Basically these are two types of instructions
A. Unconditional Branching Instructions
B. Conditional Branching Instructions

You might also like