11 Serial Communication v2
11 Serial Communication v2
Chapter 11
Serial Communication
Topics
Communication theory
Parallel vs. Serial
Direction: Simplex, Half duplex, Full duplex
Synchronization: Synchronous vs.
Asynchronous
UART protocol
UART in AVR
UART Registers
Program Examples
SPI Communication
2
Parallel vs. Serial
Parallel
For short distances
Not applicable for long distances
More expensive
Cross-talk problem
D7
3
Direction
Simplex
Half Duplex
Full Duplex
4
Synchronization
Synchronous
Asynchronous
5
USART (Universal Synchronous Asynchronous
Receive Transmit)
USART devices can be used to communicate
synchronously and asynchronously (UART) .
Since the synchronous capability of USART is not
used nowadays, we concentrate on UART.
The Universal Asynchronous Receiver/Transmitter
(UART) is the implementation of the RS-232C
standard.
Typical speed: 9600, 19200, 115200 bps (baud rate)
Baud rate: The rate as which bits are transmitted per
each second.
kbps = 1000 bits /second NOT 1024 bits/second
6
UART Protocol
Serial
Asynchronous
Full duplex
7
UART Protocol
Each byte is encapsulated with a start, stop (1 or 2
bits), and optionally a parity bit. Bits are sent
serially.
b7 b6 b5 b4 b3 b2 b1 b0
Bits are transmitted with the least significant bits first. Each
byte is preceded by a start bit (usually 0) and a stop bit
(usually 1). Stop bits can be two bits. Parity is optional.
9
UART Protocol
When measuring speed, K means 103 not 1024.
Similarly, M means 106, not 1024*1024.
xYz
Data width Stop bits width
Parity (E, O, N)
10
UART Protocol
Example: 8N1: 8-bit data, No parity, 1 stop bit
11
Review Questions
12
Review Questions
13
Using RS-232C in AVR
The pins that are shown in the adjacent
figure are used to interface the AVR RXD 2 (PD0)
microcontroller to another serial device
RXD: Receive bits
TXD 3 (PD1)
TXD: Transmit bits
ATmega8
RS232C : One of the oldest serial
communication standards implemented by
UART
Connecting AVR to an external
RS232C device may require
additional MAX232 circuitry to
convert RS232 voltage levels to
TTL levels and vice versa.
14
MAX232
15
UART registers in AVR
Control registers: (initialize speed, data size,
parity, etc.)
UBRR, UCSRB, and UCSRC
Send/receive register
UDR
Status register
UCSRA
16
UBRR Register
To set the baud rate, update the content of the
register (Only the least significant 12 bits are
used) 15 11 0
UBRRH UBRRL
-1
18
Review Questions
19
Some Standard Baud rates
1200
2400
4800
9600
19,200
38,400
57,600
115,200
20
UCSRC [Data Format]
The figure shows the register bit map:
21
UCSRC (Example)
Configure UCSRC register for 7E2 data format:
1 0 1 0 1 1 0 0
UCSRC = 0xAC;
22
UCSRA
The figure shows the register bit map:
Ready to transmit
(check before
transmission)
23
UCSRB
TXCI
RXCIE RXCIE
E
UDRIE RXEN TXEN
24
UCSRB (Example)
Configure UCSRB by enabling sending and
receiving data on UART:
TXCI
RXCIE RXCIE
E
UDRIE RXEN TXEN
0 0 0 1 1 0 0 0
26
Polling-based UART send and receive
int main()
{
initUART(); // initialize UART
// Send a character
char x = ‘A’;
// wait until device is ready to transmit
data
while ( (UCSRA & 0x20) == 0 );
UDR = x;
// Receive a character
// wait until device is ready to receive
data
while ( (UCSRA & 0x80) == 0);
x = UDR;
return 0;
27
Example 1: sending character ‘G’ continuously
#define F_CPU 16000000UL
#include <avr/io.h>
return 0;
}
28
Example 2 : Receives bytes of data serially and puts
them on Port B.
#define F_CPU 16000000UL
#include <avr/io.h>
int main (void)
{
DDRB = 0xFF; //Port B is output
UCSRB = (1<<RXEN); //initialize USART
UCSRC = (1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL); // 8N1
UBRRL = 103;
while(1)
{
//wait until receive new data
while (! (UCSRA & (1<<RXC)));
PORTB = UDR;
}
return 0;
}
29
Example 3
Transfer
“Hello” from
ATmega8 to 0x8E;
PC (baud rate
9600, 8N2
data mode)
30
UART Programming using Interrupts
It is a waste of microcontroller time to poll RXC and
UDRE flags to check whether data is received or we are
ready to transmit
31
Example 1 (interrupt-based) : sending character
‘G’ continuously
#include <avr/io.h>
#include <avr/interrupt.h>
32
Example 2 (interrupt-based) : Receive bytes of data
serially and place them on Port B.
#include <avr/io.h>
#include <avr/interrupt.h>
ISR (USART_RXC_vect)
{
PORTB = UDR;
}
int main (void)
{
DDRB = 0xFF; //Port B is output
UCSRB = (1<<RXEN)|(1<<RXCIE); //enable receive and RXC int.
UCSRC = (1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL = 103;
sei(); //enable interrupts
while(1); // wait forever
return 0;
}
33
Serial Peripheral Interface (SPI)
34
Serial Peripheral Interface (SPI)
SPI is faster than RS232C, where it can achieve speeds of up to
10Mbps.
35
SPI Signals
Four lines for communication
SCLK: Serial clock (generated by Master)
MOSI: Master-Out, Slave-In (transfer from Master to Slave)
MISO: Master-In, Slave-Out (transfer from Slave to Master)
SS: Slave Select (Master initiates the communication with the
slave)
36
SPI Configurations
Standard Daisy chain
configuration configuration
37
SPI Data Transfer
1. Communication begins when Master activates SS signal.
2. Master and Slave can place the data in the Data Register, which
is usually 8 bits (It can also be 16, and 32 bits).
3. Master toggles SCLK.
4. At each clock pulse, one bit is shifted out from Master Data
Register to Salve Data Register. Similarly, a bit is shifted out from
the Slave Data Register to the Master Data Register. This process
continues until the word is transferred to the other side
completely.
38
SPI in AVR
The figure shows how SPI signals are mapped to ATmega8 pins:
PB5
PB3
PB4
PB2
39
SPI Control Register (SPCR)
The figure shows the register bit map:
Control Speed
00
SPI Enable 0 Slave
1 Master
01
SPI Interrupt Enable:
0 No Interrupt
1 Interrupt after 10
transmission
or reception of a word 11
40
SPI Status Register (SPSR)
The figure shows the register bit map:
41
SPI Example
Configure AVR as Master, no interrupts (i.e. poling
mode), speed is 1/16 of the input lock.
42
SPI Example (cont.)
Transfer a byte
// This function is called from main
void sendByte(uint8_t x)
{
SPDR = x;
// Wait until transfer is done.
while ( (SPSR & 0x80) == 0) ;
}
Receive a byte
// This function is called from main
uint8_t getByte()
{
// Wait until transfer is done.
while ( (SPSR & 0x80) == 0) ;
return SPDR;
}
43
SPI Example
Write C program for both SPI Master and Slave to
transfer 4-bit data (input from switch at Port C of
Master), and output the on LEDs of Slave (using Port C
of Slave).
44
Master Code
45
Slave Code
SPCR
46
Inter Integrated Communication (I2C)
47
I2C Details
Two lines
Serial data line (SDA)
Serial clock line (SCL)
Only two wires for connecting multiple
devices
Multi Master capability
Many devices can be connected on two
lines
48
I2C Details
Each I2C device recognized by a unique address
49
How can any device
transfer or receive on the same two wires?
Pull ups and high-impedance mode pins
Wires default to being “on”, any device can
make a wire go “off”.
SPI and UART can’t do this, why?
50
Bit Transfer on the I2C Bus
In normal data transfer, the data line only
changes state when the clock is low
Start and Stop conditions are exceptions
SDA
SCL
Data line stable; Change
Data valid of data
allowed
51 of 40
51
Start and Stop Conditions
SDA SDA
SCL SCL
Start Stop
Condition Condition
52 of52
40
I2C Addressing
Each node has a unique 7 (or 10) bit
address
53
53 of 40
I2C-Connected System
54
Master-Slave Relationships
Who is the master?
master-transmitters
master-receivers
55
Master-Slave Relationships
Who is the master?
master-transmitters
master-receivers
If two devices want to take control of the bus, which one gets priority?
Bus Arbitration
56
Exercise: How fast can I2C run?
• How fast can you run it?
• Assumptions
– 0’s are driven
– 1’s are “pulled up”
• Some working figures
– Rp = 10 kΩ
– Ccap = 100 pF
– VDD = 5 V
– Vin_high = 3.5 V
• Recall for RC circuit
– Vcap(t) = VDD(1-e-t/τ) (1)
– Where τ = RC
– τ = 1us
– t= 1.2 us from eq 1
57