Microcontroller & Applications - ModuleIII-2023
Microcontroller & Applications - ModuleIII-2023
SERIAL COMMUNICATION
Module III
Baiju.G.S
Lecturer in Electronics Eng
CO3 : Explain interrupts, timer and serial communication in 8051.
M3.03: Illustrate TCON register and time delay calculation for Timer. 3
Understanding
3/15/2023 Baiju G S 3
8051
Interrupts
3/15/2023 Baiju G S 4
3/15/2023 Baiju G S 5
Interrupts Programming
3/15/2023 Baiju G S 6
3/15/2023 Baiju G S 7
Steps in executing an interrupt
• Finish current instruction and saves the PC on stack.
• Jumps to a fixed location in memory depend on type
of interrupt
3/15/2023 Baiju G S 8
Interrupt Sources
• Original 8051 has 6 sources of interrupts
• - Reset
– Timer 0 overflow
– Timer 1 overflow
– External Interrupt 0
– External Interrupt 1
– Serial Port events (buffer full, buffer empty, etc)
3/15/2023 Baiju G S 10
3/15/2023 Baiju G S 11
Redirecting the 8051 from the
Interrupt Vector Table at Power-up
• 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. Figure below shows the IE
register. Note that IE is a bit-addressable register.
3/15/2023 Baiju G S 12
Interrupt Enable (IE) register
All interrupt are disabled after reset
We can enable and disable them bye IE
Steps in enabling an interrupt
IP.7: reserved
IP.6: reserved
IP.5: timer 2 interrupt priority bit(8052 only)
IP.4: serial port interrupt priority bit
IP.3: timer 1 interrupt priority bit
IP.2: external interrupt 1 priority bit
IP.1: timer 0 interrupt priority bit
IP.0: external interrupt 0 priority bit
3/15/2023 Baiju G S 17
Interrupt Priorities Example
--- --- PT2 PS PT1 PX1 PT0 PX0
3/15/2023 Baiju G S 18
Interrupt inside an interrupt
--- --- PT2 PS PT1 PX1 PT0 PX0
3/15/2023 Baiju G S 19
8051
Timers / Counters
3/15/2023 Baiju G S 20
The 8051 has two counters/timers which can be used
either as timer to generate a time delay or as counter to
count events happening outside the microcontroller.
5
Difference between a Timer and a Counter
Timer
• Input from internal system clock
Counters:
• Show the number of events on registers
• External input from T0 input pin (P3.4) for Counter 0
3/15/2023 Baiju G S 25
Timer/Counter
Special Function Registers
3/15/2023 Baiju G S 28
TMOD Register
3/15/2023 Baiju G S 34
GATE (cont.)
• The hardware way of starting and
stopping the timer by an external source
is achieved by making GATE=1 in the
TMOD register
3/15/2023 Baiju G S 35
TCON Register
3/15/2023 Baiju G S 36
3/15/2023 Baiju G S 37
• TCON.7 - TF1 Timer1 over flow flag
Set when timer rolls from all 1s to 0. Cleared
When the processor vectors to execute interrupt
service routine Located at program address
001Bh.
• TCON.6- TR1 Timer 1 run control bit
Set to 1 by programmer to enable timer to
count; Cleared to 0 by program to halt timer.
. TCON.5 -TF0 Timer 0 over flow flag. Same as TF1.
. TCON.4- TR0 Timer 0 run control bit. Same as TR1
3/15/2023 Baiju G S 38
• TCON.3 -IE1 External interrupt 1 Edge flag
Not related to timer operations.
. TCON.2 -IT1 External interrupt1 signal type control bit
Set to 1 by program to Enable external
interrupt 1 to be triggered by a falling edge signal.
Set To 0 by program to enable a low level signal
on external interrupt1 to generate an interrupt.
. TCON.1 IE0 External interrupt 0 Edge flag
Not related to timer operations.
. TCON.0 – IT0 External interrupt 0 signal type control bit
Same as IT1
3/15/2023 Baiju G S 39
Timer/Counter
Special Function Registers-TCON
3/15/2023 Baiju G S 41
Timer modes
• The timers may operate in one of four modes:
• Mode 0
• Mode 1
• Mode 2
• Mode 3
3/15/2023 Baiju G S 42
Timer Modes
Mode 0
Mode 0 is identical for Timer0 and Timer1
In this mode, the timers act as 13 bit counters. It is largely
meant for providing compatibility with an older
microcontroller from intel (8048). This mode is practically
never used in fresh designs. Except for the counter size,
this mode is identical to mode 1.
Timers work as 13-bit counters, an interrupt is
generated when counter overflows. It takes
8192 input pulses to generate the next
interrupt
Timers use 8 bits of THi and 5 lower bits of TLi
After timer overflows TFi (Timer Flag in TCON)
is set, an interrupt occurs Where i=0 or 1
Timer Modes
Mode 0
Similar to mode 0
Improved capacity
Timer Modes
Mode 1
3/15/2023 Baiju G S 50
Mode 3
Structure of Timer 0 in mode 3:
Timer Modes
Example
3/15/2023 Baiju G S 54
• Program for generating 1mS delay using 8051
timer.
3/15/2023 Baiju G S 55
• DELAY: MOV TMOD,#00000001B // Sets Timer
0 to MODE1 (16 bit timer). Timer 1 is not used
• MOV TH0,#0FCH // Loads TH0 register with FCH
• MOV TL0,#018H // LOads TL0 register with 18H
• SETB TR0 // Starts the Timer 0
• HERE: JNB TF0,HERE // Loops here until TF0 is
set (ie; until roll over)
• CLR TR0 // Stops Timer 0
• CLR TF0 // Clears TF0 flag
• RET
3/15/2023 Baiju G S 56
The above delay routine can be looped twice in order to get a 2mS
delay and it is shown in the program below.
Sender Receiver
Baiju G S
3/15/2023 59
Serial vs Parallel communication
Baiju G S 60
3/15/2023
Serial v/s Parallel Communication
3/15/2023 Baiju G S 61
Basics of Serial Communication
• Serial communication uses single data line making it much cheaper
• Enables two computers in different cities to communicate over the
telephone
• Byte of data must be converted to serial bits using a parallel-in-serial-
out shift register and transmitted over a single data line
• At the receiving end there must be a serial-in-parallel-out shift
register
• If transferred on the telephone line, it must be converted to audio
tones by modem for short distance
3/15/2023 Baiju G S 62
Modes of Serial Communication
Baiju G S 63
3/15/2023
Modes of Serial Communication
• In simplex transmissions, the computer can only send or receive
data. There is only one wire.
• If the data can be transmitted and received, then it is a duplex
transmission
• Duplex transmissions can be half or full duplex depending on
whether or not the data transfer can be simultaneous
• If the communication is only one way at a time, it is half duplex
• If both sides can communicate at the same time, it is full duplex
Full duplex requires two wire conductors for the data lines
(in addition to the signal ground)
3/15/2023 Baiju G S 64
Basics of Serial Communication
• Serial Communication can be
Asynchronous
Synchronous
Synchronous Communication
• Synchronous methods transfer a block of data (characters) at a time
• The events are referenced to a clock
• Example: SPI bus, I2C bus
Asynchronous Communication
• Asynchronous methods transfer a single byte at a time
• There is no clock. The bytes are separated by start and stop bits.
• Example: UART
3/15/2023 Baiju G S 65
Basics of Serial Communication cont..
• To support serial communication, special interfaces are built in the
microcontroller.
• The microcontrollers use special IC chips called UART (universal
asynchronous receiver-transmitter) and USART (universal
synchronous asynchronous receiver-transmitter)
• 8051 chip has a built-in UART
3/15/2023 Baiju G S 66
TxD and RxD in 8051
• 8051 has two pins that are used specifically for transferring and
receiving data serially
These two pins are called TxD and RxD and are part of the
port 3 group (P3.0 and P3.1)
These pins are TTL compatible; therefore, they require a line
driver to make them RS232 compatible
• We need a line driver (voltage converter) to convert the R232’s
signals to TTL voltage levels that will be acceptable to 8051’s TxD
and RxD pins
3/15/2023 Baiju G S 67
Baud rate
• The baud rate is the rate at which
information is transferred in a
communication channel. Baud rate is
commonly used when discussing
electronics that use serial
communication. In the serial port context,
"9600 baud" means that the serial port is
capable of transferring a maximum of
9600 bits per second
3/15/2023 Baiju G S 68
Setting Baud rate in 8051
• Baud rate in the 8051 is programmable
Relationship between the crystal frequency and the baud rate in
the 8051
8051 divides the crystal frequency by 12 to get the machine cycle
frequency
XTAL = 11.0592 MHz, the machine cycle frequency is 921.6 kHz
8051's UART divides the machine cycle frequency of 921.6 kHz by
32 once more before it is used by Timer 1 to set the baud rate 921.6
kHz divided by 32 gives 28,800 Hz
3/15/2023 Baiju G S 69
Setting Baud rate in 8051
• Timer 1 must be programmed in mode 2, that is 8-bit, auto-reload
3/15/2023 Baiju G S 70
Setting Baud rate in 8051
• Timer 1 must be programmed in mode 2, that is 8-bit, auto-reload
3/15/2023 Baiju G S 71
SBUF Register
Deframed 5
GND
Baiju G S
3/15/2023 72
SBUF Register cont..
• A byte of data to be transferred via the TxD line must be placed in the
SBUF register
• SBUF holds the byte of data when it is received by the RxD line
• SBUF can be accessed like any other register
When a byte is written in SBUF, it is framed by 8051 with the start and stop
bits and transferred serially via the TxD pin
3/15/2023 Baiju G S 73
SBUF Register cont..
When the bits are received serially via RxD, it is deframed by 8051 by
eliminating the stop and start bits, making a byte out of the data
received, and then placing it in the SBUF
Framing need not be done by programmer explicitly
• The special function register SBUF is physically two registers.
One is, write-only and is used to hold data to be transmitted out of
the 8051 via TXD.
The other is, read-only and holds the received data from external
sources via RXD.
• Both mutually exclusive registers have the same address 099H.
• SBUF is not bit addressable
3/15/2023 Baiju G S 74
SCON : Serial Control Register
Bit Addressable Register
3/15/2023 Baiju G S 75
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.
3/15/2023 Baiju G S 76
SM2
• n mode-2 and mode-3 the SM2 bit is used to
enable multiprocessor communication. In
multiprocessor communication the serial port
of a number of microcontrollers can be
connected to a common serial bus. One
controller will act as a master and all other
controller will act as slave.
3/15/2023 Baiju G S 77
REN
• 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
3/15/2023 Baiju G S 78
TI and RI Flags
TI (transmit interrupt)
• When 8051 finishes the transfer of the 8-bit character, it raises the
TI flag to indicate that it is ready to transfer another byte
• TI bit is raised at the beginning of the stop bit
RI (receive interrupt)
• When the 8051 receives data serially via RxD, it places the byte in
the SBUF register then 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
3/15/2023 Baiju G S 79
TB8 and RB8
• TB8 – The 9th bit that will be transmitted
in mode 2 and mode 3 , for an
information . set/clear by software.
3/15/2023 Baiju G S 80
Serial Data Transmission Modes
Mode 0
• In this mode, the serial port works like a shift register and the
data transmission works synchronously with a clock frequency of
fosc /12.
• Serial data is received and transmitted through RXD.
• 8 bits are transmitted/ received at a time.
• Pin TXD outputs the shift clock pulses of frequency fosc /12, which
is connected to the external circuitry for synchronization.
• The shift frequency or baud rate is always 1/12 of the oscillator
frequency.
3/15/2023 Baiju G S 81
Serial Data Transmission Modes
Mode 1
• In mode-1, the serial port functions as a standard Universal
Asynchronous Receiver Transmitter (UART) mode.
• 10 bits are transmitted through TXD or received through RXD.
• The 10 bits consist of one start bit (which is usually '0'), 8 data
bits (LSB is sent first/received first), and a stop bit (which is
usually '1').
• Once received, the stop bit goes into RB8 in the special function
register SCON. The baud rate is variable.
3/15/2023 Baiju G S 82
Serial Data Transmission Modes
Mode 2
• In this mode 11 bits are transmitted through TXD or received through
RXD.
• The various bits are as follows: a start bit (usually '0'), 8 data bits (LSB
first), a programmable 9 th (TB8 or RB8)bit and a stop bit (usually '1').
• While transmitting, the 9 th data bit (TB8 in SCON) can be assigned the
value '0' or '1'.
For example, if the information of parity is to be transmitted, the
parity bit (P) in PSW could be moved into TB8. On reception of the data,
the 9th bit goes into RB8 in 'SCON', while the stop bit is ignored.
3/15/2023 Baiju G S 83
Serial Data Transmission Modes
Mode 3
• In this mode 11 bits are transmitted through TXD or received
through RXD.
• The various bits are: a start bit (usually '0'), 8 data bits (LSB first),
a programmable 9 th bit and a stop bit (usually '1').
• Mode-3 is same as mode-2, except the fact that the baud rate in
mode-3 is variable (i.e., just as in mode-1).
• f baud = (2 SMOD /32) * ( fosc / 12 (256-TH1)) .
.
3/15/2023 Baiju G S 84
PCON register and Power Saving Modes in 8051
Baiju G S 85
3/15/2023
PCON register and Power Saving Modes in 8051
3/15/2023 Baiju G S 86
PCON register and Power Saving Modes in 8051
Idle Mode
• Upon the IDL bit of the PCON register is set, the microcontroller turns
off the CPU unit while peripheral units such as serial port, timers and
interrupt system continue operating normally
• In Idle mode, the state of all registers and I/O ports remains
unchanged.
• In order to exit the Idle mode and make the microcontroller
operate normally, it is necessary to reset.
• It will cause the IDL bit to be automatically cleared and the program
resumes operation from instruction having set the IDL bit.
3/15/2023 Baiju G S 87
PCON register and Power Saving Modes in 8051
3/15/2023 Baiju G S 88
Doubling the Baud Rate in 8051
• There are two ways to increase the baud rate of data transfer
To use a higher frequency crystal
To set the SMOD bit in the PCON register
• PCON register is an 8-bit register, whose MSB is SMOD
• When 8051 is powered up, SMOD is zero
• We can set it to high by software and thereby double the baud rate
• PCON is not bit-addressable register. Hence, we cannot set SMOD bit directly. This
may be done as:
MOV A, PCON ;place a copy of PCON in ACC
SETB ACC.7 ;make D7=1
MOV PCON,A ;changing any other bits
3/15/2023 Baiju G S 89
Doubling the Baud Rate in 8051
Baiju G S 90
3/15/2023
Thank you
Baiju G S 91
3/15/2023