0% found this document useful (0 votes)
16 views63 pages

MC - Mod 4

Module 4 covers Timer/Counter, Serial Communication, and Interrupts in the 8051 microcontroller. It details the functionalities of timers, including their modes, registers, and programming, as well as the principles of serial communication and the associated registers and modes. The document also discusses baud rate generation and the RS-232 standard for serial communication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views63 pages

MC - Mod 4

Module 4 covers Timer/Counter, Serial Communication, and Interrupts in the 8051 microcontroller. It details the functionalities of timers, including their modes, registers, and programming, as well as the principles of serial communication and the associated registers and modes. The document also discusses baud rate generation and the RS-232 standard for serial communication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 4: Timer/Counter, Serial Communication & Interrupts in 8051

1. TIMER/COUNTER IN 8051
1.1 Introduction to Timers
A timer is a device that measures time intervals, while a counter counts external events. The
8051 microcontroller has two 16-bit programmable timer/counters: Timer 0 and Timer 1.
Key Features:
 Both can operate as timers or counters
 16-bit wide (can count from 0000H to FFFFH)
 Can be programmed in different modes
 Can generate interrupts on overflow

1.2 Timer Registers


The 8051 timers use several Special Function Registers (SFRs):
1.2.1 TMOD (Timer Mode Register)
 Address: 89H
 Not bit-addressable
 Controls the mode and operation of both timers
TMOD Register Format:

GATE C/T̅ M1 M0 GATE C/T̅ M1 M0

Timer 1 Timer 0

Bit Description:
 GATE (Gate control bit):
o 0 = Timer runs when TRx bit is set (x = 0 or 1)
o 1 = Timer runs only when TRx bit is set AND INTx pin is high
 C/T̅ (Counter/Timer select bit):
o 0 = Timer mode (internal clock, machine cycle)
o 1 = Counter mode (external pulses from T0 or T1 pin)
 M1, M0 (Mode select bits):
o 00 = Mode 0 (13-bit timer/counter)

NEETHU SUSAN ALEX, AP, ECE, UKFCET


o 01 = Mode 1 (16-bit timer/counter)
o 10 = Mode 2 (8-bit auto-reload)
o 11 = Mode 3 (Split timer mode - Timer 0 only)

1.2.2 TCON (Timer Control Register)


 Address: 88H
 Bit-addressable
TCON Register Format:

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Timer-related bits:
 TF1 (Timer 1 overflow flag):
o Set by hardware when Timer 1 overflows
o Must be cleared by software
 TR1 (Timer 1 run control):
o 0 = Stop Timer 1
o 1 = Start Timer 1
 TF0 (Timer 0 overflow flag):
o Set by hardware when Timer 0 overflows
o Must be cleared by software
 TR0 (Timer 0 run control):
o 0 = Stop Timer 0
o 1 = Start Timer 0

1.2.3 Timer Registers (TH and TL)


Each timer has two 8-bit registers:
 Timer 0: TH0 (8CH) and TL0 (8AH)
 Timer 1: TH1 (8DH) and TL1 (8BH)
These registers hold the count value. In 16-bit mode, they combine to form a 16-bit counter.

1.3 Timer Modes


NEETHU SUSAN ALEX, AP, ECE, UKFCET
Mode 0: 13-bit Timer/Counter
 Uses 13 bits: 5 bits of TL and 8 bits of TH
 Lower 3 bits of TL are ignored
 Maximum count: 8192 (2^13)
 Overflow occurs from 1FFFH to 0000H
Configuration:
M1 M0 = 0 0

Mode 1: 16-bit Timer/Counter


 Most commonly used mode
 Uses all 16 bits (TH and TL combined)
 Maximum count: 65536 (2^16)
 Overflow occurs from FFFFH to 0000H
Configuration:
M1 M0 = 0 1
Example Calculation: For a time delay using 11.0592 MHz crystal:
 Machine cycle = 12/11.0592 MHz = 1.085 μs
 For 50 ms delay: Count = 50ms / 1.085μs = 46083
 Initial value = 65536 - 46083 = 19453 = 4BFDH
 TH0 = 4BH, TL0 = FDH

Mode 2: 8-bit Auto-Reload


 TL is used as 8-bit counter
 TH holds reload value
 When TL overflows, it's automatically reloaded from TH
 Useful for generating baud rates
Configuration:
M1 M0 = 1 0
Advantages of Mode 2:
1. Automatic reloading: TL register is automatically reloaded from TH register on
overflow, eliminating software overhead
NEETHU SUSAN ALEX, AP, ECE, UKFCET
2. Precise timing: Provides consistent and accurate time intervals
3. Ideal for baud rate generation: Used with Timer 1 for serial communication
4. Less CPU intervention: Reduces program complexity for repetitive timing
operations
5. 8-bit operation: Suitable for applications needing shorter time delays

Mode 3: Split Timer Mode


 Only for Timer 0
 TL0 and TH0 operate as two separate 8-bit timers
 TL0 uses Timer 0 control bits (TR0, TF0)
 TH0 uses Timer 1 control bits (TR1, TF1)
 Timer 1 in Mode 3 stops (can only run in modes 0, 1, 2)
Configuration:
M1 M0 = 1 1

1.4 Timer Mode Programming


1.4.1 Steps for Timer Programming
1. Load TMOD register with appropriate mode
2. Load initial count value in TH and TL registers
3. Start timer by setting TR bit in TCON
4. Monitor TF flag for overflow
5. Stop timer by clearing TR bit
6. Clear TF flag for next operation

1.4.2 Assembly Language Example (Mode 1)


Program: Generate 50ms delay using Timer 0
assembly
ORG 0000H
MOV TMOD, #01H ; Timer 0, Mode 1
MOV TL0, #0FDH ; Load lower byte
MOV TH0, #4BH ; Load higher byte
NEETHU SUSAN ALEX, AP, ECE, UKFCET
SETB TR0 ; Start Timer 0
WAIT:
JNB TF0, WAIT ; Wait for overflow
CLR TR0 ; Stop timer
CLR TF0 ; Clear overflow flag

; Delay generated
SJMP $ ; Stay here
END

1.4.3 Embedded C Example (Mode 1)


c
#include <reg51.h>
void delay_50ms() {
TMOD = 0x01; // Timer 0, Mode 1
TL0 = 0xFD; // Load lower byte
TH0 = 0x4B; // Load higher byte
TR0 = 1; // Start timer
while(TF0 == 0); // Wait for overflow
TR0 = 0; // Stop timer
TF0 = 0; // Clear flag
}
void main() {
while(1) {
delay_50ms();
// Your code here
}
}

1.4.4 Mode 2 Example (Auto-Reload)

NEETHU SUSAN ALEX, AP, ECE, UKFCET


Program: Generate continuous pulses
assembly
ORG 0000H
MOV TMOD, #02H ; Timer 0, Mode 2
MOV TH0, #00H ; Reload value
MOV TL0, #00H ; Initial value
SETB TR0 ; Start timer

LOOP:
JNB TF0, LOOP ; Wait for overflow
CLR TF0 ; Clear flag (TL0 auto-reloaded)
CPL P1.0 ; Toggle port pin
SJMP LOOP
END

1.5 Counter Mode


When C/T̅ bit is set to 1, the timer operates as a counter, counting external pulses applied to
pins T0 (P3.4) or T1 (P3.5).
Characteristics:
 Counts 1-to-0 transitions at T0/T1 pin
 Maximum counting rate = 1/24 of oscillator frequency
 External pulse width must be at least 2 machine cycles
