100% found this document useful (1 vote)
72 views

9-Serial Comm (Autosaved)

Serial communication can be done either parallel or serial. Serial communication transfers one bit at a time and has advantages like longer distances, easier synchronization, and lower cost. Asynchronous serial communication does not synchronize clocks between sender and receiver and transfers one character at a time. The RS-232 standard defines serial communication electrical specifications and connectors. Microcontrollers use USART, SPI, and TWI for serial communication. USART supports asynchronous communication and SPI supports synchronous communication. Interrupts can be used to handle serial communication in an ISR for receiving characters asynchronously.

Uploaded by

M ADNAN Z
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
72 views

9-Serial Comm (Autosaved)

Serial communication can be done either parallel or serial. Serial communication transfers one bit at a time and has advantages like longer distances, easier synchronization, and lower cost. Asynchronous serial communication does not synchronize clocks between sender and receiver and transfers one character at a time. The RS-232 standard defines serial communication electrical specifications and connectors. Microcontrollers use USART, SPI, and TWI for serial communication. USART supports asynchronous communication and SPI supports synchronous communication. Interrupts can be used to handle serial communication in an ISR for receiving characters asynchronously.

Uploaded by

M ADNAN Z
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Serial Communication

Modified from Dr. Lam Phung’s Slides


Serial communication ─ The basics
• Computers transfer data in two ways: parallel and
serial.
– Parallel: Several data bits are transferred simultaneously,
e.g. to printers and hard disks.
– Serial: A single data bit is transferred at one time.
• Advantages of serial communication: longer distances,
easier to synchronize, fewer IO pins, and lower cost.
• Serial communication often requires
– Shift registers: convert a byte to serial bits and vice versa.
– Modems: modulate/demodulate serial bits to/from audio
tones.
Synchronous versus asynchronous
• Synchronous serial communication
– The clocks of the sender and receiver are synchronized.
– A block of characters, enclosed by synchronizing bytes, is sent at
a time.
– Faster transfer and less overhead.
– Examples: serial peripheral interface (SPI) by Motorola, binary
synchronous communication (BISYNC) by IBM.
• Asynchronous serial communication
– The clocks of the sender and receiver are not synchronized.
– One character (8 or 7 bits) is sent at a time, enclosed between a
start bit and one or two stop bits. A parity bit may be included.
– Examples: RS-232 by Electronic Industries Alliance, USART of
ATmega16
Data framing examples
Serial communication terminology
• Baud rate: The number of bits sent per second
(bps). Strictly speaking, it is the number of signal
changes per second.
• Parity bit: A single bit for error checking, sent
with data bits to make the total number of 1’s
– even (for even parity) or
– odd (for odd parity).
• Start bit: to indicate the start of a character. Its
typical value is 0.
• Stop bit: to indicate the end of a character. Its
typical value is 1.
The RS-232 standard
• The RS-232 (latest revision RS-232E) is a widely
used standard for serial interfacing.
• It covers four main aspects.
– Electrical: voltage level, rise and fall time, data rate,
distance.
– Functional: of each signal function
– Mechanical: number of pins, shape & dimension of
connectors.
– Procedural: sequence of events for transmitting data.
The RS-232 standard
• RS-232 specifies the baud rate up to 20Kbps, and
the cable length up to 15m. In practice, it
supports up to 56Kbps & 30m of shielded cables.
• It defines 25-pin D connectors. In many cases, 9-
pin connectors are also used.
RS-232 9-pin connector

• Data Terminal Equipment (DTE) essentially refers to the computer.


• Data Communication Equipment (DCE) essentially refers to a
remote device or modem.
• These terms are needed to explain the pin functions
Modem connection

• RS-2322 was originally used with modems to connect two


PCs over the public phone lines.
• When computer A has data to send, it assert its RTS pin.
• Modem A will assert its CTS when it is ready to receive.
• Computer A transmits data through its TXD
Null-modem connection

• RS-232 is now mainly used to connect a


microcontroller with PC or peripheral devices (e.g. GPS
receiver, infrared range finder, camera).
• This configuration is known as null-modem.
• Key idea:
– Connect pin TXD of a DTE with pin RXD of the other DTE.
– Wire other pins to support flow control
RS-232 interface and MAX232 chip
• Compared to TTL
computer
electronics, RS-232
interface uses
different voltage
levels.
• A level converter is
required between
RS-232 interface and
TXD/RXD pins of
microcontroller.
• MAX232 chip is often
used for this
purpose.
Serial comunnication - An example

• The sensor sends data via a serial interface to Bluetooth


