0% found this document useful (0 votes)
6 views124 pages

8051 Timer Programming Techniques

The document covers the programming of the 8051 microcontroller's timers, including their modes, registers, and interfacing techniques. It explains the differences between timers and counters, programming steps for various modes, and provides examples for generating time delays and square waves. Additionally, it discusses the use of assembly language for programming and the calculation of timer values based on crystal frequency.

Uploaded by

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

8051 Timer Programming Techniques

The document covers the programming of the 8051 microcontroller's timers, including their modes, registers, and interfacing techniques. It explains the differences between timers and counters, programming steps for various modes, and provides examples for generating time delays and square waves. Additionally, it discusses the use of assembly language for programming and the calculation of timer values based on crystal frequency.

Uploaded by

KD REBIRTH
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

1

MICROPROCESSOR, MICROCONTROLLER,
AND INTERFACING
TECHNIQUES
Contents
2

 8051 timer & its programming in ALP &


C
 8051 Interrupts and its programming in
ALP, C,
 8051 serial port communication and its
programming in ALP & C
 Interfacing ADC, Interfacing DAC
 Case studies on timers, interrupts, serial
port communication.
UNIT 2
3

8051 TIMER PROGRAMMING IN


ASSEMBLY LANGUAGE
TIMER VS COUNTER
4

The timer is used as a time delay An event counter given by external pin
generator, input by internal crystal  Count the number of events
oscillator  Show the number of events on
 Set the initial value of registers registers
 Start the timer and then the 8051 counts up.  External input from T0 input pin
 Input from internal system clock (machine cycle) (P3.4) for Counter 0
 When the registers equal to 0 and the 8051 sets
a bit to denote time out
 External input from T1 input pin
(P3.5) for Counter 1
 External input from Tx input
pin.
 We use Tx to denote
8051 T0 or T1.
8051
TH0
P1
to
P2 P1 to TL0 LCD
Set LCD P3.4
Timer 0 TH0 a switch T0

TL0
 This bit is used to decide whether the
timer is used as a delay generator or an
event counter.
C/T = 0 : timer
C/T = 1 : counter
PROGRAMMING 8051 TIMERS
6

 Basic registers of the timer


 Timer 0 and Timer 1 are 16 bits width
 each 16-bit timer is accessed as two separate
registers of low byte and high byte.
PROGRAMMING 8051 TIMERS
7

 Timer 0 registers
 low byte register is called TL0 (Timer 0 low byte)
and the high byte register is referred to as TH0
(Timer 0 high byte)
 can be accessed like any other register, such as A, B,
R0, R1, R2, etc.
 "MOV TL0, #4FH" moves the value 4FH into TL0
 "MOV R5, TH0" saves TH0 (high byte of Timer
0) in R5
PROGRAMMING 8051 TIMERS
8

Figure 9–1 Timer 0 Registers


PROGRAMMING 8051 TIMERS
9

 Timer 1 registers

 also 16 bits
 split into two bytes TL1 (Timer 1 low byte) and TH1
(Timer 1 high byte)
 accessible in the same way as the registers of Timer
0.
PROGRAMMING 8051 TIMERS
10

Figure 9–2 Timer 1 Registers


PROGRAMMING 8051 TIMERS
11

 TMOD (timer mode) register


 timers 0 and 1 use TMOD register to set operation modes
(only learn Mode 1 and 2)
 8-bit register
 lower 4 bits are for Timer 0
 upper 4 bits are for Timer 1
 lower 2 bits are used to set the timer mode
 (only learn Mode 1 and 2)
 upper 2 bits to specify the operation
 (only learn timer operation)
PROGRAMMING 8051 TIMERS
12

TMOD (timer mode) register


Gate
 Every timer has a mean of starting and stopping.
 GATE=0
 Internal control
 The start and stop of the timer are controlled by way of software.
 Set/clear the TR for start/stop timer.
 GATE=1
 External control
 The hardware way of starting and stopping the timer by software and
an external source.
 Timer/counter is enabled only while the INT pin is high and the TR
control pin is set (TR).
PROGRAMMING 8051 TIMERS
14
Example 2.1

Find the value for TMOD? if we want to program timer 0


in mode 2, use 8051 XTAL for the clock source, and use
instructions to start and stop the timer.
Example 2.1

