0% found this document useful (0 votes)
24 views21 pages

Timers in 8051 Microcontroller

The 8051 microcontroller features two 16-bit timer/counter registers (Timer 0 and Timer 1) that can operate in timer or counter modes, allowing for precise timing and event counting. The TMOD and TCON registers control the operation and management of these timers, supporting various modes such as 13-bit, 16-bit, and auto-reload. Additionally, the 8051 includes a UART for serial communication, utilizing specific baud rates and data framing for effective data transmission and reception.

Uploaded by

shreya.d2829
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views21 pages

Timers in 8051 Microcontroller

The 8051 microcontroller features two 16-bit timer/counter registers (Timer 0 and Timer 1) that can operate in timer or counter modes, allowing for precise timing and event counting. The TMOD and TCON registers control the operation and management of these timers, supporting various modes such as 13-bit, 16-bit, and auto-reload. Additionally, the 8051 includes a UART for serial communication, utilizing specific baud rates and data framing for effective data transmission and reception.

Uploaded by

shreya.d2829
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Timers in 8051 Microcontroller

The 8051 microcontroller is equipped with two 16-bit timer/counter registers, namely
Timer 0 and Timer 1. These registers can be configured independently to operate in either
timer mode or counter mode.

Timer Mode:

 When used in timer mode, these registers count the internal clock pulses generated
by the microcontroller’s clock.

 The internal clock is derived from a constant clock generator.

 Each machine cycle in the 8051 consists of 12 oscillator periods. As a result, the
counting rate for the timer is 1/12th of the oscillator frequency.

o For example, if the oscillator frequency is 12 MHz, the timer increments every
1 microsecond (since 12 MHz ÷ 12 = 1 MHz).

 The value loaded into the timer register allows for precise time intervals to be
measured.

Counter Mode:

 In counter mode, the 8051 microcontroller is programmed to count external pulses


rather than internal clock pulses.

 The counting occurs in response to a high-to-low transition on the external input


pins T0 (for Timer 0) and T1 (for Timer 1).

 Since the frequency of these external pulses may not be constant, counter mode is
typically used for counting events, rather than for precise timing operations.

Registers Overview:

 The two timers, Timer 0 and Timer 1, are both 16-bit registers.

o Each 16-bit register is split into two 8-bit registers:

 Timer Low Byte (TL): TL0 for Timer 0 and TL1 for Timer 1.

 Timer High Byte (TH): TH0 for Timer 0 and TH1 for Timer 1.

o The combination of TL0 and TH0 forms the complete 16-bit register for
Timer 0, while TL1 and TH1 form the 16-bit register for Timer 1.

In both modes, the timers can be controlled using different modes such as mode 0 (13-bit
timer), mode 1 (16-bit timer), mode 2 (8-bit auto-reload), and mode 3 (split timer mode)
depending on the desired functionality.
TMOD (Timer Mode Control) Register:

The TMOD register in the 8051 microcontroller is an 8-bit register used to control the mode
of operation for Timer 0 and Timer 1. The register is divided into two 4-bit sections: one for
Timer 0 and the other for Timer 1. Each section consists of the following control bits:

mathematica

Copy code

| Gate | C/T̅ | M1 | M0 | Gate | C/T̅ | M1 | M0 |

|--------------------------|--------------------------|

| Timer 1 Control | Timer 0 Control |

TMOD Control Bits:

1. GATE (Gating Control):

o When set to 1: The timer/counter operates only when the external interrupt
pin (INTx) is high, and the timer run control bit (TR) is set.

o When cleared to 0: The timer/counter operates based solely on the state of the
TR control bit, ignoring the external interrupt pin.

o Use: It is typically used to measure the pulse width of a signal on the external
INTx pin.

2. C/T̅ (Timer/Counter Selector):

o When set to 1: The timer operates as a counter and counts external events.

o When cleared to 0: The timer operates as a timer and counts internal clock
cycles.

o Use: This bit determines whether the timer is used for counting clock cycles
(timing) or external events (counting).

3. M1 and M0 (Mode Select Bits): These two bits select the operating mode of the
timers.

o Mode 0 (M1=0, M0=0):

 13-bit timer/counter mode.

 8 bits are stored in the timer register (TL), and 5 bits are stored in the
high register (TH).

o Mode 1 (M1=0, M0=1):


 16-bit timer/counter mode.

 Both TH and TL are cascaded to form a 16-bit register.