Example: Count external events
assembly
ORG 0000H
MOV TMOD, #05H ; Timer 0, Mode 1, Counter
MOV TL0, #00H
MOV TH0, #00H
SETB TR0 ; Start counting
; External pulses on T0 (P3.4)
CHECK:
NEETHU SUSAN ALEX, AP, ECE, UKFCET
JNB TF0, CHECK ; Wait for overflow
CLR TR0
; Process count
END

1.6 Applications of Timers


1. Time delay generation
2. Pulse width measurement
3. Frequency measurement
4. Baud rate generation for serial communication
5. Event counting
6. Real-time clock implementation
7. PWM signal generation

1.7 Difference between Timer mode and Counter mode

Timer Mode Counter Mode

Uses internal clock (machine cycles) Uses external pulses from T0/T1 pins

C/T̅ bit = 0 in TMOD register C/T̅ bit = 1 in TMOD register

Clock frequency = fosc/12 Clock = external source

Used for time delay generation Used for event counting

2. SERIAL COMMUNICATION IN 8051


2.1 Introduction to Serial Communication
Serial communication is the process of transmitting data one bit at a time over a
communication channel. The 8051 has a built-in UART (Universal Asynchronous Receiver
Transmitter) for serial communication.
Advantages of Serial Communication:
 Requires fewer pins
 Suitable for long-distance communication
 Cost-effective
NEETHU SUSAN ALEX, AP, ECE, UKFCET
 Supports multiple protocols
Types of Serial Communication:
1. Synchronous: Data and clock sent together
2. Asynchronous: No clock signal, uses start and stop bits

2.2 Serial Communication in 8051


The 8051 supports full-duplex asynchronous serial communication through pins:
 TxD (P3.1): Transmit Data
 RxD (P3.0): Receive Data
Features:
 Full-duplex operation (simultaneous transmit and receive)
 Programmable baud rate
 Four operational modes
 Buffer registers (SBUF) for transmit and receive

2.3 Serial Communication Registers


2.3.1 SBUF (Serial Buffer Register)
 Address: 99H
 Two separate registers with same address:
o One for transmit (write-only)
o One for receive (read-only)
assembly
MOV SBUF, A ; Load data to transmit
MOV A, SBUF ; Read received data

2.3.2 SCON (Serial Control Register)


 Address: 98H
 Bit-addressable
SCON Register Format:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


SM0 SM1 SM2 REN TB8 RB8 TI RI

Bit Description:
 SM0, SM1 (Serial mode select):
o 00 = Mode 0 (Shift register, baud = fosc/12)
o 01 = Mode 1 (8-bit UART, variable baud rate)
o 10 = Mode 2 (9-bit UART, baud = fosc/64 or fosc/32)
o 11 = Mode 3 (9-bit UART, variable baud rate)
 SM2 (Multiprocessor communication):
o Used in Modes 2 and 3 for multiprocessor communication
 REN (Receive enable):
o 0 = Reception disabled
o 1 = Reception enabled
 TB8 (9th transmit bit):
o In Modes 2 and 3, this is the 9th data bit to transmit
 RB8 (9th receive bit):
o In Modes 2 and 3, this is the 9th data bit received
 TI (Transmit interrupt flag):
o Set by hardware after transmission complete
o Must be cleared by software
 RI (Receive interrupt flag):
o Set by hardware when data received
o Must be cleared by software

2.3.3 PCON (Power Control Register)


 Address: 87H

NEETHU SUSAN ALEX, AP, ECE, UKFCET


 Bit 7 (SMOD): Baud rate doubler bit
o 0 = Normal baud rate
o 1 = Double baud rate

2.4 Serial Communication Modes


Mode 0: Shift Register Mode
 Synchronous mode
 8-bit data transmission
 Baud rate = fosc/12
 Used for serial port expansion

Mode 1: 8-bit UART (Most Common)


 Asynchronous mode
 10-bit transmission: 1 start + 8 data + 1 stop bit
 Variable baud rate (set by Timer 1)
 Full-duplex operation
Frame Format:

Start | D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | Stop

0 | 8 data bits | 1

Mode 2: 9-bit UART


 Asynchronous mode
 11-bit transmission: 1 start + 8 data + 1 programmable + 1 stop
 Fixed baud rate = fosc/64 (or fosc/32 with SMOD=1)

NEETHU SUSAN ALEX, AP, ECE, UKFCET


 9th bit used for parity or address/data identification

Mode 3: 9-bit UART


 Same as Mode 2 but with variable baud rate
 Baud rate set by Timer 1

2.5 Baud Rate Generation


Baud Rate: Number of bits transmitted per second (bps)
For Mode 1 and Mode 3:
Baud rate is generated using Timer 1 in Mode 2 (8-bit auto-reload)
Formula:
Baud Rate = (2^SMOD / 32) × (Oscillator Frequency / (12 × [256 - TH1]))
For SMOD = 0:
Baud Rate = (Oscillator Frequency) / (384 × [256 - TH1])
Common Baud Rates with 11.0592 MHz Crystal:

Baud Rate SMOD TH1 Value Mode

9600 1 FDH 2

4800 1 FAH 2

2400 1 F4H 2

1200 1 E8H 2

Example Calculation for 9600 baud:


9600 = (2^1 / 32) × (11.0592MHz / (12 × [256 - TH1]))
TH1 = 253 = FDH

2.6 RS-232 Standard


RS-232 is a standard for serial communication between DTE (Data Terminal Equipment) and
DCE (Data Communication Equipment).
Voltage Levels:
 Logic 1 (Mark): -3V to -15V
 Logic 0 (Space): +3V to +15V
NEETHU SUSAN ALEX, AP, ECE, UKFCET
8051 Voltage Levels:
 Logic 1: +5V
 Logic 0: 0V
Solution: MAX232 IC is used for voltage level conversion between TTL and RS-232 levels.
MAX232 IC
 Converts TTL/CMOS logic to RS-232 levels
 Contains charge pump for voltage generation
 Requires four external capacitors (typically 1μF)
Pin Configuration:
 T1IN, T2IN: TTL inputs (from 8051 TxD)
 T1OUT, T2OUT: RS-232 outputs (to PC)
 R1IN, R2IN: RS-232 inputs (from PC)
 R1OUT, R2OUT: TTL outputs (to 8051 RxD)

RS-232 Connector (DB9)

Pin Signal Direction

1 DCD Input

2 RxD Input

3 TxD Output

4 DTR Output

5 GND -

6 DSR Input

7 RTS Output

8 CTS Input

9 RI Input

For simple communication, connect:


 Pin 2 (RxD) to TxD of 8051 (via MAX232)
 Pin 3 (TxD) to RxD of 8051 (via MAX232)

NEETHU SUSAN ALEX, AP, ECE, UKFCET


 Pin 5 (GND) to Ground

2.7 Serial Port Programming


2.7.1 Initialization
Steps:
1. Set serial mode in SCON
2. Configure Timer 1 for baud rate generation (Mode 2)
3. Load TH1 for desired baud rate
4. Set SMOD if needed (in PCON)
5. Start Timer 1 (set TR1)
6. Enable reception (set REN)
2.7.2 Transmission Programming
Assembly Language:
assembly
; Initialize serial port for 9600 baud
ORG 0000H
MOV TMOD, #20H ; Timer 1, Mode 2
MOV TH1, #0FDH ; 9600 baud rate
MOV SCON, #50H ; Mode 1, REN enabled
SETB TR1 ; Start Timer 1
; Transmit data
TRANSMIT:
MOV A, #'A' ; Load data
MOV SBUF, A ; Send to SBUF
WAIT_TX:
JNB TI, WAIT_TX ; Wait for transmission complete
CLR TI ; Clear flag
SJMP $
END
Embedded C:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


