KBS SerialPort

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

Serial Communication

Kamalanath Samarakoon
Serial communication protocols
• Slow
– RS 232 (moderate distance)
– RS485 (long distance)

• Fast (Short distance)


– USB (between devices)
– I2C (IEEE 1384) (between ICs)
– FireWire (between devices)
RS232
• Most popular interface before USB
• Was available in almost all computer to mainframe
• Advantages
– Was commonly available
– Can do that USB could not
– Less number of wires compared to parallel
– Inexpensive
– Very long cable (50 ft max)/USB 16ft // 10-15 ft
– 3 wires to communicate (Unlike parallel)
RS232
• Disadvantages
– Cannot connect more than two devices
– Max speed 20,000 bits/s
– Very long lines need differential interface (RS485)
RS232 Evolution
• Originally
– PC to dumb terminal
• Then
– PC to Internet
• Also PC to
– Mouse
– Printer
– Another PC
– Microcontroller
RS232
• RS (Recommended Standard)
• Standard is – EIA 232F (Electronic Industry
Association)
• This standard defines
– Names and functions of signals
– Electrical Characteristic of signals
– Mechanical standard (size, shape pin assignment)
RS232
• 25 pins or 9 pins
• Logic 0 +3 to +25 V
• Logic 1 -3 to -25 V
• - 3 to +3 V undefined
• O/C V <25 V w.r.t to Ground
• S/C I 500mA driver should be able to drive
• Max speed 20,000 bps for 50 ft
Connectors RS232 – 9 pin

• Equipment
– Female
• Cable
– Male
Use of RS232 Link

• Historically RS232 was used to connect two


computers via modems and communication
line (PSTN – Telephone)

• DTE=Data Terminal Equipment (Dumb Terminal/Computer)


• DCE= Data Communication Equipment (Modem)
Standard 9 to 25 pin cable layout for
async data on a PC AT serial cable
Sign 9-pin
Description 25-pin DCE Source DTE or DCE
al DTE
Carrier Detect CD 1 8 from Modem
Receive Data RD 2 3 from Modem
Transmit Data TD 3 2 from Terminal/Computer

Data Terminal Ready DTR 4 20 from Terminal/Computer

Signal Ground SG 5 7 from Modem

Data Set Ready DSR 6 6 from Modem

Request to Send RTS 7 4 from Terminal/Computer

Clear to Send CTS 8 5 from Modem


Ring Indicator RI 9 22 from Modem
Full
Name Function
TD Transmit Data Serial Data Output (TXD)
RD Receive Data Serial Data Input (RXD)
CTS Clear to Send
This line indicates that the Modem is ready to exchange data.
DCD Data Carrier
Detect When the modem detects a "Carrier" from the modem at the
other end of the phone line, this Line becomes active.

DSR Data Set Ready


This tells the UART that the modem is ready to establish a link.
DTR Data Terminal This is the opposite to DSR. This tells the Modem that the UART is
Ready ready to link.
RTS Request To Send This line informs the Modem that the UART is ready to exchange
data.
RI Ring Indicator
Goes active when modem detects a ringing signal from the PSTN.
Null Modem

XX
To connect two devices without a modem
No Handshake
Full Handshake
Loop back Handshake
USART Universal Asynchronous
Receiver/Transmitter
• IC Module inside the IC for serial
communication
• Do parallel in PC to serial in RS232 conversion
and back
• Supports Half duplex and Full Duplex
• Provide all standard RS232 handshaking and
control lines
• 8250 57600 bps
• 16450 115200 bps
• 16550 16 Bytes FIFO Buffers
• 16650 32 Byte FIFO Buffers
• 16750 64 Byte FIFO Buffers 230,400 bps
Flow control

• Program should set 115,200 kbps and not 33.6


kbps as DCE do compressing
• DTE faster than DCE hence data can be lost
• Hence use flow control
Flow control
• Software
• Xoff – Xon