Find the value for TMOD if we want to program timer 0 in mode 2,


use 8051 XTAL for the clock source, and use instructions to start and
stop the timer.
Solution: # TMOD 02
timer 1 timer 0

TMOD= 0000 0010


• Timer 1 is not used.
• Timer 0, mode 2,
• C/T = 0 to use XTAL clock source (timer)
• gate = 0 to use internal (software) /start
and stop method.
PROGRAMMING 8051 TIMERS
17

 Clock source for timer


 timer needs a clock pulse to tick
 if C/T = 0, the crystal frequency attached to
the 8051 is the source of the clock for the
timer
 frequency for the timer is always 1/12th the
frequency of the crystal attached to the 8051
 XTAL = 11.0592 MHz allows the 8051 system
to communicate with the PC with no errors
 In our case, the timer frequency is 1MHz since
our crystal frequency is 12MHz
PROGRAMMING 8051 TIMERS
18

Mode 1 programming
 16-bit timer, values of 0000 to FFFFH
 TH and TL are loaded with a 16-bit initial value
 timer started by "SETB TR0" for Timer 0 and "SETB TR1"
for Timer l
 timer count ups until it reaches its limit of FFFFH
 rolls over from FFFFH to 0000H
 sets TFX (timer flag)
 when this timer flag is raised, can stop the timer with "CLR
TR0" or "CLR TR1“
 after the timer reaches its limit and rolls over, the registers TH
and TL must be reloaded with the original value and TF must
be reset to 0
Steps of Mode 1
(1/3)
1. Choose mode 1 timer 0
 MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
 MOV TH0,#FFH
 MOV TL0,#FCH
3. You had better to clear the flag to
monitor: TF0=0.
 CLR TF0
4. Start the timer.
 SETB TR0
Steps of Mode 1 (2/3)
5. The 8051 starts to count up by
incrementing the TH0-TL0.
 TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H

TR0=1 TR0=0
TH0 TL0
Start
timer
FFFC FFFD FFFE FFFF 0000
Stop
timer
TF = TF = TF = TF = TF =
TF 0 0 0 0 1

Monitor TF until
TF=1
Steps of Mode 1 (3/3)

6. When TH0-TL0 rolls over from FFFFH to


0000, the 8051 set TF0=1.
TH0-TL0= FFFEH, FFFFH, 0000H (Now TF0=1)
7. Keep monitoring the timer flag (TF) to see
if it is raised.
AGAIN: JNB TF0, AGAIN
8. Clear TR0 to stop the process.
CLR TR0
9. Clear the TF flag for the next round.
CLR TF0
PROGRAMMING 8051 TIMERS
22

Figure 9–5a Timer 0 with External Input (Mode 1)


PROGRAMMING 8051 TIMERS (for
information only)
23

Figure 9–5b Timer 1 with External Input (Mode 1)


PROGRAMMING 8051 TIMERS
24

 Steps to program in mode 1


 Set timer mode 1 or 2
 Set TL0 and TH0 (for mode 1 16 bit mode)
 Set TH0 only (for mode 2, 8 bit auto reload mode)
 Run the timer
 Monitor the timer flag bit
Example 2.2
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.
25 Timer 0 is used to generate the time delay
Timer Delay Calculation for XTAL = 11.0592 MHz

(a) in hex
 (FFFF – YYXX + 1) × 1.085 s

 where YYXX are TH, TL initial values respectively.

 Notice that values YYXX are in hex.

(b) in decimal
 Convert YYXX values of the TH, TL register to

decimal to get a NNNNN decimal number


 then (65536 – NNNNN) × 1.085 s
Example 2.3
Examine the following program and find the time delay
in seconds. Exclude the time delay due to the
instructions in the loop.
27
Example 2.4
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
28
to the timer setup instructions in the loop.
Find Timer Values

 Assume that XTAL = 11.0592 MHz .


 And we know desired delay
 how to find the values for the TH,TL ?
1. Divide the delay by 1.085 s and get n.
2. Perform 65536 –n
3. Convert the result of Step 2 to hex (yyxx )
4. Set TH = yy and TL = xx.
Example 2.5

 Assuming XTAL = 11.0592 MHz, write a program to


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