c
#include <reg51.h>
void serial_init() {
TMOD = 0x20; // Timer 1, Mode 2
TH1 = 0xFD; // 9600 baud
SCON = 0x50; // Mode 1, REN enabled
TR1 = 1; // Start Timer 1
}
void serial_transmit(char data) {
SBUF = data; // Load data
while(TI == 0); // Wait for transmission
TI = 0; // Clear flag
}
void main() {
serial_init();
serial_transmit('A');
while(1);
}

2.7.3 Reception Programming


Assembly Language:
assembly
; Initialize serial port
ORG 0000H
MOV TMOD, #20H ; Timer 1, Mode 2
MOV TH1, #0FDH ; 9600 baud
MOV SCON, #50H ; Mode 1, REN enabled
SETB TR1 ; Start Timer 1
; Receive data
RECEIVE:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


JNB RI, RECEIVE ; Wait for data
MOV A, SBUF ; Read data
CLR RI ; Clear flag
; Process received data
MOV P1, A ; Display on Port 1
SJMP RECEIVE
END

Embedded C:
c
#include <reg51.h>
void serial_init() {
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
}
char serial_receive() {
while(RI == 0); // Wait for data
RI = 0; // Clear flag
return SBUF; // Return received data
}
void main() {
char data;
serial_init();
while(1) {
data = serial_receive();
P1 = data; // Display on Port 1
}
}

NEETHU SUSAN ALEX, AP, ECE, UKFCET


2.7.4 String Transmission
Embedded C:
c
void serial_string(char *str) {
int i = 0;
while(str[i] != '\0') {
serial_transmit(str[i]);
i++;
}
}

void main() {
serial_init();
serial_string("Hello World!");
while(1);
}

2.8 Serial Communication Protocols


2.8.1 Common Protocols
 RS-232: Point-to-point, long distance (up to 15m)
 RS-485: Multi-drop, longer distance (up to 1200m)
 I2C: Two-wire, multiple devices
 SPI: High-speed, short distance
 USB: Universal Serial Bus
 UART: Universal Asynchronous Receiver/Transmitter

3. PROGRAMMING INTERRUPTS IN 8051

NEETHU SUSAN ALEX, AP, ECE, UKFCET


3.1 Introduction to Interrupts
An interrupt is an event that temporarily suspends the main program, executes a special
routine (ISR - Interrupt Service Routine), and then returns to the main program.
Advantages:
 Efficient use of CPU time
 Quick response to external events
 Better program organization
 Real-time processing

Interrupt vs Polling:
 Polling: CPU continuously checks device status (wastes CPU cycles)
 Interrupt: Device signals CPU when ready (efficient)
Difference between polling and interrupt?

Polling Interrupt

CPU continuously checks device status Device signals CPU when ready

Wastes CPU time in checking CPU free to do other tasks

Slower response time Faster response time

Simpler to implement Slightly complex implementation

No special hardware needed Needs interrupt controller

Inefficient for multiple devices Efficient for multiple devices

3.2 Interrupt Sources in 8051


The 8051 has 6 interrupt sources:
1. External Interrupt 0 (INT0) - Pin P3.2
2. Timer 0 Overflow (TF0)
3. External Interrupt 1 (INT1) - Pin P3.3
4. Timer 1 Overflow (TF1)
5. Serial Port (RI/TI)
6. Timer 2 Overflow (TF2) - Only in 8052

NEETHU SUSAN ALEX, AP, ECE, UKFCET


3.3 Interrupt Vector Table
Each interrupt has a fixed location in memory where control jumps when interrupt occurs:

Interrupt Source Vector Address Pin/Flag

Reset 0000H Reset

External INT0 0003H P3.2

Timer 0 000BH TF0

External INT1 0013H P3.3

Timer 1 001BH TF1

Serial Port 0023H RI/TI

Note: Each interrupt vector has 8 bytes of space. If ISR is longer, use a jump instruction to
the actual ISR location.

3.4 Interrupt Control Registers


3.4.1 IE (Interrupt Enable Register)
 Address: A8H
 Bit-addressable
IE Register Format:

EA - ET2 ES ET1 EX1 ET0 EX0

Bit Description:
 EA (Enable All):
o 0 = Disable all interrupts
o 1 = Enable interrupts (individually controlled)
 ET2 (Enable Timer 2): Only in 8052
 ES (Enable Serial):
o 0 = Serial interrupt disabled
o 1 = Serial interrupt enabled
 ET1 (Enable Timer 1):
o 0 = Timer 1 interrupt disabled
o 1 = Timer 1 interrupt enabled
NEETHU SUSAN ALEX, AP, ECE, UKFCET
 EX1 (Enable External 1):
o 0 = INT1 interrupt disabled
o 1 = INT1 interrupt enabled
 ET0 (Enable Timer 0):
o 0 = Timer 0 interrupt disabled
o 1 = Timer 0 interrupt enabled
 EX0 (Enable External 0):
o 0 = INT0 interrupt disabled
o 1 = INT0 interrupt enabled

Example:
assembly
MOV IE, #10000101B ; EA=1, ET0=1, EX0=1
; or
SETB EA
SETB ET0
SETB EX0
3.4.2 IP (Interrupt Priority Register)
 Address: B8H
 Bit-addressable
IP Register Format:

- - PT2 PS PT1 PX1 PT0 PX0

Bit Description:
Each bit sets priority for corresponding interrupt:
 0 = Low priority
 1 = High priority
Priority Levels:
1. High priority interrupts can interrupt low priority ISRs
2. Low priority interrupts cannot interrupt any ISR
3. If same priority, polling sequence determines priority
NEETHU SUSAN ALEX, AP, ECE, UKFCET
Default Polling Sequence (highest to lowest):
1. External INT0
2. Timer 0
3. External INT1
4. Timer 1
5. Serial Port

3.5 External Interrupt Programming


External interrupts can be triggered in two ways:
1. Level-triggered: Low signal on INT pin
2. Edge-triggered: Falling edge (1-to-0 transition)
This is controlled by bits in TCON register:
 IT0 (TCON.0): INT0 type
 IT1 (TCON.2): INT1 type
o 0 = Level-triggered
o 1 = Edge-triggered

Difference between edge-triggered and level-triggered interrupts.

Edge-Triggered Level-Triggered

Triggered by falling edge (1→0) Triggered by low level (0)

IT0/IT1 = 1 IT0/IT1 = 0

Pulse must be present for ≥1 machine cycle Must remain low until ISR starts

Preferred for push button applications Used for level detection

Flag cleared automatically Flag cleared when level goes high

3.5.1 Edge-Triggered External Interrupt


Assembly Language:
assembly
ORG 0000H

NEETHU SUSAN ALEX, AP, ECE, UKFCET


LJMP MAIN ; Jump to main program

ORG 0003H ; INT0 ISR vector


LJMP ISR_INT0 ; Jump to ISR

ORG 0030H
MAIN:
SETB IT0 ; Edge-triggered mode
SETB EX0 ; Enable INT0
SETB EA ; Enable all interrupts

LOOP:
; Main program code
SJMP LOOP

ISR_INT0:
; Interrupt service routine
CPL P1.0 ; Toggle LED
RETI ; Return from interrupt

END

Embedded C:
c
#include <reg51.h>

void external_int0() interrupt 0 {


P1_0 = ~P1_0; // Toggle LED
}

NEETHU SUSAN ALEX, AP, ECE, UKFCET


void main() {
IT0 = 1; // Edge-triggered
EX0 = 1; // Enable INT0
EA = 1; // Enable all

while(1) {
// Main program
}
}

3.6 Timer Interrupt Programming


