41
Serial Communication

Essentials of Communication This simple model requires many guarantees. Sender Receiver Communication Link Data

Embed Size (px)

DESCRIPTION

Guarantees in Communications The communication link exists. The communication link is safe and sound. The sender and receiver are the correct nodes. The sender is sending the correct data. The receiver is able to correctly interpret the incoming data.

Citation preview

Communications1
Sender
Receiver
The sender and receiver are the correct nodes.
The sender is sending the correct data.
The receiver is able to correctly interpret the incoming data.
Protocols in Communication
In order to have robust communication, the guarantees needs to be realized.
To do so, we need an elaborate and standardized mechanism.
These standard rules that defines the parameters of communications and ensures these guarantees are called protocol.
Advantages of Protocols
Usually include error-detection and error-correction mechanisms.
Available as implemented chips that can be directly used.
Types of Communication Protocols
First Categorization:
Second Categorization:
Suppose Sender sends data with a Time Period of T
What if Receiver doesn’t know the speed and assume it to be say T/2
The Data received will be
Synchronous Mode
Sender sends a clock signal along with data at every rising / falling edge of the clock, the data value is read by the receiver.
SENDER
There is no clock signal.
The receiver and the sender communicate at a predetermined speed (bauds or bits per second).
Baud Rate: Baud Rate is a measurement of transmission speed in asynchronous communication. The devices that allows communication must all agree on a single speed of information - 'bits per second'.
Synchronous vs Asynchronous Mode
Transmission Modes
SENDER
RECEIVER
Half-Duplex
Two way transmission takes place but only one end can communicate at a time
Transmission Modes
SENDER
RECEIVER
Full-Duplex
Two way transmission takes place and both end can communicate simultaneously
UART Universal Asynchronous Receiver Transmitter
Asynchronous Serial Communication
With asynchronous communication, the transmitter and receiver do not share a common clock
Transmitter
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
Asynchronous Serial Communication
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
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
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
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
UART in Atmega328P
Arduino Serial Library
int incomingByte = 0; // for incoming serial data void setup()
{         Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop()
{         // send data only when you receive data:         if (Serial.available() > 0)
{                 // read the incoming byte:                 incomingByte = Serial.read();                 // say what you got:                 Serial.print("I received: ");                 Serial.println(incomingByte, DEC);         } }
SPI Serial Peripheral Interface
SPI
SPI is an interface bus commonly used to send data between microcontrollers and small peripherals such as shift registers, sensors and SD cards. It uses separate clock and data lines, along with a select line to choose the device you wish to talk to.
SPI was originally developed by Motorola (now Freescale)
It works on serial mode of transfer.
It is also synchronous and full duplex.
It has the capability of communicate with many nodes.
SPI
In SPI, the sender and receiver follows a master-slave relationship.
There may be multiple nodes in the network.
One node is master, the rest are slaves.
The communication is always initiated by the master.
The slaves can communicate only with the master.
How do master selects the slave??
SPI Pins
SCK is generated by Master and is used as the mode is synchronous.
MOSI is Master Out Slave In: Data sent by Master to Slave.
MISO is Master In Slave Out: Data sent by Slave to Master.
SS is Slave Select: Slave communicates with Master only if this pin’s value is set as LOW. (Active Low)
SPI Schematics: Single Slave
The SPI bus uses two data lines, a clock line, and a slave select line. An additional slave select line is added for each slave device, but the other three lines are shared on the bus.
SPI Schematics: Multiple Slaves
A - No data (SS is high, SCK is low)
B - SS taken low to enable the slave (peripheral). At this point the slave should prepare to transfer data by setting the MOSI and the SCK lines as inputs, and the MISO line as an output. The slave can now prepare to notice clock pulses on the SCK line.
C - First character arrives (the letter "F" or 0x46 or 0b01000110). For each of the 8 bits the SCK line is briefly brought high, and then low again. This tells the slave to read the data on the MOSI line. Also the slave can place data on the MISO line for the master to simultaneously read in.
D - The letter "a" arrives
E - The letter "b" arrives
F - "No data" after "Fab" - however the SS is still enabled.
G - SS taken high to indicate end of the sequence of data.
http://www.gammon.com.au/forum/?id=10892
29
Notice how for each bit (starting with the most significant bit), the MOSI line is first changed to the correct state (0 or 1) and then the SCK line is pulsed to indicate that the data should be read.
Sending the Character 'F' (0x46 or 0b01000110) (MSB First)
SPI in Atmega328P
Arduino SPI Library (<SPI.h>)
The Arduino development kit comes with an SPI library. To use it you just need to include it:
#include <SPI.h>
To control the hardware you call SPI.begin() which configures the SPI pins (SCK, MOSI, SS) as outputs and MISO as input. It also sets SCK and MOSI low, and SS high.
The function SPI.transfer() does the actual transferring of bytes. It is up to you to set SS low at an appropriate time.
When finished call SPI.end() to turn the SPI hardware off.
I2C Inter-Integrated Circuit I-Squared-C
I2C (pronounced I-squared-C) created by Philips Semiconductors (now NXP) and commonly written as "I2C" stands for Inter-Integrated Circuit and allows communication of data between I2C devices over two wires.
It sends information serially using one line for data (SDA – Serial DAta) and one for clock (SCL – Serial CLock).
I2C Basics
http://www.instructables.com/file/FGX5UZOG1ZGKFPC
34
The I2C protocol defines the concept of master and slave devices. A master device is the device that is in charge of the bus. This device controls the clock and generates the START and STOP signals. Slave devices listen to the commands sent by the Master and respond to them. Basic details: Transfer rate: 10 Kb/s (low speed) - 100Kb/s  (high speed) SDA - Serial DAta line SCL - Serial CLock line 128 possible addresses (7 bits)  16 reserved addresses 112 devices max Devices have to share both 5V (Power) and GND (Ground)
I2C Basics
Theory of Operation
Regardless of how many slave units are attached to the I2C bus, there are only two signals connected to all of them. Consequently, there is an additional overhead because:
an addressing mechanism is required for the master device to communicate with a specific slave device.
an acknowledgement mechanism is required for such a communication.
http://www.totalphase.com/support/articles/200349156
36
Theory of Operation
I2C has a master/slave protocol. The master initiates the communication. The sequence of events are:
The Master device issues a start condition. This condition informs all the slave devices to listen on the serial data line for instructions. (SDA goes from HIGH to LOW when SCL is HIGH)
The Master device sends the address of the target slave device and a read/write flag. (Flag is 1 for read and 0 for write)
The Slave device with the matching address responds with an acknowledgement signal.
Communication proceeds between the Master and the Slave on the data bus. Both the master and slave can receive or transmit data depending on whether the communication is a read or write. The transmitter sends 8-bits of data to the receiver which replies with a 1-bit acknowledgement.
When the communication is complete, the master issues a stop condition indicating that everything is done. (SDA goes from LOW to HIGH when SCL is HIGH)
http://www.totalphase.com/support/articles/200349156
37
TWI (Two Wire Interface)
TWI stands for Two Wire Interface and it is identical to I²C.
The name TWI was introduced by Atmel and other companies to avoid conflicts with trademark issues with Philips related to I²C.
http://www.i2c-bus.org/twi-bus/
39
TWI in Atmega328P
I2C Requires Analog Pins 4 (SDA) and 5 (SCL) and two pull-up resistors.
You can connect more than 100 Arduino's on the same 2 pins.
It's simple, reliable and easy-to-use.
Arduino TWI Library (<Wire.h>)
Parity Bit