transmitter.
• A Bluetooth receiver, connected to a PC, is configured as a
serial port
Serial communication in AVR
• AVR provides three subsystems for serial
communication.
– Universal Synchronous & Asynchronous Receiver &
Transmitter (USART)
– Serial Peripheral Interface (SPI)
– Two-wire Serial Interface (TWI)
• USART:
– We focus on this subsystem in this lecture.
– Supports full-duplex mode between two devices.
– Typically used in asynchronous communication.
– Start bit and stop bit are used for each byte of data.
Serial communication in AVR
• 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.
Serial communication in AVR
• 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.
Serial USART ─ An overview
• USART of the ATmega16
supports
– baud rates from 960bps to
115200bps,
– character size: 5 to 9 bits,
– 1 start bit,
– 1 or 2 stop bits,
– parity bit
(optional: even or odd parity).
• Common baud rates are
19200, 9600, 4800, 2400, and
1200 bps.
Serial USART ─ Block diagram
Serial USART ─ Hardware elements
• USART Clock Generator:
– to provide clock source.
– to set baud rate using UBRR register.
• USART Transmitter:
– to send a character through TxD pin.
– to parity bit, shift register.
• USART Receiver:
– handle start/stop bit framing,
– to receive a character through RxD pin.
– to perform the reverse operation of the transmitter.
• USART Registers:
– to configure, control, and monitor the serial USART.
Serial USART ─ Three groups of
registers
• USART Baud Rate Registers
– UBRRH and UBRRL
• USART Control and Status Registers
– UCSRA
– UCSRB
– UCSRC
• USART Data Registers
– UDR
• Understanding these registers is essential in using
the serial port.
USART Baud Rate Registers
• Two 8-bit registers together define the baud rate.

• Example: Find UBRR registers for baud rate of 1200bps, assuming


system clock is 1MHz.
– UBRR = 1000000/(16 × 1200) ─ 1 = 51 = 0x0033
– Therefore, UBRRH = 0x00 and UBRRL = 0x33
– C code
UBRRH = 0x00; UBRRL = 0x33;
USART Control and Status Register A
(UCSRA)
USART Control and Status Register B
(UCSRB)
USART Data Register
• Register UDR is the buffer for characters sent or
received through the serial port.
• To start sending a character, we write it to UDR.

• To process a received character, we read it from UDR.


Serial USART ─ Main tasks
• There are 4 main tasks in using the serial port.
– Initializing the serial port.
– Sending a character
– Receiving a character.
– Sending/receiving formatted strings
Initializing serial port
Initializing serial port ─ Example
• Initialize serial port to baud rate 1200 bps, no
parity, 1 stop bit, 8 data bits. Assume a clock
speed of 1MHz.
Sending a character
Sending a character ─ Example
• Write a C function to send a character through
serial port
Receiving a character
Receiving a character ─ Example
• Write a C function to receive a character via
serial port
Echo Program
• A simple program which will echo bytes
received on the AVR's USART interface.
• Use Null Modem cabling to PC
Potential Pitfalls
• There are several “gotchas" when dealing with
the USART.
• First, make sure your AVR is running of a reliable,
known clock source with a low error percentage
at your chosen baud rate.
• New AVRs default to their internal oscillators until
their fuses are changed.
• The internal RC oscillator is of too low a tolerance
for use with the USART without external
calibration.
More Commonly used Baudrate
Sending/receiving formatted strings
• In ANSI C, the header file <stdio.h> has two functions for
formatted strings: printf and scanf.
• Function printf sends a formatted string to the standard
output device, which is usually the video display.

• Function scanf reads a formatted string from the standard


input device, which is usually the keyboard
Sending/receiving formatted strings
• Being able to send/receive formatted strings through a
serial port is useful in microcontroller applications.
• To this end, we configure the serial port as the
standard input and output devices.
• General steps:
1. Write two functions to send and receive a character via
serial port.
2. In main(), call fdevopen() to designate the two functions
as the handlers for standard output and input devices.
3. Use printf/scanf as usual. Formatted strings will be
sent/received via serial port.
Sending/receiving formatted strings ─
Example
Debugging tool: Hyper Terminal
• Sending/receiving data through serial port is useful for
debugging a microcontroller program.
• A program for monitoring serial data is Hyper Terminal
(It is part of Windows XP, its 3 files can be copied to run
on Windows 7).
• Hyper Terminal is used to
– create a serial connection between the PC and the
microcontroller.
– send a text string to the microcontroller.
– receive a text string sent from the microcontroller.
• For example, let’s use Hyper Terminal to debug the
program echo.c
Using Interrupt on USART

Enable RX Interrupt and later Enable Global Interrupt Flag.


ISR – Serial Comm
• ISR to deal with the serial reception.
Populating ISR
• We want it to echo back the byte that is sent, so we'll move
our main loop code:

• since the ISR only fires when a byte is received, and only one
byte is sent after each reception, we can guarantee that both
checks are now redundant.
Complete Code

You might also like