Timer interrupts occur when timer overflows (TF flag set).
Steps:
1. Configure timer mode (TMOD)
2. Load initial count value
3. Enable timer interrupt (ET0/ET1)
4. Enable global interrupt (EA)
5. Start timer (TR0/TR1)
6. Write ISR at interrupt vector
Example: Timer 0 Interrupt
Assembly Language:
assembly
ORG 0000H
LJMP MAIN

ORG 000BH ; Timer 0 ISR vector


LJMP ISR_TIMER0

ORG 0030H
MAIN:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


MOV TMOD, #01H ; Timer 0, Mode 1
MOV TL0, #0FDH ; Load count
MOV TH0, #4BH
SETB ET0 ; Enable Timer 0 interrupt
SETB EA ; Enable all interrupts
SETB TR0 ; Start timer

LOOP:
SJMP LOOP

ISR_TIMER0:
CLR TR0 ; Stop timer
CPL P1.0 ; Toggle LED
MOV TL0, #0FDH ; Reload count
MOV TH0, #4BH
SETB TR0 ; Restart timer
RETI

END

Embedded C:
c
#include <reg51.h>

void timer0_isr() interrupt 1 {


P1_0 = ~P1_0; // Toggle LED
TH0 = 0x4B; // Reload
TL0 = 0xFD;
}

NEETHU SUSAN ALEX, AP, ECE, UKFCET


void main() {
TMOD = 0x01; // Timer 0, Mode 1
TH0 = 0x4B;
TL0 = 0xFD;
ET0 = 1; // Enable Timer 0
EA = 1; // Enable all
TR0 = 1; // Start timer

while(1);
}

3.7 Serial Port Interrupt Programming


Serial port generates interrupt when:
 Data transmission complete (TI = 1)
 Data reception complete (RI = 1)
Both flags share the same interrupt vector (0023H). ISR must check which flag is set.
Embedded C Example:
c
#include <reg51.h>

char rx_data;

void serial_isr() interrupt 4 {


if(RI == 1) { // Check if receive
RI = 0; // Clear flag
rx_data = SBUF; // Read data
P1 = rx_data; // Display
}

if(TI == 1) { // Check if transmit

NEETHU SUSAN ALEX, AP, ECE, UKFCET


TI = 0; // Clear flag
// Transmission complete
}
}

void main() {
TMOD = 0x20; // Timer 1, Mode 2
TH1 = 0xFD; // 9600 baud
SCON = 0x50; // Mode 1
TR1 = 1; // Start Timer 1
ES = 1; // Enable serial interrupt
EA = 1; // Enable all

while(1);
}

3.8 Interrupt Priority Settings

Example: Setting Priorities


Assembly Language:
assembly
ORG 0000H
LJMP MAIN

ORG 0030H
MAIN:
SETB PX0 ; INT0 high priority
SETB PT0 ; Timer 0 high priority
CLR PX1 ; INT1 low priority
CLR PT1 ; Timer 1 low priority

NEETHU SUSAN ALEX, AP, ECE, UKFCET


SETB EX0 ; Enable interrupts
SETB ET0
SETB EX1
SETB ET1
SETB EA ; Enable all

LOOP:
SJMP LOOP

END
Key Points:
 High priority interrupt can interrupt low priority ISR
 Same priority interrupts follow polling sequence
 Interrupt with higher natural priority is serviced first

3.9 Interrupt Execution Sequence


1. Finish current instruction
2. Save PC on stack (2 bytes)
3. Clear interrupt flag (for edge-triggered and timer interrupts)
4. Jump to interrupt vector
5. Execute ISR
6. Execute RETI instruction
7. Restore PC from stack
8. Resume main program

Important Notes:
 External interrupts (edge-triggered) and timer interrupts are automatically cleared by
hardware
 Serial port flags (RI, TI) must be cleared by software
 Level-triggered external interrupts must be held low until ISR starts
NEETHU SUSAN ALEX, AP, ECE, UKFCET
3.10 Interrupt Vector Numbers in C
When writing ISR in Embedded C, use interrupt vector numbers:

Interrupt Vector Number Keyword

External INT0 0 interrupt 0

Timer 0 1 interrupt 1

External INT1 2 interrupt 2

Timer 1 3 interrupt 3

Serial Port 4 interrupt 4

Syntax:
c
void function_name() interrupt vector_number {
// ISR code
}
3.11 Complete Application Example
Application: Display counter on LCD, increment on button press (INT0), update every
second (Timer 0)
c
#include <reg51.h>
unsigned int counter = 0;
unsigned char seconds = 0;
// External interrupt - Button press
void ext_int0() interrupt 0 {
counter++;
// Update display
P1 = counter;
}

// Timer interrupt - Every second

NEETHU SUSAN ALEX, AP, ECE, UKFCET


void timer0_isr() interrupt 1 {
static unsigned char count = 0;
TH0 = 0x4B; // Reload for 50ms
TL0 = 0xFD;

count++;
if(count == 20) { // 20 × 50ms = 1 second
count = 0;
seconds++;
P2 = seconds;
}
}

void main() {
// Configure Timer 0
TMOD = 0x01;
TH0 = 0x4B;
TL0 = 0xFD;

// Configure interrupts
IT0 = 1; // Edge-triggered INT0
EX0 = 1; // Enable INT0
ET0 = 1; // Enable Timer 0
EA = 1; // Enable all

TR0 = 1; // Start timer

while(1) {
// Main program can do other tasks
}

NEETHU SUSAN ALEX, AP, ECE, UKFCET


}

MODULE SUMMARY
Key Concepts Covered
1. Timer/Counter Operations
 Two 16-bit timers (Timer 0 and Timer 1) in 8051
 Four operating modes:
o Mode 0: 13-bit timer/counter
o Mode 1: 16-bit timer/counter (most common)
o Mode 2: 8-bit auto-reload
o Mode 3: Split timer mode
 Key registers: TMOD, TCON, TH0/TL0, TH1/TL1
 Timer can work as timer (internal clock) or counter (external events)
 Applications: delays, event counting, baud rate generation
2. Serial Communication
 Full-duplex asynchronous communication via TxD and RxD
 Four modes with Mode 1 (8-bit UART) being most common
 Key registers: SBUF, SCON, PCON

NEETHU SUSAN ALEX, AP, ECE, UKFCET


 Baud rate generated using Timer 1 in Mode 2
 RS-232 standard for PC communication
 MAX232 IC for voltage level conversion
 Programming includes initialization, transmission, and reception
3. Interrupt System
 Six interrupt sources in 8051 (5 in 8051, 6 in 8052)
 Interrupt vectors at fixed memory locations
 IE register for enabling interrupts
 IP register for setting priorities
 Two priority levels: High and Low
 ISR written at interrupt vectors or using jump instructions
 Efficient for real-time applications
Important Formulas
Timer Delay Calculation:
Delay = Count × Machine Cycle Time
Count = 2^n - Initial Value (n = 13 or 16)
Machine Cycle = 12 / Crystal Frequency
Baud Rate Formula:
Baud Rate = (2^SMOD / 32) × (fosc / (12 × [256 - TH1]))
Programming Guidelines
1. Always initialize control registers before starting operation
2. Clear flags (TF0, TF1, TI, RI) in software when required
3. Use RETI instruction to return from ISR
4. Keep ISR short for better performance
5. Reload timer values in Mode 1 for continuous operation
6. Enable global interrupt (EA) before individual interrupts work

NEETHU SUSAN ALEX, AP, ECE, UKFCET


