BEC-351 Unit-II Lecture
BEC-351 Unit-II Lecture
(BEC-351)
UNIT-II
8051 Architecture
By
Dr. Ishwar Chandra Yadav
Electronics & Communication Department
Madan Mohan Malaviya University of Technology, Gorakhpur
January 2024
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 1 / 83
syllabus
Syllabus
Unit-I
Introduction to Embedded system: Embedded system and codesign issues, Pro-
cessor Embedded into a System, Embedded Hardware and Software in a system,
Examples of ES, use of VLSI Circuit Design Technology, Use of software tools for
development of an ES.
Introduction to Microcontrollers and Embedded Processors: Microcontrollers
survey, 4-bit,8-bit, 16-bit, 32-bit Microcontrollers, Comparing Microprocessors and
Microcontrollers, Overview of the 8051 Microcontroller family.
Unit-II
8051 Architecture: Hardware, Oscillator and clock-program counter, data pointer
registers, stack and stack pointer, special function registers, memory organization,
program memory, data memory, Input/Output Ports, External memory counter and
timer, serial data Input/output Interrupts.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 2 / 83
syllabus
Syllabus
Unit-III
8051 Assembly Language Programming:Structure of Assembly language, Assem-
bling and running an 8051 program, addressing modes, Accessing memory using
various addressing modes
Instruction set:arithmetic operations and Programs, Logical operations and Pro-
grams, Jump and Call instructions and Programs, Input/Output Programs, Single
bit instructions and Programs, Timer, counter and Programs.
Unit-IV
8051 Serial Communication: Connection to RS-232, Serial Communication Pro-
gramming, Interrupts Programming,
Microcontroller Interfacing: Keyboard, Displays, Pulse Measurement, D/A and
A/D conversion, Stepper Motor
Basic concept of PIC microcontroller and RTOS: Microcontroller Architec-
ture–PIC16F Family and Introduction to RTOS.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 3 / 83
syllabus
Syllabus
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 4 / 83
Time Table
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 5 / 83
Time Table
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 6 / 83
Unit-II: 8051 Architecture [1]–[3]
Contents
8051 Architecture
• Hardware
• Oscillator and clock-program counter
• Data pointer registers
• Stack and stack pointer
• Special function registers
• Memory organization
• Program memory
• Data memory
• Input/Output Ports
• External memory
• counter and timer
• Serial data Input/output
• Interrupts.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 7 / 83
Unit-II: 8051 Architecture [1]–[3] Hardware
8051 Hardware
8051 Hardware
The architectural block diagram of the 8051 is shown in the following Figure.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 8 / 83
Unit-II: 8051 Architecture [1]–[3] Oscillator and clock
• The clock is a repetitive sequence of pulses used to synchronize all the internal
activities of a microcontroller/ processor.
• It is the smallest unit of time in which the microcontroller performs a part
of the program execution/operation. The clock pulses are also referred as a
system clock.
• The oscillator unit is responsible for generating the clock signals. All 8051
family microcontrollers contain an on-chip oscillator circuit (of course partial
circuit).
• We need to connect a resonant network (crystal + capacitors) to make it
functional.
• The 8051 on chip oscillator circuit provides external interface option through
two pins of the microcontroller, namely, XTAL1 and XTAL2.
• The cheapest solution is to connect ceramic resonator across the XTAL1 and
XTAL2 pins of the chip with two external capacitors having values 15pF, 22pF,
33pF, etc.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 9 / 83
Unit-II: 8051 Architecture [1]–[3] Oscillator and clock
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 10 / 83
Unit-II: 8051 Architecture [1]–[3] Oscillator and clock
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 12 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
The 8051 On-chip memory is organized into three general categories; Special
Function Registers, internal RAM and internal ROM.
Special Function Registers (SFRs)
• Used to program and control various on-chip hardware peripherals and features
of the 8051.
• Each SFR has a name which specifies the purpose of the SFR.
• 128 bytes (80H to FFH) of the SFR address space is available, but only 21 SFRs
are defined in the standard 8051. Since SFRs are programmed by instructions,
they are RAM locations.
• SFRs are categorized as follows:
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 13 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 14 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal RAM
• The 8051 has 128 bytes of internal RAM. Since it is available on-chip, it is
fastest and most flexible in terms of read/write operations. It is used to store
temporary data and results.
• This memory is subdivided into three categories as specified below:
• Register Banks: Bank 0, Bank 1, Bank 2 and Bank 3 (00H to 1FH)
• Bit Addressable RAM: Memory locations from addresses 20H to 2FH
• General Purpose RAM: Memory locations from addresses 30H to 7FH
Internal ROM
• It is used to store program instructions to be executed by the microcontroller.
• It may also be used to store permanent data like constants, passwords and
lookup tables. The 8051 has 4Kbytes of internal ROM.
• Different variants of 8051 has different amount and type of on-chip ROM.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 15 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
1. Accumulator: A
• Accumulator register is used to store (collect or accumulate) the result of all
arithmetic and majority of logical operations and because of this reason, it is
named accumulator.
• Accumulator is the most useful and versatile register because it is used in
• All arithmetic operations like addition, subtraction, multiplication and division.
• Majority of logical operations like logical AND, OR, NOT, EX-OR and Rotate.
• All data transfer between the 8051 and any external memory.
2. B
• B is used along with A in multiplication operation to hold one of the operands
(either multiplier or multiplicand) and to store higher-order byte of the result.
• It is also used in division operation to hold divisor and to store remainder of
the result.
• When not used with multiplication or division, it can be used as a general-
purpose register where one byte of data may be stored.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 16 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
3. PSW
• Program Status Word is an 8-bit register. It is also referred as flag register or
processor status word.
• Flag is a flip-flop (1-bit storage element) used to store and indicate the nature
of result produced by execution of certain instructions.
• The state of flags (0 or 1) are tested by other instructions (program flow control
or branch instructions) to make decisions.
• PSW structure is shown in the following Table.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 17 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 18 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
3. PSW
• CY: Carry Flag It is a carry (or borrow) used in addition and subtraction
operations. It is set to 1 when there is carry out from MSB (D7 bit) after an
addition (or a borrow into D7 bit during a subtraction). It is also used as the
‘Accumulator’ for the Boolean operations. It can be directly modified by bit
level instructions.
• AC: Auxiliary Carry Flag It is a half carry (carry out from bit D3 to D4) used
in conventional BCD arithmetic.
• F0: Flag 0 It is a general-purpose flag. It can be used as a one-bit memory
location to record some event.
• RS0 and RS1: Register Bank Select Bits These bits are used to select the
register bank.
• OV: Overflow Flag It is set to 1 to indicate that result of signed arithmetic
is erroneous (out of range).
• P: Parity Flag It indicates the parity of the Accumulator; it is set to 1 if the
accumulator register has odd number of ones, otherwise reset to 0, i.e. even
parity.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 19 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
4. Program Counter: PC
• Program Counter (PC) is a 16-bit register. It always contains the memory
address of the next instruction to be executed, i.e. it points to the instruction
that is to be executed next.
• As the CPU fetches the op-code (instruction byte) from the program memory,
the PC is incremented automatically to point to the next instruction.
• It should be noted that it is not always incremented by one during instruction
execution, but it depends on size of the instruction being executed, i.e. if
2-byte instruction is being executed, the PC is incremented by 2.
• There is no direct way to modify the PC but it can be modified using jump or
call instructions.
• There is no direct way to read the value of PC.
• PC is a 16-bit register, the 8051 can access program addresses from 0000H to
FFFFH, a total of 64Kbytes of program memory.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 20 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 21 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
6. Stack Pointer: SP
• Stack pointer always points to the top of the stack and used to access data
from there. It is an 8-bit register.
• Stack memory stores the program counter address, other memory and register
values during a sub routine/function call.
• On power on, it should be initialized to a defined value (its default value is
07H).
• While writing a new data byte on the stack, SP is automatically incremented
by 1 and data byte is stored at an address SP+1.
• While retrieving, data will be read from address in SP and then SP is decre-
mented by 1.
• The data is stored on to the stack using PUSH and CALL instructions and
retrieved using POP and RET instructions.
• Interrupts also use the stack to store the return addresses.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 22 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 23 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 24 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
8. Peripheral Control Registers: IP, IE, TMOD, TCON, SCON, and PCON
• IP (interrupt priority) register is used to assign priorities to different interrupt
sources. IE (interrupt enable) register is used to enable/disable interrupts.
• TMOD (timer mode) is used to control behavior, i.e. mode of operation of
timers.
• TCON (timer control) is used to start/stop timers. It also contains the status
bits of the timers and status/control bits for the external interrupts.
• SCON (serial port control) register is used to control the modes of operation
of the serial port; it also contains the status bits to indicate completion of data
transmission and reception.
• PCON (power mode control) register is used to select power saving modes of
operations, i.e. power down and idle mode. It contains a bit to double the
baud rate for serial port and two general-purpose user flags.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 25 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal RAM
1. Register Banks
• The first 32 bytes from addresses
00H to 1FH are organized as four
banks.
• Each bank is made up of eight regis-
ters named R0 to R7.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 26 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal RAM
• The four register banks are num-
bered 0 to 3, i.e. bank0, bank1, Addresses
Name RS1 RS0
bank2 and bank3. 00 01 10 11
• Each bank is assigned different ad- R0
(Bank 0)
00H
(Bank 1) (Bank 2)
08H 10H
(Bank 3)
18H
dress range, bank 0 address locations R1 01H 09H 11H 19H
R2 02H 0AH 12H 1AH
00H to 07H are given names R0 to R3 03H 0BH 13H 1BH
R7, i.e. 00H is named R0, 01H is R4 04H 0CH 14H 1CH
R5 05H 0DH 15H 1DH
named R1 and so on. R6 06H 0EH 16H 1EH
R7 07H 0FH 17H 1FH
• Similarly, bank1 (08H to 0FH), bank2 (10H to 17H), bank3 (18H to 1FH)
locations are given names R0 to R7.
• Out of these four banks, only one bank can be accessed at any time.
• Bits RS0 to RS1 in the PSW determines which register bank is currently in use
and can be modified at any time by a program to select any one of the bank.
• Register bank 0 is the default register bank, since after power on, both bits
RS0 and RS1 are initialized with value 0;
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 27 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal RAM
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 28 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal RAM
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 29 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal RAM
Internal ROM
Internal ROM
• We can also connect external ROM if 4K of
internal memory is not sufficient for a partic-
ular application.
• Our program may reside partly into internal
ROM and partly into external ROM or en-
tire program can be stored into only external
memory starting from 0000H.
• Memory map of ROM, i.e. organization of
ROM for the 8051 system is shown in Figure.
• EA (external access) pin (pin 31) on the 8051 decides one of the above con-
figuration of ROM used by the 8051.
• If EA pin is connected to VCC (+5 V), the 8051 will access first 4K bytes
(0000H to 0FFFH) from internal ROM and any address above 0FFFH will be
accessed from external ROM.
• If EA is connected to ground (0 V) then only external memory from 0000H
to FFFFH will be accessed by the 8051.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 32 / 83
Unit-II: 8051 Architecture [1]–[3] On-Chip Memory Organization
Internal ROM
• The interfacing of an exter-
nal program memory chip is
illustrated in Figure.
• The control signal for ex-
ternal program memory ex-
ecution is P SEN (Program
Strobe Enable).
• For internal program mem-
ory fetches P SEN is not ac-
tivated.
• If the program memory is external, 16 I/O lines are used for accessing the
external memory. Port 0 and Port 2 are used for external memory accessing.
• Port 0 serves as multiplexed address/data bus for external program memory
access.
• Similar to the 8085 microprocessor, Port 0 emits the lower order address first.
• This can be latched to an 8bit external latch with the Address Latch Enable
(ALE) signal emitted by 8051.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 33 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
• Port is a group of Input/Output (I/O) lines. Each port has its own port control
unit, port driver and buffers.
• The original version of 8051 supports 32 I/O lines grouped into 4 I/O ports,
consisting of 8 I/O lines per port.
• The ports are named as Port 0, Port 1, Port 2 and Port 3.
• One output driver and one input buffer is associated with each I/O line.
• All four ports are bi-directional and an 8-bit latch (Special Function Register)
is associated with each port.
• Each port’s pin consists of a D latch, an Input Buffer and an Output Driver.
• These I/O pins can be accessed directly by instructions during the program
execution.
• The I/O ports are memory mapped in the 8051.
• Out of the 32 I/O pins, 24 pins (P0, P2 and P3) may each be used for two
different functions (but only one at a time).
• The function performed by a pin at any time depends on which instruction is
used to access a pin and what signal is connected to that pin; therefore these
factors can be directly controlled by a programmer.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 34 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 35 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 36 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 0
• The Q output of the latch is copied into the internal data bus when the ‘read
latch’ signal is activated.
• The level of a port pin is copied into the internal data bus when the ‘read pin’
signal is activated.
• Read Latch reads the content of corresponding port’s SFR/SFR bit latch
whereas Read Pin reads the present state of the corresponding port pin.
• Port 0 is designed acts as an I/O port in normal mode of operation and as
a multiplexed address data bus in external data memory/program memory
operations. I
• If the program memory is external to the chip, Port 0 emits the program counter
low byte in external program memory operation for specifi c time duration and
then acts as an input port to fetch the instruction from the address specified
by the program counter.
• In external data memory operations P0 emits the lower order byte of the DPTR
Register (DPL).
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 37 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 0
• During external memory related operations the multiplexer disconnects the port
0 bit output line from its corresponding bit latch and directly connect it to the
ADDRESS/DATA line and the output driver circuitry is driven according to
the ADDRESS/DATA line and the control.
• The output drivers of Port 0 are formed by two FETs, out of which the top
FET functions as the internal port pull-up.
• The pull-up FET driver for Port 0 is active only when the address line is emitting
1s during external memory operations.
• The pull-up FET will be off on all other conditions and the Port 0 pins which
are used as output pins will become open drain (Open Collector for TTL logic).
• Port 0 SFR(P0)(SFR-80H): Port 0 SFR is a bit addressable Special Function
Register that acts as the bit latch for each Port 0 pins.
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 38 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 39 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 1
• When Port 1 pins are configured as inputs (by writing a 1 to the corresponding
Port 1 SFR bit latch) the pins are pulled high and they can source current when
an externally connected device pulls the port pin to low, signaling a logic 0 at
the corresponding input line and places logic 0 to the internal bus in response
to a Read Pin command.
• If the externally connected device forces logic high, the Read Pin control signal
generated by a Read Pin related command places logic high into the internal
bus.
• Since Port 1 holds fi xed internal pull ups and are capable of sourcing current,
it is known as Quasi Bidirectional.
• Port 1 SFR(P1)(SFR-90H): It is also a bit addressable Special Function
Register that acts as the bit latch for each pin of Port 1. The bit details of
Port1 SFR is given below.
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 40 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 2
• Port 2 is designed to operate in two different modes.
• It acts as general purpose I/O port in normal operational mode and acts as
higher order address bus in external data memory/program memory operations.
• Following Figure illustrates the Port 2 pin organization.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 41 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 2
• Port 2 emits the higher order byte of external memory address if the address
is 16 bits wide.
• During 16-bit wide external memory operations the base drive for the O/p
driver FET is internally switched to the address line.
• If the address line is emitting a 1, the O/p driver FET is turned off and the
logic 1 is refl ected on the O/p pin.
• If the address line is emitting a 0, the O/p driver FET is turned on and the
logic 0 is reflected at the corresponding pin.
• P2 is a Quasi bi-directional port.
• Port 2 SFR(P2)(SFR-A0H): It is a bit addressable Special Function Register
that acts as the bit latch for each pins of Port 2. The reset value of Port 2
SFR is FFH (All bit latches set to 1).
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 42 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 3
• Port 3 is a general purpose I/O port which is also configurable for implementing
alternative functions.
• Port 3 Pin configuration is shown in following Figure.
• Port 3 is identical to Port 1 in operation. All the settings that need to be done
for configuring Port 1 as I/O port is applicable to Port 3 also.
• The only difference is that the SFR latch for Port 3 is P3. Port 3 supports
alternate I/O functions.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 43 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Port 3
• From the Port 3 pin configuration it is clear that the alternate I/O functions
will come into action only if the corresponding SFR bit latch is set to logic 1.
Otherwise the port pin remains at logic 0.
• Port 3 SFR(P3)(SFR-B0H): It is a bit addressable Special Function Register
that acts as the bit latch for each pin of Port 3. Reset value of Port 3 SFR is
FFH (All bit latches set to 1)
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 44 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 45 / 83
Unit-II: 8051 Architecture [1]–[3] The 8051 Ports
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 46 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
• Timers are very essential for generating precise time reference in any system.
• Timers can be implemented in either software or hardware.
• The Standard 8051architecture supports two 16bit hardware timers namely:Timer
0 and Timer 1, that can be configured to operate in either timer mode or
external event counting mode.
• In timer function mode, the timer register is incremented once in each ma-
th
chine cycle i.e. the timer increment rate is 1/12 of the oscillator frequency
(fosc/12).
• In counter mode, external input pins are sampled during S5P2 of each machine
cycle, and if a high-low transition occurs across sampling times, the counter
register increments, with the count updated during S3P1 of the subsequent
machine cycle.
• The maximum count rate for external events is fosc/24.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 47 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
• Timer 0 and Timer 1 can be configured as timer unit or counter unit by using
timer/ counter mode select bit of the special function register Timer/counter
Mode Control ( TMOD).
• Timer 0 and Timer 1 can be operated in four different modes. The mode
selection is done by the timer mode select bits of TMOD register.
• Timer/Counter Mode Control Register (TMOD) (SFR-89H):
Timer 1 Timer 0
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
GATE C/T M1 M0 GATE C/T M1 M0
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 48 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
The following table explains the meaning and use of each bit in the TMOD
register.
Bit Name Description
GATE Gating control when GATE is 1, the timer/counter will
be enabled only when the INT0 /INT1
line is high and run bit TR is enabled
in the TCON register. If GATE bit is 0,
timer/counter will be enabled only when
TR is enabled in the TCON register.
C/T Counter/Timer C/T = 0 configures the timer as a inter-
selector val timer (or time-delay generator), C/T
= 1 will configure the timer as an event
counter
M1 The Timer/Counter operation ‘mode se-
Mode select bits
lect’ bits.
M0 It can be one among the 4 modes.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 49 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
These are Timer/Counter mode select bit as per the below table.
M1 M0 Mode Operation
0 0 0 (13-bit timer mode) 13-bit timer/counter, 8-bit
of THx & 5-bit of TLx
0 1 1 (16-bit timer mode) 16-bit timer/counter, THx
cascaded with TLx
1 0 2 (8-bit auto-reload mode) 8-bit timer/counter (auto-
reload mode), TLx reload
with the value held by THx
each time TLx overflow
1 1 3 (split timer mode) Split the 16-bit timer into
two 8-bit timers i.e. THx
and TLx like two 8-bit timer
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 50 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
Timer/Counter Control Register (TCON) (SFR-88H)
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
TF1 TR1 TF0 TR 0 IE1 IT1 IE0 IT0
TF1 Timer/Counter 1 overflow flag: Set by hardware when timer/counter 1 overflows. The fl
ag is automatically cleared when timer 1 interrupt is vectored.
TR1 Timer/Counter 1 Run control: TR1=1 Start timer/counter1, TR1= 0 Stops timer/counter
1
TF0 Timer/Counter 0 overflow flag: Set by hardware when timer/counter 0 overflows. The flag
is automatically cleared when timer 0 interrupt is vectored.
TR0 Timer/Counter 0 Run control: TR0 = 1 Start Timer/Counter 0, TR0 = 0 Stops Timer/-
Counter 0
IE1 External Interrupt 1 edge detect flag: Set by hardware when external interrupt 1 edge is
detected. Cleared by hardware when interrupt is vectored
IT1 External Interrupt 1 type control: IT1 = 1 Configures the external interrupt 1 to edge
triggered (Falling Edge), IT1= 0 Configures the external interrupt 1 to level triggered (Low
level)
IE0 External interrupt 0 edge detect flag: Set by hardware when external interrupt 0 edge is
detected. Cleared by hardware when interrupt is vectored
IT0 External interrupt 0 type control: IT0 = 1 Configures the external interrupt 0 to edge
triggered (Falling edge), IT0 = 0 Configures the external interrupt 0 to level triggered (Low
level)
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 51 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
Timer/Counter in Mode 0
• Timer/Counter-x in mode 0 acts as a 13bit timer/counter for which 8 bits of
THx (Timer High) and 5 bits of TLx (Timer Low) are used.
• The timer/counter mode selection is done by the bit C/T of the register
TMOD.
• It is commonly used for generating precise delays and suitable for applications
where a 16-bit timer is needed.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 52 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
Timer/Counter in Mode 1
• Timer/Counter-x in mode 1 acts as a 16bit timer/counter for which 8 bits of
THx (Timer High) and 8 bits of TLx (Timer Low) are used.
• The timer/counter mode selection is done by the bit C/T of the register
TMOD.
• It is commonly used for generating delays.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 53 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
Timer/Counter in Mode 2
• Timer/Counter-x in mode 0 acts as a 8bit timer/counter with auto reload on
the timer/counter register overflow.
• We load an 8-bit value into the THx (Timer High) register.
• When the timer starts, the TLx (Timer Low) value gets automatically loaded
from THx.
• TLx then starts counting from that value.
• When the timer overflows, it reloads the value from THx automatically.
• Mode 2 is commonly used for generating precise delays.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 54 / 83
Unit-II: 8051 Architecture [1]–[3] Timers
Timers
Timer/Counter in Mode 3
• Timer 0 in Mode 3 behaves as two completely separate 8-bit timers
• TL0 is an 8-bit interval timer/event counter controlled by Timer 0 control bits
(C/T, GATE, TR0, TF0 and INT0)
• TH0 is the only 8-bit interval timer controlled by Timer 1 control bits TR1
and TF1 and, therefore, controls the Timer 1 interrupt. Timer Mode 3 is also
referred as split timer mode.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 55 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Interrupts
Interrupts
Use of Interrupts
In any interrupt based systems, interrupts are mainly used for accomplishing
the following tasks:
• I/O data transfer between peripheral devices and processor/controller
• Timing applications
• Handling emergency situations (e.g. switch off the system when the battery
status falls below the critical limit in battery operated systems)
• Context switching/ Multitasking/Real-Time application programming
• Event driven programming
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 57 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
• The basic 8051 and its ROMless counterpart 8031AH supports five interrupt
sources.
• Three of them are internal interrupts:namely Timer 0 (TF0), Timer1 (TF1)
and Serial Port (TI or RI) interrupts.
• The remaining two are external interrupts INT0 and INT1. These external
interrupts are also referred as IE0 and IE1 respectively.
• For each interrupt source, there is a fixed location in the program memory that
contains its Interrupt Service Routine (ISR). This part of the memory which-
stores the ISRs is called the Interrupt Vector Table (IVT) which is shown in th
efollowing Table.
Interrupt source Interrupt vector address Interrupt type Interrupt flag clearing
External interrupt 0 (INT0) 0003H External Auto*
Timer 0 interrupt (TF0) 000BH Internal Auto
External interrupt 1 (INT1) 0013H External Auto*
Timer 1 interrupt (TF1) 001BH Internal Auto
Serial port interrupt (TI or RI) 0023H Internal By program
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 58 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Enabling Interrupts
• The interrupt system of 8051 can be enabled or disabled totally under software
control and achieved by setting or clearing the global interrupt enable bit of
the Special Function Register Interrupt Enable (IE).
• :The bit details of the Interrupt Enable (IE) (SFR- A8H) Register is as:
IE.7 IE.6 IE.5 IE.4 IE.3 IE.2 IE.1 IE.0
EA RSD RSD ES ET1 EX1 ET0 EX0
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 59 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 61 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 63 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 64 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 65 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 66 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 67 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 68 / 83
Unit-II: 8051 Architecture [1]–[3] Interrupts
Interrupt Latency
• The processor latches each interrupt flag only at S5P2 of each machine cycle.
• So there is no point even if the interrupt occurs at S1P1 of the machine cycle.
• It is latched only at S5P2 of the current machine cycle
• The latched interrupts flags are polled at S5P2 of the following machine cycle
• An LCALL is generated to the corresponding ISR, if no other conditions block
the call.
• So this delay itself contributes a significant part in interrupt latency.
• The interrupt latency part which contributes the delay in servicing the ISR is
the sum of the following time delays: Time between the interrupt assertion to
the start of state S5 of current machine cycle (polling cycle) + (Time for S5
& S6) + Remaining machine cycles for the current instruction in execution
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 69 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 70 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
• There are special IC’s made by many manufacturers for serial communications.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 71 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Asynchronous – Start & Stop Bit
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 72 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 73 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 74 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 76 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 77 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 78 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 79 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 80 / 83
Unit-II: 8051 Architecture [1]–[3] Serial Port
Serial Port
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 81 / 83
References
References I
[1] R. Kamal, Embedded Systems Architecture Programming and Design, 2nd. The McGraw
Hill Companies, 2017.
[2] M. K. Patel, The 8051 Microcontroller Based Embedded Systems. McGraw Hill Education
(India) Private Limited, 2014, isbn: 9332901252; 9789332901254.
[3] K. Shibu, Introduction to embedded systems. Tata McGraw-Hill Education, 2009.
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 82 / 83
References
Thank you
&
Any Query?
Dr. Ishwar Chandra Yadav (Ph.D. NIT Patna) February 21, 2024 83 / 83