Solution:
1. The period of the square wave = 1 / 50 Hz = 20
ms.
2. The high or low portion of the square wave = 10
ms.
3. 10 ms / 1.085 s = 9216
4. 65536 – 9216 = 56320 in decimal = DC00H in
hex.
5. TL1 = 00H and TH1 = DCH.
Example 2.5
Assuming XTAL = 11.0592 MHz, write a program to generate a
square wave of 50 Hz frequency on pin P2.3.
32
PROGRAMMING 8051 TIMERS
33

 Generating a large time delay


 size of the time delay depends
 crystal frequency
 timer's 16-bit register in mode 1

 largest time delay is achieved by making both TH and


TL zero
 what if that is not enough?
Example 2.6

Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.
MOV TMOD,#10H
MOV R3,#200
AGAIN: MOV TL1,#08
MOV TH1,#01
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
DJNZ R3,AGAIN
Solution:
TH – TL = 0108H = 264 in decimal
65536 – 264 = 65272.
One of the timer delay = 65272 × 1.085 s = 70.820 ms
Total delay = 200 × 70.820 ms = 14.164024 seconds
Example
Examine the following program and find the time delay in seconds.
Exclude the time delay due to the instructions in the loop.
35
PROGRAMMING 8051 TIMERS
36

 Mode 0
 works like mode 1
 13-bit timer instead of 16­bit
 13-bit counter hold values 0000 to 1FFFH
 when the timer reaches its maximum of
1FFFH, it rolls over to 0000, and TF is set
PROGRAMMING 8051 TIMERS
37

 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
PROGRAMMING 8051 TIMERS
38

 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
Example 2.7
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.
39
PROGRAMMING 8051 TIMERS
40

 Assemblers and negative values


 can let the assembler calculate the value
for TH and TL which makes the job
easier
 "MOV TH1, # -100", the assembler will
calculate the -100 = 9CH
 "MOV TH1,#high(-10000) "
 "MOV TL1,#low(-10000) "
Example 2.8
Assuming that we are programming the timers for mode 2,
find the
value (in hex) loaded into TH for each of the following cases.
(a) MOV TH1,#-200 (b) MOV TH0,#-60 (c) MOV TH1,#-3
(d) MOV TH1,#-12 (e) MOV TH0,#-48

Solution:
Some 8051 assemblers provide this way.
-200 = -C8H  2’s complement of –200 = 100H – C8H = 38
H
2’s complement (TH
Decimal
value)
-200 = - C8H 38H
- 60 = - 3CH C4H
- 3 FDH
- 12 F4H
- 48 D0H
(MSB) (LSB)
TCON TF1 TR1
Timer 1
TF0 TR0
Timer0
IE1 IT1 IE0
for Interrupt
IT0

 Timer control register: TCON


 Upper nibble for timer/counter, lower nibble for interrupts
 TR (run control bit)
 TR0 for Timer/counter 0; TR1 for Timer/counter 1.
 TR is set by programmer to turn timer/counter on/off.
 TR=0: off (stop)
 TR=1: on (start)
 TF (timer flag, control flag)
 TF0 for timer/counter 0; TF1 for timer/counter 1.
 TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000 from FFFFH, the TF
is set to 1.
 TF=0 : not reach
 TF=1: reach
 If we enable interrupt, TF=1 will trigger ISR.
(MSB) (LSB)
TCON TF1 TR1
Timer 1
TF0 TR0
Timer0
IE1 IT1 IE0
for Interrupt
IT0
Equivalent Instructions for the Timer Control
Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4

SETB TF0 = SETB TCON.5


CLR TF0 = CLR TCON.5

For timer 1
SETB TR1 = SETB TCON.6
CLR TR1 = CLR TCON.6

SETB TF1 = SETB TCON.7


CLR TF1 = CLR TCON.7

TCON: Timer/Counter Control Register

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


COUNTER PROGRAMMING
45

Table 2–1 Port 3 Pins Used For Timers 0 and 1


Example 2.7
Assuming that clock pulses are fed into pin T1, write a program for counter 1
in mode 2 to count the pulses and display the state of the TL 1 count on P2.

Solution:
MOV TMOD,#01100000B ;mode 2, counter 1
MOV TH1,#0
SETB P3.5 ;make T1 input port
AGAIN:SETB TR1 ;start
BACK: MOV A,TL1
MOV P2,A ;display in P2
JNB TF1,Back ;overflow
CLR TR1 ;stop
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Example 2.7

 Timer 1 as an event counter fed into


