嵌入式系統
Introduction to Embedded System Design
Part 5: Serial Communication
(UART)
授課教師
Che-Wei LIN (林哲偉)
Embedded System-1
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
Embedded System-2
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)
We will study and use the UART in this course
UART: Universal Asynchronous Receiver/Transmitter
Embedded System-3
Asynchronous Serial Communication
With asynchronous communication, the transmitter and receiver
do not share a common clock
Shifts the parallel data onto Extracts the data using its own
the serial line using its own clock
clock
Converts the serial data back to
Also adds the start, stop and
the parallel form after stripping
parity check bits
off the start, stop and parity bits
Embedded System-4
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
Embedded System-5
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.
Embedded System-6
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.
Shifts the data onto the serial Extracts the data using the
line using its own clock 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
added to data
Embedded System-7
Introduction of UART in UNO
UART uses two wirelines, one for transmitting and the other one
for receiving, so as to make the data transmission bidirectional.
The communication use a predefined frequency (baud rate) to
transmit data. In Arduino, UART is called "Serial".
There is only one hardware UART on Arduino Uno, and is
primarily used to read the log and messages printed by Arduino
(so it's also called "Log UART").
If we use the hardware UART for other purposes, the Log UART
does not have resources to do its job.
Embedded System-8
Introduction of UART in UNO
To provide more UART connections, Arduino UNO uses Serial
Pin to simulate the behavior of UART by a software approach,
this is called Software Serial.
However, Arduino is equipped with a number of hardware
UART. But to be compatible with the Software Serial API of
Arduino follows the name "Software Serial"
Embedded System-9
Example01 - SerialEvent
Open this example code from Introduction
"File" -> SerialEvent occurs whenever a new data comes in
the hardware serial RX. This routine is run between
"Examples" -> each time loop() runs.
“04.Communication" ->
“SerialEvent" Test and Result.
Embedded System-10
Example02 - SoftwareSerialExample
Open this example code from
"File" ->
"Examples" ->
“SoftwareSerial" ->
“SoftwareSerial Example"
Embedded System-11
Example02 - SoftwareSerialExample
Materials
Arduino UNO Board × 1
USB to TTL Adapter × 1
Embedded System-12
Example02 - SoftwareSerialExample
Make sure that your Arduino board is attached to your computer
via USB to enable serial communication through the serial
monitor window of the Arduino Software (IDE).
In the example below, digital pins 10 and 11 on your Arduino
boards are used as virtual RX and TX serial lines. The virtual RX
pin is set up to listen for anything coming in on via the main
serial line, and to then echo that data out the virtual TX line.
Conversely, anything received on the virtual RX is sent out over
the hardware TX.
Embedded System-13
Example02 - SoftwareSerialExample
Introduction of CP2102
The CP2102 is a highly-integrated USB-to-UART Bridge Controller
providing a simple solution for updating RS-232 designs to USB using a
minimum of components and PCB space.
Royalty-free Virtual COM Port (VCP) device drivers provided by
Silicon Laboratories allow a CP2102-based product to appear as a COM
port to PC applications. The CP2102 UART interface implements all RS-
232 signals, including control and handshaking signals, so existing system
firmware does not need to be modified. In many existing RS-232 designs,
all that is required to update the design from RS-232 to USB is to replace
the RS-232 level-translator with the CP2102.
Embedded System-14
CP2102 驅動安裝
1. Download the driver from here:
https://www.silabs.com/products/development-
tools/software/usb-to-uart-bridge-vcp-drivers
2. If installation is complete, you can see
the right figure on the “device manager”.
Embedded System-15
Example02 - SoftwareSerialExample
Tera Term Software
https://ttssh2.osdn.jp/index.html.en
Result:
從mySerial輸入,會顯示在Serial視窗上;從Serial輸入,會顯示在mySerial視窗上。
Embedded System-16
Example03 - Master Reader/Slave Sender
In this example, two boards are programmed to
communicate with one another in a Master
Reader/Slave Sender configuration via the I2C
synchronous serial protocol.
Arduino 1, the Master, is programmed to request, and
then read, 6 bytes of data sent from the uniquely
addressed Slave Arduino.
Once that message is received, it can then be viewed in
the Arduino Software (IDE) serial monitor window.
Embedded System-17
Example03 - Master Reader/Slave Sender
Materials
Arduino UNO Board × 2
Code1: Code2:
https://www.arduino.cc/en/Tutorial/MasterReader? https://www.arduino.cc/en/Tutorial/MasterReader?
action=sourceblock&num=1 action=sourceblock&num=2
Embedded System-18
Example03 - Master Reader/Slave Sender
Wire Master Reader Wire Slave Sender
Embedded System-19
Example03 - Master Reader/Slave Sender
Introduction of Wire.requestFrom(address, quantity)
Used by the master to request bytes from a slave device. The bytes may
then be retrieved with the available() and read() functions.
Introduction of Wire.onRequest (handler)
Register a function to be called when a master requests data from this slave
device.
Parameters
address: the 7-bit address of the device to request bytes from
quantity: the number of bytes to request
handler: the function to be called, takes no parameters and returns nothing
Embedded System-20
Example03 - Master Reader/Slave Sender
Master will read the respond from Slave with “hello ”.
Embedded System-21
To output data via serial port (1)
Notice:
To write “binary” data or “ASCII” data in the Arduino are different
https://www.arduino.cc/en/serial/write
Embedded System-22
To output data via serial port (2)
Notice:
To write “binary” data or “ASCII” data in the Arduino are different
https://www.arduino.cc/en/serial/print
Embedded System-23
Lab Requirements
Please establish the communication between Arduino and PC first.
Please transmit AA16 between from Arduino to PC continuously, than check the
signal from oscilloscope to explain the physical meaning of the signal (High/Low,
Baudrate)
Please implement following finite state machine (FSM) in your program.
To detect the words “A”, “B”, “C”, “D”, “R” send from PC.
Once the Arduino receives one of the words: “A”, “B”, “C”, “D”, “R”, transmits current state to PC
(Please display current state of FSM in the Tera-Term software terminal).
Start Bit Parity Bit 1 or 2 Stop Bits
D0 D1 D2 D3 D4 D5 D6 D7
1 Asynchronous Byte
Embedded System-24
ASCII Code
http://www.asciitable.com/
Embedded System-25
Code Reference
https://www.arduino.cc/reference/en/language/functions/com
munication/serial/
https://www.arduino.cc/en/Reference/SoftwareSerialExample
https://www.arduino.cc/en/Tutorial/MasterReader
https://www.arduino.cc/en/Reference/WireRequestFrom
https://www.arduino.cc/en/Reference/WireOnRequest
Embedded System-26