0% found this document useful (0 votes)
11 views24 pages

Chapter 5

Chapter Five discusses the architecture of the 8051 microcontroller, focusing on interrupts, timers, and counters. It explains how interrupts allow the microcontroller to respond to external or internal events and details the steps involved in handling interrupts, including the use of an interrupt service routine (ISR). Additionally, it covers the functionality of the 8051's timers and counters, including their applications in generating time delays and counting events.

Uploaded by

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

Chapter 5

Chapter Five discusses the architecture of the 8051 microcontroller, focusing on interrupts, timers, and counters. It explains how interrupts allow the microcontroller to respond to external or internal events and details the steps involved in handling interrupts, including the use of an interrupt service routine (ISR). Additionally, it covers the functionality of the 8051's timers and counters, including their applications in generating time delays and counting events.

Uploaded by

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

Chapter Five

Interrupts, Timers and Counters


Inside Architecture of 8051
External interrupts
On-chip Timer/Counter

Interrupt ROM for


On-chip Timer 1 Counter
Control program
RAM Timer 0 Inputs
code

CPU

Bus Serial
4 I/O Ports
OSC Control Port

P0 P1 P2 P3 TxD RxD
Address/Data
Figure 1. Inside the 8051 Microcontroller Block Diagram
2
Interrupts
• An interrupt is an external or internal event that interrupts the
microcontroller to inform it that a device needs its service
• A single microcontroller can serve several devices by a ways
Interrupts
• Whenever any device needs its service, the device notifies the
microcontroller by sending it an interrupt signal
• Upon receiving an interrupt signal, the microcontroller interrupts
whatever it is doing and serves the device
• The program which is associated with the interrupt is called the interrupt
service routine(ISR) or interrupt handler
• The advantage of interrupts is that the microcontroller can serve many
devices (not all at the same time)
Each devices can get the attention of the microcontroller based on the assigned
priority
• For every interrupt, there must be an interrupt service routine (ISR), or interrupt
handler.
When an interrupt is invoked, the microcontroller runs the interrupt service routine
For every interrupt, there is a fixed location in memory that holds the address of its ISR
The group of memory locations set aside to hold the addresses of ISRs is called interrupt
vector table
• Upon activation of an interrupt, the microcontroller goes through the following
steps.
1. It finishes the instruction it is executing and saves the address of the next
instruction (PC) on the stack
2. It also saves the current status of all the interrupts internally (i.e: not on the stack)
3. It jumps to a fixed location in memory, called the interrupt vector table, that holds
the address of the ISR
4. The microcontroller gets the address of the ISR from the interrupt vector table
and jumps to it
• It starts to execute the interrupt service subroutine until it reaches the last instruction of the
subroutine which is RETI (return from interrupt)
5. Upon executing the RETI instruction, the microcontroller returns to
the place where it was interrupted
• First, it gets the program counter (PC) address from the stack by
popping the top two bytes of the stack into the PC
• Then it starts to execute from that address
• Six interrupts are allocated as follows
 Reset – power-up reset
 Two interrupts are set aside for the timers: one for timer 0 and one
for timer 1
 Two interrupts are set aside for hardware external interrupts
• P3.2 and P3.3 are for the external hardware interrupts INT0 (or EX1), and INT1
(or EX2)
 Serial communication has a single interrupt that belongs to both
receive and transfer
• The 8051 has two external hardware interrupts
 Pin 12 (P3.2) and pin 13 (P3.3) of the 8051, designated as INT0 and
INT1, are used as external hardware interrupts
• The interrupt vector table locations 0003H and 0013H are set aside for INT0
and INT1
 There are two activation levels for the external hardware interrupts
• Level trigged
• Edge trigged
 In the level-triggered mode, INT0 and INT1 pins are normally high
• If a low-level signal is applied to them, it triggers the interrupt
• Then the microcontroller stops whatever it is doing and jumps to the interrupt
vector table to service that interrupt
• The low-level signal at the INT pin must be removed before the execution of the
last instruction of the ISR, RETI; otherwise, another interrupt will be generated
 This is called a level-triggered or level activated interrupt and is the default mode
upon reset of the 8051
 Pins P3.2 and P3.3 are used for normal I/O unless the INT0 and INT1 bits in the IE
register are enabled
 After the hardware interrupts in the IE register are enabled, the controller keeps
sampling the INTn pin for a low-level signal once each machine cycle
 According to one manufacturer’s data sheet,
• The pin must be held in a low state until the start of the execution of ISR
• If the INTn pin is brought back to a logic high before the start of the execution of ISR there will
be no interrupt
• If INTn pin is left at a logic low after the RETI instruction of the ISR, another interrupt will be
 To ensure the activation of the hardware interrupt at the INTn pin,
make sure that the duration of the low-level signal is around 4
machine cycles, but no more
• This is due to the fact that the level-triggered interrupt is not
latched
• Thus the pin must be held in a low state until the start of the ISR
execution
Timers /Counters
• The 8051 has 2 timers/counters: timer/counter 0 and timer/counter 1.
They can be used as
1. The timer is used as a time delay generator.
• The clock source is the internal crystal frequency of the 8051.
2. An event counter.
• External input from input pin to count the number of events on registers.
• These clock pulses cold represent the number of people passing through an
entrance, or the number of wheel rotations, or any other event that can be
converted to pulses.