pin3.5.
 “SETB P3.5” make P3.5 an input port by
making it high
8051
P2 is connected to 8 LEDs and
input T1 to pulse. P
2 to
LEDs
P3.5
T1
COUNTER PROGRAMMING
48
C/T PROGRAMMING
49

Figure 9–8 Timer/Counter 0


C/T PROGRAMMING
50

Figure 9–9 Timer/Counter 1


Interrupt Programming with
8051
Introduction to Interrupts
• An interrupt is an external or internal event that
interrupts the microcontroller to inform it that a
device needs its service.
• A set of program instructions written to service
an interrupt is called the Interrupt Service
Routine
• 8051 has six different sources of interrupts
External: Power-up reset, INT0, INT1
Internal: Timer0, Timer1, Serial Port
Interrupts vs Polling
53

 A single microcontroller can serve several devices.


 There are two ways to do that: interrupts and polling.
 In the interrupt method, 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.
 In polling method, the microcontroller continuously monitors
the status of a given device; when the status conditions met, it
performs the service. After that, it moves on to monitor the
next device until each one is serviced
Interrupt service routine
54

 For every interrupt, there must be an


interrupt service routine (ISR) or
interrupt handler. When an interrupt is
invoked, the microcontroller runs ISR.
 For ever 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 the interrupt vector
table.
Types of Interrupts
55

 There are two types of interrupts:


a) External interrupts
b) Internal interrupts
8051 has three external interrupts and
three internal interrupts
Interrupt Vector Table
56
Steps in executing an
57
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. 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
Interrupt Enable (IE)
Register

1
Programming IE
60
Example 2.8
61

Show the instructions to


(a) enable the serial interrupt, timer 0 interrupt, and external
hardware interrupt 1 (EX1),and
(b) disable (mask) the timer 0 interrupt, then
(c) show how to disable all the interrupts with a single instruction.
Example 2.8
62

Show the instructions to (a) enable the serial interrupt, timer 0


interrupt, and external hardware interrupt 1 (EX1),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
(b) CLR IE.1; mask (disable) timer 0 interrupt only

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


Programming Timer Interrupts

• If timer interrupt bit in IE register is enabled, whenever


the timer rolls over, TF flag is SET, and the 8051 is
interrupted.
• The interrupt service routine for timer can be placed at
– interrupt vector location if it is small enough, or
– elsewhere by using proper redirection at interrupt
vector location
Programming External Hardware
Interrupts
• 8051 has the following external interrupts
– RESET
– INT0
– INT1
• RESET is used for power-on reset
• Therefore there are two external interrupts INT0 &
INT1, that can be used by external hardware (devices)
to interrupt 8051
How interrupts are activated?

• There are two activation levels for the external


hardware interrupts
– Level triggered
– Edge triggered
• Level triggered interrupts is the default mode
upon RESET of the 8051
Level Triggered Interrupt

• INT0 and INT1 are normally held high


• If a low-level signal is applied to them, it triggers
the interrupt
Vcc

LED
Vcc
P1.3

INT0
How 8051 knows that an interrupt is
activated?

Ifthe hardware interrupts are enabled in the IE register, 8051


samples the INT 0/1 pin for a low level signal once each
machine cycle.
If during sampling 8051 senses the signal on INT0/1 to be low,
the interrupt is activated Vcc
Normally High

Interrupt LED
Activated
P1.3

Device INT0
What is the minimum duration for which
INT 0/1 pin must be held low for
interrupt to be activated?

How can we make sure that a single


interrupt is not interpreted as multiple
interrupts?
What is the minimum duration for which
INT 0/1 pin must be held low for
interrupt to be activated?
 The pin must be held low until the start
of the execution of Interrupt Service
Routine
 The minimum duration is 4 machine
cycles.

How can we make sure that a single


The interrupt
low-level on INT0/1
is notpin be brought back
interpreted as to high before
multiple
the interrupts?
execution of RETI instruction in Interrupt Service
Routine
Remember that…..

• There are two activation levels for the


external hardware interrupts
– Level triggered
– Edge triggered
• Level triggered interrupts is the default
mode upon RESET of the 8051
Edge Triggered Interrupts

We must program the bits of TCON register to make
interrupts edge-triggered

