• the way in which the operand of an instruction is specified.
Microcontrollers for Embedded Systems:
A microcontroller (MCU) is a compact
integrated circuit designed for controlling
embedded systems.
It contains a processor, memory, and
input/output (I/O) peripherals on a single chip.
Microcontrollers are widely used in applications
such as IoT, automation, robotics, healthcare,
consumer electronics, and automotive systems.
Key Features of Microcontrollers in Embedded Systems
Processor (CPU) – Executes instructions and performs
computations.
Memory – Includes RAM (for temporary storage) and
ROM/Flash (for permanent program storage).
I/O Ports – Interfaces with external devices (sensors,
displays, actuators, etc.).
Timers & Counters – Used for timing operations and event
counting.
Communication Interfaces – Supports protocols like
UART, SPI, I2C, CAN, etc.
Power Efficiency – Designed for low power consumption in
battery-operated devices.
Real-Time Processing – Some microcontrollers have real-
time capabilities for fast and predictable responses.
Applications of Microcontrollers in Embedded
Systems IoT Devices – Smart home automation, wearable
devices.
Automotive Systems – Engine control units (ECUs), ABS,
airbag controllers.
Consumer Electronics – Smart TVs, washing machines,
digital cameras.
Medical Devices – Heart rate monitors, glucose meters,
pacemakers.
Industrial Automation – PLCs, motor controllers, factory
automation.
The image presents the block diagram of the 8051
microcontroller, which consists of various functional blocks that
enable it to perform operations.
1. CPU (Central Processing Unit)
The brain of the microcontroller.
Executes instructions stored in memory.
Controls all operations of the microcontroller.
2. On-Chip ROM (Read-Only Memory) for Program Code
Stores the program code permanently.
The 8051 microcontroller typically has 4 KB of ROM.
3. On-Chip RAM (Random Access Memory)
Stores temporary data and variables during execution.
The 8051 has 128 bytes of internal RAM.
4. Interrupt Control
Handles external and internal interrupts.
The 8051 has five interrupt sources (two external and three
internal).
Allows priority-based handling of tasks.
5. Timer/Counter (Timer 0 & Timer 1)
Used for generating precise time delays.
Can function as counters to count external events.
Supports mode-based operation.
6. OSC (Oscillator)
Generates the clock signal required for microcontroller operation.
Usually operates at 11.0592 MHz frequency.
The clock frequency determines the execution speed of instructions.
7. Bus Control
Manages the flow of data between different components.
Coordinates communication between CPU, memory, and I/O ports.
8. 4 I/O Ports (P0, P1, P2, P3)
The 8051 has four 8-bit input/output ports.
These ports (P0, P1, P2, P3) are used for interfacing with
external devices.
Some ports have dual functions (such as serial communication,
timers, etc.).
9. Serial Port (TxD & RxD)
Enables serial communication using protocols like UART.
TxD (Transmit Data) sends data, while RxD (Receive Data)
receives data.
Used for interfacing with serial devices like PCs, sensors, GSM
modules, etc..
10. Address/Data Bus
The microcontroller uses 8-bit data buses for transferring data.
Uses a 16-bit address bus to access external memory (if
needed).
Handles communication between CPU, memory, and peripherals.
An 8051 microcontroller has the following 12 major
components:
1. ALU (Arithmetic and Logic Unit)
2. PC (Program Counter)
3. Registers
4. Timers and counters
5. Internal RAM and ROM
6. Four general purpose parallel input/output ports
7. Interrupt control logic with five sources of interrupt
8. Serial date communication
9. PSW (Program Status Word)
10. Data Pointer (DPTR)
11. Stack Pointer (SP)
12. Data and Address bus.
Registers
Registers are usually known as data storage devices. 8051
microcontroller has 2 registers, namely Register A and Register
B.
Register A serves as an accumulator while Register B functions
as a general purpose register.
These registers are used to store the output of mathematical and
logical instructions.
The operations of addition, subtraction, multiplication and
division are carried out by Register A.
Register B is usually unused and comes into picture only when
multiplication and division functions are carried out by Register
A.
Register A also involved in data transfers between the
microcontroller and external memory.
Carry (CY) flag: Set
to 1 if there is a
carry-out from the
MSB
Overflow (OV)
flag: Indicates if
there was an
overflow during a
signed operation
Parity (P) flag: Set
to 1 if the result
has an even
number of set bits.
Pin Diagram of 8051
Addressing Modes
in
8051 MC
Internal Indirect addressing mode:
Indirect Addressing Mode in 8051 Microcontroller
In 8051 microcontroller, indirect addressing mode allows
accessing data stored in internal RAM or external memory
using a register as a pointer instead of a direct address.
How Indirect Addressing Mode Works
Instead of specifying the memory address directly, a
register (R0 or R1) or the DPTR (Data Pointer Register)
holds the address of the memory location.
The instruction operates on the value stored at the
memory location pointed to by the register.
Registers Used for Indirect Addressing
R0 and R1 (8-bit registers)
Used to access internal RAM (00H – 7FH).
Example: MOV A, @R0 (Moves the value from the address
stored in R0 to A).
DPTR (16-bit register)
Used for accessing external memory.
Example: MOVX A, @DPTR (Moves data from external
memory pointed by DPTR to A).
Internal RAM Access Using Indirect Addressing
MOV R0, #30H ; Load R0 with address 30H
MOV @R0, #55H ; Store 55H at address 30H
MOV A, @R0 ; Load value from address 30H into A
External RAM Access Using DPTR
MOV DPTR, #2000H ; Load DPTR with address 2000H
MOVX A, @DPTR ; Load A with data from external memory 2000H
MOVX @DPTR, A ; Store A’s value back to 2000H
Indexed Addressing Mode
• In the indexed addressing mode, the content of a given
index register gets added to an instruction’s address part
so as to obtain the effective address.
• Here, the index register refers to a special CPU register
that consists of an index value.
• An instruction’s address field defines the beginning
address of any data array present in memory.
Instruction Set
1. Data Transfer Group
The data transfer group of instructions in the 8051
allows for the movement of data between the
accumulator, registers, and memory.
For example, the MOV A, source instruction transfers
data from the source to the accumulator, while MOV
destination, source moves data from the source to the
destination.
MOV Instruction
MOVC Instruction
MOVX Instruction
PUSH and POP Instructions
1. Register to Accumulator:
MOV A, Rn: Moves the content of register Rn to the
accumulator.
MOV Rn, A: Moves the content of the accumulator to register
Rn.
Example:
MOV A, R1 ; Move content of R1 to accumulator
MOV R2, A ; Move content of accumulator to R2
2. Immediate to Accumulator (MOV A, #data):
MOV A, #data: Moves an immediate data value to the
accumulator.
Example:
MOV A, #25 ; Move the immediate value 25 to the
accumulator
3.Register to Register (MOV register1, register2):
MOV register1, register2: Moves the content of
register2 to register1.
Example:
MOV R3, R4 ; Move content of R4 to R3
4. Direct to Register (MOV A, direct):
MOV A, direct: Moves the content of the specified
memory location (direct address) to the accumulator.
Example:
MOV A, 0x30 ; Move content of memory location 0x30
to accumulator
5.Register to Direct (MOV direct, A):
MOV direct, A: Moves the content of the accumulator to
the specified memory location (direct address).
Example:
MOV 0x40, A ; Move content of accumulator to
memory location 0x40
6. Immediate to Register (MOV Rn, #data):
MOV Rn, #data: Moves an immediate data value to the
specified register.
Example:
MOV R5, #10 ; Move the immediate value 10 to
register R5
7. The MOVC instruction, short for "Move
Code," is a specific instruction in the 8051
microcontroller's instruction set. It is designed to
fetch code or data from program memory and
move it into the accumulator.
Example:
MOVC A, @A+DPTR
A is the accumulator.
@A+DPTR signifies that the content of the
address calculated by adding the accumulator (A)
and the Data Pointer (DPTR) is fetched from
program memory and moved to the accumulator.
.
8. MOVX Instruction
The MOVX instruction, short for "Move External," is
an instruction in the 8051 microcontroller's
instruction set designed to facilitate data transfer
between the accumulator and external memory,
specifically External RAM.
MOVX A, @DPTR
A is the accumulator.
@DPTR signifies that the content of the address
pointed to by the Data Pointer (DPTR) is fetched from
external RAM and moved to the accumulator.
2. Arithmetic Group:
ADD A, source: Add the content of the source
to the accumulator.
ADDC A, source: Add the content of the source
and the carry flag to the accumulator.
SUBB A, source: Subtract the content of the
source and the borrow from the accumulator.
INC register: Increment the specified register.
Arithmetic and Logical Groups :
In the arithmetic group, instructions like ADD, ADDC, SUBB, and
INC facilitate arithmetic operations.
The logical group includes instructions like ANL, ORL, and XRL
for bitwise AND, OR, and XOR operations. Additionally, the CJNE
instruction compares the accumulator with a byte and jumps if not
equal, providing conditional branching based on comparison
results.
Example: 1
MOV A, #25 ; Load A with the value 25
ADD A, #15 ; Add the immediate value 15 to A
Example: 2
Register Operand:
MOV A, #50 ; Load A with the value 50
MOV B, #30 ; Load B with the value 30
ADD A, B ; Add the content of B to A
ADDC:
This instruction is used to add the contents of the accumulator (A)
with another operand, along with the carry flag (CY), and store
the result back in the accumulator.
MOV A, #40 ; Load A with the value 40
MOV B, #30 ; Load B with the value 30
MOV CY, #1 ; Set the carry flag to 1
ADDC A, B ; Add the content of B and the carry flag to A
SUB:
The SUB instruction in the 8051 microcontroller's instruction set is
used for subtracting two operands and storing the result back in
the accumulator.
SUB A, source
Example:
MOV A, #40 ; Load A with the value 40
SUB A, #20 ; Subtract the immediate value 20 from A
The SUBB instruction in the 8051 microcontroller's instruction
set is used for subtracting two operands, including the borrow
flag (BORROW), and storing the result back in the accumulator.
SUBB A, source
Example:
MOV A, #40 ; Load A with the value 40
MOV B, #30 ; Load B with the value 30
MOV BORROW, #1 ; Set the borrow flag to 1
SUBB A, B ; Subtract the content of B and the borrow flag
from A
The INC instruction in the 8051 microcontroller's instruction set
is used to increment the value of a specified register or memory
location by 1.
MOV R0, #5 ; Load register R0 with the value 5
INC R0 ; Increment the content of register R0 by 1
3. Stack Operations:
PUSH source/POP destination: Push data
onto the stack/pull data from the stack.
POP Instruction:
The POP instruction is used to pop the topmost data
from the stack and store it in a register or the
accumulator.
MOV A, #20; Load A with a value
PUSH A; Push A onto the stack
MOV A, #30; Load A with another value
PUSH A; Push the new value onto the stack
POP A; Pop the top of the stack into A
(retrieving the second value)
4. Logical Group:
1.ANL A, source: Perform a bitwise AND operation
between the accumulator and the source.
2.ORL A, source: Perform a bitwise OR operation
between the accumulator and the source.
3.XRL A, source: Perform a bitwise XOR operation
between the accumulator and the source.
i) The AND operation performs a bitwise AND between
corresponding bits of two operands.
Syntax:
AND A, source
Example:
MOV A, #0xAA ; Binary: 10101010
AND A, #0x0F ; Binary: 00001111; After AND operation,
A will be 00001010 (0x0A)
ii) The OR operation performs a bitwise OR between
corresponding bits of two operands.
Syntax:
OR A, source
Example:
MOV A, #0xAA ; Binary: 10101010
OR A, #0x0F ; Binary: 00001111; After OR operation, A
will be 10101111 (0xAF)
iii) XOR (Logical XOR):
The XOR operation performs a bitwise exclusive OR between
corresponding bits of two operands.
Syntax:
XOR A, source
Example:
MOV A, #0xAA ; Binary: 10101010
XOR A, #0xF0 ; Binary: 11110000; After XOR operation, A
will be 01011010 (0x5A)
iv) NOT (Logical NOT):
The NOT operation (also referred to as the one's complement)
inverts each bit of the operand.
Syntax:
CPL A
Example:
MOV A, #0xAA ; Binary: 10101010
CPL A ; After NOT operation, A will be 01010101 (0x55)
v) Bit Shifting (Logical Shift Left and Right):
Bit-shifting operations move the bits of a binary number to
the left (LSL or RLC) or right (LSR or RRC). These are often
used for multiplication or division by powers of 2.
Syntax:
LSL A ; Logical Shift Left
LSR A ; Logical Shift Right
Example:
MOV A, #0x0A ; Binary: 00001010
LSL A ; After left shift, A will be 00010100 (0x14)
5. Branching Group:
1.SJMP address: Unconditional short jump.
2.JZ/JNZ address: Jump if zero/not zero.
3.JC/JNC address: Jump if carry/not carry.
4.JMP @A+DPTR: Jump to the address
pointed by the DPTR and A.
1. The SJMP (Short Jump) instruction in the 8051 microcontroller's
instruction set is used for unconditional short jumps, allowing for
altering the program flow by specifying a target address relative to the
current program counter.
ORG 0x00 ; Start of the code section
LOOP_START:
; Assume P1.0 is connected to an LED
MOV P1.0, #1 ; Turn on the LED
CALL DELAY ; Call a delay subroutine
MOV P1.0, #0 ; Turn off the LED
CALL DELAY ; Call a delay subroutine
SJMP LOOP_START ; Unconditional short jump back to
LOOP_START
DELAY:
; Assume a delay routine is implemented here
; For simplicity, you can use a loop or a timer-based delay
; The actual delay implementation may vary based on the hardware
and requirements
2. The JC (Jump if Carry) and JNC (Jump if No Carry) instructions in
the 8051 microcontroller's instruction set are conditional jump
instructions based on the status of the Carry Flag (CY).
JC (Jump if Carry):
The JC instruction jumps to the specified target address only if the
Carry Flag is set, indicating that there was a carry out of the most
significant bit during a previous addition or a borrow during a
previous subtraction.
Example:
MOV A, #255 ; Load A with the maximum value (255)
ADD A, #1 ; Attempt to add 1 to A (carry will occur)
JC CARRY_OCCURRED ; Jump to CARRY_OCCURRED if there is a carry
; Code executed if there is no carry
; ...
CARRY_OCCURRED:
; Code executed if there is a carry
; ...
3. The JZ (Jump if Zero) and JNZ (Jump if Not Zero) instructions
in the 8051 microcontroller's instruction set are conditional jump
instructions based on the status of the Zero Flag (Z).
MOV A, #0 ; Load A with the value 0
JZ ZERO_CASE ; Jump to ZERO_CASE if A is zero
; Code executed if A is not zero
; ...
ZERO_CASE:
; Code executed if A is zero
; ...
MOV A, #5 ; Load A with the value 5
JNZ NOT_ZERO_CASE ; Jump to NOT_ZERO_CASE if A is not
zero
; Code executed if A is zero
; ...
NOT_ZERO_CASE:
; Code executed if A is not zero
4. The CJNE instruction (Compare and Jump if Not Equal)
in the 8051 microcontroller's instruction set is used for
comparing the accumulator (A) with an immediate value, a
register
MOV A, #30 ; Load A with the value 30
CJNE A, #40, NOT_EQUAL ; Compare A with the immediate
value 40
; Code executed if A is equal to 40
; ...
NOT_EQUAL:
; Code executed if A is not equal to 40
; ...
The JNC instruction jumps to the specified target address only if the
Carry Flag is cleared, indicating that there was no carry from the most
significant bit during a previous addition or no borrow during a
previous subtraction.
Example:
MOV A, #200 ; Load A with a value
SUBB A, #50 ; Attempt to subtract 50 from A (no borrow will occur)
JNC NO_BORROW ; Jump to NO_BORROW if there is no borrow
; Code executed if there is a borrow
; ...
NO_BORROW:
; Code executed if there is no borrow
6. Control Instructions:
NOP: No operation.
HLT: Halt the CPU.
DI/EI: Disable/Enable interrupts.
CLR bit: Clear a specific bit in a register or
a memory location
In many instruction sets, including the 8051 microcontroller's
instruction set, NOP (No Operation) is an instruction that does
nothing and simply advances the program counter to the next
instruction.
HLT: Halt the CPU.
The HLT instruction in the 8051 microcontroller's instruction set is
used to halt the CPU (Central Processing Unit). When the HLT
instruction is executed, the CPU stops executing instructions,
effectively entering a halt state. In this state, the CPU will not proceed
to execute any further instructions until it is re
Example:
START:
MOV A, #10 ; Load A with a value
ADD A, #20 ; Add 20 to A
JZ ZERO_CASE ; Jump to ZERO_CASE if A is zero
HLT ; Halt the CPU (stops further execution)
ZERO_CASE:
; Code executed if A is zero
The DI (Disable Interrupts) and EI (Enable Interrupts) instructions in
the 8051 microcontroller's instruction set are used to control the
interrupt system.
DI (Disable Interrupts):
The DI instruction is used to disable interrupts. When executed, it sets
the interrupt enable bit in the Interrupt Control Register to 0, preventing
the microcontroller from responding to external interrupts.
START:
DI ; Disable interrupts
; Critical section of code where interrupts should not occur
; ...
EI ; Enable interrupts
; Rest of the program where interrupts can occur
; ...
The "CLR bit" operation is a common term used in microcontroller
programming to clear (set to 0) a specific bit in a register or a
memory location
7. Data Manipulation Group:
RL A/RR A: Rotate left/right through carry.
SWAP A: Swap the nibbles in the accumulator.
DA A: Decimal adjust the accumulator after addition.
8. Bit Manipulation Group:
SETB bit: Set a specific bit in a register or a memory location.
CLR bit: Clear a specific bit in a register or a memory location.
CPL bit: Complement (invert) a specific bit.
Memory organisation of 8051
Memory Organization:
8051 supports two types of memories.
1. Internal Memory
2. External Memory
Internal Memory:
Internal memory means on chip memory, this is the
memory, which is inbuilt into a microcontroller. 8051
microcontroller has separate on-chip program memory
and data memory. Internal data memory is accessible by
the user. Internal memories supported by 8051 are
grouped as:
1. 128 bytes RAM
2. 4K bytes ROM
Internal RAM:
The 128 bytes of Internal RAM is grouped
as follows and is shown in the figure below:
1. Register Bank
2. Bit addressable Area
3. Scratch Pad Area
Register Banks (0, 1, 2, 3):
There are 4-register banks supported by 8051 and each bank with
8 registers, denoted as register banks 0,1,2,3 (Total number of
registers are 32).
The registers in each bank are denoted as R0, R1, …… and R7 of
one-byte each.
The current register bank is denoted by the status of RS0 and RS1
bits of the PSW. This means, there can be 4 registers having the
name R0.
These can be at actual locations 00H, 08H, 10H or 18H depending
on the RS0 and RS1 bits of the PSW, this is illustrated in the table
below.
In other words, the definition of R0 can be changed from location
00H to location 08H by programming RS0 and RS1 bits from 00
to 01.
Bit addressable Area:
Sixteen 8-bit locations, 20H through 2FH, are bit addressable.
Each bit of these locations (i.e., 16 locations*8 bits =128 bits)
can be directly accessed.
These bits can be addressed either as 00H to 7FH or as 20.0 to
2F.7. Each location can also be addressed as a byte.
These bits can be individually (and independent of other bits in
the register) Set, cleared, Complimented, AND, OR, X-OR etc
with carry bit using bit manipulation instructions. Checking the
status of these bits will call for conditional jumps.
Scratch Pad Area:
Locations 30H through 7FH are available as data RAM or scratch
pad locations. These can store any data like partial results and
such other variables. In general it can be used as general-purpose
memory locations.
Internal ROM of 4K Bytes
8051 microcontroller supports 4K Internal ROM for program.
Setting EA pin to high level (Vcc) can access this internal ROM.
External Memory
8051 supports external memory of 64K Bytes of data memory and
64K Bytes of program memory (total 128K Bytes).
The external memory is interfaced and accessed by using 16
address lines available on port-0 and port-2.
The address and data lines are multiplexed on Port-0. In order to
use address and data lines separately for external memory, signals
on port-0 have to be de-multiplexer.
ALE signal is used to de-multiplex address and data bus. The other
main signals used to interface external memory are RD, WR, PSEN
and EA.
Data Memory:
8051 supports 64K Bytes of external data memory in addition to
128 Bytes of internal RAM. The external data memory is accessed
using 16-bit address lines and its address varies from 0000H to
FFFFH.
Program Memory:
8051 supports 64K Bytes of program memory. This 64K Bytes of program
memory is organized as follows:
4k Internal + 60K External
64K External
Vectored interrupts are a type of hardware interrupts that
allow the CPU to quickly identify the source of the interrupt
and execute the corresponding interrupt service routine (ISR)
ISR (Interrupt Service Routine), which is a short program to instruct the
microprocessor on how to handle the interrupt.
Timer and Counter
• 8051 microcontrollers are mainly used to
speed up our tasks because they are very easy
to use and they are also fast to complete tasks
easily.
• These microcontrollers have one main feature
which is Timers and counters. This feature is
widely used in microcontrollers to measure the
time and as well as to count the events or
tasks.
• By learning about these timers and
counters ,we can make good embedded
systems. In this article let us see how these
counters and timers work in microcontrollers.
• 8051 Timers and Counters
• Timers and counters are one of the best
feature that is provided by microcontrollers.
• Timers are used to measure the time and for
creating time delays .
• Counters are used to count the events or
tasks that are taking place outside the
microcontrollers.
• We can setup these timers and counters with
the microcontrollers to make our tasks in
different ways to fit in different tasks.
• Types of 8051 Timers and Counters
• The 8051 microcontroller mainly has two
timers they are Timer 0 and Timer 1.
• These are used as both timers as well
as counters.
• They are 16-bit long but the format of the
microcontroller is 8-bit, due to that the Timers
or counters are divided into two 8-bit parts
a low byte and a high byte.
• Let’s see one by one,
• What is Timer 0 (T0) ?
• Timer 0 is one of the main timers/counters in
the 8051 microcontroller, used for doing
timing operations and counting events. It is
divided into two 8-bit registers they are TL0
(Timer 0 Low byte) and TH0 (Timer 0 High
byte). By combining both, these form a 16-bit
timer/counter.
• TL0 (Timer 0 Low Byte)
• TL0 is the lower 8-bit register of Timer 0.
• It stores the lower byte of the count value.
• When timer 0 functions as a 16-bit timer, TL0
increases first, and TH0 increases by one
following an overflow (when 255 is achieved).
• TH0 (Timer 0 High Byte)
• TH0 is the upper 8-bit register of Timer 0.
• It stores the higher byte of the count value.
• When TL0 overflows, Timer 0 can count up to
65,535 (FFFFH) before spilling since TH0 is
increased.
Given below is the Structure of Timer 0.
• What is Timer 1 (T1) ?
• Timer 1 is also one of the main
timers/counters in the 8051 microcontroller,
used for doing timing operations and counting
events.
• It is also divided into two 8-bit registers they
are TL1 (Timer 1 Low byte) and TH1 (Timer 1
High byte). By combining both, these form a
16-bit timer/counter
• What is Timer 1 (T1) ?
• Timer 1 is also one of the main
timers/counters in the 8051 microcontroller,
used for doing timing operations and counting
events.
• It is also divided into two 8-bit registers they
are TL1 (Timer 1 Low byte) and TH1 (Timer 1
High byte). By combining both, these form a
16-bit timer/counter
• TL1 (Timer 1 Low Byte)
• Timer 1's bottom 8-bit register is designated
as TL1.
• It contains the count value's bottom byte.
• When using a 16-bit timer, TL1 increases first
and then TH1 by 1 once TL1 reaches its
maximum value of 255.
• TH1 (Timer 1 High Byte)
• Timer 1's upper 8-bit register is designated as
TH1.
• It contains the count value's higher byte.
• When TL1 overflows, TH1 increments,
allowing Timer 1 to count up to 65,535
(FFFFH) before overflowing.
• .
• Timer Control Registers-TCON and TMOD
• TCON and TMOD are the special function
registers in the 8051 microcontroller. These are
used to control the timers and counters.
• TCON (Timer Control Register): The timers'
start and stop are aided by this register. It
additionally indicates whether the timer is done
counting.
• TMOD (Timer Mode Register): The timers'
mode is adjusted using this register. It selects
whether the timers will record events occurring
outside of the microcontroller or record time.
• TCON (Timer Control Register)
• The 8051 microcontroller has a unique
function register called the TCON (Timer
Control Register). In order to provide precise
output, timers and counters are controlled by
it.
• The data in the registers may overflow if these
timers and counters are not under control.
Thus, the TCON is utilized to control the
timers and counters. The structure in detail is
displayed below.
The TCON register is an 8-bit register, where
each bit has a specific function:
Bit
Description
Name
7 TF1 (Timer 1 Overflow Flag) Set to 1 when Timer 1 overflows else 0.
Set to 1 to start Timer 1, and set to 0 to stop
6 TR1 (Timer 1 Run Control Bit)
Timer 1.
5 TF0 (Timer 0 Overflow Flag) Set to 1 when Timer 0 overflows else 0.
Set to 1 to start Timer 0, and set to 0 to stop
4 TR0 (Timer 0 Run Control Bit)
Timer 0.
Set to 1 when an external interrupt 1 occurs else
3 IE1 (Interrupt 1 Edge Flag)
0.
IT1 (Interrupt 1 Type Control Set to 1 to configure external interrupt 1 as
2
Bit) edge-triggered, and set to 0 for level-triggered.
Set to 1 when an external interrupt 0 occurs else
1 IE0 (Interrupt 0 Edge Flag)
0.
IT0 (Interrupt 0 Type Control Set to 1 to configure external interrupt 0 as
0
Bit) edge-triggered, and set to 0 for level-triggered.
• TMOD (Timer Mode Register)
• The TMOD (Timer Mode Register) is a special function
register in the 8051 microcontroller. Timer 0 and Timer 1
are the modes of operation that it is utilized to set.
Whether a timer or counter needs to be set, it is done so
using this register. The structure in detail is displayed below.
• Structure of TMOD Register
• The eight bits of the TMOD register are split into two
sections: Timer 0 is controlled by the lower four bits, and
Timer 1 is controlled by the upper four bits. Similar bit
fields in each component set the timers' mode and behavior.
Bit Bit Name Description
Gate Control
7 or 3 Gate (0 or 1)
Bit
C/Tx Counter/Timer
6 or 2
Select Bit
TxM1 Timer Select
5 or 1
Bits for Mode1
TxM0 Timer Select
4 or 0
Bits for Mode0
• GATE (Gate Control Bit): When set to 1, the
timer/counter is enabled only while the INT
pin is high and TR control bit is set. When
cleared to 0, the timer is enabled whenever
the TR control bit is set.
• C/T (Counter/Timer Select Bit): When set to
1, the timer operates as a counter (counts
external events). When cleared to 0, it
operates as a timer (measuring time).
• M1 and M0 (Mode Select Bits):
M0 M1 Mode Description
13-bit Timer mode (8 Bit of
0 0 Mode 0
THx and 5 Bit of TLx).
0 1 Mode 1 16-bit Timer mode.
8-bit Auto-reload mode (TLx
reload with the THx
1 0 Mode 2
value each time when TLx
overflows).
Split Timer mode - Split 16
1 1 Mode 3 bit timer into two 8 bit
timers(THx and TLx).
What is Timer Mode ?
• The Modes which are used to create time
delays , measure time intervals, or generate
precise time-based signals are known as
Timer modes. The modes that acts as timers
are given below,
Timer Modes
Mode 0: 13-bit Timer Mode
• Description: In Mode 0, 0 or 1 timer functions
as a 13-bit timer. It counts from 0 to 8191
counts, or from 0000H to 1FFFH. It makes use
of all eight bits of TH0 or TH1, and five bits of
TL0 or TL1.
• Working Principle: TLx counts in steps of 0 to
31, resets to 0, and then increases THx. Out of
the two timer bytes, only 13 bits are used.
• Example: It is used for Generating Short Delays
The Block Diagram for Mode 1 is shown
below.
Counter Modes
Mode 2: 8-bit Auto-Reload Mode
• Description: The configuration of Timer 0 in
Mode 2 is an 8-bit auto-reload timer. It
counts from 00H to FFH (0 to 255) and then
loads the starting value automatically.
• Working Principle: TLx starts at 0 and
increases to 255, but instead of returning to 0,
it reloads with the value from THx.
The Block Diagram for Mode 2 is shown
below
Mode 3: Split Timer Mode
• Description: Timer 0 is divided into two 8-bit timers in
Mode 3, which enables Timer 0 to function as two
independent timers while Timer 1 continues to function
as a 16-bit timer as usual.
• Working Principle: Both timers separately count from
0 to 255, then overflow back to 0. The control bits of
Timer 1 are connected to TH0.
• Example: Used in Real-time Clock with Alarm
Functionality.
The Block Diagram for Mode 3 is shown
below.
• In order to create time delays, measure time
intervals, and count events in embedded
systems, 8051 timers and counters are
necessary components.