SlideShare a Scribd company logo
Lecture 10
Serial Communication
2
Serial Communication
 Introduction
 Serial communication buses
 Asynchronous and synchronous communication
 UART block diagram
 UART clock requirements
 Programming the UARTs
 Operation modes
 Baud rate calculations—timer 1
 Initializing the UART—using timer 1
 Baud rate calculations—timer 2
 Initializing the UART—using timer 2
 UARTx interrupt flags—receiving data
 UARTx Interrupt Flags—sending data
3
Introduction
 Parallel communication implies sending a whole byte (or
more) of data over multiple parallel wires
 Serial communication implies sending data bit by bit over a
single wire
 There are 2 types of serial communication:
 Asynchronous
 Synchronous
4
Serial Communication Buses
 Many popular serial communication standards exist—some
examples are:
 RS-232 (using UART)
 Serial peripheral interface (SPI)
 System management bus (SMBus)
 Serial ATA (SATA)
 The C8051F020 features two UARTs, one SPI, and one
SMBus hardware peripherals
 We will study and use the UART in this course
 UART: Universal asynchronous receiver/transmitter
5
Asynchronous Serial Communication
 With asynchronous communication, the transmitter and
receiver do not share a common clock
Transmitter Receiver+
1 byte-wide Data
Data
–
1 byte-wide Data
The Receiver
 Extracts the data using its
own clock
 Converts the serial data back
to the parallel form after
stripping off the start, stop
and parity bits
The Transmitter
 Shifts the parallel data onto
the serial line using its own
clock
 Also adds the start, stop
and parity check bits
Add: Start, Stop, Parity Bits Remove: Start, Stop, Parity Bits
6
Asynchronous Serial Communication
 Start bit—indicates the beginning of the data word
 Stop bit—indicates the end of the data word
 Parity bit—added for error detection (optional)
 Data bits—the actual data to be transmitted
 Baud rate—the bit rate of the serial port
 Throughput—actual data transmitted per sec (total bits transmitted—
overhead)
 Example: 115200 baud = 115200 bits/sec
 If using 8-bit data, 1 start, 1 stop, and no parity bits, the effective
throughput is: 115200 * 8 / 10 = 92160 bits/sec
7
Asynchronous Serial Communication
 Asynchronous transmission is easy to implement but less
efficient as it requires an extra 2-3 control bits for every 8
data bits
 This method is usually used for low volume transmission
D0 D1 D2 D3 D4 D5 D6 D7
Start Bit 1 or 2 Stop BitsParity Bit
1 Asynchronous Byte
8
Synchronous Serial Communication
 In the synchronous mode, the transmitter and receiver share a
common clock
 The transmitter typically provides the clock as a separate signal in
addition to the serial data
Transmitter Receiver
Data
Clock
The Receiver
 Extracts the data using
the clock provided by the
transmitter
 Converts the serial data
back to the parallel form
The Transmitter
 Shifts the data onto the serial
line using its own clock
 Provides the clock as a
separate signal
 No start, stop, or parity bits
added to data
1 byte-wide Data 1 byte-wide Data
9
UART Block Diagram
10
UART Block
 Each UART is accessed by two SFRs—SBUFx and SCONx
 The Serial Port Buffer (SBUFx) is essentially two buffers:
writing loads data to be transmitted to the buffer and reading
accesses received data from the buffer.
 These are two separate and distinct buffers (registers): the transmit
write-only buffer and the receive read-only register
 The Serial Port Control register (SCONx) contains status
and control bits
 The control bits set the operating mode for the serial port, and status
bits indicate the end of the character transmission or reception
 The status bits are tested in software (polling) or programmed to
cause an interrupt
11
UART Clock Requirements
 A UART needs a clock input for bit timing
 UART baud rates are usually much lower than the MCU
system clock, so the system clock cannot be directly used
as the UART clock
 Timers are used to generate the UART baud rate by dividing
down the system clock
 Example: MCU system clock—22 MHz; UART baud rate—115200
 A bit time accuracy of 2% or better is required at both the
transmitter and receiver ends to be able to communicate
without errors
 To meet this accuracy requirement, external crystal oscillators with
accuracies of 0.1% or better are typically used in systems that use a
UART
12
Programming the UARTs
The UARTs can be programmed through the following
sequence:
 Step 1: configure the digital crossbar (XBR0 or XBR2) to enable UART
operation
 Set the TXx pin to be push-pull by setting the corresponding PnMDOUT bit
(PnMDOUT.n)
 The digital crossbar has to be configured to enable TXx and RXx as
external I/O pins (XBR0.2 for UART0 and XBR2.2 for UART1)
 In addition, XBARE (XBR2.6) must be set to 1 to enable the crossbar
 Step 2: initialize the appropriate timers for desired baud rate generation
 Timer 1 can be used to generate baud rate for UART0 and UART1
 Timer 2 can be used to generate baud rate for UART0
 Timer 4 can be used to generate baud rate for UART1
 Step 3: enable/disable the baud rate doubler SMODx (PCON register)
 Step 4: select the serial port operation mode and enable/disable UART
