Cse Communication 2016

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 79

Computer communication

Introduction
Mechanisms applied in communicating
between a computer and another
computer or with other devices.
Mainly serial (bit by bit) while in the I/O
interface section, we have already studied
the parallel communication (byte-wise)
Serial Communication, the Universal
Serial Bus (USB) and I2C are the most
commonly used. A brief discussion will
be given
Application
Most likely
USB
Computer

External
device
Serial communication
Serial communication is used for connecting peripheral
units, such as display, modems, to a microcomputer
Traditional Serial communication (RS232) is a one-to-
one communication, ie. only one device is connected to
the communication interface
Latest serial communication is one-to-many such as
USB
Data (8-bit) is exchanged in a bit-by-bit manner or
serially
Only 3 wires (send, receive, and ground) to setup a
communication link between two devices
For serial communication, there are two types of transfer
mode asynchronous and synchronous
Serial communication
Synchronous requires a clock
signal to synchronize the transmitter
and the receiver
Asynchronous communication,
without a common clock, uses pre-
defined protocol to achieve the data
transfer
Synchronous communication
Synchronous communication needs a common clock
Transmitter first sends out synchronization characters to
the receiver at the proper clock tick (for example at the
rising edge)
Receiver reads the synchronization bit (eg 10101)
pattern and compares it to a default sync pattern
If the sync pattern is identified, the receiver begins to
read character data off the communications line
Sync characters may be periodically resent to assure
that synchronization is maintained
Synchronous communication
Synchronous type of communication is typically
used in high-speed data transfer
However, synchronous communication requires
a common clock and this will increase the cost
and less common in simple applications
Programming is difficult to generate the clock
signal
Synchronous communication
Example
Synchronous communication is applied in
Serial memory device
Serial sensor
I2C (I2C) and SPI are commonly used
synchronous communication techniques
More example
8x8 dot matrix led
CPU must send two bytes of data
representing the row number (high byte)
and LEDs to turn-on in each row.
The two bytes combine into a 16-bit (D0 to
D15) data stream and transferred bit by bit
with D15 being sent first.
8x8 dot matrix
A clock signal must be issued when a bit is
being sent and the clock signal must have a low
(0) to high (1) transition
Insider the dot matrix, there is an internal
register to store the incoming data. The register
is controlled by the CS (chip select) signal.
The CS signal must be low, once all 16 bits of
data have been transferred then CS signal must
be a 1 so that data can be latched into the
internal register.
Timing diagram for 8x8 dot
matrix
; CS must be low during the whole time Program
; cs is Port2. 0 clk is Port2.1
shift_2byte: codes for
CLR P2.0 ; CS =LOW
CLR P2.1 ; clk sending
NOP ; a small delay
;; send first byte store in R2
serial data
MOV A, R2 ; A is a register
MOV R0, #8 ; this is a counter to control the number of shift
LOOP8A: ; using a loop to shift a 8-bit data to P0.7 bit by bit
MOV P0, A ; send data out only in bit 7
SETB P2.1 ; generating a High in the clock
CLR P2.1 ; generating a Low in the clock
RL A ; rotate the content of A left 1 time
DJNZ R0, LOOP8A ; determine if all 8 bits being sent
Asynchronous communication
Data transfer is not sent in predefined time
slots because there is no common clock.
Data transfer can start at any given time
The traditional hardware connector for
asynchronous communication is the RS232
connector (or Serial port)
The setup in the lab-session, the connection
between the PC and the ADuC842 board is
via asynchronous communication.
Asynchronous communication
Interface consists of receive data, transmit data, and
signal common (no common clock)
Data are sent one character at a time (the data bit
varies in different systems, but usually either 7, or 8 bits
are used)
Both receiver and transmitter must agree on the number
of data bits, as well as the baud rate (bits per second to
be transmitted)
Receiver examines synchronization bits that are included
at the beginning and end of each character
Start bit, parity bit and stop bit are used for
synchronization
Asynchronous communication
Asynchronous communication
Data bits are sent with a predefined frequency - baud
rate.
Both the transmitter and receiver must use the same bit
frequency.
If the transmitter is sending at 9600 baud but the
receiver is running at 19600 baud then every bit sent by
the transmitter will equal to 2 bits at the receiver side!!!!!
After the first bit (the start bit) is received, the receiver
calculates at which moments the other data bits will be
received (based on the baud rate)
It will check the line voltage levels for the stop bit (a
1) at those moments.
Example
For example, if the receiver gets the first
bit at time X, and the pre-defined transfer
rate is 9600 baud, 8-bit per character and
1 stop bit, no parity then the stop bit
should be received at X+0.9375ms (9x
1/9600).
Asynchronous communication
operation sequence
1. A start bit is sent first a 0. Since the
communication line is kept at 1 when there is no
data so a 1->0 transition in the line will indicate a
possible start bit
2. The data bits follow the start bit (LSB transmit first)
3. A stop bit is sent (stop bit has a value of 1)
4. The stop bit can be detected correctly even if the
previous data bit also had a value of 1, due to timing
as stated previously.
5. Stop bits can be 1, 1.5, or 2 bit periods in length.
Operation sequence
1. The parity bit is optional. A parity bit
affords a small amount of error
checking, to help detect data
corruption that might occur during
transmission
2. Either even or odd parity can be used
Parity bit for error checking
Parity could be even or odd
When even parity is chosen, the parity bit is
transmitted with a value of 0 if the number of
preceding `1` is an even number (i.e including
the parity bit, the total data bits sent have a
even number of 1 if even parity is used).
Even parity => no. of 1 in data + parity bit =
even
ODD parity => no. of 1 in data + parity bit = odd
Parity for error checking
Example
If the data is 10101101 (with 5 1s odd)
using even parity then the parity bit is 1
so the total number of 1 sent is even.
However, if odd parity is used then the
parity bit is 0 so total number of 1 sent is
ODD.
Parity bit
If the original data is 10111001 now if odd parity is used
then the parity bit is 0 so data being sent is 1 0 1011
1001 0
if the received data is 1 0 1111 1001 0 then comparing
the received data and the parity bit 0 then you can
detect possible error in the data and resending the data
if necessary.
But if the received data is 1001 0001 then comparing to
the parity then no error can be detected.
So parity bit is not a very useful technique to detect
communication errors and usually not included in the
data stream in order to reduce overhead
Configure the
serial
communication
port under
WindowsXP
Question
A device is connected to a computer via a serial
asynchronous communication link (RS232). If
the device can transmit 800 characters per
second. What is the possible setting of the
communication port?
The baud rate is 9600, with 1 stop bit and no parity
The baud rate is 19600, with 2 stop bit and 2 parity
bits
The baud rate is 9600, with 2 stop bit and 1 parity
The baud rate is 4800, with 1 stop bit and no parity
Serial communication
Serial communication usually is character
based and you can send max. 8 bits
Usually send ASCII characters not values
So if you want to send 101 then you need
to convert the value 101 into 101 three
characters
The values being sent are 49 48 49
49 = ASCII of 1; 48 = ASCII of 0
At the receiving end, you need to convert
the string back to value
Exercise
A computer is communicating with an
external device using an asynchronous
serial communication link with 1 stop bit
and 1 parity bit. Determine the bit pattern
being transmitted for the character a.
Ascii of a is 61H
Even parity is used
UART and USART
Uart and Usart are devices to perform
serial communication
UART universal asynchronous
receiver/transmitter
USART universal
synchronous/asynchronous
receiver/transmitter
UART

