Micro Controllers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 101

MICROPROCESSORS AND

MICROCONTROLLERS

B. Tech – CSE (IOT)


III Year II Semester
UNIT – IV
The 8051 Architecture: Architecture of 8051 Micro
controller, Memory Organization. Special Function
Registers. Input/Output Ports and Circuits, External
Memory, Counter and Timers, Serial data Input/Output,
Interrupts

2
Microcontroller
(microcontroller)
Block diagram &
Internal Architecture of
8051 microcontroller
Internal architecture of 8051
■ The 8051 is the original member of the MCS-51family,
and is the core for all MCS-51 devices.
■ The features of the 8051 core are:
◻ 8 bit ALU, with two registers A & B
◻ 16 bit Program Counter & Data Pointer
◻ 8 bit Program Status Word
◻ 8bit Stack Pointer
◻ 128 bytes Internal RAM
◻ 4 register banks, each containing 8 registers.
◻ 16 bytes, which may be addressed at bit level.
◻ 80 bytes of gent purpose data memory.
◻ Internal ROM / EPROM / Flash memory of 4KB
◻ Can be extended separate 64KB address space for data & code
memory.
◻ 32 discrete I/O pins, grouped as four 8 bit ports.
◻ Full Duplex UART.
◻ Two l6 bit timer / counter - T0 & T1
◻ Two external & three internal interrupt sources
◻ Control registers –TCON, TMOD, SCON, PCON, and IP & IE
◻ Oscillator & clock circuits
The architectural structure of 8051
■ The programming model of 8051 is a collection of 8 bit
and 16 bit registers and 8 bit memory locations.
■ Registers:
◻ 8051 has 34 general purpose or working registers.
Two of them A & B, hold results of many instructions,
particularly Mathematical and logical operations, of
8051 CPU.
◻ The other 32 Reg. arranged as a part of internal RAM
in four banks RBO-RB3.
◻ A (Accumulator) register is used for many operations -
Addition,
◻ Reg. B is used with Reg.A for multiplication & division
operations and also for data transfer.
◻ Subtraction, integer- multiplication & division and
Boolean bit manipulations.
CY AC FO RSI RSO OV - P
■ Carry out Auxiliary General Register Banks Over flow Parity of
From carry for purpose RB0-RB3 flag Accumulator
MSB of BCD set by
ALU operations hardware to 1
Operand if it contains
odd number
of ones

■ Note: The contents of (RS1-RS0) enable the working register banks as


follows
(0, 0) - Bank 0 (00H-07H)
(0, 1) - Bank 1 (08H-0FH)
(1, 0) - Bank 2 (10H-17H)
(1, 1) - Bank 3 (18H-1FH)
■ Carry flag is set whenever there is carry out from MSB. This flag is
after 8 bit ADD/ SUB operation.
■ It can also be set to 1 or 0 directly using SETB C or CLR C.
■ Auxiliary carry: If there is a carry from D3 to D4 position during
Add/sub operation, this bit will set. Otherwise, it is cleared. This flag
is used for BCD operations.
■ Parity Flag reflects the number of 1’s in ‘A’. If ‘A’ contains an odd
number of 1’s, then P=1. Therefore P=0, if A has an even number of
1’s.
■ Over Flow Flag: This flag is set whenever the result of a signed
number operation is too large to be accommodated in 7bits, causing
the Higher order bit to over flow into the Sign Bit.
8051 Oscillator & Clock
■ 8051 has on Chip Oscillator but needs an external clock to run it. A Quartz
crystal
■ oscillator is connected to XTAL2 & XTAL1. C1 &C2 are also connected as
shown above.
■ C1= C2= 30pf (typical)
■ xtal oscillator =1 to 16 MHz.

■ Each Machine Cycle is made up of 6 states. A state is basic time interval for
discrete operations of µcontroller such as Opcode fetch, decode an opcode,
Execute an opcode.Two oscillator pulses define a state.
■ Program Instructions may require 1/2/3/4 machine cycles, depending on its
type.
■ RST
■ It is an input and is active High (Normally Low) by applying
a High pulse to this pin, µc will be reset and terminate all
activities.
◻ On reset PC - 0000
■ ACC - 0000
■ B - 0000
■ PSW - 0000
■ SR - 0007
■ DPTR-0000

