Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Interface & Telemetry System

Topic: Serial Communication Protocol

Heri Subagiyo

Program Studi Teknik Elektronika


Politeknik Caltex Riau

pcr@pcr.ac.id 0761 - 53939 https://pcr.ac.id


Contents
Types of Communication Protocols
Transmission Modes
Clock Synchronization
UART Protocol
SPI Protocol
I2C Protocol
Implementation on Arduino
Advantages & Disadvantages
Types of Communication Protocols
What is Protocol:
• Protocol is just like a common language that system uses to understand the data

Serial Communication Protocol

Examples: I2C, SPI, USB, 1-Wire,


SATA, ETHERNET, RS232
The most widely used approach to
transfer information between data Parallel Communication Protocol
processing peripherals
Examples: ISA, ATA, SCSI, PCI
https://circuitdigest.com/sites/default/files/inlineimages/ https://circuitdigest.com/sites/default/files/
u/Serial-communication.png inlineimages/u/Parallel-communication.png
Advantages of Serial over Parallel
Communication
Requires Less Space: The Serial Communication configuration requires less
space because the requirement of cable is less in serial connection

No Cross-talks: There is a minimal presence of conductors in the nearby


space. It is therefore, the chances of cross-talks are rare

Low Cost: The cost of serial link is less in comparison to parallel links
What is Protocol?
Protocol is just like a common language that system uses to understand the data

Serial
Communication
Protocols

Asynchronous Synchronous
type type

UART RS-232, RS- CAN I2C USB eSPI


SPI Protocol
Protocol 422, RS-485 Protocol Protocol Protocol Protocol
Transmission Modes
Simplex Method
• Either sender or receiver can be active at a time
• If the sender is transmitting the data then receiver can only accept and vice versa
• One-way communication technique. Examples are Television and Radio

Half Duplex Method


• Both sender and receiver can be active but not at the same time
• If the sender is transmitting then receiver can accept but cannot send and similarly vice versa
• The well-known example is the internet

Full Duplex Method


• Both receiver and transmitter can send data to each other at the same time
• The well-known example is mobile phone
Clock Synchronization
Synchronous Serial Asynchronous Serial
• It is a point-to-point connection from a master to slave • The external clock signal is absent
• All the devices use single CPU bus to share data and clock • Mostly in long distance app. and perfect for stable
• No mismatch in baud rate in this interface comm.
• No start, stop and parity bits added to data • It rely on parameters: Data Flow, Error Control, Baud
• The well-known examples are I2C and SPI Rate, Transmission and Reception Control
• It adds the start, stop and parity check bits
• Most µC: UART. Ex Standards: RS-232, RS-422 and RS-485

https://circuitdigest.com/sites/default/files/ https://circuitdigest.com/sites/default/files/inlineimages/
inlineimages/u1/Synchronous-Serial-Communication.png u1/Asynchronous-Serial-Communication.png
Other Terms (1)

Baud Rate
• Rate at which the data is transferred between the transmitter and receiver in bits per
second (bps)
• The most commonly used baud rate is 9600. Others: 1200, 2400, 4800, 57600, 115200
• The baud rate has to be same for both transmitter and receiver

Framing
• The number of data bits to be sent from transmitter to receiver
• Most of the application uses 8 bits as the standard data bits
Other Terms (2)
Synchronisation
• It tells the start and end of the data bits
• The transmitter will set start and stop bits to the data frame and the receiver will identify it

Error Control
• The parity bits are used
• If the data frame contains the even number of 1’s then it is known as even parity and the
parity bit in the register is set to 1
• If the data frame contains odd number of 1’s then it is known as odd parity and clears the
odd parity bit in the register
UART Protocol (1)
Universal Asynchronous Receiver/Transmitter (UART)
Term UART, refers to the onboard hardware that manages packaging & translation of serial data
The wiring involved with setting up UART communication is very simple, only two-wired:
➢ one line for transmitting data (TX),
➢ one line for receiving data (RX)

GPIO 0 and GPIO 1 are Serial RX and TX.