PRACTICE QUESTIONS
2 MARKS QUESTIONS
1. What are the two timers available in 8051 microcontroller?
2. Differentiate between timer mode and counter mode.
3. What is the function of TMOD register?
4. List the four modes of timer operation.
5. What is the purpose of TF0 and TR0 bits in TCON register?
6. What is the advantage of Mode 2 (auto-reload mode)?
7. Define baud rate in serial communication.
8. What are the two pins used for serial communication in 8051?
9. What is the function of SBUF register?
10. List the bits of SCON register.
11. What is the purpose of MAX232 IC?
Answer: MAX232 IC is a voltage level converter used in serial communication.
Purpose:
 Converts TTL/CMOS logic levels (0V and +5V) to RS-232 levels (-15V to +15V)

NEETHU SUSAN ALEX, AP, ECE, UKFCET


 Converts RS-232 levels back to TTL levels
 Eliminates need for external ±12V power supply
 Contains internal charge pump circuitry
Application: Interface 8051 (TTL levels) with PC serial port (RS-232 levels)
12. What is an interrupt?
13. How many interrupt sources are there in 8051?
14. What is the function of IE register?
15. List the interrupt vector addresses in 8051.
Q16. What is the difference between polling and interrupt?
17. What is ISR (Interrupt Service Routine)?
18. What is the function of IP register?
19. Differentiate between edge-triggered and level-triggered interrupts.
20. What is the purpose of EA bit in IE register?
21. What is the maximum count value in Mode 1 timer?
22. What happens when timer overflows?
23. Calculate TH0 and TL0 values for 10ms delay with 12MHz crystal.
24. What is the difference between synchronous and asynchronous communication?
Synchronous Asynchronous
Clock signal transmitted with data No clock signal transmitted
Both devices share common clock Each device has own clock
Faster data transfer Slower data transfer
More complex hardware Simpler hardware
No start/stop bits needed Uses start and stop bits
Better for high-speed, short distance Better for long distance
Examples: SPI, I2C Examples: UART, RS-232

25. What is full-duplex communication?


Full-duplex communication is a mode of communication where data can be transmitted and
received simultaneously in both directions.
Characteristics:
- Uses separate lines for transmission (TxD) and reception (RxD)
- Both devices can send and receive at the same time
- More efficient than half-duplex
- Requires two data lines
NEETHU SUSAN ALEX, AP, ECE, UKFCET
8051 Implementation:
- TxD (P3.1): Transmit data
- RxD (P3.0): Receive data
- Both can operate simultaneously
- Separate transmit and receive buffers (SBUF)

Example: Telephone conversation (both can talk and listen simultaneously)

26. What is the purpose of start and stop bits in asynchronous communication?
Start and Stop bits are control bits used in asynchronous serial communication for frame
synchronization.
Start Bit:
- Always 0 (low)
- Signals the beginning of data transmission
- Allows receiver to synchronize with incoming data
- Tells receiver to start sampling data bits

Stop Bit:
- Always 1 (high)
- Signals the end of data transmission
- Provides recovery time for receiver
- Returns line to idle state (high)
- Can be 1, 1.5, or 2 bits long
Purpose:
- Compensate for clock differences between devices
- Provide frame synchronization
- Enable reliable data detection

27. What is the role of Timer 1 in serial communication?


Timer 1 plays a crucial role in serial communication by generating the baud rate for serial
transmission and reception.
NEETHU SUSAN ALEX, AP, ECE, UKFCET
Functions:
1. Baud Rate Generation:
- Timer 1 in Mode 2 (8-bit auto-reload) is commonly used
- Provides precise clock pulses for serial port
- Value in TH1 determines baud rate
2. Configuration:
- Set Timer 1 in Mode 2 for automatic reload
- Load TH1 with calculated value
- Start Timer 1 by setting TR1 bit
- Timer runs continuously without overflow interrupt
3. Formula:
Baud Rate = (2^SMOD / 32) × (fosc / (12 × [256 - TH1]))
Example:
MOV TMOD, #20H ; Timer 1, Mode 2
MOV TH1, #0FDH ; For 9600 baud
SETB TR1 ; Start Timer 1

Q28. What is RETI instruction?


RETI (Return from Interrupt) is a special instruction used to return from an Interrupt Service
Routine (ISR).
Function:
- Pops the Program Counter (PC) from stack (2 bytes)
- Returns control to the interrupted program
- Restores the interrupt system to accept new interrupts
Difference between RET and RETI
RETI RET
Used in ISR Used in subroutines
Restores interrupt logic Only returns from subroutine
2-byte PC restoration 2-byte PC restoration
Clears interrupt-in-progress flag No interrupt handling

Syntax:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