o Mode 2 (M1=1, M0=0):

 8-bit auto-reload mode.

 The TH register holds a value that is automatically reloaded into the


TL register when the timer overflows.

o Mode 3 (M1=1, M0=1):

 Split timer mode.

 Timer 0 is split into two 8-bit timers, with TL0 controlled by Timer 0
control bits and TH0 controlled by Timer 1 control bits.

 Timer 1 is disabled in this mode.

TCON (Timer Control) Register:

The TCON register controls the start/stop of the timers and handles interrupt flags. It is an 8-
bit register, and its structure is as follows:

Copy code

| TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | IE0 | IT0 |

TCON Bits:

1. TF1 (Timer 1 Overflow Flag):

o Set by hardware when Timer 1 overflows.

o Cleared automatically when the microcontroller jumps to the interrupt service


routine.

2. TR1 (Timer 1 Run Control Bit):

o Set or cleared by software to start or stop Timer 1.

3. TF0 (Timer 0 Overflow Flag):

o Set by hardware when Timer 0 overflows.

o Cleared automatically when the interrupt is processed.

4. TR0 (Timer 0 Run Control Bit):


o Set or cleared by software to start or stop Timer 0.

5. IE1 (Interrupt 1 Edge Flag):

o Set by hardware when an external interrupt edge is detected on INT1.

o Cleared automatically when the interrupt is processed.

6. IT1 (Interrupt 1 Type Control Bit):

o Set/cleared by software to select edge or level-triggered interrupts on INT1.

o 1: Edge-triggered.

o 0: Level-triggered.

7. IE0 (Interrupt 0 Edge Flag):

o Set by hardware when an external interrupt edge is detected on INT0.

o Cleared when the interrupt is processed.

8. IT0 (Interrupt 0 Type Control Bit):

o Set/cleared by software to select edge or level-triggered interrupts on INT0.

o 1: Edge-triggered.

o 0: Level-triggered.

Timer Modes of Operation in 8051 Microcontroller

The 8051 microcontroller has four different timer modes that are controlled by the mode
control bits M1 and M0 in the Timer Mode Control (TMOD) register. The timers can operate
in four different modes:

Mode 0:

 In mode 0, the timer/counter is a 13-bit counter. When this mode is selected, the THx
register is used as the high byte, and the TLx register is used as the lower byte for the
timer.

 The timer is enabled when the control bits TR = 1, Gate = 0, and INTx = 1.

Mode 1:

 Key difference from Mode 0: Mode 1 functions similarly to mode 0, except the
timer/counter is 16-bit rather than 13-bit.
 In mode 1, the timer is again controlled by the TR and Gate bits. The timer is enabled
when TR = 1 and Gate = 0. If Gate = 1, external input INTx is used to control the
timer for pulse-width measurement.

 Program flow for Mode 1:

1. Load the TMOD register to select the timer (Timer 0 or Timer 1) and mode
(Mode 1).

2. Load the initial count value into registers TLx and THx.

3. Start the timer.

4. Monitor the Timer Flag (TF) using the JNB TFx instruction to determine when
it overflows.

5. Stop the timer and clear the TF flag.

6. Reload the TL and TH registers for the next count.

Example program for Mode 1:

assembly

Copy code

MOV TMOD, #01H ; Timer 0, Mode 1 (16-bit mode)

MOV TL0, #0F2H ; Load lower byte of the initial count

MOV TH0, #0FFH ; Load higher byte of the initial count

CPL P1.6 ; Toggle bit P1.6 to generate a square wave

ACALL DELAY ; Call delay subroutine

SJMP HERE ; Jump to the start of the loop

DELAY:

SETB TR0 ; Start Timer 0

AGAIN:

JNB TF0, AGAIN ; Wait until Timer 0 overflows

CLR TR0 ; Stop Timer 0

CLR TF0 ; Clear Timer 0 flag

RET ; Return from subroutine

Mode 2:
 Mode 2 is an 8-bit auto-reload mode. When the timer overflows, the content of the
THx register is automatically loaded into TLx, and the timer continues to run.

 Program flow for Mode 2:

1. Load the TMOD register with the appropriate timer mode and selection.

2. Load THx with the reload value.

3. Start the timer.

4. Monitor the Timer Flag (TF) to detect overflow.

5. Upon overflow, the timer automatically reloads the THx content to TLx.

6. Clear the TF flag for the next round.