UART UART CPU


CPU
BUS
BUS GND

From the CPUs point of view the UART is another I/O device
Some microprocessor (or microcontroller such as ADuC832) has built-in UART
Uart and Usart
The major functions of UART or USART: parallel-to-
serial conversion and serial-to-parallel conversions
Automatically frame (or insert) character with start bit,
parity bit and stop bit
Check for correct parity, framing error, and overrun error
Framing error occurs when stop bits are not detected
Overrun error occurs when prior character that was
received was not read out of the USART receive data
register by the microprocessor before another character
was received.
Connecting two USART
Device Computer
RTS DSR
CTS DTR

DSR RTS

DTR CTS
Rx Rx

Tx Tx

Common Common

The RTS, CTS, DSR, DTR are handshake signals


These signals can improve the communication reliability but usually not used
Handshake of USART
When a device wants to send a data then
it will first set the RTS (Ready, or Request
To Send).
The receiving end will see DSR (Data Set
Ready) active and acknowledge by setting
the DTR (Data Terminal Ready) line.
The sending end see the CTS (Clear to
send) active then
Transmission of data proceed
Different kinds of
communication links
Simplex communication link with one
communication link and for a single direction
Half-duplex data are transmitted and received
over the same line, and transmission and
reception cannot take place at the same time
Full duplex with separate transmit and receive
lines. Data can be transferred in both directions
at the same time
8251A Programmable
communication interface
The 8251A is one of the most widely used
USART IC (Universal Synchronous
Asynchronous Receiver/transmitter )
Contains a full-duplex receiver and transmitter
Configurable through software (asynchronous,
or synchronous mode) sent by the host CPU
Automatically detection of parity, framing, and
overrun errors
The physical connection
The traditional serial data interface for a PC
is usually via the RS232C connector.
RS-232 - Recommend Standard number 232
and C is the latest revision of the standard. The
serial ports on most computers use a subset of
the RS-232C standard. The full RS-232C
standard specifies a 25-pin "D" connector of
which 22 pins are used. Most of these pins are
not needed for normal PC communications,
Most new PCs are equipped with male D type
connectors having only 9 pins or even without
RS232 connector
R2232C
1. Incoming signal
2. Incoming data
3. Outgoing data
4. DTR
5. Ground
6. DSR
7. RTS
8. CTS
9. Ring indicator