```assembly
ISR_TIMER0:
; ISR code here
CPL P1.0
RETI ; Must use RETI, not RET
Important: Always use RETI at the end of ISR, never RET, otherwise interrupt system may
malfunction.

29. Can interrupts be nested? Explain briefly.


Yes, interrupts can be nested in 8051 based on priority levels.
Nesting Rules:
1. High priority can interrupt low priority:
- If a low priority ISR is executing, a high priority interrupt can interrupt it
2. Same priority cannot nest:
- Interrupts of the same priority level cannot interrupt each other
- Must wait until current ISR completes
3. Low cannot interrupt high:
- Low priority interrupts must wait if high priority ISR is running
30. What is the default priority of interrupts in 8051?
By default (when IP register = 00H), all interrupts have LOW priority and follow a polling
sequence to resolve conflicts.
Default Polling Sequence (Highest to Lowest):
1. External INT0 - Highest natural priority
2. Timer 0 (TF0)
3. External INT1
4. Timer 1 (TF1)
5. Serial Port (RI/TI) - Lowest natural priority
- All interrupts start at low priority when IP = 00H
- If multiple low-priority interrupts occur simultaneously, polling sequence determines which
is serviced first
- Priority can be changed using IP register

NEETHU SUSAN ALEX, AP, ECE, UKFCET


- External INT0 has highest natural priority, useful for critical events
- Serial port has lowest priority

6 MARKS QUESTIONS
1. Explain the Timer modes of 8051 microcontroller in detail.
2. Explain serial communication modes of 8051 with frame format.
3. Explain the interrupt system of 8051 with IE and IP registers.
4. Write an assembly language program to generate a 1 second delay using Timer 1 in
Mode 1.
Answer:
PROBLEM ANALYSIS
Given:
 Crystal frequency = 11.0592 MHz
 Required delay = 1 second
 Timer 1 in Mode 1 (16-bit)
CALCULATIONS
Step 1: Calculate machine cycle time
Machine cycle = 12 / Crystal frequency
= 12 / 11.0592 MHz
NEETHU SUSAN ALEX, AP, ECE, UKFCET
= 1.085 μs
Step 2: Calculate count for 1 second
Count needed = Delay / Machine cycle
= 1 second / 1.085 μs
= 1,000,000 μs / 1.085 μs
= 921,658 counts
Step 3: Maximum count in 16-bit timer
Maximum count = 2^16 = 65,536
Since 921,658 > 65,536, we need multiple overflows.
Step 4: Calculate number of overflows
Number of overflows = 921,658 / 65,536 = 14.06 ≈ 14 overflows
Step 5: Calculate remaining count
Total count = 14 × 65,536 = 917,504
Remaining = 921,658 - 917,504 = 4,154 counts
Step 6: Calculate initial value for remaining counts
Initial value = 65,536 - 4,154 = 61,382 = EFD6H
TH1 = EFH
TL1 = D6H
ASSEMBLY LANGUAGE PROGRAM
assembly
; Program: 1 Second Delay Using Timer 1 (Mode 1)
; Crystal: 11.0592 MHz
; Timer: Timer 1, Mode 1 (16-bit)
; Method: 14 overflows + partial count

ORG 0000H
LJMP MAIN

ORG 0030H
MAIN:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


MOV TMOD, #10H ; Timer 1, Mode 1

ONE_SEC_DELAY:
MOV R2, #14 ; Load overflow counter (14 times)

RELOAD:
MOV TH1, #0EFH ; Load initial value
MOV TL1, #0D6H ; EFD6H = 61,382
SETB TR1 ; Start Timer 1

WAIT:
JNB TF1, WAIT ; Wait for Timer 1 overflow
CLR TR1 ; Stop timer
CLR TF1 ; Clear overflow flag

DJNZ R2, RELOAD ; Repeat for 14 overflows

; 1 second delay completed


CPL P1.0 ; Toggle LED to verify delay

SJMP ONE_SEC_DELAY ; Repeat continuously

END
ALTERNATIVE METHOD - Using 50ms × 20 = 1 second
This is more common and easier to implement:
assembly
; Program: 1 Second Delay (50ms × 20 method)
; Crystal: 11.0592 MHz

ORG 0000H

NEETHU SUSAN ALEX, AP, ECE, UKFCET


LJMP MAIN

ORG 0030H
MAIN:
MOV TMOD, #10H ; Timer 1, Mode 1

ONE_SEC:
MOV R3, #20 ; 20 × 50ms = 1 second

NEXT_50MS:
ACALL DELAY_50MS ; Call 50ms delay
DJNZ R3, NEXT_50MS ; Repeat 20 times

CPL P1.0 ; Toggle LED


SJMP ONE_SEC ; Repeat

;-------------------------------------------------------
; Subroutine: 50ms Delay
; Calculation: 50ms / 1.085μs = 46,083 counts
; Initial value: 65,536 - 46,083 = 19,453 = 4BFDH
;-------------------------------------------------------
DELAY_50MS:
MOV TH1, #4BH ; Load higher byte
MOV TL1, #0FDH ; Load lower byte
SETB TR1 ; Start Timer 1

WAIT_50MS:
JNB TF1, WAIT_50MS ; Wait for overflow
CLR TR1 ; Stop timer
CLR TF1 ; Clear flag

NEETHU SUSAN ALEX, AP, ECE, UKFCET


RET

END
EMBEDDED C VERSION
c
#include <reg51.h>

void delay_50ms() {
TMOD = 0x10; // Timer 1, Mode 1
TH1 = 0x4B; // Load count value
TL1 = 0xFD; // for 50ms delay
TR1 = 1; // Start timer
while(TF1 == 0); // Wait for overflow
TR1 = 0; // Stop timer
TF1 = 0; // Clear flag
}

void delay_1sec() {
unsigned char i;
for(i = 0; i < 20; i++) {
delay_50ms(); // 50ms × 20 = 1 second
}
}

void main() {
while(1) {
P1_0 = ~P1_0; // Toggle LED
delay_1sec(); // 1 second delay
}
}

NEETHU SUSAN ALEX, AP, ECE, UKFCET


PROGRAM EXPLANATION
1. Initialization:
o TMOD = 10H configures Timer 1 in Mode 1
o TH1 and TL1 loaded with initial count value
2. Delay Generation:
o TR1 = 1 starts the timer
o Timer counts up from initial value to FFFFH
o TF1 flag set on overflow
o Program waits in loop checking TF1
3. Multiple Iterations:
o Since 1 second requires multiple overflows
o Counter register (R2 or R3) tracks iterations
o DJNZ decrements and jumps until zero
4. Verification:
o LED on P1.0 toggles every 1 second
o Visual confirmation of timing accuracy
TIMING VERIFICATION
Method 1 (14 overflows):
14 × 65,536 × 1.085 μs = 0.9956 seconds ≈ 1 second

Method 2 (20 × 50ms):


20 × 46,083 × 1.085 μs = 1.000 second (exact)
Method 2 is preferred for its simplicity and accuracy.

5. Explain serial data transmission and reception programming in 8051.

6. Write a program to toggle Port 1 pins using External Interrupt 0 and Timer 0
interrupt with appropriate priority settings.
Answer:
PROBLEM SPECIFICATION
Requirements:
NEETHU SUSAN ALEX, AP, ECE, UKFCET
 External INT0 (P3.2) toggles P1.0 on button press
 Timer 0 toggles P1.1 every 500ms
 INT0 has higher priority than Timer 0
 Use interrupt-driven approach
SYSTEM DESIGN
Priority Settings:
 INT0: High priority (can interrupt Timer 0 ISR)
 Timer 0: Low priority
Hardware Connections:
 Push button connected to P3.2 (INT0)
 LED1 connected to P1.0 (toggled by INT0)
 LED2 connected to P1.1 (toggled by Timer 0)
 Crystal: 11.0592 MHz
CALCULATIONS
For 500ms delay using Timer 0:
Machine cycle = 1.085 μs
Count for 50ms = 50ms / 1.085μs = 46,083
Initial value = 65,536 - 46,083 = 19,453 = 4BFDH
Number of 50ms delays = 500ms / 50ms = 10

TH0 = 4BH
TL0 = FDH
Repeat 10 times for 500ms
ASSEMBLY LANGUAGE PROGRAM
assembly
;=======================================================
; Program: Dual Interrupt with Priority
; INT0: Toggle P1.0 (High Priority)
; Timer 0: Toggle P1.1 every 500ms (Low Priority)
; Crystal: 11.0592 MHz

NEETHU SUSAN ALEX, AP, ECE, UKFCET


;=======================================================

ORG 0000H
LJMP MAIN ; Jump to main program

;-------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------
ORG 0003H ; INT0 vector address
LJMP ISR_INT0 ; Jump to INT0 ISR

ORG 000BH ; Timer 0 vector address


LJMP ISR_TIMER0 ; Jump to Timer 0 ISR

;-------------------------------------------------------
; Main Program
;-------------------------------------------------------
ORG 0030H
MAIN:
; Configure Timer 0
MOV TMOD, #01H ; Timer 0, Mode 1 (16-bit)
MOV TH0, #4BH ; Load initial value
MOV TL0, #0FDH ; for 50ms delay

; Configure Interrupts
MOV IE, #83H ; EA=1, ET0=1, EX0=1
; Enable: Global, Timer0, INT0

MOV IP, #01H ; PX0=1 (INT0 high priority)


; PT0=0 (Timer0 low priority)

NEETHU SUSAN ALEX, AP, ECE, UKFCET


; Configure INT0
SETB IT0 ; Edge-triggered mode (falling edge)

; Start Timer 0
SETB TR0 ; Start Timer 0

; Initialize counter for 500ms


MOV R7, #10 ; 10 × 50ms = 500ms

; Main loop
LOOP:
SJMP LOOP ; Wait for interrupts

;-------------------------------------------------------
; ISR for External Interrupt 0 (High Priority)
; Function: Toggle P1.0 on button press
;-------------------------------------------------------
ISR_INT0:
PUSH ACC ; Save accumulator
PUSH PSW ; Save PSW

CPL P1.0 ; Toggle P1.0 (LED1)

; Debounce delay (optional)


ACALL DEBOUNCE

POP PSW ; Restore PSW


POP ACC ; Restore accumulator
RETI ; Return from interrupt

NEETHU SUSAN ALEX, AP, ECE, UKFCET


;-------------------------------------------------------
; ISR for Timer 0 (Low Priority)
; Function: Toggle P1.1 every 500ms
;-------------------------------------------------------
ISR_TIMER0:
PUSH ACC ; Save registers
PUSH PSW

CLR TR0 ; Stop timer

; Reload timer for 50ms


MOV TH0, #4BH
MOV TL0, #0FDH

SETB TR0 ; Restart timer

; Decrement 500ms counter


DJNZ R7, EXIT_TIMER ; If not 500ms, exit

; 500ms elapsed
MOV R7, #10 ; Reload counter
CPL P1.1 ; Toggle P1.1 (LED2)

EXIT_TIMER:
POP PSW ; Restore registers
POP ACC
RETI ; Return from interrupt

;-------------------------------------------------------

NEETHU SUSAN ALEX, AP, ECE, UKFCET


; Subroutine: Debounce Delay (~20ms)
;-------------------------------------------------------
DEBOUNCE:
PUSH 00H
PUSH 01H

MOV R0, #200 ; Outer loop


D_LOOP1:
MOV R1, #200 ; Inner loop
D_LOOP2:
DJNZ R1, D_LOOP2
DJNZ R0, D_LOOP1

POP 01H
POP 00H
RET

END
EMBEDDED C PROGRAM
c
//======================================================
=
// Program: Dual Interrupt with Priority
// INT0: Toggle P1.0 (High Priority)
// Timer0: Toggle P1.1 every 500ms (Low Priority)
//======================================================
=

#include <reg51.h>

unsigned char timer_count = 0;


NEETHU SUSAN ALEX, AP, ECE, UKFCET
//-------------------------------------------------------
// External Interrupt 0 ISR (High Priority)
//-------------------------------------------------------
void external_int0() interrupt 0 {
P1_0 = ~P1_0; // Toggle P1.0 (LED1)
}

//-------------------------------------------------------
// Timer 0 ISR (Low Priority)
//-------------------------------------------------------
void timer0_isr() interrupt 1 {
// Reload timer for 50ms
TH0 = 0x4B;
TL0 = 0xFD;

timer_count++;

if(timer_count == 10) { // 10 × 50ms = 500ms


timer_count = 0;
P1_1 = ~P1_1; // Toggle P1.1 (LED2)
}
}

//-------------------------------------------------------
// Main Function
//-------------------------------------------------------
void main() {
// Configure Timer 0
TMOD = 0x01; // Timer 0, Mode 1

NEETHU SUSAN ALEX, AP, ECE, UKFCET


TH0 = 0x4B; // Load count for 50ms
TL0 = 0xFD;

// Configure Interrupts
IE = 0x83; // EA=1, ET0=1, EX0=1
IP = 0x01; // PX0=1 (INT0 high priority)

// Configure INT0
IT0 = 1; // Edge-triggered

// Start Timer 0
TR0 = 1;

// Main loop
while(1) {
// Main program can perform other tasks
// Interrupts handle LED toggling
}
}

7. Explain baud rate generation in 8051 with calculations for common baud rates.
PROGRAMMING EXAMPLES
Example 1: 9600 baud with 11.0592 MHz

Assembly:
```assembly
; Set SMOD = 1 for double baud rate
MOV PCON, #80H ; SMOD = 1 (bit 7 of PCON)

