MPMC Eee Unit - III 8051 Microcontroller
MPMC Eee Unit - III 8051 Microcontroller
MPMC Eee Unit - III 8051 Microcontroller
chip oscillator which works as a clock source for Central Processing Unit. As the
output pulses of oscillator are stable therefore it enables synchronized work of all
parts of 8051 Microcontroller.
Timing and Control Unit: It provides necessary timing and control signal for all
the operations. The signals associated with this unit are PSEN, EA, ALE and RST.
Arithmetic Logic Unit (ALU): It performs all arithmetic operations like addition,
subtraction, multiplication, division, integer multiplication, integer division, string
handling and logical operations like AND, OR, NOT and EX-OR.
A Register (Accumulator) : A register is an 8-bit general-purpose register used
for storing intermediate results obtained during operation.
B Register : It is an 8-bit register, always used with A register. Multiplication
and division can be performed only upon numbers stored in the A and B registers.
All other instructions in the program can use this register as a spare accumulator
(A).
Program Status Word (PSW) Register
PSW register is one of the most important SFRs. It contains several status bits that
reflect the current state of the CPU. Besides, this register contains Carry bit,
Auxiliary Carry, two register bank select bits, Overflow flag, parity bit and userdefinable status flag.
Stack Pointer (SP) Register: A value stored in the Stack Pointer points to the first
free stack address and permits stack availability. Stack pushes increment the value
in the Stack Pointer by 1. Likewise, stack pops decrement its value by 1
Data Pointer Register (DPTR) : DPTR register is not a true one because it
doesn't physically exist. It consists of two separate registers: DPH (Data Pointer
High) and (Data Pointer Low). For this reason it may be treated as a 16-bit register
or as two independent 8-bit registers. Their 16 bits are primarly used for external
memory addressing. Besides, the DPTR Register is usually used for storing data
and intermediate results.
Input/Output Port: As we know that Microcontroller is used in Embedded
systems to control the operation of machines. Therefore to connect it to other
machines, devices or peripherals we requires I/O interfacing ports
in Microcontroller. For this purpose Microcontroller 8051 has 4 input/output ports
to connect it to other peripherals.
Timers/Counters: Microcontroller 8051 has 2 16 bit timers and counters. The
counters are divided into 8 bit registers. The timers are used for measurement of
intervals , to determine pulse width etc.
Interrupts: There are five interrupt sources in 8051 Microcontroller. Two of them
are external interrupts, Two timer interrupts and One serial port interrupt.
Memory (ROM): Microcontroller 8051 has 4KB of internal Code memory which
is used to store the program of Microcontroller, is known as Program memory. It is
known
as
'ROM'(Read
Only
Memory).
Memory (RAM): Microcontroller has 128 bytes of data memory to store data or
operands temporarily known as 'RAM'(Random Access Memory).
The area of direct and indirect addressing is made up of the first 128 bytes of
internal memory of the microcontroller. This in turn is divided into three
segments differentiated by their use.
1. The Register Banks,
memory
3.
Scratch Pad
Above this 128 bytes RAM memory another 128 bytes memory location used for
maintain Special Function Registers (SFR)
8051 Architecture:
Pins 1-8: Port 1 Each of these pins can be configured as bidirectional input/output
line. The Port 0 is bit addressable. This port also used as time multiplexed low
order Address/Data (AD0 to AD7) lines for accessing external memory.
Pin 9: RST A logic one on this pin disables the microcontroller and clears the
contents of most registers. By applying logic zero to this pin, the program starts
execution from the beginning.
VPD may be used to supply power to the internal RAM during power failure or
power down modes.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general purpose
Input/Output line. Besides, all of them have alternative functions.
Pin 10: RXD Serial asynchronous communication input or Serial synchronous
communication output.
Pin 11: TXD Serial asynchronous communication output or Serial synchronous
communication clock output.
Pin 12: INT0 Interrupt 0 input.
Pin 13: INT1 Interrupt 1 input.
Pin 14: T0 Counter 0 clock input.
Memory Organization
The 8051 has two types of memory and these are Program Memory and Data
Memory.
Program Memory (ROM) is used to permanently save the program being
executed, while Data Memory (RAM) is used for temporarily storing data
and intermediate results created and used during the operation of the
microcontroller.
All 8051 microcontrollers have a 16-bit addressing bus and are capable of
addressing 64 KB memory.
Program Memory
The 8051 microcontroller have a 4 Kbyte ROM embedded.
Even though such an amount of memory is not sufficient for writing most of the
programs, the additional ROM memory (64KB) should be connected.
To interface a memory chip to the microprocessor, the following pins should be
connected
Address Bus
Data Bus
Memory chip select
Read Control
Write Control (Only in the case of RAM)
P2.0 to P2.7: High order address lines (A7 to A15)
P0.0 to P0.7: Time multiplexed Low order address/data (AD0 to AD7)
The chip select of memory should connected address decoder.
P0.6 and P0.7 provides Write and Read control signals.
ALE should be connected to Address Latch for enabling the latch when address
signals are appear in the Address/Data lines.
PSEN should be connected to Read/Out Enable pin of external ROM memory.
The microcontroller handle external memory depends on the EA pin logic state:
EA=0 In this case, the microcontroller completely ignores internal program
memory (4 KB) and executes only the program stored in external memory (64KB).
The 8051 access the external memory location starts from 0000H to FFFFH.
EA=1 In this case, the microcontroller executes first the program from built-in
ROM (4 KB), then the program stored in external memory.
The 8051 first access the internal ROM by start from 0000H to 0FFFH and then
access the external memory from 1000H to FFFFH
.
In both cases, P0 and P2 are not available for use since being used for data and
address transmission. Besides, the ALE and PSEN pins are also used.
Data Memory
The External Data Memory is used for storing the data. The maximum capacity of
RAM is 64 KB.
Besides, RAM memory built in the 8051 family includes many registers such as
hardware counters and timers, input/output ports, serial data buffers etc.
However, the first 256 memory locations (addresses 0-FFh) are the heart of
memory common to all the models belonging to the 8051 family.
Locations available to the user occupy memory space with addresses 0-7Fh, i.e.
first 128 registers. This part of RAM is divided in several blocks.
The first block consists of 4 banks each including 8 registers denoted by R0-R7.
The next memory block (address 20h-2Fh) is bit- addressable, which means that
each bit has its own address (0-7Fh). Since there are 16 such registers, this block
contains in total of 128 bits with separate addresses (address of bit 0 of the 20h
byte is 0, while address of bit 7 of the 2Fh byte is 7Fh).
The third group of registers occupies addresses 2Fh-7Fh, i.e. 80 locations, and does
not have any special functions or features.
If external program/data memory are to be interfaced, they are interfaced in the following
way.
PSW register is one of the most important SFRs. It contains several status bits that
reflect the current state of the CPU. Besides, this register contains Carry bit,
Auxiliary Carry, two register bank select bits, Overflow flag, parity bit and userdefinable status flag.
P - Parity bit. If a number stored in the accumulator is even 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.
Bit 1. This bit is intended to be used in the future versions of microcontrollers.
OV - Overflow occurs when the result of an arithmetical operation is larger than
255 and cannot be stored in one register. 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
Bank0 00h-07h
Bank1 08h-0Fh
Bank2 10h-17h
Bank3 18h-1Fh
Timer 0 and Timer 1 register: The 8051 has two numbers of 16-bit timers. It is
available as four 8-bit registers like TH0, TL0, TH0 and TL1. These registers are
used to load the values during timer and counter functions. Also offers four modes
of operations like mode0, mode1, mode2 and mode3.
Serial Buffer Register (SBUF) : This register is used for serial data
communication. It holds the data to be transmitted and received. For this purpose,
SBUF is interconnected to two 8-bit shift registers.
Control and Status Registers: The Special function registers like Interrupt
Priority (IP), Interrupt Enable (IE), Power Control Register (PCON), Serial Control
(SCON) that are used for controlling the internal resources or to see the status of
these resources.
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.
address/data bus (internal) is '0', the upper MOSFET is 'off' and the lower MOSFET is 'on'.
The output becomes '0'. If the address/data bus is '1', the upper transistor is 'on' and the lower
transistor is 'off'. Hence the output is '1'. Hence for normal address/data interfacing (for
external memory access) no pull-up resistors are required.
Port-0 latch is written to with 1's when used for external memory access.
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-1 does not have any alternate function i.e. it is dedicated only for I/O
interfacing. When used as output port, the pin is pulled up or down through
internal pull-up.
To use port-1 as input port, '1' has to be written to the latch. In this input mode
when '1' is written to the pin by the external device then it read fine.
But when '0' is written to the pin by the external device then the external source
must sink current due to internal pull-up. If the external device is not able to sink
the current the pin voltage may rise, leading to a possible wrong reading.
Port 2
Port-2 is used for higher external address byte or a normal input/output port. The
I/O operation is similar to Port-1. Port-2 latch remains stable when Port-2 pin are
used for external memory access. Here again due to internal pull-up there is limited
current driving capability.
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.
Each pin of Port-3 can be individually programmed for I/O operation or for alternate
function. The alternate function can be activated only if the corresponding latch has been
written to '1'. To use the port as input port, '1' should be written to the latch. This port also has
internal pull-up and limited current driving capability.
Alternate functions of Port-3 pins are P3.0 RxD
P3.1 TxD
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
T0
T1
Note:
1. Port 1, 2, 3 each can drive 4 LS TTL inputs.
2. Port-0 can drive 8 LS TTL inputs in address /data mode. For digital output port, it
needs external pull-up resistors.
3. Ports-1,2and 3 pins can also be driven by open-collector or open-drain outputs.
4. Each Port 3 bit can be configured either as a normal I/O or as a special function bit.
TIMERS / COUNTERS
Timer Mode control (TMOD) Special Function Register:
TMOD register is not bit addressable.
TMOD
Address: 89 H
The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count. Upper 3 bits of TLX
are ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt
is generated.
The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0, the
counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the
counter is controlled by
input. This mode is useful to measure the width of a given pulse
fed to
input.
Timer Mode-1: (16-bit mode)
This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit mode.
This is a 8 bit counter/timer operation. Counting is performed in TLX while THX stores a
constant value. In this mode when the timer overflows i.e. TLX becomes FFH, it is fed with
the value stored in THX. For example if we load THX with 50H then the timer in mode 2 will
count from 50H to FFH. After that 50H is again reloaded. This mode is useful in applications
like fixed time sampling.
Register Addressing:
It permits access to eight Registers (R0 R7) of the register bank. Other Registers
used are A, B, DPTR
eg: MOV A, R7
The name of the Register will be specified in the instruction itself. So it is called
as Register addressing mode.
Direct Addressing:
In this mode of addressing, the operands are specified using the 8-bit address field,
in instruction format. Only internal data RAM and SFRS can be directly
addressed.
eg: MOV A, 4EH
The content which is present in the memory location 4E is moved to Register A.
Indirect Addressing:
In this mode of addressing, the 8-bit address of the operand is stored in a register
specified in the instruction. The Registers R0 and R1 of the selected bank of
registers or stack pointer can be used as address Registers for storing the 8-bit
address. The address register for 16-bit address can only be the data pointer
(DPTR).
eg:1 MOV A, @ R0
If R0 contains 5FH, and address location 5FH has 43H then after this instruction
43H will be stored in Accumulates
eg: 2 MOVX A, @ EPTR
Immediate Addressing
In this mode of addressing, an immediate data ie, a constant specified in the
instruction, after the Op-code byte.
eg: ADD A, # OFH
This instruction adds OFH with the content of A and the result will be stored in the
Accumulator (A)itself.
Based Induced indirect Addressing:
Only program memory can be accessed using this addressing mode. The DPTR or
PC register may act as the base Register And register A acts as index Register.
The summation of contents of the base register and index Register determines the
Operand address.
eg: MOVC A, A+ DPTR
If A = 02H, and DPTR = OE7EH and OE80H + 56H then after this instruction
56H will be stored in Accumulator (A)
Instruction Set of 8051:
Instruction sets of 8051 can be classified into four different categories. They are
1)
Data Transfer Instructions
2)
Arithmetic Instructions
3)
Logical Instructions
4)
Control Transfer instructions
8051 I/O Ports structure:
The 8051 has 32 I/o pins configured as four eight bit parallel posts (P 0, P1, P2 and
P3) All four posts are bidirectional i.e., each pin will be configured as input or
output or both. All post pins are multiplexed except the pins of post 1. Each post
consists of a latch, an output drives and an input buffer.
Post 0 (Pins 32 39):
Post 0 pins can be used as I/o pins. The output drives and input buffers of post 0
are used to access external memory. Post 0 outputs the low order bytes of the
external memory address, time multiplexed with data being written or read. Thus
post 0 can be used as multiplexed address/data bus.
Post 1 (Pins 1 8)
Post 1 Pins can only be used as I/o pins.
Post 2 (Pins 21 28)
The output drives of post 2 are used to access external memory. Post 2 outputs the
higher order byte of the external memory address when the address is 16 bits wide.
Otherwise, post 2 is used as an I/o post.
2SMOD X
Oscillator frequency
32d
Mode 2:
In this mode, 11 bits are transmitted or received. This 11 bit frame is classified as
1 bit for start. 8 bits for data, 1 bit can be programmed and 1 bit for stop. The 9 th
data bit is programmable. This 9th bit is the 7B8 bit for transmission and RB8 bit
for reception in the SCON register
The band rate is programmable either 1/22th
f band 2
2 SMOD x
64d
Oscillator frequency
Mode 3:
This mode is same as the mode 2 except for the band rate. Here the band rate is
variable and is same as mode 1.
SCON : (Serial Control Register)
The SCON Register is an 8 - bit Register used to program the start bit, stop bit and
data bits of data framing, among other things.
The following describes various bits of the SCON register.
SMO
SMO , SM1
SM2
REN
TB8
RB8
T1
R1
SM1
SM2
-
REN
TB8
RB8
T1
R1
INTERRUPTS
An interrupt is a special feature which allows the 8051 to provide the hope of
"multitasking". An interrupt is triggered whenever a corresponding event occurs.
When the event occurs, the 8051 temporarily puts "on hold" the normal execution
of the program and executes a special section of code referred to as an interrupt
handler.
The interrupt handler performs whatever special functions are required to handle
the event and then returns control to the 8051 at which point program execution
continues as if it had never been interrupted.
EX0
ET0
EX1
ET1
ES
ET2
EA
Setting 1'
Enable/Disable all
Enable the corresponding interrupt
Setting 0'
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.
If two interrupts of different priority levels are received simultaneously, the request of higher
priority level is served. If the requests of the same priority level are received simultaneously,
an internal polling sequence determines which request is to be serviced.
Thus, within each priority level, there is a second priority level determined by the polling
sequence, as follows.
Interrupt Source
Vector
Priority within level
location
External Hardware Interrupt (INT0)
0003H
Highest
Timer 0 Interrupt (TF0)
000BH
External Hardware Interrupt (INT1)
00013H
Timer 1 Interrupt (TF1)
001BH
Serial communication Interrupt (RI
0023H
Lowest
and TI)
0'
low priority
1'
high priority
Interrupt handling:
When an interrupt comes and the program is directed to the interrupt vector address, the
Program Counter (PC) value of the interrupted program is stored (pushed) on the stack.
The required Interrupt Service Routine (ISR) is executed. At the end of the ISR, the
instruction RETI returns the value of the PC from the stack and the originally interrupted
program is resumed.
The schematic diagram of the detection and processing of interrupts is given as follows.
Instruction Cycles
2. Register Addressing
3. Direct Addressing
4. Register-Indirect Addressing
Immediate Addressing
Immediate addressing simply means that the operand (which immediately follows
the instruction op. code) is the data value to be used. For example the instruction:
MOV A, #dada (8-bit)
Example:
MOV A, #99H
Accumulator
Number 99H
Moves the value 99 into the accumulator (note this is 99 Hexa-decimal since we
used 99H). The # symbol tells the assembler that the immediate addressing mode is
to be used.
Register Addressing
One of the eight general-registers, R0 to R7, can be specified as the instruction
operand. The assembly language documentation refers to a register generically as
Rn. An example instruction using register addressing is :
ADD A, Rn ; Add contents of register Rn to accumulator.
ADD A, R5 ; Adds register R5 to A (accumulator)
A (A+R5)
Example:
Accumulator
R5
Internal RAM
Accumulator
47h
46h
The instruction reads the data from Internal RAM address 47h and stores this in
the accumulator. Direct addressing can be used to access Internal RAM,
including the SFR registers.
Indirect Addressing
Indirect addressing provides a powerful addressing capability, which needs to be
appreciated. An example instruction, which uses indirect addressing, is as
follows:
MOV A, @Ri
MOV A, @R0: The contents of register R0 give the address of the location from
where the operand is picked up.
Internal RAM
Accumulator
55h
R0
54h
53h
54h
Note: The @ symbol indicated that the indirect addressing mode is used. R0
contains a value, for example 54h, which is to be used as the address of the internal
RAM location, which contains the operand data. Indirect addressing refers to
Internal RAM only and cannot be used to refer to SFR registers.
Note: Only R0 or R1 can be used as register data pointers for indirect addressing
when using MOV instructions.