MC C5.1 Serial Communication Rv01 - HC v19.1
MC C5.1 Serial Communication Rv01 - HC v19.1
Serial Communication
Serial Communication
Introduction
Serial communication buses
Asynchronous and synchronous communication
UART block diagram
Operation modes
UART clock requirements
Programming the UARTs
UART1 interrupt flags - receiving data
UART1 Interrupt Flags - sending data
Baud rate calculations - Timer 1
Initializing the UART - using Timer 1
2
Introduction
Parallel communication implies sending a whole byte (or
more) of data over multiple parallel wires
3
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)
4
Asynchronous Serial Communication
With asynchronous communication, the transmitter and
receiver do not share a common clock
Add: Start, Stop, Parity Bits Remove: Start, Stop, Parity Bits
Transmitter + – Receiver
Data
Shifts the parallel data onto Extracts the data using its
the serial line using its own own clock
clock
Converts the serial data back
Also adds the start, stop to the parallel form after
and parity check bits stripping off the start, stop
and parity bits
5
Asynchronous Serial Communication
Start bit—indicates the beginning of the data word
6
Asynchronous Serial Communication
D0 D1 D2 D3 D4 D5 D6 D7
1 Asynchronous Byte
7
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
Clock
Transmitter Receiver
Data
9
UART1 Block
UART1 is accessed by two SFRs—SBUF1 and SCON1
10
Operation Modes
UART1 has two modes of operation, selectable by
configuring the S1MODE bit in SCON1 register
8-bit UART with variable baud rate (mode 1)
Most commonly used mode of operation
9-bit UART with variable baud rate (mode 3)
Used if 9-bit data transmission is required
TB81 and RB81 store the 9th transmission/reception bit
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 (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 Timer 1 for desired baud rate generation
Step 3: select the serial port operation mode and enable/disable UART
reception (SCON1 register)
Step 4: enable UART interrupts and set priority (if desired)
13
SCON1 Register
14
UART1 Interrupt Flags - Receiving Data
The receive and transmit flags (RI1 and TI1) in SCON1 play
an important role in serial communications
Both the bits are set by hardware but must be cleared by
software
RI1 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 RI1 is set, then clear RI1 and read the
character from SBUF1
15
UART1 Interrupt Flags - Sending Data
TI1 is set at the end of character transmission and indicates
―transmit buffer empty‖
16
UART1 Interrupt Flags - Programming
Data transmission is initiated by writing to SBUF1
The TI1 transmit interrupt flag (SCON1.1) is set at the beginning of the
stop-bit time
TI1 bit must be cleared manually by software
17
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 UART1
18
Baud Rate Calculations - Timer 1
The Baud Rate, Timer 1 clock source (T1CLK) and Timer 1
reload value (for TH1 register) are related by the following
equation:
19
Baud Rate Calculations - Example
20
Initializing the UART - Using Timer 1
21
www.silabs.com/MCU