; Configure Timer 1

NEETHU SUSAN ALEX, AP, ECE, UKFCET


MOV TMOD, #20H ; Timer 1, Mode 2 (8-bit auto-reload)
MOV TH1, #0FDH ; Load 253 for 9600 baud
MOV SCON, #50H ; Serial Mode 1, REN=1

; Start Timer 1
SETB TR1 ; Begin baud rate generation
```

Embedded C:
```c
#include <reg51.h>

void init_serial_9600() {
PCON = 0x80; // SMOD = 1
TMOD = 0x20; // Timer 1, Mode 2
TH1 = 0xFD; // 9600 baud
SCON = 0x50; // Mode 1, REN enabled
TR1 = 1; // Start Timer 1
}
```

Example 2: 4800 baud with 11.0592 MHz


```c
void init_serial_4800() {
PCON = 0x80; // SMOD = 1
TMOD = 0x20;
TH1 = 0xFA; // 250 for 4800 baud
SCON = 0x50;
TR1 = 1;
}

NEETHU SUSAN ALEX, AP, ECE, UKFCET


```

8. Design and write a program for a temperature monitoring system using Timer
interrupt and Serial communication to send data to PC.
SYSTEM SPECIFICATION
Requirements:
- Read temperature from ADC every 1 second using Timer 0
- Display temperature on LCD
- Send temperature data to PC via serial port
- Alert if temperature exceeds threshold
- Crystal: 11.0592 MHz
HARDWARE COMPONENTS
1. 8051 Microcontroller
2. Temperature Sensor: LM35 (10mV/°C)
3. ADC: ADC0804 (8-bit)
4. LCD: 16×2 character LCD
5. MAX232: RS-232 level converter
6. LED: For alert indication
HARDWARE CONNECTIONS
Temperature Sensor (LM35):
 Output → ADC0804 Analog Input
ADC0804:
 D0-D7 → Port 1 (P1.0 to P1.7)
 CS → P3.5
 RD → P3.6
 WR → P3.7
 INTR → P3.4
LCD (16×2):
 RS → P2.0
 RW → P2.1
 EN → P2.2
NEETHU SUSAN ALEX, AP, ECE, UKFCET
 D4-D7 → P2.4 to P2.7 (4-bit mode)
Serial Communication:
 TxD (P3.1) → MAX232 → PC
 RxD (P3.0) → MAX232 → PC
Alert LED:
 P0.0 → LED → GND (with resistor)
SYSTEM FLOWCHART
Start ↓ Initialize:
 LCD
 Serial Port (9600 baud)
 Timer 0 (1 second)
 ADC ↓ Enable Timer 0 Interrupt ↓ Main Loop (Display, Monitor) ↓ Timer 0 ISR
(Every 1 second): ├→ Read ADC ├→ Convert to Temperature ├→ Display on LCD
├→ Send to PC via Serial ├→ Check Threshold └→ Activate Alert if needed

COMPLETE PROGRAM - ASSEMBLY LANGUAGE


```assembly
;=======================================================
; Temperature Monitoring System
; Features: Timer interrupt, Serial communication, ADC
; Crystal: 11.0592 MHz
;=======================================================

; ADC Control Pins


ADC_CS EQU P3.5
ADC_RD EQU P3.6
ADC_WR EQU P3.7
ADC_INTR EQU P3.4

; LCD Control Pins


RS EQU P2.0
NEETHU SUSAN ALEX, AP, ECE, UKFCET
RW EQU P2.1
EN EQU P2.2

; Alert LED
ALERT_LED EQU P0.0

; Temperature Threshold (35°C)


THRESHOLD EQU 35

ORG 0000H
LJMP MAIN

ORG 000BH ; Timer 0 vector


LJMP TIMER0_ISR

ORG 0030H
MAIN:
; Initialize LCD
ACALL LCD_INIT

; Display startup message


MOV DPTR, #MSG1
ACALL LCD_STRING
ACALL DELAY_2S
ACALL LCD_CLEAR

; Initialize Serial Port (9600 baud)


MOV PCON, #80H ; SMOD = 1
MOV TMOD, #21H ; Timer1: Mode2, Timer0: Mode1
MOV TH1, #0FDH ; 9600 baud

NEETHU SUSAN ALEX, AP, ECE, UKFCET


MOV SCON, #50H ; Mode 1, REN=1
SETB TR1 ; Start Timer 1

; Initialize Timer 0 for 1 second


MOV TH0, #4BH ; 50ms delay
MOV TL0, #0FDH
MOV R7, #20 ; Counter for 1 second

; Enable Timer 0 interrupt


SETB ET0
SETB EA
SETB TR0

; Send startup message to PC


MOV DPTR, #PC_MSG
ACALL SEND_STRING

LOOP:
SJMP LOOP ; Wait for interrupt

;-------------------------------------------------------
; Timer 0 ISR - Execute every 1 second
;-------------------------------------------------------
TIMER0_ISR:
CLR TR0
PUSH ACC
PUSH PSW

; Reload timer
MOV TH0, #4BH

NEETHU SUSAN ALEX, AP, ECE, UKFCET