When a high-to-low signal is applied to INT0/1 pin, the
8051 will be interrupted
Vcc

LED

High-to-low P1.3
transition

Device INT0
Role of
TCON
Register
D0 IT0 TCON.0 0, INT0 becomes level-triggered interrupt
1, INT0 becomes edge-triggered
IE0
interrupt
IT1 TCON.2 0, INT1 becomes level-triggered interrupt
1, INT1 becomes edge-triggered
IE1
interrupt
TR0
To make INT0 edge-triggered the instruction
TF0 is SETB TCON.0

TR1
To make INT1 edge-triggered the instruction
D7 TF1
is SETB TCON.2
How Edge-triggered Interrupts are
activated and serviced by 8051?

• The falling edge on INT0/1 pins is latched by


8051, and held by the TCON register
• TCON.1(IE0) AND TCON.3(IE1) bits are used
by 8051 to keep track of edge-triggered
interrupts only
• When edge(high-to-low) transition takes place
on INT0/1 pin, 8051 does the following-
– Sets high IE0/1 bit in the TCON register
– Jumps to the interrupt vector location
– Executes the interrupt service routine

to be continued…
Role of TCON Register
D0 IT0

IE0 TCON.1 Set by CPU when external interrupt edge on


INT0 pin is detected
IT1

IE1 TCON.3 Set by CPU when external interrupt edge on


INT1 pin is detected
TR0

TF0 IE0/1 are also called Interrupt-in-service flags


TR1 When set to 1, indicates 8051 is executing an
interrupt service routine
D7 TF1
• At the end of service routine, RETI instruction is
executed
– This clears the IE0/1 interrupt-in-service flag
Programming Serial
Communication
Interrupts
Serial Communication Interrupt Flags
SCON
SM0 SM1 SM2 REN TB8 RB8 TI RI
Register

•TI(Transmit Interrupt): is raised when


last bit, i.e. the stop bit, is transferred
•RI(Receive Interrupt): is raised when
the entire frame of data, including the
stop bit is received
•In polling method, 8051 just waits for
the TI or RI flag to be raised, does
nothing else
SM0 SM1 SM2 REN TB8 RB8 TI RI
SCON
Bit Bit Indi Function Description
Po Na vid
sit me ual
io Add
n
Serial Mode
7 SM0 9FH Mode selection bit (MSB)
0
Serial Mode
6 SM1 9EH Mode selection bit (LSB)
1
Multiproces Enables multiprocessor communication in
5 SM2 9DH
sor Enable modes 2&3
Receive
4 REN 9CH 1=Enable reception, 0=Disable reception
Enable
Transmit Bit
3 TB8 9BH 9th data bit for transmission in modes 2&3
8
Receive Bit 9th received data bit in modes 2&3, stop bit in
2 RB8 9AH
8 mode 1
Transmit Hardware-set flag indicating transmission
1 TI 99H
Interrupt complete
Using Serial Interrupt with
8051
• In 8051 there is only one interrupt for both
serial data transmission and reception
• When TI or RI is raised by serial data
transfer, 8051 is interrupted, and jumps to
interrupt vector location 0023H to execute
the interrupt service routine(ISR)
• In ISR, 8051 must examine TI and RI flags
to determine which one caused the
interrupt
Interrupt Priority in 8051

What happens if two interrupts are


activated at the same time?
Which of these two interrupts is serviced
first?
– The interrupt which has the highest priority is
serviced first
– By default, 8051 assigns a priority level to all
interrupts upon RESET
What happens if interrupts INT0, TF0, INT1 are
activated at the same time? Assume default priority
levels and edge-triggered external interrupts.

• They are latched and kept internally by 8051


• Then 8051 polls all interrupts according to the
default priority levels
• If any interrupt is activated, it is serviced in that
sequence.
• Therefore IE0(INT0) is serviced first, then
TF0(timer0), and finally IE1(INT1)
Interrupt Priority Register
D7 D0
IP
-- -- PT2 PS PT1 PX1 PT0 PX0
Register
Priority bit = 1, assigns high priority
0, assigns low priority

PS Serial Port Priority bit