Example program for Mode 2:

assembly

Copy code

MOV TMOD, #20H ; Timer 1, Mode 2 (auto-reload)

MOV TH1, #05H ; Load reload value

SETB TR1 ; Start Timer 1

BACK:

JNB TF1, BACK ; Wait until Timer 1 overflows

CPL P1.2 ; Toggle bit P1.2 to generate square wave

CLR TF1 ; Clear Timer 1 flag

SJMP BACK ; Repeat loop

Mode 3:

 Mode 3 is known as the split timer mode. In this mode, Timer 0 is divided into two
8-bit counters: TL0 and TH0, operating independently. Timer 1 is halted in this mode.

 Example use of Mode 3:

o In this mode, two separate timers can be used, with one controlling machine
cycles and the other controlling external inputs.

Example program for Mode 3:

assembly
Copy code

MOV TMOD, #03H ; Set Timer 0 in Mode 3 (split timer mode)

These timer modes allow flexibility in configuring delays, generating pulses, and creating
waveforms in embedded systems using the 8051 microcontroller

Introduction to Serial Communication in the 8051 Microcontroller


The 8051 microcontroller is equipped with an on-chip UART (Universal Asynchronous
Receiver/Transmitter), which allows serial data transmission and reception. This feature is
crucial for communication with other devices such as computers, sensors, and other
microcontrollers. The serial communication facility of the 8051 operates through two
dedicated pins:

 RXD (P3.0) for serial data reception.

 TXD (P3.1) for serial data transmission.

Data Transmission/Reception Modes

There are two primary modes of data transmission and reception:

1. Parallel Transmission/Reception:

o In parallel communication, multiple bits are transmitted or received


simultaneously.

o This method is faster but requires more data lines (wires), making it suitable
for short-range communication.

o In the 8051, ports P0, P1, P2, and P3 can be used to transmit/receive 8-bit
parallel data.

2. Serial Transmission/Reception:

o Serial communication sends one bit at a time, using fewer data lines (often just
one or two).

o It is slower than parallel communication but is more efficient for long-distance


communication.

o The TXD (P3.1) and RXD (P3.0) pins are used for transmitting and receiving
serial data, respectively.

Serial Communication Methods

Serial communication in the 8051 uses two main methods for data transmission and
reception:
1. Asynchronous Transmission:

o This method is used for character-oriented transmission. Each byte of data is


framed between start and stop bits to ensure the correct timing and
synchronization between the transmitter and receiver.

2. Synchronous Transmission:

o This method is block-oriented, allowing for the transmission of entire blocks


of data at once.

Asynchronous Serial Communication and Data Framing

In asynchronous serial communication, data framing is used to send individual characters


with start and stop bits:

 Start Bit: Always set to 0 (low) and signals the start of data transmission.

 Stop Bit(s): One or two bits set to 1 (high), indicating the end of data transmission.

 Example: For an 8-bit ASCII character like "A" (binary 0100 0001), the character is
framed with a start bit (0) and a stop bit (1). The least significant bit (LSB) is sent
first.

Example of Serial Data Framing:

 Start Bit: 0

 ASCII character "A": 0100 0001

 Stop Bit: 1

Thus, the complete data frame for the letter "A" would be:
0 0100 0001 1

The 8051's UART simplifies this process by handling the serial data framing, transmission,
and reception automatically, allowing for efficient communication with external devices.

Baud Rate in 8051 Microcontroller Serial Communication

In serial communication, the baud rate is the rate at which bits are transmitted per second,
measured in bits per second (bps). It is a critical factor in determining how fast data can be
transmitted or received. Some standard baud rates include 110, 150, 300, 600, 1200, 2400,
4800, and 9600 bps. The 8051 microcontroller can transmit and receive serial data at various
baud rates using software instructions.

Setting the Baud Rate Using Timer 1 in Mode 2

To achieve the desired baud rate in serial communication, Timer 1 is used in mode 2 (8-bit
auto-reload). The baud rate is controlled by setting a specific value in TH1 (Timer 1 high
byte) to adjust the transmission speed. An external crystal oscillator with a frequency of
11.0592 MHz is used to generate the clock signal for baud rate calculation.

Steps for calculating baud rate in the 8051:

1. The crystal frequency is 11.0592 MHz.