reception (SCONx register)
 Step 5: enable UART interrupts and set priority (if desired)
13
Operation Modes
 The UARTs have four modes of operation, selectable by
configuring the SM0x-SM1x bits in SCONx register
 Three modes enable asynchronous communications (modes
1 to 3) while the fourth mode (Mode 0) operates as a simple
shift register (synchronous)
 8-bit shift register (mode 0)
 Used for port expansion using an external latch
 8-bit UART with variable baud rate (mode 1)
 Most commonly used mode of operation
 9-bit UART with fixed baud rate (mode 2)
 No timer required
 Choose between SYSCLK/32 or SYSCLK/64 for clock
 9-bit UART with variable baud rate (mode 3)
 Used if 9-bit data transmission is required
14
SCONx Register
Bit Symbol Description
7-6 SM0x-SM1x
Serial Port Operation Mode
00: Mode 0: Shift Register Mode
01: Mode 1: 8 Bit UART, Variable Baud Rate
10: Mode 2: 9 Bit UART, Fixed Baud Rate
11: Mode 3: 9 Bit UART, Variable Baud Rate
5 SM2x
Multiprocessor Communication Enable
The function of this bit depends on the Serial Port Operation Mode.
Mode 0: No effect.
Mode 1: Checks for valid stop bit.
0: Logic level of stop bit is ignored.
1: RIx will only be activated if stop bit is 1
Mode 2 & 3: Multiprocessor Communications Enable.
0: Logic level of 9th
bit is ignored.
1: RIx is set and an interrupt is generated only when the 9th
bit is 1 and the received address matches
the UARTx address or broadcast address.
4 RENx
Receive Enable
0: UARTx reception disabled
1: UARTx reception enabled
3 TB8x
9th
Transmission Bit
The logic level of this bit will be assigned to the 9th transmission bit in Modes 2 & 3. It is not used in Modes 0 & 1.
Set or cleared by software as required.
2 RB8x
9th
Receive Bit
This bit is assigned the logic level of the 9th bit received in Modes 2 & 3. In Mode 1, if SM2x is 0, RB8x is assigned the logic level of the
received stop bit. RB8 is not used in Mode 0.
1 TIx
Transmit Interrupt Flag
Set by hardware when a byte of data has been transmitted by UARTx (after the 8th
bit in Mode 0, or at the beginning of the stop bits in
other modes). When the UARTx interrupt is enabled, setting this bit causes the CPU to vector to the UARTx ISR. This bit must be
cleared manually by software.
0 RIx
Receive Interrupt Flag
Set by hardware when a byte of data has been received by UARTx (as selected by the SM2x bit). When the UARTx interrupt is enabled,
setting this bit causes the CPU to vector to the UARTx ISR. This bit must be cleared manually by software.
15
PCON—Power Control Register
Bit Symbol Description
7 SMOD0
UART0 Baud Rate Doubler Enable
0: UART0 baud rate divide-by-two enabled.
1: UART0 baud rate divide-by-two disabled.
6 SSTAT0 UART0 Enhanced Status Mode Select
5 Reserved Read is undefined. Must write 0.
4 SMOD1
UART1 Baud Rate Doubler Enable
0: UART1 baud rate divide-by-two enabled.
1: UART1 baud rate divide-by-two disabled.
3 SSTAT1 UART1 Enhanced Status Mode Select
2 Reserved Read is undefined. Must write 0.
1 STOP
STOP Mode Select
This bit will always read ‘0’. Writing a ‘1’ will place the microcontroller
into STOP mode. (Turns off oscillator).
0 IDLE
IDLE Mode Select
This bit will always read ‘0’. Writing a ‘1’ will place the microcontroller
into IDLE mode. (Shuts off clock to CPU, but clock to Timers, Interrupts,
and all peripherals remain active).
16
Using Timer 1 to Generate Baud Rate
 Timer 1 in mode 2 (8-bit auto-reload mode) can be used to
generate the baud rate for UART0 and UART1
Block diagram of Timer 0 in Mode 2 (8-bit Auto-reload mode)
Timer 1 is identical to Timer 0
17
Baud Rate Calculations—Timer 1
 The Baud Rate and Timer 1 reload value (for TH1 register)
are related by the following equation:
 If SMODx=1 (UART Baud Rate divide-by-two disabled)
 
















1256
12
32
2 11
TH
SYSCLK
BaudRate
MTSMODx
 
















1256
12
16
1 11
TH
SYSCLK
BaudRate
MT
18
Baud Rate Calculations—Timer 1
 If T1M=1 (timer 1 uses the system clock, NOT divided by
12):
 If SYSCLK=22.1184 MHz and Baud Rate=115200, then:
 
















1256
12
16
1 11
TH
SYSCLK
BaudRate
1 22118400
115200
16 256 1TH
� �� �
 �� �� �
� �� �
1 22118400
256 1 12
16 115200
TH
� �� �
  �� �� �
� �� �
244122561 TH
401 xFTH 
19
Initializing the UART—Using Timer 1
void Init_UART0(void)
{
    //­­ Set up Timer 1 to generate the baud rate (115200)for UART0 ­­­­­­­
    CKCON |= 0x10;  //­­ T1M=1; Timer 1 uses the SYSCLK 22.11845 MHz
    TMOD = 0x20; //­­ Timer 1 in Mode 2 (8­bit auto­reload)
    TH1 = 0xF4; //­­ Baudrate = 115200
    TR1 = 1;         //­­ Start Timer 1 (TCON.6 = 1)
    T2CON &= 0xCF; //­­ Timer 1 overflows are used for receive
//   and transmit clock. RCLK0=0 and TCLK0=0 
    //­­ Set up UART0 ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ 
    PCON |= 0x80; //­­ SMOD0=1 (UART0 baud rate divide­by­2 disabled)
    SCON0 = 0x50; //­­ UART0 Mode 1, Logic level of stop bit ignored 
//   and Receive enabled
    //­­ Enable UART0 interrupt ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
    IE |= 0x10;
    IP |= 0x10; //­­ Set to high priority level
    RI0 = 0; //­­ Clear the receive interrupt flag; 
    //   ready to receive more
}
20
Using Timer 2 to Generate Baud Rate
 If timer 2 (or timer 4) is used to generate the baud rate, it must be
configured for mode 2 operation (auto-reload mode)
21
Baud Rate Calculations—Timer 2
 The baud rate and timer 2 reload value (for RCAP2 register) are related by the
following equation:
 If SYSCLK=22.1184 MHz and BaudRate=115200, then:
  LRCAPHRCAP
SYSCLK
BaudRate
2,26553632 

  
22118400
115200
32 65536 2RCAP

�
 
22118400
65536 2 6
32 115200
RCAP  
�
655306655362 RCAP
xFFFARCAP 02 
22
Initializing the UART—Using Timer 2
void Init_UART0_T2(void)
{
    //­­ Set up Timer 2 to generate the Baudrate (115200) for UART0 ­­­
    CKCON |= 0x20; //­­ T2M=1; Timer 2 uses the SYSCLK 22.11845 MHz
    T2CON = 0x30; //­­ Timer 2 in Mode 2 (Baudrate Generation Mode)
//   RCLK0=1 and TCLK0=1
    RCAP2 = 0xFFFA; //­­ Capture Register value for Baudrate = 115200
    TR2 = 1; //­­ Start Timer 2 (T2CON.2 = 1)
    //­­ Set up the UART0 ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
    PCON |= 0x80; //­­ SMOD0=1 (UART0 BaudRate divide­by­2 disabled)
    SCON0 = 0x50; //­­ UART0 Mode 1, Logic level of stop bit ignored
//   and Receive enabled
    //­­ Enable UART0 interrupt ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
    IE |= 0x10;
    IP |= 0x10; //­­ Set to high priority level
    RI0 = 0;       //­­ Clear the receive interrupt flag;
//   ready to receive more
}
23
Baud Rate Calculations—Timer 2
 If a different time base (other than SYSCLK) is required,
setting the C/T2 bit (in T2CON register) to 1 will allow the
time base to be derived from the external input pin T2
 In this case, the baud rate for the UART is calculated as:
 FCLK is the frequency of the signal supplied to timer 2 and
[RCAP2H, RCAP2L] is the 16-bit value held in the capture
registers
(65536 [ 2 , 2 ]) 16
CLKF
BaudRate
RCAP H RCAP L

 �
24
UARTx Interrupt Flags—Receiving Data
 The receive and transmit flags (RIx and TIx) in SCONx play
an important role in serial communications
 Both the bits are set by hardware but must be cleared by
software
 RIx is set at the end of character reception and indicates
“receive buffer full”
 This condition is tested in software (polled) or programmed
to cause an interrupt
 If the application wishes to input (i.e. read) a character from
the device connected to the serial port (e.g. COM1 port of
PC), it must wait until RIx is set, then clear RIx and read the
character from SBUFx
Note: x = 0 or 1 for UART0 or UART1
25
UARTx Interrupt Flags—Receiving Data
void UART0_ISR(void) interrupt 4
{
//­­ Pending flags RI0 (SCON0.0) and TI0(SCON0.1)
if ( RI0 == 1) //­­ Interrupt caused by
{ //   received byte
received_byte = SBUF0; //­­ Read the input buffer
RI0 = 0; //­­ Clear the flag
new_cmd_received=1;
}
if ( TI0 == 1) //­­ Interrupt caused by 
{ //   transmitted byte
TI0 = 0; //­­ Clear the flag
}
}
26
UARTx Interrupt Flags—Sending Data
 TIx is set at the end of character transmission and indicates
“transmit buffer empty”
 If the application wishes to send a character to the device
connected to the serial port, it must first check that the serial
port is ready
 If a previous character was sent, we must wait until
