Microprocessor Theory and Applications
CHAPTER 11
AVR Serial Port Programming
in Assembly and C
Assoc.Prof. Dr.Yuttapong Rangsanseri
Parallel vs. Serial Communication
Parallel Data Transmission
RECEIVER
♦ N bits transmitted at a
time over N data lines
♦ Synchronization
among all N bits
♦ Note: each N bit is
called a word
TRANSMITTER
2 Microprocessor Theory and Applications
Parallel vs. Serial Communication (2)
RECEIVER Serial Data Transmission
♦ Transfers one bit at a time
on one data line
TRANSMITTER
3 Microprocessor Theory and Applications
Parallel vs. Serial Communication (3)
Comparison
♦ Parallel requires Serial requires less
more transfer lines transfer lines
♦ Bits have to be Transfers one bit at
synchronized a time
♦ Fast, but Slow comparatively,
expensive but less expensive
4 Microprocessor Theory and Applications
Transmission Modes
♦ Simplex transmissions – data can be transmitted
in one direction only
Radio and television broadcast
Radio controlled models
Surveillance cameras
Telemetry
♦ Duplex transmission – data can be both
transmitted and received:
Half duplex – data is transmitted one way at a time
walkie-talkie
Full duplex – data can go both ways at the same time
Twisted-pairs Ethernet
5 Microprocessor Theory and Applications
Types of Serial Communication
♦ Serial data communication uses two methods
Synchronous - transfers a block of data (characters)
at a time
Asynchronous - transfers a single byte at a time
♦ Special IC chip referred to as
UART (universal asynchronous receiver-transmitter)
USART (universal synchronous-asynchronous
receiver-transmitter)
♦ AVR chip has a built-in USART
6 Microprocessor Theory and Applications
Synchronous Serial Communication
♦ Requires clock signal to synchronize transmitter
and receiver
♦ Continuous transmission to keep clock
synchronized
♦ Data transfer rate is determined by clock rate
7 Microprocessor Theory and Applications
Asynchronous Serial Communication
♦ Transmitter and Receiver operate
independently
Transmitter sends data at any time
Receiver is ready to accept data at all times
♦ No need for clock signals
♦ …but during transmission, format and
transfer rate of data must match
8 Microprocessor Theory and Applications
Asynchronous Serial Communication
♦ With asynchronous communication, the transmitter and
receiver do not share a common clock
Add: Start, Stop, Parity Bits Remove: Start, Stop, Parity Bits
Transmitter + – Receiver
Data
1 byte-wide Data 1 byte-wide Data
The Transmitter The Receiver
♦ Shifts the parallel data ♦ Extracts the data using its
onto the serial line using own clock
its own clock
♦ Converts the serial data back to
♦ Also adds the start, stop the parallel form after stripping
and parity check bits off the start, stop and parity bits
9 Microprocessor Theory and Applications
Data Format
♦ Start bit – indicates the beginning of word
♦ Data bit – data user is transmitting
♦ Parity bit – checks integrity of data
♦ Stop bit – indicates the end of word
Start Bit Parity Bit 1 or 2 Stop Bits
D0 D1 D2 D3 D4 D5 D6 D7
1 Asynchronous Byte
10 Microprocessor Theory and Applications
Baud & Bitrate
♦ Baud rate is the number of line state changes
possible per second
♦ Bit rate (bps) is the number of bits transmitted per
second
♦ Direct wired connection (without modulation) has two line
states (high/low), can be represented with one bit
1 baud = 1 bit
♦ If a hardware can produce and recognize more than two
line states using voltage, frequency, or phase modulation
resulting in more bits per baud
bps = baud rate x number of bits per baud
11 Microprocessor Theory and Applications
Baud & Bitrate (2)
♦ Not all bits transmitted are data
♦ Start/stop/parity bits are transmission overhead
♦ Throughput = data transmission excluding 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
12 Microprocessor Theory and Applications
UART
♦ The UART, or Universal Asynchronous Receiver
Transmitter, provides hardware support for a serial
port on AVR processors
Signaling is compatible with PC/Mac/Unix serial (RS-232C)
♦ The UART provides:
Parallel-to-Serial and Serial-to-Parallel conversion
Start and Stop Bit framing
Parity Generation
Baud-Rate Generation (2400-115.2kbps)
Interrupts
Transmit Complete
Transmit Data Register Empty
Receive Complete
13 Microprocessor Theory and Applications
RS-232 Standards
♦ An old standard for serial communication
♦ Data
Start bit
6,7,8,9 data bits
Parity bit optional
Stop bit
♦ Voltage levels
“1” (mark) is represented by -3 to -25 V
“0” (space) is represented by +3 to +25 V
Signal levels of ±5 V, ±10 V, ±12 V, and ±15 V are all commonly seen
depending on the voltages available to the line driver circuit
14 Microprocessor Theory and Applications
RS232 Character Transmission
15 Microprocessor Theory and Applications
RS-232 Null Modem Connection
♦ 2 types of connectors: DB-25 and DB-9
♦ Since it is asynchronous, no external clock is
needed, only 3 wires are required for the simplest
RS232 connection (GND, TxD, RxD)
16 Microprocessor Theory and Applications
Connecting AVR to RS-232 Port
♦ The first step to connecting a device to the RS-232 port is to transform
the RS-232 levels into 0 and 5 V
♦ This is done by RS-232 Level Converters
♦ MAX232 includes a Charge Pump, which generates +10V and -10V
from a single 5V supply. This IC also includes two receivers and two
transmitters in the same package
17 Microprocessor Theory and Applications
AVR Serial Port Programming
There are 5 registers associated with USART
♦ UDR
(USART Data Register)
♦ UCSRA, UCSRB, UCSRC
(USART Control Status Register)
♦ UBRR
(USART Baud Rate Register)
18 Microprocessor Theory and Applications
UBRR and Baud Rate in AVR
UBRRH
UBRRL
♦ USART Baud Rate Register (12 bits)
♦ The most significant byte has a shared address with UCSRC
Desired Baud Rate = Fosc / (16(X+1))
X = (Fosc / (16(Desired Baud Rate))) – 1
X is the value we load into the UBRR[11:0]
♦ Assuming that Fosc = 8 MHz
Desired Baud Rate = Fosc/16(X+1) = 8MHz/16(X+1) = 500kHz/(X+1)
X = (500 kHz/ Desired Baud Rate) - 1
19 Microprocessor Theory and Applications
Typical Baud Rate
20 Microprocessor Theory and Applications
21 Microprocessor Theory and Applications
UDR and USART Data I/O
♦ To provide a full-duplex, there are 2 shift registers referred to as
Transmit Shift Register and Receive Shift Register
♦ Share the same address and named USART Data Register (UDR)
22 Microprocessor Theory and Applications
UCSRs and USART Configurations
♦ USART Control and Status Registers
♦ Three 8-bit registers to control the USART
operation
♦ They are UCSRA, UCSRB, and UCSRC
23 Microprocessor Theory and Applications
UCSRA
24 Microprocessor Theory and Applications
UCSRA (2)
25 Microprocessor Theory and Applications
UCSRB
26 Microprocessor Theory and Applications
UCSRB (2)
27 Microprocessor Theory and Applications
UCSRC
Note on bit7: UCSRC and UBRR share the same
address. Set URSEL=1 when you want the data to
be written to UCSRC, otherwise set URSEL=0 to
write to UBRR
28 Microprocessor Theory and Applications
UCSRC (2)
29 Microprocessor Theory and Applications
UCSZ (3 bits)
30 Microprocessor Theory and Applications
UCSRB RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
UCSRC URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
31 Microprocessor Theory and Applications
UCSRB RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
UCSRC URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
32 Microprocessor Theory and Applications
USART in Tx Mode
UCSRA RXC TXC UDRE FE DOR PE U2X MPCM
UCSRB RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
UCSRC URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
33 Microprocessor Theory and Applications
USART in Rx Mode
UCSRA RXC TXC UDRE FE DOR PE U2X MPCM
UCSRB RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
UCSRC URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
34 Microprocessor Theory and Applications
Doubling the Baud Rate in AVR
♦ There are two ways to increase the baud rate of
data transfer in AVR:
35 Microprocessor Theory and Applications
Doubling the Baud Rate in AVR (2)
36 Microprocessor Theory and Applications
AVR Serial Port Programming in C
UCSRB RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
UCSRC URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
37 Microprocessor Theory and Applications
AVR Serial Port Programming in C (2)
38 Microprocessor Theory and Applications
AVR Serial Port Programming in C (3)
39 Microprocessor Theory and Applications