Pin 1 and 9 are only available in


Connection to a modem
RS232
The voltage level used to represent a 0 (also
called a space) is +5 to +15 for transmitter, and
+3 to +25 for receiver. The voltage level for 1
(or mark) is -5 to -15 on the transmitter and -3 to
-25 on the receiver. The maximum transfer rate
is 1.5Mbps. The maximum cable length is 50
feet.
A device (such as the MAX232) is used to
convert the voltage level from 0V and 5V to the
+V to V used by the RS232 standard.
Hardware configuration

UART built-in
Applications Communication is
via the RS232 so
PC sends characters
to the ADuC842
PC PC may send a 10
RS232 to represent 10%
duty cycle
ADuC832
Motor

PWM

Using a PC to control a motor can be done via a ADuC842


PC talks to ADuC842 via the serial communication
ADuC842 control the motor using Ports (just like the lab exercise)
Serial communication (RS232)
The PC can be programmed using Visual C++
Forms programming by including the serialPort1
object.
Functions such as READ, WRITE can be used to
send or receive data via the RS232.
Example for sending data using MSVC++:
serialPort1->Write(act_arr, action, 1);
The above statement will send (write) a character
stored in the array act_arr to the serial port.
Serial port under MSVC
Serial programming in ADuC832
Programming the ADuC842 serial port is more
complicated and the following steps are
necessary:
Configure the serial port such as setting the Baud
rate, stop bit etc
Enable the serial port
Wait for incoming data or send data out
The following codes configure the serial port
parameters
mov SCON, #01010000B ; 8-bit
Codes example
getchar:
jnb RI, $ ; wait for RI = 1
mov A, sbuf ; move data from SBUF to register A
clr RI ; clear RI so next byte can be received
ret
;Send data via the RS232
reply_ans:
clr TI
mov sbuf, R2 ; move data to the SBUF
jnb TI, $ ;
ret
Polling Vs Interrupt
Since you cannot predict when data will be
available in the serial port, therefore, polling can be
used to monitor the port to check for incoming
data.
In the ADuC842, this is achieved by the statement:
JNB RI, $
JNB (jump not bit) and $ (current statement) so
when RI is equal to 0 then the CPU will jump back
to the current location forming a loop.
Polling Vs Interrupt
Polling loop will not exit if no data is being
received and the CPU cannot perform
other functions.
So a better way to implement this kind of
communication is using interrupt. The
serial port can issue an interrupt when
data is received. Therefore, when there is
no data coming from the serial port, the
CPU can execute other programs.
In MSVC, the event handler is similar to
the interrupt service routine
Other form of serial
communication
Bluetooth module
Xbee