transmission is finished before sending the next character
27
UARTx Interrupt Flags—Sending Data
void Init_UART0(void)
{
     //­­ Set up Timer 1 to generate the baud rate (115200) for UART0
CKCON |= 0x10; //­­ T1M=1; Timer 1 uses the
//   system clock 22.11845 MHz
     TMOD = 0x20; //­­ Timer 1 in Mode 2 (8­bit auto­reload)
     TH1 = 0xF4; //­­ Baudrate = 115200
     TR1 = 1; //­­ Start Timer 1 (TCON.6 = 1)
     T2CON &= 0xCF; //­­ Timer 1 overflows are used for receive
//   and transmit clock. RCLK0=0 and TCLK0=0 
     //­­ Set up the UART0 
PCON |= 0x80; //­­ SMOD0=1 (UART0 baud rate divide­by­2 
//   disabled)
SCON0 = 0x50; //­­ UART0 Mode 1, Logic level of stop bit 
//   ignored and Receive enabled
//­­ Enable UART0 interrupt
IE |= 0x10;
RI0 = 0; //­­ Clear the receive interrupt flag; 
//   Ready to receive more
TI0 = 1; //­­ TX0 ready to transmit
}
28
UARTx Interrupt Flags—Sending Data
int  i,n;
char sendbuf[20]; //­­ Buffer to hold string for
//   transmission
n = sprintf(sendbuf, "Hello! %c", '0');
for (i=0; i<n; i++)
{
    while (TI0 == 0);    //­­ Wait while the transmission is
//   going on
    TI0 = 0; //­­ Clear TI0
    SBUF0 = sendbuf[i];  //­­ Load the serial buffer
//   with the char to send
}
 Data transmission is initiated by writing to SBUFx
 The TIx transmit interrupt flag (SCONx.1) is set at the beginning of the
stop-bit time
 TIx bit must be cleared manually by software
www.silabs.com/MCU

More Related Content

PPTX
Serial vs Parallel communication & Synchronous and Asynchronous transmission
PPTX
Serial Communication
PDF
Serial Communication Interfaces
PDF
Digital Data, Digital Signal | Scrambling Techniques
PPTX
8255 PPI
PPTX
Digital Data to Digital Signal Conversion
PPTX
Serial Communication
Serial vs Parallel communication & Synchronous and Asynchronous transmission
Serial Communication
Serial Communication Interfaces
Digital Data, Digital Signal | Scrambling Techniques
8255 PPI
Digital Data to Digital Signal Conversion
Serial Communication

What's hot (20)

PPTX
3.programmable interrupt controller 8259
PPTX
INTERRUPTS OF 8086 MICROPROCESSOR
PPTX
Assembly 8086
PPTX
Presentation on cyclic redundancy check (crc)
PPTX
UART(universal asynchronous receiver transmitter ) PPT
PPTX
I2c protocol - Inter–Integrated Circuit Communication Protocol
PPT
Error detection correction (CRC)
PPTX
4. block coding
PPTX
8251 USART
PPTX
I2C introduction
PDF
DAC Interfacing with 8051.pdf
PPT
Manchester Encoding
PPTX
Digital to digital
PPT
IEEE 802.11
PPTX
Controlled Access Protocols
PPTX
8255 Programmable parallel I/O
PPT
Memory & I/O interfacing
PPS
Addressing modes of 8085
PPT
Serial Peripheral Interface(SPI)
PPTX
8251 USART
3.programmable interrupt controller 8259
INTERRUPTS OF 8086 MICROPROCESSOR
Assembly 8086
Presentation on cyclic redundancy check (crc)
UART(universal asynchronous receiver transmitter ) PPT
I2c protocol - Inter–Integrated Circuit Communication Protocol
Error detection correction (CRC)
4. block coding
8251 USART
I2C introduction
DAC Interfacing with 8051.pdf
Manchester Encoding
Digital to digital
IEEE 802.11
Controlled Access Protocols
8255 Programmable parallel I/O
Memory & I/O interfacing
Addressing modes of 8085
Serial Peripheral Interface(SPI)
8251 USART
Ad

Similar to Lecture 10 (serial communication) (20)