MOV TL0, #0FDH
SETB TR0

; Decrement counter
DJNZ R7, ISR_EXIT

; 1 second elapsed
MOV R7, #20 ; Reload counter

; Read temperature
ACALL READ_ADC
MOV R6, A ; Store ADC value

; Convert to temperature (ADC value × 0.5 ≈ °C for LM35)


MOV B, #2
DIV AB ; Divide by 2
MOV R5, A ; Store temperature

; Display on LCD
ACALL LCD_CLEAR
MOV DPTR, #TEMP_MSG
ACALL LCD_STRING
MOV A, R5
ACALL DISPLAY_TEMP

; Send to PC
ACALL SEND_TEMP_PC

; Check threshold
MOV A, R5

NEETHU SUSAN ALEX, AP, ECE, UKFCET


CJNE A, #THRESHOLD, CHECK_TEMP
SJMP ISR_EXIT

CHECK_TEMP:
JC ISR_EXIT ; If A < THRESHOLD, no alert
SETB ALERT_LED ; Temperature high
MOV DPTR, #ALERT_MSG
ACALL SEND_STRING
SJMP ISR_EXIT_END

ISR_EXIT:
CLR ALERT_LED ; Normal temperature

ISR_EXIT_END:
POP PSW
POP ACC
RETI

;-------------------------------------------------------
; Read ADC Value
;-------------------------------------------------------
READ_ADC:
SETB ADC_CS
CLR ADC_CS ; Select ADC
SETB ADC_WR
CLR ADC_WR ; Start conversion
NOP
NOP
SETB ADC_WR

NEETHU SUSAN ALEX, AP, ECE, UKFCET


WAIT_ADC:
JB ADC_INTR, WAIT_ADC ; Wait for conversion

CLR ADC_RD ; Read data


MOV A, P1 ; Get ADC value
SETB ADC_RD
SETB ADC_CS
RET

;-------------------------------------------------------
; Display Temperature on LCD
;-------------------------------------------------------
DISPLAY_TEMP:
MOV B, #100
DIV AB ; Get hundreds digit
ADD A, #30H ; Convert to ASCII
ACALL LCD_DATA

MOV A, B
MOV B, #10
DIV AB ; Get tens digit
ADD A, #30H
ACALL LCD_DATA

MOV A, B ; Get ones digit


ADD A, #30H
ACALL LCD_DATA

MOV A, #'C'
ACALL LCD_DATA

NEETHU SUSAN ALEX, AP, ECE, UKFCET


RET

;-------------------------------------------------------
; Send Temperature to PC
;-------------------------------------------------------
SEND_TEMP_PC:
; Send "Temp: "
MOV DPTR, #TEMP_PC_MSG
ACALL SEND_STRING

; Send temperature value


MOV A, R5
MOV B, #100
DIV AB
ADD A, #30H
ACALL SERIAL_TX

MOV A, B
MOV B, #10
DIV AB
ADD A, #30H
ACALL SERIAL_TX

MOV A, B
ADD A, #30H
ACALL SERIAL_TX

; Send " C\r\n"


MOV A, #' '
ACALL SERIAL_TX

NEETHU SUSAN ALEX, AP, ECE, UKFCET


MOV A, #'C'
ACALL SERIAL_TX
MOV A, #0DH ; Carriage return
ACALL SERIAL_TX
MOV A, #0AH ; Line feed
ACALL SERIAL_TX
RET

;-------------------------------------------------------
; Serial Transmit Character
;-------------------------------------------------------
SERIAL_TX:
MOV SBUF, A
WAIT_TX:
JNB TI, WAIT_TX
CLR TI
RET

;-------------------------------------------------------
; Send String to PC
;-------------------------------------------------------
SEND_STRING:
CLR A
NEXT_CHAR:
MOVC A, @A+DPTR
JZ STRING_END
ACALL SERIAL_TX
INC DPTR
SJMP NEXT_CHAR
STRING_END:

NEETHU SUSAN ALEX, AP, ECE, UKFCET


RET

;-------------------------------------------------------
; LCD Initialization
;-------------------------------------------------------
LCD_INIT:
ACALL DELAY_20MS
MOV A, #38H ; 2 lines, 5x7 matrix
ACALL LCD_CMD
MOV A, #0EH ; Display ON, Cursor ON
ACALL LCD_CMD
MOV A, #01H ; Clear display
ACALL LCD_CMD
MOV A, #06H ; Entry mode
ACALL LCD_CMD
RET

;-------------------------------------------------------
; LCD Command
;-------------------------------------------------------
LCD_CMD:
MOV P2, A
CLR RS
CLR RW
SETB EN
ACALL DELAY_1MS
CLR EN
RET

;-------------------------------------------------------

NEETHU SUSAN ALEX, AP, ECE, UKFCET


; LCD Data
;-------------------------------------------------------
LCD_DATA:
MOV P2, A
SETB RS
CLR RW
SETB EN
ACALL DELAY_1MS
CLR EN
RET

;-------------------------------------------------------
; LCD Clear
;-------------------------------------------------------
LCD_CLEAR:
MOV A, #01H
ACALL LCD_CMD
ACALL DELAY_2MS
RET

;-------------------------------------------------------
; LCD String Display
;-------------------------------------------------------
LCD_STRING:
CLR A
NEXT_LCD_CHAR:
MOVC A, @A+DPTR
JZ LCD_STR_END
ACALL LCD_DATA
INC DPTR

NEETHU SUSAN ALEX, AP, ECE, UKFCET


SJMP NEXT_LCD_CHAR
LCD_STR_END:
RET

;-------------------------------------------------------
; Delay Routines
;-------------------------------------------------------
DELAY_1MS:
MOV R0, #4
D1: MOV R1, #250
D2: DJNZ R1, D2
DJNZ R0, D1
RET

DELAY_2MS:
ACALL DELAY_1MS
ACALL DELAY_1MS
RET

DELAY_20MS:
MOV R2, #20
D3: ACALL DELAY_1MS
DJNZ R2, D3
RET

DELAY_2S:
MOV R3, #200
D4: ACALL DELAY_1MS
DJNZ R3, D4
RET

NEETHU SUSAN ALEX, AP, ECE, UKFCET


;-------------------------------------------------------
; Messages
;-------------------------------------------------------
MSG1: DB "Temp Monitor", 0
TEMP_MSG: DB "Temp: ", 0
TEMP_PC_MSG: DB "Temperature: ", 0
ALERT_MSG: DB "ALERT! High Temp!", 0DH, 0AH, 0
PC_MSG: DB "Temperature Monitoring System Started", 0DH, 0AH, 0

END
```

EMBEDDED C VERSION
```c
//=======================================================
// Temperature Monitoring System - Embedded C
// Features: Timer ISR, Serial, ADC, LCD, Alert
//=======================================================

#include <reg51.h>
#include <stdio.h>

// Pin Definitions
sbit ADC_CS = P3^5;
sbit ADC_RD = P3^6;
sbit ADC_WR = P3^7;
sbit ADC_INTR = P3^4;

sbit RS = P2^0;

NEETHU SUSAN ALEX, AP, ECE, UKFCET


sbit RW = P2^1;
sbit EN = P2^2;

sbit ALERT_LED = P0^0;

// Constants
#define THRESHOLD 35
#define LCD_DATA_PORT P2

// Global Variables
unsigned char temperature = 0;
unsigned char timer_count = 0;
bit read_flag = 0;

// Function Prototypes
void delay_ms(unsigned int);
void lcd_init(void);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void lcd_string(char*);
void lcd_clear(void);
unsigned char read_

NEETHU SUSAN ALEX, AP, ECE, UKFCET

You might also like