■ EA - External Access (of Memory)


■ PSE - Program Store Enable
■ ALE - Address Latch Enable.
WHAT DO THE SFRs CONTAIN JUST AFTER POWER-ON OR A RESET
■ The following table lists the contents of each SFR after power-on or a hardware
reset Register Value in Binary
*ACC 0000 0000
*B 0000 0000
*PSW 0000 0000
SP 0000 0111
DPTR 0000 0000
DPH 0000 0000
DPL 1111 1111
*P0 1111 1111
*P1 1111 1111
*P2 1111 1111
*P3 8051 XXX00000
*IP 8052 XX000000
*IE 8051 0XX00000
TMOD 8052 0X000000
*TCON 0000 0000
*+T2CON 0000 0000
TH0 0000 0000
TL0 0000 0000
TH1 0000 0000
TL1 0000 0000
+TH2 0000 0000
+TL2 0000 0000
+RCAP2H 0000 0000
+RCAP2L 0000 0000
*SCON 0000 0000
SBUF 0000 0000
PCON Indeterminate
HMOS 0XXXXXXX
CHMOS 0XXX0000
■ Memory organization
■ 8051 has separate address spaces for program memory
and data memory.
■ Program memory can be up to 64KB, lower 4KB may
reside ON-CHIP.
■ 8051 can address up to 64KB of data memory.
■ It has 128bytes of ON-CHIP RAM and SFR (128B).
■ The lower 128bytes can be accessed either by direct
addressing or indirect addressing.
F8 FF

F0 B F7

E8 EF

E0 ACC E7

D8 DF

D0 PSW D7

C8 T2CON RCAP2L RCAP2H TL2 TH2 CF

C0 C7

B8 IP BF

B0 P3 B7

A8 IE AF

A0 P2 A7

98 SCON SBUF 9F

90 P1 97

88 TCON TMOD TL0 TL1 TH0 TH1 8F

80 P0 SP DPL DPH PCON 87


Special Function Registers

Symbol Name Address


*ACC Accumulator 0E0H
*B B register 0F0H
*PSW Program Status Word 0D0H
SP Stack Pointer 81H
DPTR Data Pointer 2 Bytes 82H
DPH Low Byte 83H
DPL High Byte 80H
*P0 Port 0 90H
*P1 Port 1 0A0H
*P2 Port 2 0B0H
*P3 Port 3 0B8H
*IP Interrupt Priority Control 0A8H
*IE Interrupt Enable Control 89H
TMOD Timer/Counter Mode Control 88H
*TCON Timer/Counter Control 0C8H
*+T2CON Timer/Counter 2 Control 8CH
TH0 Timer/Counter 0 High Byte 8AH
TL0 Timer/Counter 0 Low Byte 8DH
TH1 Timer/Counter 1 High Byte 8BH
TL1 Timer/Counter 1 Low Byte 0CDH
+TH2 Timer/Counter 2 High Byte 0CCH
+TL2 Timer/Counter 2 Low Byte 0CBH
+RCAP2H T/C 2 Capture Reg. High Byte 0CAH
+RCAP2L T/C 2 Capture Reg. Low Byte 98H
*SCON Serial Control 90H
SBUF Serial Data Buffer 87H
PCON Power Control
■ RBO-RB3: 32Bytes are kept aside for register Banks, of four. In each Bank, R0-R7
(8) registers are present

■ Though 4banks of register are kept aside, micro controller at a time can access only
one register bank, where as other register banks will be used as simple RAM
locations.
■ By default RB0 is selected, on power ON.
■ We can switch to other banks, by using D4 & D3 bits of PSW.
■ State the contents of RAM locations after the following program.
■ SetB PSW4 select bank 2
■ Mov R0, #99h
■ Mov R1, #85h
■ Mov R2, #3Fh.
■ PC can access the memory from 0000H –FFFFH. Program address higher than
0FFFH, which exceed the internal ROM capacity will cause 8051
Timer