2. The machine cycle frequency is derived by dividing the clock frequency by 12:
Machine Cycle Frequency=11.0592 MHz12=921.6 kHz\text{Machine Cycle
Frequency} = \frac{11.0592 \text{ MHz}}{12} = 921.6 \
text{ kHz}Machine Cycle Frequency=1211.0592 MHz=921.6 kHz

3. The UART circuit further divides this frequency by 32 to get the baud frequency:
Baud Frequency=921.6 kHz32=28,800 Hz\text{Baud Frequency} = \frac{921.6 \text{
kHz}}{32} = 28,800 \text{ Hz}Baud Frequency=32921.6 kHz=28,800 Hz

4. The value loaded into TH1 determines the baud rate. The table below shows the TH1
values for different baud rates:

Baud Rate TH1 (Decimal) TH1 (Hex) Baud Frequency

9600 -3 FDH 28,800 / 3

4800 -6 FAH 28,800 / 6

2400 -12 F4H 28,800 / 12

1200 -24 E8H 28,800 / 24

SBUF Register (Serial Buffer)

The SBUF register is an 8-bit Special Function Register (SFR) used for serial data
transmission and reception:

 During transmission, the data to be sent must be loaded into SBUF.

 During reception, the received data is stored in SBUF and can be retrieved from
there.

For example:

 To send the character 'A':


SBUF = 'A';

 To read received data:


mybyte = SBUF;

SCON Register (Serial Control Register)


The SCON register is another important 8-bit SFR used for configuring serial
communication modes in the 8051. The bits of the SCON register and their roles are
explained below:

Bit Name Description

D7 SM0 Serial Mode Select Bit 0

D6 SM1 Serial Mode Select Bit 1

D5 SM2 Multiprocessor Communication Enable

D4 REN Receive Enable (Set to '1' to enable reception)

D3 TB8 9th Transmitted Bit in Mode 2 and 3

D2 RB8 9th Received Bit in Mode 2 and 3

D1 TI Transmit Interrupt (Set when transmission is complete)

D0 RI Receive Interrupt (Set when reception is complete)

Serial Modes in the 8051

The serial modes are determined by the SM0 and SM1 bits of the SCON register:

SM0 SM1 Mode Description

0 0 Mode 0 Shift Register Mode

0 1 Mode 1 8-bit UART (1 start and 1 stop bit)

1 0 Mode 2 9-bit UART (Fixed Baud Rate)

1 1 Mode 3 9-bit UART (Variable Baud Rate)

Mode 1 is the most commonly used mode, allowing standard 8-bit UART communication.

SM2 (Multiprocessing Communication Bit)

 The SM2 bit enables multiprocessing communication in Mode 2 and Mode 3. This
feature is useful when communicating with multiple devices.

REN (Receive Enable)

 REN is critical for enabling data reception. Setting REN to 1 allows data to be
received via the RXD pin.

TB8 and RB8

 TB8 and RB8 are used for the 9th bit of transmission and reception in Modes 2 and 3.
TI and RI (Transmit and Receive Interrupts)

 TI is set when data transmission is complete, indicating readiness for the next byte.

 RI is set when data reception is complete, indicating that the received data is available
in SBUF.

In Modes 2 and 3, a total of 11 bits (1 start bit, 9 data bits, and 1 stop bit) are transmitted or
received

Modes in Serial Communication (8051 Microcontroller)

The 8051 microcontroller supports four modes of serial communication, determined by the
SM0 and SM1 bits in the SCON register. The modes are as follows:

1. Mode 0: Shift Register Mode

o Type: Half-duplex (data sent and received using the same pin).

o RXD pin is used for both data transmission and reception.

o TXD pin is used as a clock output.

o Data width: 8 bits are transmitted/received.

o Baud rate: Fixed at F/12 (where F is the crystal oscillator frequency).

o Typically used for short-range or simple data transfers.

2. Mode 1: 8-bit Standard UART Mode

o Type: Full-duplex (separate pins for transmission and reception).

o RXD pin is used for data reception, and TXD pin is used for data
transmission.

o Data width: 10 bits (1 start bit, 8 data bits, and 1 stop bit).

o Baud rate: Variable, set using Timer 1 in mode 2 (8-bit auto-reload).

o This is the most widely used mode for UART communication.

3. Mode 2: 9-bit UART Mode (Fixed Baud Rate)

o Type: Full-duplex.

o Data width: 11 bits (1 start bit, TB8/RB8 for transmission/reception, 8 data


bits, and 1 stop bit).