Any GPIO pin can be used as Serial RX or TX
https://www.deviceplus.com/wp-content/ with the SoftwareSerial library
uploads/2016/12/image003.jpg
UART Protocol (2)
UART transmits data asynchronously, which induces that no clock signal
UART embed start and stop bits with actual data bits, which defines the start and end of data
packet
When receiver end detects the start
bit, it starts to read the data bits at
specific baud rate meaning both
transmitting and receiving peripherals
should work under same baud rate

UART works under half duplex


communication mode meaning it either
transmits or receives at a time

UART Data packet visualized


UART Communication Implementation (1)
When implementing UART serial communication on most embedded platforms such as the
Arduino, the user does not have to deal with communication at the bit level
Instead, the platform often provides higher level software libraries that are the only aspect of
the communication process the user has to deal with
Users can use the Serial and SoftwareSerial libraries to implement UART communication

Serial and SoftwareSerial


Purpose Code
Method
Constructor Define the GPIO pins that will serve as
SoftwareSerial comms (2 , 3);
(SoftwareSerial only) the UART RX and TX lines
Define the baud rate (transmission
begin speed) for the serial connection in comms.begin(9600);
range 4800 to 115200
UART Communication Implementation (2)
Serial and SoftwareSerial
Purpose Code
Method
Write byte data converted into
print human-readable characters over serial comms.println(“Hello World”);
connection
Write raw byte data over the serial
write comms.write(45);
connection

Evaluates to true when data is


available if (comms.available())
available over the serial connection

read Read data from the serial connection comms.read();


Advantages & Disadvantages of UART

• Clock signal is not required


• Cost effective
Advantages
• Uses parity bit for error detection
• Requires only 2 wires for data communication

• Doesn’t support multiple master slave functionality


Disadvantages • Baud rate of communicating UART should be within
10 percent of each other
SPI Protocol (1)
• Serial Peripheral Interface (SPI) is a synchronous interface which allows several SPI microcontrollers
to be interconnected
• The SPI may be configured
either as master or as a slave

SPI is a 4-wire protocol:


• MOSI (Master Out Slave In),
• MISO (Master In Slave Out,
• SS (Slave Select), and
• SCLK (Serial Clock)

• The four basic SPI signals (MISO,


MOSI, SCK and SS), Vcc and
Ground are the part of data
communication. Total 6 wires

https://circuitdigest.com/sites/default/files/inlineimages/u/
SPI-communication.png
SPI Protocol (2)
• MOSI (“Master Out Slave In”): Data transmission line
from master to slave
• SCK (“Clock”): Clock line defining transmission speed
and transmission start/end characteristics
• SS (“Slave Select”): Line for master to select a
particular slave to communicate with
• MISO (“Master In Slave Out”): Data transmission line
from slave to master

The master device first configures the clock at a particular frequency


Furthermore the SS line is used to select the appropriate slave by
pulling the SS line Low where it is normally held high
The communication is established between the selected slave and the
master device as soon as appropriate slave device is selected
SPI is a full duplex communication protocol

https://www.deviceplus.com/wp-content/uploads/2016/
12/image011.jpg
SPI Implementation on the Arduino (1)
SPI implementation on the Arduino
using the Arduino as the master
device (use library SPI.h)

For the Arduino Uno, the connections


are as follows:
• SCK: GPIO 13 or ICSP 3
• MOSI: GPIO 11 or ICSP 4
• MISO: GPIO 12 or ICSP 1
• SS: GPIO 10 or another digital pin
SPI Implementation on the Arduino (2)
SPI Method Purpose Code

Define the clock speed, data bit order (most SPI.beginTransaction


Constructor significant bit first or least significant bit first), (SPISettings(14000000,
and SPI mode MSBFIRST, SPI_MODE0));

Select the slave device attached to this GPIO pin digitalWrite(10, LOW);
digitalWrite [ Drive the pin high afterwards to de-select the …
slave ] [ digitalWrite(10, HIGH); ]

transfer Transfer the byte to the selected slave SPI.transfer(0x00);

Ends the SPI transaction (should be called after a


endTransaction SPI.endTransaction();
digitalWrite(high) is called on the SS line)
SPI Protocol Mode
CPHA = 0 CPHA = 1
(“first edge” of clock signal) (“second edge” of clock signal)

• “SPI mode 0” • “SPI mode 1”


• Data capture begins when • Data capture begins when
CPOL = clock goes from 0 (idle) to 1 for clock goes from 0 (idle) to 1 for
0 the first time. the first time.
(idle 0) • Data capture occurs on the • Data capture occurs on the
rising edge of the clock signal falling edge of the clock signal
(0→1) (1→0)

• “SPI mode 2” • “SPI mode 3”


• Data capture begins when • Data capture begins when
CPOL = clock goes from 1 (idle) to 0 for clock goes from 1 (idle) to 0 for CPOL and CPHA settings visualized
1 the first time. the first time.
(idle 1) • Data capture occurs on the • Data capture occurs on the
rising edge of the clock signal falling edge of the clock signal
(0→1) (1→0)
Advantages & Disadvantages of SPI
• Faster than asynchronous serial communication
protocol.
Advantages • Support multiple slaves connectivity.
• Universally accepted protocol and low cost.

• Requires more wires than other communication


protocols.
• Master device should control all slave
Disadvantages communications (slave-slave communication is
impossible).
• Numerous slave devices leads to circuit complexity.
I2C Protocol (1)
• Inter Integrated Circuit (I2C) atau (I2C) : two-line communication between different ICs or modules
• Two lines are SDA (Serial Data Line) and SCL (Serial Clock Line)
• These two active wires are said to be bidirectional
I2C protocol is a master to slave communication
protocol
Each slave is been provided with unique address.
To establish communication:
• Master device initially sends the target slave
address along with R/W (Read/Write) flag.
• The corresponding slave device will move into
active mode leaving other devices in off state.
• One bit acknowledgment is replied by the
receiver if transmitter transmits 1 byte data
https://circuitdigest.com/sites/default/files/inlineimages/u/
I2C-Communication-Working.png
I2C Protocol (2)
• I2C is unique because it solves the issue of interfacing with multiple slave devices through addressing.
• These addresses might look something like this: 0x1B
• For these specifications, the user must refer to the slaves’ datasheets for device addresses, register
addresses, and device settings

The address and data portions of the I2C communication line

https://www.deviceplus.com/wp-content/uploads/
2016/12/image019.jpg
I2C Implementation on the Arduino (1)
On the Arduino, I2C implementation
occurs through the Wire library
(Wire.h)

For the Arduino Uno, the connections


are as follows:
• SDA: Analog Pin 4
• SCL: Analog Pin 5

I2C protocol is a half-duplex


communication, just like UART
I2C Implementation on the Arduino (2)
I2C (Wire) Method Purpose Code

Initiate the library and join the I2C bus as either a master
begin Wire.begin();
or slave

For Arduino configured as a I2C master: initiate


beginTransmission Wire.beginTransmission(0x68);
transmission with the slave that has the given address

write Write the byte data over the I2C bus Wire.write(0x6B);

Request a specified number of bytes from the slave with


the given address; Has the option of releasing the I2C line
or holding onto it for further communication
The requested bytes are put into a buffer to be
requestFrom Wire.requestFrom(0x68, 6, true);
subsequently read through Wire.read() calls.
If the last parameter is false, the Arduino will hold onto
the I2C line for further communication, not allowing
other devices to communicate over it
I2C Implementation on the Arduino (3)
I2C (Wire) Method Purpose Code

Read bytes put into the buffer after transmissions


from the slave device.
This method is called after a call to Wire.requestFrom

read To read two bytes from the buffer, this method must Wire.read();
be called twice, Example:
Wire.requestFrom(0x68, 2, true);
Wire.read();
Wire.read();

Ends the current transmission; Has the option of


endTransmission releasing the I2C line or holding onto it for further Wire.endTransmission(true);
communication
Advantages & Disadvantages of I2C

• The ability to connect multiple masters to multiple


slaves
• Addressing mechanism eases master slave
communication
Advantages
• Simplicity: implementation only requires two wires
and some resistors
• Cost and circuit complexity does not end up on
number of devices

• The biggest disadvantage of I2C Communication


Disadvantages Protocols is its limited speed
What have we
discussed? Contents Types of Communication Protocols
Transmission Modes
Clock Synchronization
UART Protocol
SPI Protocol
I2C Protocol
Implementation on Arduino
Advantages & Disadvantages
This Photo by Unknown Author is licensed under CC BY-SA

You might also like