7/12/2024 24
Timer / counter programming in the 8051:

The 8051 has two timers\counters. They can


be used either as timers to generate time
delay or as counters to count events
happening outside the micro computer. Now
we shall see how they are programmed.

25 7/12/2024
Programming 8051 timers:

8051 has two timers, timer 0 & timer 1. this


module has two 16bit registers. T0 and T1
registers. These registers can be configured to
operate either as timers or event counters.
In the ‘timer’ function. The register is incremented
every machine cycle. Thus, one can think it as
counting machine cycles. Since a machine cycle
consists of 12 oscillator periods, the count rate is
1/12 of the oscillator frequency.

26 7/12/2024
The 16 bit register of T0 / T1 is accessed as
low byte and high byte (TH0 / TH1)

THO TLO

DB15 DB0

27 7/12/2024
TMOD register:

Both timers t0 and t1 use the same register called TMOD to set
the various timer operating modes.

GATE C/T’ M1 M0 GATE C/T’ M1 M0

28 7/12/2024
Gate: Gating control when set timer/counter is enabled only
while the INTx pin is high and TRx pin is set. When cleared, the
timer is enabled whenever the TRx control bit is set.

C / T :Timer / Counter selected cleared for timer application( c


input from interval system clock). Set for counter operation( input
from Tx input pin).
M1, M0🡪 MODE BITS
M1 M0 MODE OPERATING MODE

0 0 0 13 bit timer mode


0 1 1 16 bit timer mode
1 0 2 8 bit timer mode
1 1 3 split timer mode
29 7/12/2024
Source of the clock:

if C/T =0 the crystal frequency attached to the 8051 is the source


of the clock for the timer.
Find the timer’s clock frequency and its period for various 8051
based systems with the following crystal frequency.
1) 12 MHZ
2) 16 MHZ
11.0592 MHZ

÷12
oscillator

30 7/12/2024
the frequency for the timer is always 1/12th of the frequency of
crystal attached to the 8051
1) 1/12 X 12 MHZ =1MHZ and T=1/1MHZ =1μs.
2) 1/12 X 16 MHZ=1.33MHZ and T=1/1.33MHZ=0.75μs
3) 1/12 X 11.0592 MHZ=921.6KHZ and
T=1/921.6kHz=1.085μs

31 7/12/2024
Influence of gate:

It is to start or stop the timer, using hardware or software


control.
SETB TR1, CLR TR1 for timer 1 and
SETB TR0,CLR TR0 for timer 0 instruction start or stop
timers as long as gate =0in TMOD register.

32 7/12/2024
The case of gate = 1 in TMOD:

So far, the task of timer is started through software using SETB


TR0 and TR1 instruction.
If gate bit in TMOD is set to 1,the start and stop of a the timer are
done externally through pins P3.2 and P3.3 of T0 & T1.
This way of operation has several application.
For example, assume that on 8051 system is used to sound an
alarm every second using T0, in addition to many things. A
switch can be connected to pin 3.2 , which can be used to turn on
and off the timer, there by shutting down the alarm.

33 7/12/2024
XTAL
oscillator % 12
C/ T=0
O O
C/ T =1
T 0 Pin
Pin 3.4

TR 0

Gate O

INT0 Pin
Pin3.2
34 7/12/2024
Mode 0:
This mode is compatible for MCS 48 family. The 13 bit
counter can hold values between 0000 to 1FFF in TH and
TL registers. Therefore, when the timer reaches its
maximum of 1FFF it rolls over to 0000 and TF is raised.