PPT
Lecture 10 _serial_communication
PPTX
INTERFACING 8051-MICROCONTROLLER with timer and display
PPT
UART Protocol For Serial Communication.ppt
PPT
lesson01.ppt
PPT
PPTX
Microcontrollers and microprocessors in electrical communication engineering....
PDF
4 ql uart_psb_ds_revc
PPT
Gsm presentation
PPT
Gsm presentation
PPTX
Communication_Protocols[2][1].pptx on protocoals
DOC
NAVEEN UART BATCH 43
PDF
MPS W8-L2L3 Programming AVR Serial Port Ia (in C).pdf
DOCX
Tutorial
PDF
Lecture 3 - Serial Communicationkahfag.pdf
PDF
PA UNIT 4 (FR INFORMATION TECHNOLOGY) SPPU
PDF
020419.pdf
PPTX
IO INTERFACING in unit 2 8086 Microprocessor
PPTX
Serial Communication Uart soc
PDF
UART Serial Communication Module Design and Simulation Based on VHDL
PPT
Serial Communication In Atmega 16
Lecture 10 _serial_communication
INTERFACING 8051-MICROCONTROLLER with timer and display
UART Protocol For Serial Communication.ppt
lesson01.ppt
Microcontrollers and microprocessors in electrical communication engineering....
4 ql uart_psb_ds_revc
Gsm presentation
Gsm presentation
Communication_Protocols[2][1].pptx on protocoals
NAVEEN UART BATCH 43
MPS W8-L2L3 Programming AVR Serial Port Ia (in C).pdf
Tutorial
Lecture 3 - Serial Communicationkahfag.pdf
PA UNIT 4 (FR INFORMATION TECHNOLOGY) SPPU
020419.pdf
IO INTERFACING in unit 2 8086 Microprocessor
Serial Communication Uart soc
UART Serial Communication Module Design and Simulation Based on VHDL
Serial Communication In Atmega 16
Ad

More from cairo university (20)