PT1 Timer1 interrupt priority bit
External interrupt 1 priority bit
PX1 Timer0 interrupt priority bit
External interrupt 0 priority bit
Assume that after RESET, the Interrupt priority is
set by the instruction MOV IP, 00001100B
Discuss the sequence in which the interrupts are
serviced.
What happens if the 8051 is executing the ISR of an
interrupt and another higher priority interrupt is
activated?
Upon RESET all interrupts have the same priority.
(T/F)
Assume that after RESET, the Interrupt priority is
set by the instruction MOV IP, 00001100B

This sets External INT1 (PX1) and Timer 1


(PT1) interrupts to high priority
, while External INT0 (PX0), Timer 0 (PT0), Serial
Port (PS) and remain at low priority
Discuss the sequence in which the interrupts are
serviced.

1. Interrupt Service Sequence


The 8051 follows a two-level priority system with high and low priority
interrupts. The service sequence is determined by:
High Priority Interrupts (Priority Level 1)
• External INT1

• Timer 1 (high priority due to PT1=1)

Low Priority Interrupts (Priority Level 0)


• External INT0 (natural highest priority among low-priority
interrupts)
• Timer 0
• Serial Port
What happens if the 8051 is executing the ISR of an
interrupt and another higher priority interrupt is
activated?

When the 8051 is executing an ISR and a higher priority interrupt is


activated:
•The current ISR is immediately suspended
•The processor saves the current context (program counter and processor
status)
•Jumps to the higher priority interrupt vector and executes its ISR
•After completing the higher priority ISR with RETI, execution returns to
the suspended lower priority ISR
•The suspended ISR continues from where it was interrupted
Upon RESET all interrupts have the same priority.
(T/F)

After reset, the IP register is initialized to 00000000B, meaning:


•All interrupt priority bits (PX0, PT0, PX1, PT1, PS) are cleared to 0
•All interrupts are assigned low priority by default
•The natural polling sequence determines service order
when multiple interrupts occur simultaneously
89
Serial Communication using Polling
A program to transfer character ‘A’ serially at 4800 baud,
continuously
ORG 0H
MOV TMOD, #20H ;timer 1, mode 2
MOV TH1, #-6 ;set baud rate to 4800
MOV SCON, #50H ;serial mode 1
SETB TR1 ;start timer 1

AGAIN: MOV SBUF, #’A’ ; send character ‘A’ for ;transmission

HERE: JNB TI, HERE ;wait until TI flag is set

CLR TI ;clear TI to transmit next char


SJMP AGAIN
92
Methods of communication
96
SCON register
101
8051-INTERFACING WITH 7
SEGMENT DISPLAY
8051 Interfacing 7 Segment
Prepared By: Ms. K. D. Patil 14

Display
• 7 segment display is a 10 pin electronic component with eight
led’s embedded inside it to displays number from 0 to 9 even we
can display few characters like A, B, C, H, E, e, F, etc. .
• It is available in two configurations:
• common cathode
• common anode
• In Common Anode to light up any given segment the
corresponding Cathode pin should be connected to ground or
reference ,which completes the circuit and LED will be in
forward bias.
• In Common Cathode to corresponding LED Anode pin the
circuit will be completed and the particular LED will be light up
since it will be in forward bias.
Prepared By: Ms. K. D. Patil 15

8051 Interfacing 7 Segment


Display
• A seven segment display consists of seven
LEDs arranged in the form of a “squarish
eight” slightly inclined to the right and a
single LED as the dot character.
• Different characters can be displayed by
selectively glowing the required LED
segments.
Prepared By: Ms. K. D. Patil 16
8051 Interfacing 7 Segment
Display
8051 INTERFACING WITH
ADC
Prepared By: Ms. K. D. Patil 17

8051 Interfacing
ADC
• Digital computers use binary values but
in physical world everything is Analog
(continuous) eg. Temperature, Pressure
• A physical quantity is converted into
electrical (voltage, current) signals using
a device called as Transducer (also
referred as Sensors).
• We need Analog-to-Digital converter
(ADC) to translate Analog signals to
Digital number so that Microcontroller
can read and process them.
• ADC0808 allows us to monitor upto 8
different analog input channels using
single chip
Prepared By: Ms. K. D. Patil 18

ADC080
8
Prepared By: Ms. K. D. Patil 19

Address Lines
(A,B,C)
• ADC0808 has 8-bit
Selected ADC
channel
C B A data output
IN0 0 0 0 • 8 analog input channels
IN1 0 0 1 are multiplexed and
IN2 0 1 0
selected using three
IN3 0 1 1
IN4 1 0 0
address pins A, B & C.
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1
Prepared By: Ms. K. D. Patil 20