35 7/12/2024
Mode 1:
The characteristics of this mode are as follows:
1) it is a 16 bit timer. It allows values of 0000 to FFFFH
into timer register TH and TL.
2) After TH and TL are loaded with 16 bit initial value.
The timer must be started. This is done by SETB TR0 for
SETB TR1 for T1.
3) After timer is started, it starts to count up. It counts
up to FFFFH . when it rolls over from FFFFH to 0000, it
sets a flag bit timer flag(TF). This flag can be monitored.
When it is raised, one option is to stop the timer with the
instruction CLR TR0 or CLR TR1.
4)After the timer reaches its limit and rolls over, in order to
repeat the process the register TH and TL must be
reloaded with the original value and TF must be reset to 0.
36 7/12/2024
Xtal ÷12 TH TL TF
osc

TR TF goes high when Over flow


C/T’ FFFF flag

37 7/12/2024
Mode 1 programming :

To generate a time delay, using time mode, the following steps are
taken:
∙ load the TMOD value register indicating which timer(T0 0r T1) is
to be used and which timer mode is selected.
∙ Load register TL and TH with initial count values.
∙ Start the timer.
∙ Keep monitoring the status of timer flag (TF). Come out of the
loop, when TF becomes HIGH.
∙ Stop the timer.
∙ Clear the TF flag for the next count.
∙ Go back to step 2.
.

38 7/12/2024
NAME EXAMPLE 11
ORG 0H
MOV TMOD,#01H
HERE: MOV TL0, #0F2 H 2
MOV TH0, #OFFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY: SETB TR0 1
AGAIN: JNB TF0,AGAIN 14
CLR TR0 1
CLR TF0 1
RET 1

27
39 7/12/2024
In the above program, observe the following:
∙ TMOD is loaded.
∙ FFF2H is loaded into TH0-TL0.
∙ P1.5 is toggled for the high and low portions of the pulse.
∙ The DELAY subroutine , timer0 is started by SETB TR0
instruction.
∙ Timer0 counts up with the passing of each clock. As timer counts
up, it goes through FFF3, ….., FFFF, 0000 states . now TF0
becomes HIGH.
∙T0 is stopped by the instruction CLR TR0.And the process is
repeated.

40 7/12/2024
FFF2 FFF3 FFF4 FFFF 0000

TF=0 TF=0 TF=0 TF=1


TF=0

41 7/12/2024
PROGRAMMING 8051 TIMERS

■ Steps to program in mode 1


◻ Set timer mode 1 or 2
◻ Set TL0 and TH0 (for mode 1 16 bit mode)
◻ Run the timer
◻ Monitor the timer flag bit