PPSX
Tocci chapter 13 applications of programmable logic devices extended
PPSX
Tocci chapter 12 memory devices
PPSX
Tocci ch 9 msi logic circuits
PPSX
Tocci ch 7 counters and registers modified x
PPSX
Tocci ch 6 digital arithmetic operations and circuits
PPSX
Tocci ch 3 5 boolean algebra, logic gates, combinational circuits, f fs, - re...
PPSX
A15 sedra ch 15 memory circuits
PPSX
A14 sedra ch 14 advanced mos and bipolar logic circuits
PPSX
A13 sedra ch 13 cmos digital logic circuits
PPSX
A09 sedra ch 9 frequency response
PPTX
5 sedra ch 05 mosfet.ppsx
PPSX
5 sedra ch 05 mosfet
PPSX
5 sedra ch 05 mosfet revision
PDF
Fields Lec 2
PDF
Fields Lec 1
PDF
Fields Lec 5&amp;6
PDF
Fields Lec 4
PDF
Fields Lec 3
PPT
Lecture 2 (system overview of c8051 f020) rv01
PPT
Lecture 1 (course overview and 8051 architecture) rv01
Tocci chapter 13 applications of programmable logic devices extended
Tocci chapter 12 memory devices
Tocci ch 9 msi logic circuits
Tocci ch 7 counters and registers modified x
Tocci ch 6 digital arithmetic operations and circuits
Tocci ch 3 5 boolean algebra, logic gates, combinational circuits, f fs, - re...
A15 sedra ch 15 memory circuits
A14 sedra ch 14 advanced mos and bipolar logic circuits
A13 sedra ch 13 cmos digital logic circuits
A09 sedra ch 9 frequency response
5 sedra ch 05 mosfet.ppsx
5 sedra ch 05 mosfet
5 sedra ch 05 mosfet revision
Fields Lec 2
Fields Lec 1
Fields Lec 5&amp;6
Fields Lec 4
Fields Lec 3
Lecture 2 (system overview of c8051 f020) rv01
Lecture 1 (course overview and 8051 architecture) rv01

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
DOCX
573137875-Attendance-Management-System-original
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Digital Logic Computer Design lecture notes
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
additive manufacturing of ss316l using mig welding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
OOP with Java - Java Introduction (Basics)
Model Code of Practice - Construction Work - 21102022 .pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
Structs to JSON How Go Powers REST APIs.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Digital Logic Computer Design lecture notes
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CH1 Production IntroductoryConcepts.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
additive manufacturing of ss316l using mig welding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
UNIT 4 Total Quality Management .pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Lecture 10 (serial communication)

  • 2. 2 Serial Communication  Introduction  Serial communication buses  Asynchronous and synchronous communication  UART block diagram  UART clock requirements  Programming the UARTs  Operation modes  Baud rate calculations—timer 1  Initializing the UART—using timer 1  Baud rate calculations—timer 2  Initializing the UART—using timer 2  UARTx interrupt flags—receiving data  UARTx Interrupt Flags—sending data
  • 3. 3 Introduction  Parallel communication implies sending a whole byte (or more) of data over multiple parallel wires  Serial communication implies sending data bit by bit over a single wire  There are 2 types of serial communication:  Asynchronous  Synchronous
  • 4. 4 Serial Communication Buses  Many popular serial communication standards exist—some examples are:  RS-232 (using UART)  Serial peripheral interface (SPI)  System management bus (SMBus)  Serial ATA (SATA)  The C8051F020 features two UARTs, one SPI, and one SMBus hardware peripherals  We will study and use the UART in this course  UART: Universal asynchronous receiver/transmitter
  • 5. 5 Asynchronous Serial Communication  With asynchronous communication, the transmitter and receiver do not share a common clock Transmitter Receiver+ 1 byte-wide Data Data – 1 byte-wide Data The Receiver  Extracts the data using its own clock  Converts the serial data back to the parallel form after stripping off the start, stop and parity bits The Transmitter  Shifts the parallel data onto the serial line using its own clock  Also adds the start, stop and parity check bits Add: Start, Stop, Parity Bits Remove: Start, Stop, Parity Bits
  • 6. 6 Asynchronous Serial Communication  Start bit—indicates the beginning of the data word  Stop bit—indicates the end of the data word  Parity bit—added for error detection (optional)  Data bits—the actual data to be transmitted  Baud rate—the bit rate of the serial port  Throughput—actual data transmitted per sec (total bits transmitted— overhead)  Example: 115200 baud = 115200 bits/sec  If using 8-bit data, 1 start, 1 stop, and no parity bits, the effective throughput is: 115200 * 8 / 10 = 92160 bits/sec
  • 7. 7 Asynchronous Serial Communication  Asynchronous transmission is easy to implement but less efficient as it requires an extra 2-3 control bits for every 8 data bits  This method is usually used for low volume transmission D0 D1 D2 D3 D4 D5 D6 D7 Start Bit 1 or 2 Stop BitsParity Bit 1 Asynchronous Byte
  • 8. 8 Synchronous Serial Communication  In the synchronous mode, the transmitter and receiver share a common clock  The transmitter typically provides the clock as a separate signal in addition to the serial data Transmitter Receiver Data Clock The Receiver  Extracts the data using the clock provided by the transmitter  Converts the serial data back to the parallel form The Transmitter  Shifts the data onto the serial line using its own clock  Provides the clock as a separate signal  No start, stop, or parity bits added to data 1 byte-wide Data 1 byte-wide Data
  • 10. 10 UART Block  Each UART is accessed by two SFRs—SBUFx and SCONx  The Serial Port Buffer (SBUFx) is essentially two buffers: writing loads data to be transmitted to the buffer and reading accesses received data from the buffer.  These are two separate and distinct buffers (registers): the transmit write-only buffer and the receive read-only register  The Serial Port Control register (SCONx) contains status and control bits  The control bits set the operating mode for the serial port, and status bits indicate the end of the character transmission or reception  The status bits are tested in software (polling) or programmed to cause an interrupt
  • 11. 11 UART Clock Requirements  A UART needs a clock input for bit timing  UART baud rates are usually much lower than the MCU system clock, so the system clock cannot be directly used as the UART clock  Timers are used to generate the UART baud rate by dividing down the system clock  Example: MCU system clock—22 MHz; UART baud rate—115200  A bit time accuracy of 2% or better is required at both the transmitter and receiver ends to be able to communicate without errors  To meet this accuracy requirement, external crystal oscillators with accuracies of 0.1% or better are typically used in systems that use a UART
  • 12. 12 Programming the UARTs The UARTs can be programmed through the following sequence:  Step 1: configure the digital crossbar (XBR0 or XBR2) to enable UART operation  Set the TXx pin to be push-pull by setting the corresponding PnMDOUT bit (PnMDOUT.n)  The digital crossbar has to be configured to enable TXx and RXx as external I/O pins (XBR0.2 for UART0 and XBR2.2 for UART1)  In addition, XBARE (XBR2.6) must be set to 1 to enable the crossbar  Step 2: initialize the appropriate timers for desired baud rate generation  Timer 1 can be used to generate baud rate for UART0 and UART1  Timer 2 can be used to generate baud rate for UART0  Timer 4 can be used to generate baud rate for UART1  Step 3: enable/disable the baud rate doubler SMODx (PCON register)  Step 4: select the serial port operation mode and enable/disable UART reception (SCONx register)  Step 5: enable UART interrupts and set priority (if desired)
  • 13. 13 Operation Modes  The UARTs have four modes of operation, selectable by configuring the SM0x-SM1x bits in SCONx register  Three modes enable asynchronous communications (modes 1 to 3) while the fourth mode (Mode 0) operates as a simple shift register (synchronous)  8-bit shift register (mode 0)  Used for port expansion using an external latch  8-bit UART with variable baud rate (mode 1)  Most commonly used mode of operation  9-bit UART with fixed baud rate (mode 2)  No timer required  Choose between SYSCLK/32 or SYSCLK/64 for clock  9-bit UART with variable baud rate (mode 3)  Used if 9-bit data transmission is required
  • 14. 14 SCONx Register Bit Symbol Description 7-6 SM0x-SM1x Serial Port Operation Mode 00: Mode 0: Shift Register Mode 01: Mode 1: 8 Bit UART, Variable Baud Rate 10: Mode 2: 9 Bit UART, Fixed Baud Rate 11: Mode 3: 9 Bit UART, Variable Baud Rate 5 SM2x Multiprocessor Communication Enable The function of this bit depends on the Serial Port Operation Mode. Mode 0: No effect. Mode 1: Checks for valid stop bit. 0: Logic level of stop bit is ignored. 1: RIx will only be activated if stop bit is 1 Mode 2 & 3: Multiprocessor Communications Enable. 0: Logic level of 9th bit is ignored. 1: RIx is set and an interrupt is generated only when the 9th bit is 1 and the received address matches the UARTx address or broadcast address. 4 RENx Receive Enable 0: UARTx reception disabled 1: UARTx reception enabled 3 TB8x 9th Transmission Bit The logic level of this bit will be assigned to the 9th transmission bit in Modes 2 & 3. It is not used in Modes 0 & 1. Set or cleared by software as required. 2 RB8x 9th Receive Bit This bit is assigned the logic level of the 9th bit received in Modes 2 & 3. In Mode 1, if SM2x is 0, RB8x is assigned the logic level of the received stop bit. RB8 is not used in Mode 0. 1 TIx Transmit Interrupt Flag Set by hardware when a byte of data has been transmitted by UARTx (after the 8th bit in Mode 0, or at the beginning of the stop bits in other modes). When the UARTx interrupt is enabled, setting this bit causes the CPU to vector to the UARTx ISR. This bit must be cleared manually by software. 0 RIx Receive Interrupt Flag Set by hardware when a byte of data has been received by UARTx (as selected by the SM2x bit). When the UARTx interrupt is enabled, setting this bit causes the CPU to vector to the UARTx ISR. This bit must be cleared manually by software.
  • 15. 15 PCON—Power Control Register Bit Symbol Description 7 SMOD0 UART0 Baud Rate Doubler Enable 0: UART0 baud rate divide-by-two enabled. 1: UART0 baud rate divide-by-two disabled. 6 SSTAT0 UART0 Enhanced Status Mode Select 5 Reserved Read is undefined. Must write 0. 4 SMOD1 UART1 Baud Rate Doubler Enable 0: UART1 baud rate divide-by-two enabled. 1: UART1 baud rate divide-by-two disabled. 3 SSTAT1 UART1 Enhanced Status Mode Select 2 Reserved Read is undefined. Must write 0. 1 STOP STOP Mode Select This bit will always read ‘0’. Writing a ‘1’ will place the microcontroller into STOP mode. (Turns off oscillator). 0 IDLE IDLE Mode Select This bit will always read ‘0’. Writing a ‘1’ will place the microcontroller into IDLE mode. (Shuts off clock to CPU, but clock to Timers, Interrupts, and all peripherals remain active).
  • 16. 16 Using Timer 1 to Generate Baud Rate  Timer 1 in mode 2 (8-bit auto-reload mode) can be used to generate the baud rate for UART0 and UART1 Block diagram of Timer 0 in Mode 2 (8-bit Auto-reload mode) Timer 1 is identical to Timer 0
  • 17. 17 Baud Rate Calculations—Timer 1  The Baud Rate and Timer 1 reload value (for TH1 register) are related by the following equation:  If SMODx=1 (UART Baud Rate divide-by-two disabled)                   1256 12 32 2 11 TH SYSCLK BaudRate MTSMODx                   1256 12 16 1 11 TH SYSCLK BaudRate MT
  • 18. 18 Baud Rate Calculations—Timer 1  If T1M=1 (timer 1 uses the system clock, NOT divided by 12):  If SYSCLK=22.1184 MHz and Baud Rate=115200, then:                   1256 12 16 1 11 TH SYSCLK BaudRate 1 22118400 115200 16 256 1TH � �� �  �� �� � � �� � 1 22118400 256 1 12 16 115200 TH � �� �   �� �� � � �� � 244122561 TH 401 xFTH 
  • 19. 19 Initializing the UART—Using Timer 1 void Init_UART0(void) {     //­­ Set up Timer 1 to generate the baud rate (115200)for UART0 ­­­­­­­     CKCON |= 0x10;  //­­ T1M=1; Timer 1 uses the SYSCLK 22.11845 MHz     TMOD = 0x20; //­­ Timer 1 in Mode 2 (8­bit auto­reload)     TH1 = 0xF4; //­­ Baudrate = 115200     TR1 = 1;         //­­ Start Timer 1 (TCON.6 = 1)     T2CON &= 0xCF; //­­ Timer 1 overflows are used for receive //   and transmit clock. RCLK0=0 and TCLK0=0      //­­ Set up UART0 ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­      PCON |= 0x80; //­­ SMOD0=1 (UART0 baud rate divide­by­2 disabled)     SCON0 = 0x50; //­­ UART0 Mode 1, Logic level of stop bit ignored  //   and Receive enabled     //­­ Enable UART0 interrupt ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­     IE |= 0x10;     IP |= 0x10; //­­ Set to high priority level     RI0 = 0; //­­ Clear the receive interrupt flag;      //   ready to receive more }
  • 20. 20 Using Timer 2 to Generate Baud Rate  If timer 2 (or timer 4) is used to generate the baud rate, it must be configured for mode 2 operation (auto-reload mode)
  • 21. 21 Baud Rate Calculations—Timer 2  The baud rate and timer 2 reload value (for RCAP2 register) are related by the following equation:  If SYSCLK=22.1184 MHz and BaudRate=115200, then:   LRCAPHRCAP SYSCLK BaudRate 2,26553632      22118400 115200 32 65536 2RCAP  �   22118400 65536 2 6 32 115200 RCAP   � 655306655362 RCAP xFFFARCAP 02 
  • 22. 22 Initializing the UART—Using Timer 2 void Init_UART0_T2(void) {     //­­ Set up Timer 2 to generate the Baudrate (115200) for UART0 ­­­     CKCON |= 0x20; //­­ T2M=1; Timer 2 uses the SYSCLK 22.11845 MHz     T2CON = 0x30; //­­ Timer 2 in Mode 2 (Baudrate Generation Mode) //   RCLK0=1 and TCLK0=1     RCAP2 = 0xFFFA; //­­ Capture Register value for Baudrate = 115200     TR2 = 1; //­­ Start Timer 2 (T2CON.2 = 1)     //­­ Set up the UART0 ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­     PCON |= 0x80; //­­ SMOD0=1 (UART0 BaudRate divide­by­2 disabled)     SCON0 = 0x50; //­­ UART0 Mode 1, Logic level of stop bit ignored //   and Receive enabled     //­­ Enable UART0 interrupt ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­     IE |= 0x10;     IP |= 0x10; //­­ Set to high priority level     RI0 = 0;       //­­ Clear the receive interrupt flag; //   ready to receive more }
  • 23. 23 Baud Rate Calculations—Timer 2  If a different time base (other than SYSCLK) is required, setting the C/T2 bit (in T2CON register) to 1 will allow the time base to be derived from the external input pin T2  In this case, the baud rate for the UART is calculated as:  FCLK is the frequency of the signal supplied to timer 2 and [RCAP2H, RCAP2L] is the 16-bit value held in the capture registers (65536 [ 2 , 2 ]) 16 CLKF BaudRate RCAP H RCAP L   �
  • 24. 24 UARTx Interrupt Flags—Receiving Data  The receive and transmit flags (RIx and TIx) in SCONx play an important role in serial communications  Both the bits are set by hardware but must be cleared by software  RIx is set at the end of character reception and indicates “receive buffer full”  This condition is tested in software (polled) or programmed to cause an interrupt  If the application wishes to input (i.e. read) a character from the device connected to the serial port (e.g. COM1 port of PC), it must wait until RIx is set, then clear RIx and read the character from SBUFx Note: x = 0 or 1 for UART0 or UART1
  • 25. 25 UARTx Interrupt Flags—Receiving Data void UART0_ISR(void) interrupt 4 { //­­ Pending flags RI0 (SCON0.0) and TI0(SCON0.1) if ( RI0 == 1) //­­ Interrupt caused by { //   received byte received_byte = SBUF0; //­­ Read the input buffer RI0 = 0; //­­ Clear the flag new_cmd_received=1; } if ( TI0 == 1) //­­ Interrupt caused by  { //   transmitted byte TI0 = 0; //­­ Clear the flag } }
  • 26. 26 UARTx Interrupt Flags—Sending Data  TIx is set at the end of character transmission and indicates “transmit buffer empty”  If the application wishes to send a character to the device connected to the serial port, it must first check that the serial port is ready  If a previous character was sent, we must wait until transmission is finished before sending the next character
  • 27. 27 UARTx Interrupt Flags—Sending Data void Init_UART0(void) {      //­­ Set up Timer 1 to generate the baud rate (115200) for UART0 CKCON |= 0x10; //­­ T1M=1; Timer 1 uses the //   system clock 22.11845 MHz      TMOD = 0x20; //­­ Timer 1 in Mode 2 (8­bit auto­reload)      TH1 = 0xF4; //­­ Baudrate = 115200      TR1 = 1; //­­ Start Timer 1 (TCON.6 = 1)      T2CON &= 0xCF; //­­ Timer 1 overflows are used for receive //   and transmit clock. RCLK0=0 and TCLK0=0       //­­ Set up the UART0  PCON |= 0x80; //­­ SMOD0=1 (UART0 baud rate divide­by­2  //   disabled) SCON0 = 0x50; //­­ UART0 Mode 1, Logic level of stop bit  //   ignored and Receive enabled //­­ Enable UART0 interrupt IE |= 0x10; RI0 = 0; //­­ Clear the receive interrupt flag;  //   Ready to receive more TI0 = 1; //­­ TX0 ready to transmit }
  • 28. 28 UARTx Interrupt Flags—Sending Data int  i,n; char sendbuf[20]; //­­ Buffer to hold string for //   transmission n = sprintf(sendbuf, "Hello! %c", '0'); for (i=0; i<n; i++) {     while (TI0 == 0);    //­­ Wait while the transmission is //   going on     TI0 = 0; //­­ Clear TI0     SBUF0 = sendbuf[i];  //­­ Load the serial buffer //   with the char to send }  Data transmission is initiated by writing to SBUFx  The TIx transmit interrupt flag (SCONx.1) is set at the beginning of the stop-bit time  TIx bit must be cleared manually by software