Signals for A/D


Conversion
• Address Latch Enable (ALE): A LOW-TO-HIGH signal at this
pin will latch the above-selected address and selected the
respective channel for ADC conversion.
• START Conversion (SC): The A/D converter‟s successive
approximation register (SAR) is reset on the positive edge of the
start conversion (SC) pulse. Thus we need to generate a LOW-
HIGH pulse for starting the ADC conversion.
• End of Conversion (EOC): Once the conversion is over, this pin
is pulled HIGH by ADC0808. This pin needs to be monitored for
the conversion to complete and then read the data.
• Output Enable(OE): ADC0808 does the A/D conversion and
holds the data in the internal registers. A HIGH signal on this pin
will bring the data on the output lines.
• D0-D7 are the digital Data output lines.
Prepared By: Ms. K. D. Patil 22

8051 Interfacing
ADC
Prepared By: Ms. K. D. Patil 21

8051 Interfacing ADC

If the clock frequency is high D Flip Flop is added to reduce the frequency by
2^(N), N- no of flipflops
Prepared By: Ms. K. D. Patil 23

8051 Interfacing Temperature


Sensor (LM34/LM35)
• Temperature is converted to electrical signals using a
transducer called as Thermistor.
• Simple and widely used Temperature sensors include
LM34 & LM35 from Semiconductor Corp.
• LM34 does not require external calibration since it is
internally calibrated.
• LM35 are precision integrated circuit temperature sensors
whose output voltage is linearly proportional to Celsius
temp. It does not require external calibration since it is
internally calibrated. It outputs 10mV for each degree of
centigrade temperature.
Prepared By: Ms. K. D. Patil 24

Signal
Conditioning
Analog World
• Sensors produce the output in the (temperature, pressure)

form of voltage, current, charge,


resistance & capacitance. Transducer (Sensor)
• But, we need to convert these
signals to voltage in order to send
input to ADC. Signal Conditioning
• This conversion is called as
Signal conditioning.
• Signal conditioning can be current ADC
to voltage conversion or signal
amplification.
Microcontroller
Prepared By: Ms. K. D. Patil 25

Connecting LM35 to
ADC
Temperature Vs Vout for
ADC Since ADC has 8 bit
resolution with a max of
Temp (C) Vin (mV) Vout (D7-
D0)
256 steps and LM35
produces 10mV for every
0 0 0000 0000
degree of temperature
1 10 0000 0001
change, we can condition
2 20 0000 0010 Vin of the ADC to produce
3 30 0000 0011 a Vout of 2.56Vfor full
10 100 0000 1010 scale output. So Vref= 2.56
30 300 0001 1110
Prepared By: Ms. K. D. Patil 26

Interfacing
Diagram
8051 INTERFACING WITH DAC
Prepared By: Ms. K. D. Patil 27

8051 Interfacing
DAC
• Microcontroller are used in wide variety of applications like for
measuring and control of physical quantity like temperature,
pressure, speed, distance, etc.
• In these systems microcontroller generates output which is in
digital form but the controlling system requires analog signal as
they don't accept digital data thus making it necessary to use
DAC which converts digital data into equivalent analog voltage
• Digital to Analog Converter is a device used to convert digital
pulses to analog signals.
• In the figure shown, we use 8-bit DAC 0808. This IC converts
digital data into equivalent analog Current. Hence we require an I
to V converter to convert this current into equivalent voltage.
• DAC0808 provides 256 discrete voltage (or current) levels of
output.
Prepared By: Ms. K. D. Patil 28

DAC08
08
• In the MC1408 (DAC0808), the digital inputs are converted
to current (Iout), and by connecting a resistor to the I out pin,
we convert the result to voltage.
• The total current provided by the I out pin is a function of the
binary numbers at the D0 – D7 inputs of the DAC0808 and
the reference current (Iref), and is as follows:
• where D0 is the LSB, D7 is the MSB for the inputs, and I ref
is the input current that must be applied to pin 14. The I ref
current is generally set to 2.0 mA.
Prepared By: Ms. K. D. Patil 29
Prepared By: Ms. K. D. Patil 30

Interfacing
Diagram
125

You might also like