Chapter 5 - AVR Serial Communication
Chapter 5 - AVR Serial Communication
Chapter 5
AVR Serial Communication
www.vut.ac.za
1
CONTENTS
2
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
3
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
4
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
5
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The clock generation logic generates the base clock for the Transmitter and Receiver.
▪ The USART supports four modes of clock operation:
▪ Normal Asynchronous,
▪ Double Speed Asynchronous
▪ Master Synchronous
▪ Slave Synchronous mode.
▪ The UMSEL bit in USART Control and Status Register C (UCSRC) selects between asynchronous and synchronous
operation.
▪ Double Speed (Asynchronous mode only) is controlled by the U2X found in the UCSRA Register.
▪ When using Synchronous mode (UMSEL = 1), the Data Direction Register for the XCK pin (DDR_XCK) controls
whether the clock source is internal (Master mode) or external (Slave mode).
▪ The XCK pin is only active when using Synchronous mode.
6
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ Internal clock generation is used for the asynchronous and the synchronous master modes of operation.
▪ The USART Baud Rate Register (UBRR) and the down-counter connected to it function as a programmable
prescaler or baud rate generator.
▪ The down-counter, running at system clock (fosc), is loaded with the UBRR value each time the counter has counted
down to zero or when the UBRRL Register is written.
▪ A clock is generated each time the counter reaches zero. This clock is the baud rate generator clock output (=
fosc/(UBRR+1)).
▪ The Transmitter divides the baud rate generator clock output by 2, 8 or 16 depending on mode.
▪ The baud rate generator output is used directly by the receiver’s clock and data recovery units.
▪ However, the recovery units use a state machine that uses 2, 8 or 16 states depending on mode set by the state of the
UMSEL, U2X and DDR_XCK bits
7
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
Signal description:
• txclk - Transmitter clock (Internal Signal).
• rxclk - Receiver base clock (Internal Signal).
• xcki - Input from XCK pin (Internal Signal).
Used for synchronous slave operation.
• Xcko - Clock output to XCK pin (Internal
Signal). Used for synchronous master
operation.
• fosc - XTAL pin frequency (System Clock).
8
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
USART Initialization
▪ The USART has to be initialized before any communication can take place.
▪ The initialization process normally consists of setting the baud rate, setting frame format and enabling the
Transmitter or the Receiver depending on the usage.
▪ For interrupt driven USART operation, the Global Interrupt Flag should be cleared (and interrupts globally disabled)
when doing the initialization.
▪ Before doing a re-initialization with changed baud rate or frame format, be sure that there are no ongoing
transmissions during the period the registers are changed.
▪ The TXC Flag can be used to check that the Transmitter has completed all transfers, and the RXC Flag can be used
to check that there are no unread data in the receive buffer.
▪ Note that the TXC Flag must be cleared before each transmission (before UDR is written) if it is used for this
purpose.
▪ The following simple USART initialization code examples show one assembly and one C function that are equal in
functionality.
▪ The examples assume asynchronous operation using polling (no interrupts enabled) and a fixed frame format. The
baud rate is given as a function parameter. 9
▪ • For the assembly code, the baud rate parameter is assumed to be stored in the r17:r16 registers.
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
USART Initialization
▪ For the assembly code, the baud rate parameter is assumed to be stored in the R17:R16 registers.
▪ When the function writes to the UCSRC Register, the URSEL bit (MSB) must be set due to the sharing of I/O
location by UBRRH and UCSRC.
▪ More advanced initialization routines can be made that include frame format as parameters, disable interrupts and so
on.
▪ However, many applications use a fixed setting of the Baud and Control Registers, and for these types of
applications the initialization code can be placed directly in the main routine, or be combined with initialization code
for other I/O modules.
10
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
11
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The USART transmitter has two flags that indicate its state: USART Data Register Empty (UDRE) and Transmit
Complete (TXC). Both flags can be used for generating interrupts.
▪ The Data Register Empty (UDRE) Flag indicates whether the transmit buffer is ready to receive new data.
▪ This bit is set when the transmit buffer is empty, and cleared when the transmit buffer contains data to be transmitted that
has not yet been moved into the Shift Register.
▪ For compatibility with future devices, always write this bit to zero when writing the UCSRA Register.
▪ When the Data Register empty Interrupt Enable (UDRIE) bit in UCSRB is written to one, the USART Data
Register Empty Interrupt will be executed as long as UDRE is set (provided that global interrupts are enabled).
▪ UDRE is cleared by writing UDR.
▪ When interrupt-driven data transmission is used, the Data Register Empty Interrupt routine must either write new
data to UDR in order to clear UDRE or disable the Data Register empty Interrupt, otherwise a new interrupt will
occur once the interrupt routine terminates.
12
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The Transmit Complete (TXC) Flag bit is set one when the entire frame in the transmit Shift Register has been
shifted out and there are no new data currently present in the transmit buffer.
▪ The TXC Flag bit is automatically cleared when a transmit complete interrupt is executed, or it can be cleared by writing
a one to its bit location.
▪ The TXC Flag is useful in half-duplex communication interfaces (like the RS485 standard), where a transmitting
application must enter receive mode and free the communication bus immediately after completing the
transmission.
▪ When the Transmit Compete Interrupt Enable (TXCIE) bit in UCSRB is set, the USART Transmit Complete
Interrupt will be executed when the TXC Flag becomes set (provided that global interrupts are enabled).
▪ When the transmit complete interrupt is used, the interrupt handling routine does not have to clear the TXC Flag,
this is done automatically when the interrupt is executed.
13
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The USART Receiver is enabled by writing the Receive Enable (RXEN) bit in the UCSRB Register to one.
▪ When the receiver is enabled, the normal pin operation of the RxD pin is overridden by the USART and given the
function as the receiver’s serial input.
▪ The baud rate, mode of operation and frame format must be set up once before any serial reception can be done.
▪ If synchronous operation is used, the clock on the XCK pin will be used as transfer clock.
14
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The USART Receiver has one flag that indicates the receiver state.
▪ The Receive Complete (RXC) Flag indicates if there are unread data present in the receive buffer.
▪ This flag is one when unread data exist in the receive buffer, and zero when the receive buffer is empty (that is, does not
contain any unread data).
▪ If the receiver is disabled (RXEN = 0), the receive buffer will be flushed and consequently the RXC bit will become
zero.
▪ When the Receive Complete Interrupt Enable (RXCIE) in UCSRB is set, the USART Receive Complete Interrupt
will be executed if the RXC Flag is set (provided that global interrupts are enabled).
▪ When interrupt-driven data reception is used, the receive complete routine must read the received data from UDR to
clear the RXC Flag, otherwise a new interrupt will occur once the interrupt routine terminates.
▪
15
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
16
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ USART Control and Status Register A (UCSRA) – It is used for control and status flags
▪ Bit 7 - USART Receive Complete (RXC)
▪ This flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty (that is, does not contain any unread
data).
▪ If the receiver is disabled, the receive buffer will be flushed and consequently the RXC bit will become zero.
▪ The RXC Flag can be used to generate a Receive Complete interrupt
▪ Bit 6 – USART Transmit Complete (TXC)
▪ This flag bit is set when the entire frame in the transmit Shift Register has been shifted out and there are no new data currently present in the transmit
buffer (UDR).
▪ The TXC Flag bit is automatically cleared when a transmit complete interrupt is executed, or it can be cleared by writing a one to its bit location.
▪ The TXC Flag can generate a Transmit Complete interrupt
▪ Bit 5 – USART Data Register Empty (UDRE)
▪ The UDRE Flag indicates if the transmit buffer (UDR) is ready to receive new data. If UDRE is one, the buffer is empty, and therefore ready to be
written.
▪ The UDRE Flag can generate a Data Register empty Interrupt
▪ UDRE is set after a reset to indicate that the transmitter is ready.
▪ Bit 4 – Frame Error (FE)
▪ This bit is set if the next character in the receive buffer had a Frame Error when received.
▪ that is, when the first stop bit of the next character in the receive buffer is zero. This bit is valid until the receive buffer (UDR) is read.
▪ The FE bit is zero when the stop bit of received data is one.
17
▪ Always set this bit to zero when writing to UCSRA.
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ USART Control and Status Register A (UCSRA) – It is used for control and status flags
▪ Bit 3 – Data Over Run (DOR)
▪ This bit is set if a Data OverRun condition is detected.
▪ A Data OverRun occurs when the receive buffer is full (two characters), it is a new character waiting in the receive Shift Register, and a new start bit
is detected.
▪ This bit is valid until the receive buffer (UDR) is read.
▪ Always set this bit to zero when writing to UCSRA.
▪ Bit 2 – Parity Error (PE)
▪ This bit is set if the next character in the receive buffer had a Parity Error when received and the parity checking was enabled at that point (UPM1 =
1).
▪ This bit is valid until the receive buffer (UDR) is read.
▪ Always set this bit to zero when writing to UCSRA.
▪ Bit 1 – Double the USART Transmission Speed (U2X)
▪ This bit only has effect for the asynchronous operation.
▪ Write this bit to zero when using synchronous operation.
▪ Writing this bit to one will reduce the divisor of the baud rate divider from 16 to 8 effectively doubling the transfer rate for asynchronous
communication.
▪ Bit 0 – Multi-processor Communication Mode (MPCM)
▪ This bit enables the Multi-processor Communication mode.
▪ When the MPCM bit is written to one, all the incoming frames received by the USART receiver that do not contain address information will be
ignored. 18
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
22
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The Two-wire Serial Interface (TWI) is ideally suited for typical microcontroller applications.
▪ The TWI protocol allows the systems designer to interconnect up to 128 different devices using only two bi-
directional bus lines, one for clock (SCL) and one for data (SDA).
▪ The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines.
▪ All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are
inherent in the TWI protocol.
▪ The Main features of TWI as:
▪ Simple Yet Powerful and Flexible Communication Interface, Only Two Bus Lines Needed
▪ Both Master and Slave Operation Supported
▪ Device Can Operate as Transmitter or Receiver
▪ 7-bit Address Space allows up to 128 Different Slave Addresses
▪ Multi-master Arbitration Support
▪ Up to 400kHz Data Transfer Speed
▪ Slew-rate Limited Output Drivers
▪ Noise Suppression Circuitry Rejects Spikes on Bus Lines
▪ Fully Programmable Slave Address with General Call Support
▪ Address Recognition causes Wake-up when AVR is in Sleep Mode 23
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
TWI Terminology
24
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
26
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
29
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
30
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
31
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
Transmission Modes
32
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ The Serial Peripheral Interface (SPI) allows high-speed synchronous data transfer between the ATmega32 and peripheral devices or
between several AVR devices. The
▪ The SPI peripheral logic of the AVR supports all four standard SPI modes of operation.
▪ I/O pins on the AVR device may be configured to act as the MOSI, MISO, and SCK1 signals used by SPI.
▪ These pins are different from the RxD and TxD (receive data and transmit data) pins used by the USART.
▪ ATmega32 SPI includes the following features:
▪ Full-duplex, Three-wire Synchronous Data Transfer
▪ Master or Slave Operation
▪ LSB First or MSB First Data Transfer
▪ Seven Programmable Bit Rates
▪ End of Transmission Interrupt Flag
▪ Write Collision Flag Protection
▪ Wake-up from Idle Mode
▪ Double Speed (CK/2) Master SPI Mode
33
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ Slave Mode
▪ When the SPI is configured as a Slave, the Slave Select (SS) pin is always input.
▪ When SS is held low, the SPI is activated, and MISO becomes an output if configured so by the user. All other pins are inputs.
▪ When SS is driven high, all pins are inputs except MISO which can be user configured as an output, and the SPI is passive, which
means that it will not receive incoming data.
▪ Note that the SPI logic will be reset once the SS pin is driven high.
▪ The SS pin is useful for packet/byte synchronization to keep the slave bit counter synchronous with the master clock generator.
▪ When the SS pin is driven high, the SPI Slave will immediately reset the send and receive logic, and drop any partially received data
in the Shift Register
34
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
▪ Master Mode
▪ When the SPI is configured as a Master (MSTR in SPCR is set), the user can determine the direction of the SS pin.
▪ If SS is configured as an output, the pin is a general output pin which does not affect the SPI system.
▪ Typically, the pin will be driving the SS pin of the SPI Slave. If SS is configured as an input, it must be held high to ensure Master
SPI operation.
▪ If the SS pin is driven low by peripheral circuitry when the SPI is configured as a Master with the SS pin defined as an input, the
SPI system interprets this as another master selecting the SPI as a slave and starting to send data to it.
▪ To avoid bus contention, the SPI system takes the following actions: 1. The MSTR bit in SPCR is cleared and the SPI system
becomes a slave.
▪ As a result of the SPI becoming a slave, the MOSI and SCK pins become inputs. 2. The SPIF Flag in SPSR is set, and if the SPI
interrupt is enabled, and the I-bit in SREG is set, the interrupt routine will be executed.
▪ Thus, when interrupt-driven SPI transmission is used in master mode, and there exists a possibility that SS is driven low, the
interrupt should always check that the MSTR bit is still set.
▪ If the MSTR bit has been cleared by a slave select, it must be set by the user to re-enable SPI master mode.
35
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
36
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
37
M o d u l e 5 – AV R S e r i a l C o m m u n i c a t i o n
38
THANK YOU
Andries Po tgieter Blvd. Vanderbij lpark, 1900, So uth Africa | T 098 008 8900 | E [email protected]. za www.vut.ac.za
39