42
Example
In the following program, we are creating a square wave of 50% duty cycle
(with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay

43
Example
The following program generates a square wave on pin P 1.5 continuously using
Timer 1 for a time delay. Find the frequency of the square wave if XTAL = 11.0592
MHz. In your calculation do not include the overhead due to the timer setup
instructions in the loop.

44
PROGRAMMING 8051 TIMERS

■ Finding values to be loaded into the


timer
◻ XTAL = 11.0592 MHz (12MHz)
◻ divide the desired time delay by 1.085μs
(1μs) to get n
◻ 65536 – n = N
◻ convert N to hex yyxx
◻ set TL = xx and TH = yy

45
Example
Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of
50 Hz frequency on pin P2.3.

■ T = 1/50 Hz = 20 ms
■ 1/2 of it for the high and low portions of
the pulse = 10 ms
■ 10 ms / 1.085 us = 9216
■ 65536 - 9216 = 56320 in decimal =
DC00H
■ TL = 00 and TH = DCH
■ The calculation for 12MHz crystal uses
the same steps
46
Example (cont)
Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of
50 Hz frequency on pin P2.3.

47
PROGRAMMING 8051 TIMERS

■ Mode 2 programming
◻ 8-bit timer, allows values of 00 to FFH
◻ TH is loaded with the 8-bit value
◻ a copy is given to TL
◻ timer is started by ,"SETB TR0" or "SETB TR1“
◻ starts to count up by incrementing the TL register
◻ counts up until it reaches its limit of FFH
◻ when it rolls over from FFH to 00, it sets high TF
◻ TL is reloaded automatically with the value in TH
◻ To repeat, clear TF
◻ mode 2 is an auto-reload mode

48
PROGRAMMING 8051 TIMERS

■ Steps to program in mode 2


1. load TMOD, select mode 2
2. load the TH
3. start timer
4. monitor the timer flag (TF) with "JNB”
5. get out of the loop when TF=1
6. clear TF
7. go back to Step 4 since mode 2 is
auto-reload

49
Example
Assuming that XTAL = 11.0592 MHz, find (a) the frequency of the square
wave generated on pin P1.0 and (b) the smallest frequency achievable in
this program, and the TH value to do that.

50
COUNTER PROGRAMMING

■ The case of GATE = 1 in TMOD


◻ GATE = 0, the timer is started with
instructions "SETB TR0" and "SETB TR1“
◻ GATE = 1, the start and stop of the timers
are done externally through pins P3.2 and
P3.3
◻ allows us to start or stop the timer
externally at any time via a simple switch

51
COUNTER PROGRAMMING

Figure 9–8 Timer/Counter 0


52
COUNTER PROGRAMMING

Figure 9–9 Timer/Counter 1


53
COUNTER PROGRAMMING

■ TCON register
◻ TR0 and TR1 flags turn on or off the timers
◻ bits are part of a register called TCON (timer control)
◻ upper four bits are used to store the TF and TR bits of both
Timer 0 and Timer 1
◻ lower four bits are set aside for controlling the interrupt bits
◻ "SETB TRl" and "CLR TRl“
◻ "SETB TCON. 6" and "CLR TCON. 6“

54
COUNTER PROGRAMMING

Table 9–2 Equivalent Instructions for the Timer Control Register (TCON)
56
serial communication

7/12/2024 57
Basics of Serial Communication
■ Computers transfer data in two ways:
◻ Parallel: Often 8 or more lines (wire conductors)
are used to transfer data to a device that is only a
few feet away.

◻ Serial: To transfer to a device located many


meters away, the serial method is used. The data
is sent one bit at a time.

8051 Microcontroller 58
Basics of Serial Communication
■ Serial data communication uses two methods
◻ Synchronous method transfers a block of data at
a time

◻ Asynchronous method transfers a single byte at


a time

■ There are special IC’s made by many


manufacturers for serial communications.
◻ UART (universal asynchronous Receiver
transmitter) 8051 Microcontroller 59
Asynchronous – Start & Stop Bit
■ Asynchronous serial data communication is widely
used for character-oriented transmissions
◻ Each character is placed in between start and stop bits,
this is called framing.
◻ Block-oriented data transfers use the synchronous
method.

■ The start bit is always one bit, but the stop bit
can be one or two bits

■ The start bit is always a 0 (low) and the stop


bit(s) is 1 (high)
8051 Microcontroller 60
Asynchronous – Start & Stop Bit

8051 Microcontroller 61
Data Transfer Rate
■ The rate of data transfer in serial data
communication is stated in bps (bits per second).
■ Another widely used terminology for bps is baud
rate.
◻ It is modem terminology and is defined as the number of
signal changes per second
◻ In modems, there are occasions when a single change of
signal transfers several bits of data
■ As far as the conductor wire is concerned, the
baud rate and bps are the same.

8051 Microcontroller 62
8051 Serial Port
■ Synchronous and Asynchronous
■ SCON Register is used to Control
■ Data Transfer through TXd & RXd pins
■ Four Modes of Operation:

Mode 0 :Synchronous Serial Communication


Mode 1 :8-Bit UART with Timer Data Rate
Mode 2 :9-Bit UART with Set Data Rate
Mode 3 :9-Bit UART with Timer Data Rate

8051 Microcontroller 63
TIMER 1 TH1 VALUES FOR VARIOUS BAUD RATES

BUAD RATE TH1 TH1(HEX)


(DECIMAL)
9600 -3 FD

4800 -6 FA

2400 -12 F4

1200 -24 E8

64
Registers related to Serial
Communication

1. SBUF Register

2. SCON Register

3. PCON Register

8051 Microcontroller 65
SBUF Register
■ SBUF is an 8-bit register used solely for serial
communication.
■ For a byte data to be transferred via the TxD line, it must be
placed in the 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 line.
■ SBUF holds the byte of data when it is received by 8051 RxD
line.
■ 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 SBUF.
8051 Microcontroller 66
SBUF Register
■ Sample Program:

8051 Microcontroller 67
SCON Register

SM0 SM1 SM2 REN TB8 RB8 TI RI

Set when a Cha-


ractor received
Set to Enable
Serial Data
reception Set when Stop bit Txed

Enable Multiprocessor 9th Data Bit 9th Data Bit


Communication Mode Sent in Mode 2,3 Received in Mode 2,3

8051 Microcontroller 68
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following
features:

1. Serial data enters and exits through RXD

2. TXD outputs the clock

3. 8 bits are transmitted / received

4. The baud rate is fixed at (1/12) of the oscillator frequency

8051 Microcontroller 69
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following
features:

1. Serial data enters through RXD


2. Serial data exits through TXD
3. On receive, the stop bit goes into RB8 in SCON
4. 10 bits are transmitted / received
1. Start bit (0)
2. Data bits (8)
3. Stop Bit (1)
5. Baud rate is determined by the Timer 1 over flow rate.

8051 Microcontroller 70
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the
following features:

1. Serial data enters through RXD


2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
1.Start bit (0)
2.Data bits (9)
3.Stop Bit (1)
6. Baud rate is programmable
8051 Microcontroller 71
8051 Serial Port – Mode 3
The Serial Port in Mode-3 has the
following features:

1. Serial data enters through RXD


2. Serial data exits through TXD
3. 9th data bit (TB8) can be assign value 0 or 1
4. On receive, the 9th data bit goes into RB8 in SCON
5. 11 bits are transmitted / received
1.Start bit (0)
2.Data bits (9)
3.Stop Bit (1)
6. Baud rate is determined by Timer 1 overflow rate.
8051 Microcontroller 72
Programming Serial Data Transmission
1. TMOD register is loaded with the value 20H, indicating the use of timer
1 in mode 2 (8-bit auto-reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data
transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode
1, where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF
register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx to see
if the character has been transferred completely.
8. To transfer the next byte, go to step 5

8051 Microcontroller 73
Programming Serial Data Reception
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate.
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI, xx
to see if an entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved
into a safe place.
8. To receive the next character, go to step 5.

8051 Microcontroller 74
Doubling Baud Rate
■ There are two ways to increase the baud rate
of data transfer
1. By using a higher frequency crystal
2. By changing a bit in the PCON register

■ PCON register is an 8-bit register.

•When 8051 is powered up, SMOD is zero

•We can set it to high by software and thereby double the baud rate.

8051 Microcontroller 75
Doubling Baud Rate (cont…)

8051 Microcontroller 76
8051
Interrupts
8051 Microcontroller 77
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 two ways:
1. Interrupt
2. Polling
8051 Microcontroller 78
Interrupt Vs Polling
1. 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.
2. Polling
◻ The microcontroller continuously monitors the status of a given
device.
◻ When the conditions met, it performs the service.
◻ After that, it moves on to monitor the next device until every one is
serviced.

8051 Microcontroller 79
Interrupt Vs Polling
■ The polling method is not efficient, since it wastes much of
the microcontroller’s time by polling devices that do not need
service.
■ 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 the polling method, it is not possible to assign priority
since it checks all devices in a round-robin fashion.

■ The microcontroller can also ignore (mask) a device request


for service in Interrupt.
8051 Microcontroller 80
Steps in Executing an Interrupt
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.
5. It starts to execute the interrupt service subroutine until it
reaches the last instruction of the subroutine which is RETI
(return from interrupt).
6. Upon executing the RETI instruction, the microcontroller
returns to the place where it was interrupted.
8051 Microcontroller 81
Six Interrupts in 8051
Six interrupts are allocated as follows:
1. Reset – power-up reset.

2. Two interrupts are set aside for the timers.


◻ one for timer 0 and one for timer 1

3. 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)

4. Serial communication has a single interrupt that


belongs to both receive and transfer.
8051 Microcontroller 82
What events can trigger Interrupts?
■ We can configure the 8051 so that any of the
following events will cause an interrupt:

◻ Timer 0 Overflow.
◻ Timer 1 Overflow.
◻ Reception/Transmission of Serial Character.
◻ External Event 0.
◻ External Event 1.

■ We can configure the 8051 so that when Timer 0


Overflows or when a character is sent/received, the
appropriate interrupt handler routines are called.

8051 Microcontroller 83
8051 Interrupt Vectors

8051 Microcontroller 84
8051 Interrupt related Registers
■ The various registers associated with the use of
interrupts are:
◻ TCON - Edge and Type bits for External Interrupts 0/1

◻ SCON - RI and TI interrupt flags for RS232

◻ IE - Enable interrupt sources

◻ IP - Specify priority of interrupts

8051 Microcontroller 85
Enabling and Disabling an Interrupt
■ Upon reset, all interrupts are disabled (masked),
meaning that none will be responded to by the
microcontroller if they are activated.

■ The interrupts must be enabled by software in order


for the microcontroller to respond to them.

■ There is a register called IE (interrupt enable) that


is responsible for enabling (unmasking) and
disabling (masking) the interrupts.

8051 Microcontroller 86
Interrupt Enable (IE) Register

--

• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.

MOV IE,#08h • ES : Enable Serial port interrupt.


or
SETB ET1
• ET1 : Enable Timer 1 control bit.
• EX1 : Enable External 1 interrupt.
• ET0 : Enable Timer 0 control bit.
• EX0 : Enable External 0 interrupt.
8051 Microcontroller 87
Enabling and Disabling an Interrupt
■ Example: Show the instructions to (a) enable the serial interrupt, timer 0
interrupt, and external hardware interrupt 1 and (b) disable (mask) the
timer 0 interrupt, then (c) show how to disable all the interrupts with a
single instruction.
■ Solution:

◻ (a) MOV IE,#10010110B ;enable serial, timer 0, EX1


■ Another way to perform the same manipulation is:
◻ 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

◻ (b) CLR IE.1 ;mask (disable) timer 0 interrupt only


◻ (c) CLR IE.7 ;disable all interrupts

8051 Microcontroller 88
Interrupt Priority
■ When the 8051 is powered up, the priorities are assigned
according to the following.

■ In reality, the priority scheme is nothing but an internal polling


sequence in which the 8051 polls the interrupts in the
sequence listed and responds accordingly.

8051 Microcontroller 89
Interrupt Priority
■ We can alter the sequence of interrupt priority by assigning a
higher priority to any one of the interrupts by programming a
register called IP (interrupt priority).
■ To give a higher priority to any of the interrupts, we make the
corresponding bit in the IP register high.

8051 Microcontroller 90
Interrupt Priority (IP) Register

Reserved PS PT1 PX1 PT0 PX0

Serial Port
INT 0 Pin
Timer 1 Pin

INT 1 Pin Timer 0 Pin

Priority bit=1 assigns high priority


Priority bit=0 assigns low priority
8051 Microcontroller 91
SBUF register
It is an eight bit register used for serial
communication in 8051.

For a byte to be transformed via TXD line, it must

be placed in SBUF. Similarly, SBUF holds the byte

of data when it is received by 8051’s RXD line.

92
•The following is a program to transfer letter ‘A’ serially at
4800 baud , continuously. Observe the status of serial
window, serial port and timer port for results.

NAME EXAMPLE 16
ORG OH
MOV TMOD,#20H; Timer 1, mode 2(auto reload)
MOV TH1,# -6 H; 4800 baud rate
MOV SCON,#50H; 8 BIT,1 Stop, REN enabled
SETB TR1; Start timer 1
AGAIN: MOV SBUF, #”A”; letter A to be transferred
HERE: JNB TI,HERE ; Wait for the last bit
CLR TI; clear T1 for next char
SJMP AGAIN ; keep sending A
END
93
• The significance of TI flag: once the timer is on
TI is reset(=0). When the stop bit is transferred,
8051 rises the TI flag is set, indicating that the last
character was transmitted and ready to transfer
next character. By monitoring TI flag we make sure
that we are not overloading the SBUF register.
When 8051 finishes transferring a byte, it raises
the TI flag to indicate it is ready for the next
character. After SBUF is loaded with a new byte,
TI flag is forced to zero to transmit the new
byte.

.
94
The following is a program to transfer a message ‘YES’
serially at 9600 baud, 8-bit data,1-stop bit , continuously.
Observe the results on serial window, serial port, and timer
1.
NAME EXAMPLE 17
ORG 0H
MOV TMOD,#20H; timer 1, mode2
MOV TH1,#-3H; 9600 baud rate
MOV SCON,#50H; 8-BIT, 1-stop bit, REN
enabled
SETB TR1; start timer 1
AGAIN: MOV A, #”Y” ; Transfer ‘y’
ACALL TRANS
MOV A,#”E”
ACALL TRANS
MOV A,#”S”; TRANSFER S
95
ACALL TRANS
SJMP AGAIN ; Keep doing it
: serial letter transfer subroutine
TRANS: MOV SBUF,A; Load SBUF
HERE: JNB TI,HERE ; wait for the last bit to
transfer
CLR TI ; Get ready for next byte
RET
END

96
PROGRAMMING THE 8051 TO RECEIVE DATA SERIALLY
To transfer character bytes serially, the following steps must be
followed:

the TMOD register loaded with the value 20h, indicating the timer
1 in mode 2 to the baud rate.

The TH1 is loaded to set the baud rate for serial data transfer.
The SCON register is loaded with the value 50h,indicaitng the
mode 1 , where an 8-bit data is started with start and stop bits.
TR1 is set to start timer1.
RI is cleared by software.
The RI bit is monitored to whether entered character is received or
not.
When RI is raised, SBUF has the byte, move its contents to a safe
place.
To transfer next character, go to step 5. 97
The importance of RI flag : when the stop bit is
received during receiving, 8051 makes RI=1,
indicating that an entire byte is received.

By checking RI flag, when it is raised we know that


the byte is received and is residing in SBUF
register.
After content of SBUF is placed in a safe place, RI
must be forced to reset to allow the next received
character to place in SBUF register.

Assume that the 8051 serial port is connected to


the COM port of IBM PC and PC is to send and
receive data serially, P1 and P2 are connected to
LED’S and switches respectively 98
Write an 8051 program to
a) Send to PC the message “WE ARE READY”
b) Receive any data sent by PC and put it on LED’S connected
port 1
c) Get data on switches connected to P2 and send it to PC
serially
The program should perform part-a once, but parts b and c
continuously. Use the 4800 baud rate. Observe the results on
serial window, timer port, and P1 and P2.
NAME EXAMPLE 18
ORG OH
MOV P2,#OFFH;make P2 an input port
MOV TMOD, #20H; timer 1 mode 2(auto reload)
MOV TH1, #OFA; 4800 baud rate
MOV SCON, #50H; 8 bit, 1 stop, REN enabled
SETB TR1;start timer1
99
MOV DPTR,#200H; load pointer for
message
H_1: CLR A
MOVC A.,@A+DPTR; get the character
JZ B_1; if last character is get out
ACALL SEND; other wise call transfer
INC DPTR
SJMP H_1 ; stay in loop
B_1: MOV A,P2 ; read data on P2
ACALL SEND; transfer it serially
ACALL RECV ; Get the serial data
MOV P1,A; display it on led’s
JMP B_1; stay in loop indefinitely
;---------------serial data transfer. ACC has the data100
SEND: MOV SBUF, A; load the data
H_2: JNB TI,H_2; stay until here last bit
CLR TI ; get ready for next character
RET ;return to caller
;-------------------- receive data serially in simulator
RECV: JNB RI, RECV ; wait here for
MOV A, SBUF; save it in ACC
CLR RI; get ready for next character
RET ; return to caller
;-----------the message
ORG 200H
MYDATA: DB “WE ARE READY”, 0;
END

101

You might also like