• Hardware
– Use RTS and DTS lines
Asynchronous communication

Add parity for error detecting


1 8 N 1 = 8N1 1 7 E 1 = 7E1
Parity

8 bits including parity


7 bits of data
(count of 1-bits)
even odd
0000000 0 00000000 00000001
1010001 3 10100011 10100010
1101001 4 11010010 11010011
1111111 7 11111111 11111110
References
• Parallel port, Serial Port and USB
– beyondlogic.org
– http://retired.beyondlogic.org/spp/parallel.htm
• Jan Axelson
– Parallel port complete
– Serial port complete
– USB complete
Asynchronous serial communication by
USART
• UART uses clock that is 16 times bit frequency
• E.g. 300 bps 4800 Hz
• Detect start bit, wait 8, wait 16, read, wait 16, read…
Calculation of bit rate
• In PC UART cock rate is 1.8432 MHz
• Calculate maximum bit rate
Quiz
• In PC UART cock rate is 1.8432 MHz
• Calculate maximum bit rate

• If bit rate is 9600bps


1200 bytes
• How many bytes units it can transmit per second?

• How many data bytes it can transmit per second when data
format is 7E1? 840 bytes

• If 7 bits represent an ASCII code what is the Baud Rate


960
16550 UART in PCs
USART of ATmega16
Modes
• Asynchronous Normal Mode
• Asynchronous Double Speed Mode
• Synchronous Mode (Three wire)
• Multiprocessor mode
RS232 and TTL interfacing
Logic RS232 TTL
1 -15V to -3V +2V to +5V
0 +3V to +15V 0V to 0.8V

• RS232 has different voltages compared to AtMega


• A Voltage Level Controller is required
• MAX232 is often use
Block Diagram

Full-duplex:
Independent
Receive and
Transmit Registers
TX, Rx pins to o
devices

Configuration
/Monitor
Registers
Clock Generation

• Division can be by 16, 8 and 2


– Normal serial communication: device by 16
– Double speed communication divide by 8
– Synchronous communication divide by 2
USART Hardware Elements
• Clock Generator
– To provide clock source
– To set baud rate using UBRR register
• Transmitter
– Send a character through TxD
– Handle Start/Stop, parity bits
• Receiver
– Receive a character through RxD pin
• Registers
– To configure, control and monitor the operation
Registers
• Baud Rate Registers
– UBRRH and UBRRL
• Control and Status Regsiters
– UCSRA
– UCSRB
• Data Register
– UDR
Baud rate generator
Control and Status Register A
Control and Status Register B
Control and Status Register C
Setting character size
Data Register UDR
Main Tasks
Initializing serial port
Initializing serial port: Example
• Baud rate 1200
• No Parity
• 1 Stop bit
• 8 Data bot
• 1 MhZ clock
Initializing serial port: Example
Sending a character
Sending a character:Example
Receiving a Character
Receiving a Character: Example
Sending/Receiving a formatted string
Sending/Receiving a formatted string
Sending/Receiving a formatted string
Example
Example application
Camera.c
Debugging tool: Hyper Termninal
Hyper Terminal
Hyper Terminal
Serial Communication in ATMega16
• Has 3 sub systems for serial communication
• Universal Synchronous & Asynchronous
Receiver and Transmitter (USART)
• Serial Peripheral Interface (SPI)
• Two wire Serial Interface (TWI)
Serial Peripheral Interface (SPI)
• The receiver and transmitter share a common
clock line.
• Supports higher data rates.
• The transmitter is designated as the master,
the receiver as the slave.
• Examples of devices using SPI
– liquid crystal display
– high-speed analogue-to-digital converter
Inter Integrated Circuits (I2C) or
Two-wire Serial Interface (TWI)
• Connect several devices such as
microcontrollers and display boards, using a
two-wire bus.
• Up to 128 devices are supported.
• Each device has a unique address and can
exchange data with other devices in a small
network.

You might also like