o Baud rate: Fixed at F/32 or F/64 (where F is the crystal frequency).


o This mode is useful when 9-bit communication is required with fixed baud
rates.

4. Mode 3: 9-bit UART Mode (Variable Baud Rate)

o Type: Full-duplex.

o Data width: 11 bits (1 start bit, TB8/RB8 for transmission/reception, 8 data


bits, and 1 stop bit).

o Baud rate: Variable, set using Timer 1 in mode 2 (8-bit auto-reload).

o Similar to Mode 2, but allows for variable baud rates.

RS232 Standards

The RS232 standard is an interfacing standard developed by the Electronics Industries


Association (EIA) for compatibility between serial communication equipment. It is widely
used for communication between computers and peripherals. However, RS232 uses voltage
levels that are not directly compatible with the TTL logic levels used by the 8051
microcontroller.

 RS232 voltage levels:

o Logic '1': Between -3 V and -25 V.

o Logic '0': Between +3 V and +25 V.

o Undefined range: -3 V to +3 V.

Since these voltage levels are incompatible with the TTL (Transistor-Transistor Logic)
levels of the 8051, a voltage level converter is required for communication between devices
using RS232 and the 8051 microcontroller.

MAX232

The MAX232 is a popular IC used as a voltage level converter between RS232 and TTL
levels. It is commonly referred to as a line driver.

 Function: Converts RS232 voltage levels to TTL logic levels and vice versa.

 Pin configuration: It has two sets of drivers for transmitting (T1, T2) and receiving
(R1, R2) signals.

 Capacitors: The MAX232 requires four external capacitors (C1, C2, C3, C4) with
values ranging from 1 µF to 22 µF for internal voltage pumping.

 Power supply: It operates on a +5 V power supply.

Interfacing MAX232 with 8051 Microcontroller


 The TXD pin of the 8051 (used for data transmission) is connected to the T1IN pin of
the MAX232.

 The RXD pin of the 8051 (used for data reception) is connected to the R1OUT pin of
the MAX232.

 The T1OUT and R1IN pins of MAX232 are connected to the DB9 connector pins 2
and 3, allowing the 8051 to communicate with a computer's serial port via RS232.

This setup ensures that the TTL levels of the 8051 are properly converted to RS232 levels for
compatibility with devices using the RS232 standard.

Doubling the Baud Rate Using the 8051 Microcontroller

There are two primary methods for increasing the baud rate in the 8051 microcontroller
during serial communication:

1. Increasing the Crystal Frequency:

o Increasing the crystal oscillator frequency can increase the baud rate.
However, this is not a practical solution for achieving standard baud rates (like
9600 bps) because using higher frequencies may not match PC baud rate
standards.

o For example, if the crystal frequency is 12 MHz, the machine cycle frequency
becomes 1 MHz (12 MHz/12 = 1 MHz), and dividing 1 MHz by 32 gives
31,250 Hz. This would not result in a precise baud rate.

2. Using the PCON Register:

o The PCON (Power Control) register provides a more efficient method of


doubling the baud rate.

PCON Register Overview

The PCON (Power Control) register is an 8-bit special function register in the 8051
microcontroller. It is byte-addressable and is used for:

 Doubling the baud rate in serial communication.

 Power control operations in CHMOS microcontrollers.

SMOD Bit in the PCON Register

 SMOD (Serial Mode) is the D7 bit in the PCON register.

o By default, SMOD = 0, and the baud rate remains the same as set by Timer 1
in mode 2.

o If SMOD = 1 (set via software), the baud rate doubles.


When SMOD = 1, the internal clock used for serial communication divides the machine
cycle frequency by 16 instead of the usual 32, effectively doubling the baud rate.

Baud Rate Calculation

Let's see how the baud rate changes based on the value of SMOD:

 Crystal frequency: 11.0592 MHz.

 Machine cycle frequency: 11.0592 MHz / 12 = 921.6 kHz.

SMOD Serial Communication Frequency Example with TH1 = -6 Baud Rate

0 921.6 kHz / 32 = 28,800 Hz 28,800 Hz / 6 4800 bps

1 921.6 kHz / 16 = 57,600 Hz 57,600 Hz / 6 9600 bps

 When SMOD = 1, the baud rate doubles from 4800 to 9600 bps for the same value of
TH1.

Example Assembly Code for Doubling Baud Rate

The following program sets SMOD to 1, doubling the baud rate without changing the crystal
frequency:

assembly

Copy code

MOV A, PCON ; Copy PCON register into accumulator

SETB ACC.7 ; Set D7 bit of the accumulator to 1 (SMOD = 1)

MOV PCON, A ; Store the updated value back to PCON

Other Bits in PCON Register

 GF0 and GF1: General-purpose flag bits, typically used for user-defined purposes.

 PD: Power down mode bit (used in CHMOS processors).

 IDL: Idle mode bit (used in CHMOS processors).

Interrupts in 8051 Microcontroller

Introduction to Interrupts

In many real-time processes, the microcontroller may need to handle multiple tasks or events
simultaneously. Interrupts provide a way to temporarily halt the execution of the main
program, handle an event or service a device, and then return to the main program where it
left off. For example, in a scenario where the microcontroller needs to:
1. Receive data serially from an external device and send it to P0.

2. Read from port P1 and transmit it serially to the external device while also sending a
copy to P2.

3. Generate a square wave on pin P3.1 with a frequency of 5 kHz.

Using interrupts, the microcontroller can efficiently manage all these tasks by responding to
events (such as serial data reception) while performing other operations.

Interrupts vs. Polling

1. Interrupts:

o Devices signal the microcontroller when they need service by generating an


interrupt.

o Upon receiving the interrupt, the microcontroller stops its current task and
executes the Interrupt Service Routine (ISR).

o ISRs are special routines dedicated to handling specific interrupt events.

o Priorities can be assigned to interrupts, and the microcontroller can mask


(ignore) specific interrupts if necessary.

2. Polling:

o The microcontroller continuously checks (polls) devices in a round-robin


manner to determine if they need service.

o Polling wastes time as it checks devices even if they do not need attention.

o In polling, no priority can be assigned to devices, and all are checked


sequentially.

Interrupt Service Routine (ISR)

 For every interrupt, there is an associated ISR.

 When an interrupt occurs, the microcontroller jumps to a fixed memory location


where the address of the ISR is stored. This set of memory locations is called the
Interrupt Vector Table.

 The ISR is executed, and after finishing, the microcontroller returns to the main
program at the point where it was interrupted.

Steps in Executing an Interrupt

1. Complete the Current Instruction: The microcontroller finishes executing the


current instruction.
2. Save Program Counter (PC): The address of the next instruction to be executed is
saved on the stack.

3. Save Interrupt Status: The current status of interrupts is saved internally (not on the
stack).

4. Jump to Interrupt Vector Table: The microcontroller jumps to a predefined location


in memory (interrupt vector table) where the ISR’s address is stored.

5. Execute the ISR: The ISR is executed until it reaches the RETI instruction (Return
from Interrupt).

6. Return to Main Program: Upon executing RETI, the microcontroller retrieves the
saved PC value from the stack and resumes executing the main program from where it
was interrupted.

Interrupt Vector Table in 8051

In the 8051 microcontroller, there is a fixed memory location for each interrupt, known as the
interrupt vector table. These addresses hold the starting points for their respective ISRs.

Here’s a summary of interrupt vector locations in the 8051:

Interrupt Source Vector Address Priority

External Interrupt 0 (INT0) 0003H 1

Timer 0 Interrupt 000BH 2

External Interrupt 1 (INT1) 0013H 3

Timer 1 Interrupt 001BH 4

Serial Communication Interrupt (RI/TI) 0023H 5

Each of these vector addresses contains the starting point for the corresponding ISR.

Types of Interrupts in the 8051 Microcontroller

The 8051 microcontroller recognizes five main interrupt sources:

1. Timer 0 Overflow Interrupt (TF0)

2. Timer 1 Overflow Interrupt (TF1)

3. External Hardware Interrupt 0 (INT0)

4. External Hardware Interrupt 1 (INT1)

5. Serial Communication Interrupt (RI/TI)


Interrupt Vector Table

 Upon reset, all interrupts are disabled.

 Interrupts must be enabled via software, typically using the IE (Interrupt Enable)
register.

IE (Interrupt Enable) Register

 EA (IE.7): Global enable for all interrupts.

 ET2 (IE.5): Enable/disable Timer 2 overflow/capture interrupt (only in 8952).

 ES (IE.4): Enable/disable serial port interrupt.

 ET1 (IE.3): Enable/disable Timer 1 overflow interrupt.

 EX1 (IE.2): Enable/disable External Interrupt 1.

 ET0 (IE.1): Enable/disable Timer 0 overflow interrupt.

 EX0 (IE.0): Enable/disable External Interrupt 0.