processor bluetooth

Serial communication
USB to serial converter
Modern computer
(especially notebook)
has only USB
Connectors

A USB to serial converter can


convert the USB signal to
RS232 signal so traditional serial
communication techniques can
still applied.
Universal Serial Bus (USB)
The most popular interface between a
computer and external devices in modern
computer system
Provides low to medium speed serial
interface
It is a bus many devices can attach to
the bus at the same time but RS232 is a
one-to-one connection
Basic USB 2.0
Support three different communication
speed: High-speed (480 Mb/s), full-speed
(12Mbit/s) low-speed (1.5 Mb/s).
Cable lengths are limited to 5 meters
maximum for full-speed and 3 meters
maximum for low-speed interface
Basic USB 3.0
SuperSpeed 5 Gbit/s
Cable lengths 3 meters
The USB uses a tiered star topology
The supported devices are connected to
the system host via nodes on USB hubs
USB connection

Universal Serial Bus


Many devices can connect to the bus at the
same time
USB hub architecture

Root hub
inside
the computer
Basic USB
Each device in a USB system is assigned an unique
address when first attached to the bus. This process is
called bus enumeration. As many as 127 different
devices can be accommodated.
When a USB device is first attached to a hub, the host
assigns it with an address and establishes a connection
between itself and the device.
The characteristics of this connection provide speed,
direction, required bandwidth, error-handling
requirements, transfer type, maximum packet size. All
these are referred to as a pipe.
USB connector
There are 4-pin on the connector
USB devices can be powered directly from
the USB connection as long as the current
drawn is less than 500mA per device
The receiver defines a differential 1 as D+
200mV greater than D- and a differential 0
as D+ 200mV less than D-. This is called
bi-phase
USB connector
Pin Signal

1 5.0V
Type B connector

2 -Data
+D > -D 200mv then it is a 1
+D< -D 200mV then it is a 0
3 +Data

4 Ground
USB data
For RS232, there are the start bit, stop bit
In USB, data transfer is more complicated
and there are encoding as well as error
detection, data are sent in packet
Encoding - NRZI (non-return to zero
inverted) data encoding for transmitting
packets of data
NRZI
NRZI 1 is represented by no change in
signal level and 0 is represented by a
change in level.
Example - a string of 0 causes the NRZI
data toggle at each bit time, while a string of
1 causes long period with no transition in
the data.
When a logic 1 is transmitted for six bits in a
row a sync bit is added. This is called bit-
stuffing
The sync bit is a logic 0
NRZI
Example:
assume idle state is 1
Data: 1101001100010
NRZI : 11100100010110
Data are always transmitted beginning
with the LSB first, followed by
subsequent bits
Data 1 1 0 1 0 0 1 1 0 0 0 1 0
11 1 0 0 1 0 0 0 1 0 1 1 0

Idle state
NRZI
Example
1001010100011 (assume idle is 1)
Result 11011001101000
0001111111101 (assume idle is 0)
Result 010111111011100

