0% found this document useful (0 votes)
47 views22 pages

MC C5.1 Serial Communication Rv01 - HC v19.1

The document discusses serial communication and UARTs. It covers asynchronous and synchronous serial communication, UART block diagrams, operation modes, clock requirements, programming UARTs using interrupts and flags, baud rate calculations using timers, and initializing UARTs using timers to generate baud rates. It provides details on using Timer 1 in the C8051F040 microcontroller to generate baud rates for UART1 serial communication.

Uploaded by

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

MC C5.1 Serial Communication Rv01 - HC v19.1

The document discusses serial communication and UARTs. It covers asynchronous and synchronous serial communication, UART block diagrams, operation modes, clock requirements, programming UARTs using interrupts and flags, baud rate calculations using timers, and initializing UARTs using timers to generate baud rates. It provides details on using Timer 1 in the C8051F040 microcontroller to generate baud rates for UART1 serial communication.

Uploaded by

BodeaGabriela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 10

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

Serial communication implies sending data bit by bit over a


single wire

There are 2 types of serial communication:


 Asynchronous
 Synchronous

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)

The C8051F040 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

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

1 byte-wide Data 1 byte-wide Data

The Transmitter The Receiver

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

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

6
Asynchronous Serial Communication

Start Bit Parity Bit 1 or 2 Stop Bits

D0 D1 D2 D3 D4 D5 D6 D7

1 Asynchronous Byte

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

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

1 byte-wide Data 1 byte-wide Data

The Transmitter The Receiver


Shifts the data onto the serial Extracts the data using
line using its own clock the clock provided by the
transmitter
Provides the clock as a
separate signal Converts the serial data
back to the parallel form
No start, stop, or parity bits
8 added to data
UART1 Block
Diagram

9
UART1 Block
UART1 is accessed by two SFRs—SBUF1 and SCON1

The Serial Port Buffer (SBUF1) 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 (SCON1) 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

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

9-Bit UART mode supports multiprocessor communication


 A master processor and one or more slave processors
 The 9th bit is used to select the slave

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‖

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

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

sendByte: mov SBUF1, R0 ; Transmit the byte stored in R0


jnb TI1, $ ; Wait for the end of transmission
clr TI1 ; Clear the end of transmission flag
ret

receiveByte: jnb RI1, $ ; Wait for the end of reception


mov A, SBUF1 ; Copy the received data in the accumulator
clr RI1 ; Clear the end of reception flag
ret

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

Block diagram of Timer 0 in Mode 2 (8-bit Auto-reload mode)


Timer 1 is identical to Timer 0

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:

T1CLK can be SYSCLK or SYSCLK divided by 4, 8, 12 or


48

Given a specific SYSCLK, Timer 1 can be configured


(TICLK and reload value) to meet a specific Baud Rate
requirement

19
Baud Rate Calculations - Example

20
Initializing the UART - Using Timer 1

initOscillator: mov SFRPAGE, #CONFIG_PAGE ; Use SFRs in the config Page


mov CLKSEL, #0x00
mov OSCICN, #0xC3

initTimer1: mov SFRPAGE, #TIMER01_PAGE ; Use SFRs in Timer 1 Page


mov TMOD, #0x20 ; Use Timer 1 in 8-bit auto-reload mode
mov TH1, #0x96 ; Set the reload value
mov CKCON, #0x10 ; Timer 1 will use the system clock
mov TCON, #0x40 ; Enable Timer 1

initUART1: mov SFRPAGE, #CONFIG_PAGE ; Use SFRs in the config Page


mov XBR2, #0x44 ; Enable the crossbar and UART 1
mov SFRPAGE, #UART1_PAGE ; Use SFRs in UART 1 Page
mov SCON1, #0x10 ; Use UART 1 in 8-bit mode; Enable reception

21
www.silabs.com/MCU

You might also like