Microprocessors and Microcontrollers
Unit III Part 5
8051 Real-time control
B.Tech, ETM,
II Year, II Semester
N.Ramakrishna
Dept. of Electronics and Telematics
GNITS, Hyderabad
15 June 2022 MPMC- N.Ramakrishna
Syllabus
Unit IV
8051 Real Time Control: (Chapter 13, Lyla B. Das)
• Timer/ Counter
• Programming 8051 timers and counters
• Interrupts
• Programming Timer Interrupts
• Programming external hardware interrupts
• Serial communication
• Programming the serial communication
interrupts
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
8051 Inerrupts
Six interrupts
(i) Two external hardware interrupts EX0, EX1
(Pins INT0, INT1 )
(ii) Two timer interrupts for Timer 0 and Timer 1
(iii) One interrupt for serial communication
- Serial transmission and reception
(iv) Reset
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
Interrupt vectors of 8051
Sl. No. Interrupt Interrupt vector Pin
(Hex)
1 Reset 0000 9
2 External (EX0) 0003 12 (P3.2)
3 Timer0 (TF0) 000B ---
4 External (EX1) 0013 13 (P3.3)
5 Timer1 (TF1) 001B ---
6 Serial 0023 ---
• Vectored interrupts, Only 8 bytes for each vector
• Jump instruction points to ISR location
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
Example 13.13
Avoid using ROM locations
where interrupt vectors are located
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
Example 13.13
Avoid using ROM locations
where interrupt vectors are located
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
Example 13.13
• ORG 0 Startup location 0
• Overlaps with Interrupt vector locations
• Use SJMP, AJMP, LJMP
– To transfer to another location
• ISR_T0 written in program starting from 40H
• From main, when Timer 0 interrupt occurs,
control transfers to ISR_T0
• ISR executed
• On instruction RETI returns control to main.
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
Enabling interrupts: Interrupt Enable (IE) register
Name Notation Function performed
EA IE.7 EA =1 enables all interrupts, but individual
activation is also necessary
- IE.6 Unused
- IE.5 Unused
ES IE.4 ES = 1 enables serial communication
interrupt
ET1 IE.3 ET1 = 1 enables timer interrupt 1
EX1 IE.2 EX1 = 1 enables external interrupt 1
ET0 IE.1 ET0 = 1 enables timer interrupt 0
EX0 IE.0 EX0 = 1 enables external interrupt 0
15 June 2022 MPMC- N.Ramakrishna
8051 Interrupts
Enabling interrupts
• On startup, no interrupt is enabled
• Interrupt enable (IE) register to be loaded with
appropriate configuration
• IE register – bit addressable
• Both EA and other bit corresponding to Interrupt
must be set
Example:
SETB IE.7 ; Enable all
SETB IE.1 ; Enable Timer 0 interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 9
8051 Interrupts
Example 13.14
Write control word enabling the two timer interrupts
and the two external interrupts.
Answer:
IE register bit configuration = 10001111 = 8FH
Instruction :
MOV IE, #8FH
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 10
8051 Interrupts
Using Timers in Interrupt mode
• Polling method was used before
• Timer flag is checked
• Interrupt driven method
• Timer flag set
• Interrupt activated
• Control goes to interrupt vector location
• ISR is executed
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 11
8051 Interrupts
Example 13.15 – Timers in interrupt mode
Generate square wave of frequency 1.9 KHz (at pin
P2.4) using Timer 0 in Mode 2.
Solution: Timer 0 interrupt to be set, IE register = 82H
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 12
8051 Interrupts
Timer 0 used in Interrupt driven mode
(i) Write IE register with control word
(ii) Load timer register with appropriate words and
start timer
(iii) When TF0 set, interrupt occurs,
program jumps to 000BH
(iv) ISR executed,
RETI – returns control to main
(v) Once control branched to interrupt vector,
TF0 automatically cleared
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 13
8051 Interrupts
Example 13.16 - To do a task instead of waiting.
Generate a square wave on P2.4 and toggle P2.0
depending on data coming through Port 0 and Port 1
Solution: Same as Ex. 13.15, compares data
in two ports while waiting for interrupt to occur.
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 14
8051 Interrupts
Example 13.16 - To do a task instead of waiting
Solution:
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 15
8051 Interrupts
Example 13.16
- To do a task instead of waiting
- Generates a square wave on P2.4
- Compares two numbers from Port 1 and Port 0
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 16
8051 Interrupts
Example 13.17 Self study
A symmetric square wave to be generated using
Mode 1, which needs the count to be reloaded after
each cycle. Uses the value delay in Example 13.5.
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 17
8051 Interrupts
Example 13.17 - Self study
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 18
8051 Interrupts
Example 13.17 - Self study
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 19
8051 Interrupts
Example 13.18
Generate a square wave with different ON time and
OFF time on P1.3 using Timer 1 in the interrupt mode
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 20
8051 Interrupts
Example 13.18 - continued
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 21
8051 Interrupts
Example 13.19
Generate Two waveforms with different periods
simultaneously from pins P2.0 and P2.1.
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 22
8051 Interrupts
Example 13.19 - Solution
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 23
8051 Interrupts
Example 13.19 - Solution
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 24
8051 Interrupts
Example 13.19 – Solution
Waveform
Generated by
T0 interrupt
Waveform
Generated by
T1 interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 25
8051 Interrupts
External hardware interrupts
• Two hardware interrupts
EX0 - Pin P3.2 (IC Pin 12)
EX1 - Pin P3.3 (IC Pin 13)
• On reset,
these pins are high
• Pins respond to low signal for interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 26
8051 Interrupts
Example 13.20
When push button switch is pressed, EX0 goes low
LED turns ON and OFF
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 27
8051 Interrupts
Example 13.20 – Solution
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 28
8051 Interrupts
Edge triggering of external interrupts
TCON register
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 29
8051 Interrupts
Example 13.21 – Edge triggering
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 30
8051 Interrupts
Example 13.22 – Self study
A Square wave of 1 Hz frequency applied to P3.3.
At High to Low edge of square wave, Output to LEDs
connected to Port 1 complemented
P2.1 connected to relay is also complemented
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 31
8051 Interrupts
Example 13.22 – Self study
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 32
8051 Interrupts
Example 13.22 – Self study
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 33
8051 Serial communication
Serial communication
• 8051
– Inbuilt UART
• Used to communicate between two microcontrollers
or a microcontroller and a PC
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 34
8051 Serial communication
8051 Serial transmission
TxD
Pin 11, P3.1
TI
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 35
8051 Serial communication
Serial transmission
• To send a byte, byte is moved to serial buffer (SBUF)
• Framing bits added
• UART converts parallel data into serial data
• Sent through TxD (Pin: 11, P3.1)
at baud rate
• Baud rate fixed by Timer 1 in Mode 2
• When transmission of a byte is over,
Transmit Interrupt (TI) is activated
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 36
8051 Serial communication
8051 Serial reception
Pin 10, P3.0
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 37
8051 Serial communication
Serial reception
• Reverse of transmission process
• Data received serially through RxD pin (Pin 10, P3.0)
• Converted to parallel form,
stripped off the framing bits
• Brought to SBUF (serial buffer)
• Can be moved to Register A
• When reception completed,
Receive Interrupt (RI) flag is set
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 38
8051 Serial communication
Connections between a microcontroller and PC
using RS 232
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 39
8051 Serial communication
RS 232C
• For transmission over long distances
RS 232 cable is used
• Data converted to RS 232 format using IC MAX 232
• Figure shows part of the embedded board
with MAX 232 IC, Microcontroller unit,
connections to serial data pins of MCU,
DB-9 male connector of serial port
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 40
8051 Serial communication
Generation of PC compatible baud rates
/32
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 41
8051 Serial communication
Baud rate setting
• PC has certain standard baud rates
• 8051 baud rate should be the same
• 8051 crystal frequency = 11.0592 MHz
• Timer 1 in Mode 2 used for setting baud rate
• Basic frequency for timer= Crystal frequency / 12
= 11.0592 MHz /12 = 921.6 KHz
• UART frequency = Basic frequency/32
= 921.6 KHz/32 = 28,800 Hz
• Sent to Timer 1 for setting baud rate
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 42
8051 Serial communication
Generation of PC compatible baud rates
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 43
8051 Serial communication
Baud rate setting
• Table 13.3 lists the hex values to be loaded into TH1
• Consider FDH loaded into TH1
• Count increases to FEH, FFH, 00
• Thus 28,800/3 = 9600 Hz
= Frequency of the signal generated by Timer 1
• Baud rate = reference frequency of
serial transmission
• Baud rate of PC to be changed
to match that of Microcontroller
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 44
8051 Serial communication
Registers in UART
(i) SBUF (Serial buffer)
• 8-bit SFR register, holds the data to be transmitted
MOV SBUF, A
MOV SBUF, #data
For reception
MOV A, SBUF
(ii) SCON (Serial control register)
• 8-bit register,
used to configure the serial communication
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 45
8051 Serial communication
Bit configuration of SCON register
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 46
8051 Serial communication
Bit configuration of SCON register
TI
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 47
8051 Serial communication
UART - Modes
• SM0, SM1 = mode bits
• 4 possible modes
• Mode 1 discussed – 1 start and 1 stop bit
Total 10 bits including 8-bit data
• Compatible with COM port of PC
Transmit Interrupt (TI) flag
Data loaded in SBUF, Frame bits added ,
converted to serial data, transmitted through TxD
TI flag raised when stop bit being sent
New byte can be moved into SBUF
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 48
8051 Serial communication
Steps in Serial data transmission
(i) Configure Timer 1 in Mode 2 to generate baud rate
(ii) Configure SCON register for Mode 1
(iii) Start Timer 1
(iv) Move data byte to SBUF
(v) Monitor TI flag to verify transmission is over
(vi) Data sent through TxD pin at baud rate
(vii) Clear TI
(viii) To transmit next byte, go to step (iv)
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 49
8051 Serial communication
Example 13.23a
Serial data transmission using polling mechanism
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 50
8051 Serial communication
Example 13.23b
Serial data transmission using Transmission Interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 51
8051 Serial communication
Example 13.23b
Serial data transmission using Transmission Interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 52
8051 Serial communication
Serial Reception
• Pin RxD used (Pin 10, P3.0)
• Data received : Start bit , data byte, stop bit
• When Stop bit received,
Receive interrupt (RI) flag set
• RI flag indicates byte available in SBUF
and can be moved to A and stored somewhere
• Setting RI bit activates serial interrupt,
if interrupt is enabled
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 53
8051 Serial communication
Steps in Serial data reception
(i) Configure Timer 1 in Mode 2 to generate baud rate
(ii) Configure SCON register for Mode 1
(iii) Start Timer 1
(iv) Clear RI
(v) Monitor RI flag to verify transmission is over
(vi) When RI raised, Copy SBUF to A and store it
(vii) To receive next byte, go to step (iv)
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 54
8051 Serial communication
Example 13.24a
Serial data reception using polling
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 55
8051 Serial communication
Example 13.24b
Serial data reception using interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 56
8051 Serial communication
Example 13.24b
Serial data reception using interrupt
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 57
Text Books
TEXT BOOKS:
1. Advanced Microprocessors and Peripherals – A. K.
Ray and K.M. Bhurchandi, Tata-McGraw Hill,
2nd edition 2006.
2. D. V. Hall, Microprocessors and Interfacing,
Tata-McGraw Hill, 2nd edition 2006.
3. Kenneth. J. Ayala, The 8051 microcontroller ,
3rd edition, Cengage learning, 2010
4. Microprocessors and Microcontrollers, Lyla B.Das,
Pearson, 2012
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 58
References
REFERENCES:
1. The 8051 Microcontrollers, Architecture and programming and
Applications -K. Uma Rao, Andhe Pallavi, Pearson, 2009.
2. The 8051 Microcontoller and Embedded Systems Using
Assembly and C, Second Edition, Muhammad Ali Mazidi, Janice
Gillispie Mazidi, Rolin D. McKinlay, Prentice Hall
3. Micro Computer System 8086/8088 Family Architecture,
Programming and Design - Liu and GA Gibson,
Prentice Hall India, 2nd Ed.
4. Microcontrollers and application, Ajay. V. Deshmukh,
Tata-McGraw Hill, 2005
5. The 8085 Microprocessor: Architecture, programming and
Interfacing – K. Uday Kumar, B.S. Umashankar, 2008, Pearson
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 59
References
Websites:
http://www.win.tue.nl/~aeb/comp/8051/set8051.html
http://www.keil.com/support/man/docs/is51/is51_cjne.htm
http://www.atmel.com/images/doc0509.pdf
http://radio.feld.cvut.cz/personal/matejka/download/it8051.pdf
15 June 2022 MPMC- N.Ramakrishna Unit IV Part 5 60