11
Timer
• 8051 timers use 1/12 of XTAL frequency as the
input of timers, regardless of machine cycle.
• Because the input of timer is a regular, fixed-
periodic square wave, we can count the number of
pulses and calculate the time delay.

8051
XTAL
÷ 12 Timer
oscillator
P1 to
LCD
TH0
Set
TL0
Timer 0 12
Counter
• Count the number of events
• External input from Tx input pin (x=0 or 1).
• We use Tx to denote T0 or T1.
• External input from T0 input pin (P3.4) for Counter 0
• External input from T1 input pin (P3.5) for Counter 1

8051
TH0
P1 to
TL0
LCD
Vcc P3.4
a switch T0
13
Figure : Timer/Counter 0
timer input

XTAL
÷ 12
oscillator
C/T = 0

TH0 TL0
C/T = 1
counter input
T0 Pin
1:start
Pin 3.4
TR0 0:stop TF0

Gate 1. monitor by JNB


2. interrupt
INT0 Pin
Pin 3.2
hardware control
Sec 9.2 
14
Figure : Timer/Counter 1
timer input

XTAL
÷ 12
oscillator
C/T = 0

TH1 TL1
C/T = 1
counter input
T1 Pin
Pin 3.5 1:start
TR1 TF1
0:stop

Gate 1. monitor by JNB


2. interrupt
INT1 Pin
Pin 3.3
hardware control

15
Interfacing 16×2 LCD with 8051
• In this session we will have brief discussion on how to interface 16×2
LCD module to AT89C51which is a 8051 family microcontroller. We
use LCD display for the messages for more interactive way to operate
the system or displaying error messages etc. interfacing LCD to
microcontroller is very easy if you understanding the working of LCD,
in this session I will not only give the information of LCD and also
provide the code in C language which is working fine without any
errors.
• Interfacing LCD to AT89C51:
• LCD: 16×2 Liquid Crystal Display which will display the 32 characters at
a time in two rows (16 characters in one row). Each character in the
display of size 5×7 pixel matrix, Although this matrix differs for
different 16×2 LCD modules if you take JHD162A this matrix goes to
5×8. This matrix will not be same for all the 16×2 LCD modules. There
are 16 pins in the LCD module, the pin configuration us given below
• So by reading the above table you can get a brief idea how to display a character.
For displaying a character you should enable the enable pin (pin 6) by giving a
pulse of 450ns, after enabling the pin6 you should select the register select pin
(pin4) in write mode. To select the register select pin in write mode you have to
make this pin high (RS=1), after selecting the register select you have to configure
the R/W to write mode that is R/W should be low (R/W=0).
• Follow these simple steps for displaying a character or data
• E=1; enable pin should be high
• RS=1; Register select should be high
• R/W=0; Read/Write pin should be low.
• To send a command to the LCD just follows these steps:
• E=1; enable pin should be high
• RS=0; Register select should be low
• R/W=1; Read/Write pin should be high.
• Commands: There are some preset commands which will do a specific task in
the LCD. These commands are very important for displaying data in LCD. The list
of commands given below:
Examples to show LCD display using c program(AT89c51) and
assembly(atmega32) lcd_comm(0x80);
c program(AT89c51) lcd_msg("LCD INTERFACING");
#include<reg51.h> lcd_comm(0xc2);
sbit rs = P2^0; lcd_msg("WITH 8051");
lcd_comm(0xc2);
sbit rw = P2^1;
}
sbit en = P2^2;
}
void init_lcd(); void init_lcd()
void lcd_comm(unsigned char value1); {
void lcd_data(unsigned char value2); lcd_comm(0x38);
void lcd_msg(unsigned char *ch); delay(10);
void delay(unsigned int ms); lcd_comm(0x0e);
void main() delay(10);
lcd_comm(0x01);
{
delay(10);
P0 = 0x00;
lcd_comm(0x06);
P2 = 0x00; delay(10);
init_lcd(); lcd_comm(0x80);
while(1) delay(10);
void lcd_cmd(unsigned char value1)
{
P0=value1; LCD1
LM016L
rs=0;
rw=0;
en=1;
delay(1);

V DD
VSS
VEE

RW
U1

RS

D0
D1
D2
D3
D4
D5
D6
D7
E
en=0; 19
XTAL1 P0.0/AD0
39
38

1
2
3
4
5
6
7
8
9
10
11
12
13
14
P0.1/AD1
37
} 18
XTAL2
P0.2/AD2
P0.3/AD3
36
35
P0.4/AD4
void lcd_msg(unsigned char *ch) P0.5/AD5
34
33
P0.6/AD6
9 32
{ RST P0.7/AD7
21
P2.0/A8
while(*ch!=0) P2.1/A9
22
23
P2.2/A10
{ 29
30
PSEN
ALE
P2.3/A11
P2.4/A12
24
25
31 26
lcd_data(*ch); EA P2.5/A13
P2.6/A14
27
28
P2.7/A15
ch++; 1
P1.0 P3.0/RXD
10
2 11
} 3
4
P1.1
P1.2
P3.1/TXD
P3.2/INT0
12
13
P1.3 P3.3/INT1
} 5
6
P1.4
P1.5
P3.4/T0
P3.5/T1
14
15
7 16
void delay(unsigned int ms) 8
P1.6
P1.7
P3.6/WR
P3.7/RD
17

{ AT89C51

unsigned int i,j;


for(i=0;i<=ms;i++)
for(j=0;j<=120;j++);

You might also like