Stuff bit
Flow
chart
for
NRZI
Data being
sent
USB data communication
USB is a one to many connection so the data transmitted
is more complicated
To begin communication, a sync byte (80H) is
transmitted first, followed by the packet identification
byte (PID)
Packet contains many bytes of data but in RS232 it is
character (8-bit) based
The PID contains 8 bits, the rightmost 4 bits contain the
type of packet that follows, if any.
The left most 4 bits are complementing the rightmost 4
bits and this help detecting errors. Example: if 1000 is
sent then PID is 0111 1000.
Packet format
USB packet
Token, Start of Frame, Data, Handshake
are packet types
Only the host can issue the token packet
and the token packet will determine action
to be taken in the subsequent
transmission.
Handshake signals are employed in order
to secure the data communication.
PID codes
PID Name Type Description
E1H OUT Token Host -> function transaction
D2H ACK Handshake Receiver accepts packet
C3H Data0 Data Data packet PID even
A5H SOF Token Start of frame
69H IN Token Function -> host transaction
5AH NAK Handshake Receiver does not accept data
4BH Data1 Data Data packet PID odd
3CH PRE Special Host preamble
2DH Setup Token Setup command
1EH Stall Token Stalled
Packet format
CRC cyclic redundancy checks
CRC is used for error detection
A transmitted bit stream (M-bit) is appended with
a n-bit FCS (frame check sequence) so that the
result, the cascading, (M+n bits) divided by P
(the CRC polynomial) has NO remainder
The no. of bits for FCS is related to P
P is n+1 bits and the FCS appended to the
data has n bits
Two types of CRC are used in USB
communication
5-bit CRC and 16-bit CRC
CRC mechanism
Original data M bits
Appended FCS n bits
Total data transferred M+n bits
Error check (M+n) / P with no remainder
then data is correct
P has n+1 bits and is called the polynomial
CRC polynomial
5-bit CRC is generated by X5 + X2 + 1
polynomial; P = 1 0 0 1 0 1
FCS is 5 bits as P has 6 bits
16-bit CRC is generated by X16+X15+X2+1
P = 11000000000000101
FCS is 16 bits as P is 17 bits
How to generate the FCS
FCS is the remainder of M (original data)
* 2n (ie shift M n bits to the left and append
n 0s ) divided by P
For example is M is 10101010 and n is 4
Then M * 2n
10101010 0000
Next 1010100000 / P to determine the
remainder (FCS)
CRC example
Example : original 1010001101
P is 110101 = X5+X4+X2+1 (P has 6 bits)
Appending 5 0s get 1010001101 00000
Do the division by XOR (not by normal
division!!!!)
110101 1 0 1 0 0 0 1 1 0 1 0 0 0 0 0
110 1 0 1
XOR 11 1 0 1 1 Appended 0s
11 0 1 0 1
XOR 1 1 1 0 1 0
1 1 0 1 0 1
1 1 1 1 1 0
1 1 0 1 0 1
1 0 1 1 0 0
1 1 0 1 0 1
1 1 0 0 1 0
1 1 0 1 0 1
0 1 1 1 0
Question
Based on the concept of CRC (cyclic
redundancy checks), if the original
message is 1011 1010 1011 and the
constant divisor is 10101. Determine the
transmitted message in hexademical.
CRC creation process
Get the raw frame or raw data
Left shift the raw frame by n bits and the
divide it by P (using XOR)
The remainder of the last action is the
FCS.
Append the FCS to the raw frame. The
result is the frame to transmit
CRC check process
Receive the frame.
Divide it by P.
Check the reminder. If not zero then
there is an error in the frame
Token Packet contents
Token packet includes PID, ADDR, ENDP and CRC
The ADDR address field specifies which device the
packet is designated for. Since USB can support up to
127 devices so ADDR is only 7 bits
ENDP endpoints which is the source or sink of data.
Example, if a device can read/write then there will be 2
different endpoints
CRC for error checking
Frame packet
Start of Frame Packet (SOF) are issued
by the host at a nominal rate of once every
1ms. The SOF packet delivers timing
information and is used by frame timing
related devices such as video/audio
streaming.
For example, a webcam must be
transferring 30 frames per second
Flow-control
Token packets indicate the type of transaction to follow.
There are 3 types of token packets: IN, OUT, and
SETUP.
IN informs the USB device that the host wishes to read
information
OUT host wishes to send information
SETUP used to begin control transfers
Tokens are only sent by the host, never a device. IN
and OUT tokens contain a 7-bit device address and
command the device to transmit DATA packets, or
receive the following DATA packets, respectively.
Token Packet
ACK and NAK tokens are used to coordinate the
transfer of data packets between the host system
and the USB device.
Host transfers a data packet to a USB device, the device
must either transmit an ACK (acknowledge) or a NAK
(not acknowledge) token back to the host. If data and
CRC are received correctly then ACK is sent; if not NAK
is sent
If the host received the NAK then it re-transmits the data
packet until the receiver finally receives it correctly. The
flow control method is called stop and wait flow control.
The host must wait for the client to send an ACK or NAK
before transferring additional data packets
Flow-control
1. Host issues OUT token (host wants to
send data to a device)
2. Data Packet follows (host sends the data
packet)
3. Wait for Ack Handshake from device

You might also like