L7 - 8051 Interrupt, Timer and Addressing Modes
L7 - 8051 Interrupt, Timer and Addressing Modes
2
INTERRUPT STRUCTURE
After executing the RETI the microcontroller returns to the
place where it was interrupted.
The 8051 microcontroller has FIVE interrupts in addition to
Reset. They are :
Timer 0 overflow Interrupt
Timer 1 overflow Interrupt
External Interrupt 0 (INT0)
External Interrupt 1 (INT1)
Serial port events Interrupt.
3
INTERRUPT STRUCTURE
Each interrupt has a specific place in code memory where
program execution (interrupt service routine) begins.
External Interrupt 0: 0003 H
Timer 0 overflow: 000B H
External Interrupt 1: 0013 H
Timer 1 overflow: 001B H
Serial Interrupt : 0023 H
Upon reset all Interrupts are disabled & do not respond to
the Microcontroller. These interrupts must be enabled by
software in order for the Microcontroller to respond to
them.
This is done by an 8-bit register called Interrupt Enable 4
Register (IE).
INTERRUPT ENABLE REGISTER
6
INTERRUPT PRIORITY
REGISTER
External interrupts
Reset
Interrupt control
Interrupt Enable/Disable
8
TIMERS IN 8051
MICROCONTROLLERS
The 8051 microcontroller has two 16-bit timers Timer 0 (T0)
and Timer 1 (T1) which can be used either to generate accurate
time delays or as event counters.
These timers are accessed as two 8-bit registers TL0, TH0 &
TL1 ,TH1 because the 8051 microcontroller has 8-bit
architecture.
9
TIMER 0
The Timer 0 is a 16-bit register and can be treated as two 8-bit
registers (TL0 & TH0) and these registers can be accessed
similar to any other registers like A,B or R1,R2,R3 etc…
Ex : The instruction Mov TL0,#07 moves the value 07 into
lower byte of Timer0.
Similarly Mov R5,TH0 saves the contents of TH0 in the R5
register.
10
TIMER 1
The Timer 1 is also a 16-bit register and can be treated as two
8-bit registers (TL1 & TH1) and these registers can be accessed
similar to any other registers like A,B or R1,R2,R3 etc…
Ex : The instruction MOV TL1,#05 moves the value 05 into
lower byte of Timer1.
Similarly MOV R0,TH1 saves the contents of TH1 in the R0
register
11
TIMER MODE OPERATION (FROM
BOOK)
Timer Mode 0
Timer Mode 1
Timer Mode 2
Timer Mode 3
12
TMOD REGISTER
The various operating modes of both the timers T0 and T1 are
set by an 8-bit register called TMOD register. In this TMOD
register the lower 4-bits are meant for Timer 0 and the higher 4-
bits are meant for Timer1.
13
TMOD REGISTER
GATE: This bit is used to start or stop the timers by hardware.
When GATE = 1 ,the timers can be started / stopped by the
external sources. When GATE = 0, the timers can be started or
stopped by software instructions like SETB TR0 or SETB TR1
C/T (clock/Timer) : This bit decides whether the timer is used
as delay generator or event counter. When C/T = 0 ,the Timer
is used as delay generator and if C/T = 1 the timer is used as
an event counter. The clock source for the time delay is the
crystal frequency of 8051.
14
TMOD REGISTER
M1,M0 (Mode) : These two bits are the timer mode bits. The
timers of the 8051 can be configured in four modes.Mode0,
Mode1, Mode2 and Mode3.The selection and operation of the
modes is shown below.
S.No M0 M1 Mode Operation
1 0 0 0 13-bit Timer mode
Use the THX register as an 8-bit counter and the TLX
as a 5-bit counter.
Immediate
Register
Direct
Register Indirect
Indexed
16
IMMEDIATE ADDRESSING MODE
Immediate Data is specified in the instruction itself
Egs:
MOV A, #65H
MOV A, #’A’
MOV R6, #65H
MOV DPTR, #2343H
MOV P1, #65H
17
REGISTER ADDRESSING MODE
MOV Rn, A ;n=0,..,7
ADD A, Rn
MOV DPTR, R6
18
DIRECT ADDRESSING MODE
Although the entire of 128 bytes of RAM can be accessed using
direct addressing mode, it is most often used to access RAM
loc. 30 – 7FH.
19
REGISTER INDIRECT ADDRESSING MODE
In this mode, register is used as a pointer to the data.
MOV @R1, B
20
INDEXED ADDRESSING MODE AND
ON-CHIP ROM ACCESS
This mode is widely used in accessing data elements of look-up
table entries located in the program (code) space ROM at the
8051
MOVC A, @A+DPTR
A= content of address A + DPTR from ROM
Note:
Because the data elements are stored in the program (code )
space ROM of the 8051, it uses the instruction MOVC instead
of MOV. The “C” means code.
21
8051- SERIAL COMMUNICATION
22
BASICS OF SERIAL
COMMUNICATION
23
TYPES OF SERIAL
COMMUNICATIONS
24
DATA TRANSFER AND PARALLEL
DATA TRANSFER
S.No Serial Communication Parallel Communication
1 Data is transmitted bit after the bit in a single line Data is transmitted simultaneously through group of
lines(Bus)
2 Data congestion takes place No, Data congestion
3 Low speed transmission High speed transmission
4 Implementation of serial links is not an easy task. Parallel data links are easily implemented in hardware
7 No effect of inter symbol interference and noise Parallel ports suffer extremely from inter-symbol
interference (ISI) and noise, and therefore the data can be
corrupted over long distances.
8 The bandwidth of serial wires is much higher. The bandwidth of parallel wires is much lower.
9 Serial interface is more flexible to upgrade , without Parallel data transfer mechanism rely on hardware
changing the hardware resources and hence not flexible to upgrade.
25
10 Serial communication work effectively even at high Parallel buses are hard to run at high frequencies.
frequencies.
SERIAL COMMUNICATION IN 8051
MICROCONTROLLER
The 8051 has two pins for transferring and receiving data by
serial communication.
These two pins are part of the Port3(P3.0 &P3.1).
26
SCON (SERIAL CONTROL)
REGISTER
REN SCON.4 Receive enable. Set or cleared by making this bit either 1 or
0 foe enable /disable reception.
TB8 SCON.3 9th data bit transmitted in modes 2 and 3 (not widely used)
RB8 SCON.2 9th data bit received in modes 2 and 3.it is not used in mode
0 & mode 1.If SM2 = 0 RB8 is the stop bit . (not widely
used)
28
SBUF REGISTER
The moment a byte is written into SBUF, it is framed with the
start and stop bits and transferred serially via the TxD pin.
Similarly, when the bits are received serially via RxD, the 8051
deframes it by eliminating the stop and start bits, making a byte
out of the data received, and then placing it in the SBUF.
29
SCON (SERIAL CONTROL)
REGISTER
SM0, SM1 : These two bits of SCON register determine the
framing of data by specifying the number of bits per character
and start bit and stop bits. There are 4 serial modes.
SM0 SM1
0 0 : Serial Mode 0
0 1 : Serial Mode 1
1 0 : Serial Mode 2
1 1 : Serial Mode 3
30
SCON (SERIAL CONTROL)
REGISTER
In the SCON register, when serial mode 1 is chosen, the data
framing is 8 bits, 1 stop bit, and 1 start bit, which makes it
compatible with the COM port of IBM/compatible PCs.
In serial mode 1, for each character a total of 10 bits are
transferred, where the first bit is the start bit, followed by 8 bits
of data, and finally 1 stop bit.
31
SCON (SERIAL CONTROL)
REGISTER
REN (Receive Enable) also referred as SCON.4. When it is
high, it allows the 8051 to receive data on the RxD pin. So to
receive and transfer data REN must be set to 1.When
REN=0,the receiver is disabled. This is achieved as below
SETB SCON.4
& CLR SCON.4
32
SCON (SERIAL CONTROL)
REGISTER
TI (Transmit interrupt) is the D1 bit of SCON register. When
8051 finishes the transfer of 8-bit character, it raises the TI flag
to indicate that it is ready to transfer another byte. The TI bit is
raised at the beginning of the stop bit.
RI (Receive interrupt) is the D0 bit of the SCON register.
When the 8051 receives data serially ,via RxD, it gets rid of
the start and stop bits and places the byte in the SBUF register.
Then it raises the RI flag bit to indicate that a byte has been
received and should be picked up before it is lost. RI is raised
halfway through the stop bit.
33
Thank You
34