Enabling and Disabling Interrupts

Example 1: Enabling and Disabling Interrupts

 Enable Serial, Timer 0, and External Interrupt 1:

assembly

Copy code

MOV IE, #10010110B ; enable serial, Timer 0, EX1

; Alternatively

SETB IE.7 ; EA = 1 (global enable)

SETB IE.4 ; Enable Serial Interrupt

SETB IE.1 ; Enable Timer 0 Interrupt

SETB IE.2 ; Enable EX1

 Disable Timer 0 Interrupt:

assembly

Copy code

CLR IE.1 ; mask (disable) Timer 0 interrupt only


 Disable All Interrupts:

assembly

Copy code

CLR IE.7 ; disable all interrupts

Timer Interrupts

 Timer interrupts occur when a timer overflows, raising the TF bit and prompting the
microcontroller to execute the ISR (Interrupt Service Routine).

Example 2: Generating a Square Wave using Timer 0

assembly

Copy code

ORG 0000H

LJMP MAIN

ORG 000BH ; Timer 0 ISR

CPL P2.1 ; Toggle P2.1 pin

RETI ; Return from ISR

ORG 0030H ; Main program

MAIN:

MOV TMOD, #02H ; Timer 0 in Mode 2

MOV P0, #0FFH ; P0 as input port

MOV TH0, #-92 ; Load TH0 for 200 μs

MOV IE, #82H ; Enable Timer 0 interrupt

SETB TR0 ; Start Timer 0

BACK:

MOV A, P0 ; Get data from P0

MOV P1, A ; Send it to P1


SJMP BACK ; Loop indefinitely

END

External Hardware Interrupts

 The 8051 supports two external hardware interrupts (INT0 and INT1) on pins P3.2
and P3.3.

 They can be configured to trigger on level or edge.

Level-Triggered Interrupt

 A low-level signal on the INT pin activates the interrupt.

 The low-level signal must be removed before the last instruction of the ISR (RETI) is
executed to prevent a new interrupt.

Edge-Triggered Interrupt

 Configure INT0 and INT1 as edge-triggered using the TCON register.

 The TCON register includes flags that indicate whether an interrupt is being serviced.

Example: Using INT1 to Control an LED

assembly

Copy code

ORG 0000H

LJMP MAIN

ORG 0013H ; INT1 ISR

SETB P1.3 ; Turn on LED

MOV R3, #255

BACK: DJNZ R3, BACK ; Keep LED on for a while

CLR P1.3 ; Turn off LED

RETI ; Return from ISR

ORG 0030H

MAIN:

MOV IE, #10000100B ; Enable External INT 1


HERE: SJMP HERE ; Stay here until interrupted

END

Serial Communication Interrupts

 The TI (Transmit Interrupt) is raised after the last bit of data is sent.

 The RI (Receive Interrupt) is raised when a full byte is received.

Example: Serial Communication ISR

assembly

Copy code

ORG 0000H

LJMP MAIN

ORG 0023H ; Serial ISR

LJMP SERIAL ; Jump to Serial Interrupt Service Routine

ORG 0030H

MAIN:

MOV P1, #0FFH ; P1 as input port

MOV TMOD, #20H ; Timer 1, auto reload

MOV TH1, #0FDH ; Set baud rate to 9600

MOV SCON, #50H ; 8-bit, 1 stop, receive enabled

MOV IE, #10010000B ; Enable Serial Interrupt

SETB TR1 ; Start Timer 1

BACK:

MOV A, P1 ; Read data from P1

MOV SBUF, A ; Send data to Serial Buffer

MOV P2, A ; Output to P2

SJMP BACK ; Loop indefinitely


; Serial Port ISR

ORG 100H

SERIAL:

JB TI, TRANS ; If TI is high, jump to TRANS

MOV A, SBUF ; Otherwise, read received data

MOV P0, A ; Output to P0

CLR RI ; Clear RI

RETI ; Return from ISR

TRANS:

CLR TI ; Clear TI

RETI ; Return from ISR

END

Interrupt Priority

 The default priority order is fixed but can be changed using the IP (Interrupt
Priority) register.

Example: Setting Interrupt Priority

assembly

Copy code

MOV IP, #00000100B ; Set INT1 to highest priority

You might also like