Serial Input/Output. Lecturer: Sri Parameswaran Notes by: Annie Guo

Similar documents
EE 308: Microcontrollers

Unit D. Serial Interfaces. Serial vs. Parallel. Serial Interfaces. Serial Communications

Concept of Serial Communication

Electronics / Water analogy. Resistor. Inductance. Capacitor. Water Electronics Energy - Energy Pressure - Voltage Flow - Current Volume - Charge

APPLICATION BULLETIN. SERIAL BACKGROUNDER (Serial 101) AB23-1. ICS ICS ELECTRONICS division of Systems West Inc. INTRODUCTION CHAPTER 2 - DATA FORMAT

ECE 4510/5530 Microcontroller Applications Week 6 Lab 5

Lecture #3 RS232 & 485 protocols

Chapter 9: Serial Communication Interface SCI. The HCS12 Microcontroller. Han-Way Huang. September 2009

Microcontrollers. Serial Communication Interface. EECE 218 Microcontrollers 1

ROM/UDF CPU I/O I/O I/O RAM

ATmega 16. Dariusz Chaberski

Local Asynchronous Communication. By S.Senthilmurugan Asst.Professor/ICE SRM University. Chennai.

EECE494: Computer Bus and SoC Interfacing. Serial Communication: RS-232. Dr. Charles Kim Electrical and Computer Engineering Howard University

PERIPHERAL INTERFACING Rev. 1.0

Fanuc Serial (RS232) Communications Information

King Fahd University of Petroleum & Minerals Computer Engineering Dept

CL4790 HARDWARE INTEGRATION GUIDE VERSION 3.0. Americas: Europe: Hong Kong:

Serial Communications RS232, RS485, RS422

PRODUCT INFORMATION B&B ELECTRONICS. Port-Powered RS-232 Fiber Optic Modem with Handshake Support. Model 9PFLST. Description. RS-232 Connections

Universal RS232/CMOS to RS422/RS485 Converter

APPLICATION NOTE ANI6. Connecting the SP504 Multiprotocol Transceiver to the 85C30 Universal Enhanced Serial Communications Controller (ESCC)

Programmable communications interface (PCI)

CL4490 HARDWARE INTEGRATION GUIDE VERSION 1.0. FCC Notice.

DigiPoints Volume 1. Student Workbook. Module 2 Modem Overview

9600 bps, Dual Modem 170 Card Private Wire Only

Model 4xx. Plug-in Series Of FSK Modems USER GUIDE. (TI) 20 Jan 06 DWG: A GDI COMMUNICATIONS LLC PO Box I-80 Exit 1 Verdi, NV 89439

2070-6B Manual B Manual. Dual 9600 baud Modem For The 2070 Controller. GDI B Manual. Rev B

CONV232/422 SERIAL DATA CONVERTER

TCSS 372 Laboratory Project 2 RS 232 Serial I/O Interface

Universal Converter Covers All the Bases- RS-232 to 4-wire RS-422, 2-wire or 4-wire RS-485

DSP4xxFP SA USER S MANUAL. A01561 Rev. A. This Manual covers all configurations of the DSP4xxSA Modem with the Serial Number SA and up.

SMARTALPHA RF TRANSCEIVER

Home Automation, Inc. Omnistat2. RC-1000 and RC-2000 Communicating Thermostat. Serial Protocol Description

SC16C550B. 1. General description. 2. Features. 5 V, 3.3 V and 2.5 V UART with 16-byte FIFOs

Applications. Operating Modes. Description. Part Number Description Package. Many to one. One to one Broadcast One to many

Asynchronous Serial Communications The MC9S12 Serial Communications Interface (SCI) Asynchronous Data Transfer

A50/A51/A60. June 2008 (4th Edition) All Rights Reserved

EIA-232 Information. DTE Signal Direction. DCE Signal Direction. Signal EIA

USER'S MANUAL. Model : K

CHAPTER 5 TRANSMISSION OF DIGITAL DATA INTERFACES & MODEMS

ANALOG AND DIGITAL PHYSICAL INTERFACES

6551 ASYNCHRONOUS COMMUNICATION INTERFACE ADAPTER

Semiconductor MSM82C51A-2RS/GS/JS UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER

2070-6A Manual A Manual. Dual 1200 baud Modem For The 2070 Controller GDI A MANUAL

SC16C750B. 1. General description. 2. Features. 5 V, 3.3 V and 2.5 V UART with 64-byte FIFOs

RS-232 Electrical Specifications and a Typical Connection

USING RS-232 to RS-485 CONVERTERS (With RS-232, RS-422 and RS-485 devices)

9.4. Synchronization:

PART 4 DIGITAL METHODS. Chapter 13 Digital Fundamentals. Parallel Data Transfer. Fig Parallel data handshaking

MODEL 800. GDI Communications, LLC. Dual 1200 BAUD Modem. Verdi, Nevada. rzidnanga-arnnt= Pnnc1 rsf 17

Modem RS232/RS422/RS485. to MM optic fibers. RS-232 to Multimode Optic. Fiber Converter. Characteristics DIN rail mounting.

+5V Multiprotocol, Software-Selectable Cable Terminator

TS4000 Radio Modem. User s Manual

Modem 1200 baud 500FSD10 EDS500 series - FSK modems

LCC-10 Product manual

Interface: Serial EIA RS-232D/CCITT V.24, DCE; RTS/CTS delay 0,8 or 64 ms (user-selectable).

SC16C650B. 1. General description. 2. Features. 5 V, 3.3 V and 2.5 V UART with 32-byte FIFOs and infrared (IrDA) encoder/decoder

D16550 IP Core. Configurable UART with FIFO v. 2.25

SC16C550 Rev June 2003 Product data General description Features

Chapter 12: Digital Modulation and Modems

Technical description MT24. User manual WARNING GENERAL INSTRUCTIONS GENERAL DESCRIPTION

Series 2000 Reader System. Reader S251B RI-STU-251B. Reference Guide. April 2000 SCBU035

+3.3V Multiprotocol 3Tx/3Rx Software-Selectable Control Transceivers

CL024 USER S GUIDE VERSION

MODEL PAXCDC -SERIAL COMMUNICATIONS PLUG-IN OPTION CARDS

ZCS485 User s Manual. Version ZYPEX, Inc.

G3P-R232. User Manual. Release. 2.06

EXPERIMENT NO:-1. OBJECTIVE: To study Basics of serial communication ports and protocols.

AT-XTR-7020A-4. Multi-Channel Micro Embedded Transceiver Module. Features. Typical Applications

SC16C Description. 2. Features. Dual UART with 32 bytes of transmit and receive FIFOs

RS-232 / 20mA Interface Converter ELO E00Q. Operation manual

Programmable RS-232/RS-485 Transceiver

a6850 Features General Description Asynchronous Communications Interface Adapter

WWVB Receiver/Decoder With Serial BCD or ASCII Interface DESCRIPTION FEATURES APPLICATIONS

+3.3V, Multiprotocol, 3 Tx/3 Rx, Software- Selectable Clock/Data Transceiver

EEL 4744C: Microprocessor Applications. Lecture 9. Part 2. M68HC12 Serial I/O. Dr. Tao Li 1

CDR-915 Data Radio Module INTEGRATOR S GUIDE

MULTI-DROPS DIGITAL MODEM FOR PRIVATE LINE

CL4424. Industrial 2.4GHz ConnexLink. User s Manual Version 1.1

SC16C652B. 1. General description. 2. Features. 5 V, 3.3 V and 2.5 V dual UART, 5 Mbit/s (max.) with 32-byte FIFOs and infrared (IrDA) encoder/decoder

Dual Protocol Transceivers Ease the Design of Industrial Interfaces

CS601 Data Communication Solved Objective For Midterm Exam Preparation

MSP430 Teaching Materials

S O P H I S T I C A T E D A U T O M A T I O N

CS601-Data Communication Latest Solved Mcqs from Midterm Papers

+3.3V-Powered, EIA/TIA-562 Dual Transceiver with Receivers Active in Shutdown

Week 2 Lecture 1. Introduction to Communication Networks. Review: Analog and digital communications

March 30, W65C51N Asynchronous Communications Interface Adapter (ACIA)

MDR24x Wireless 2.4GHz Modem

SC16C2552B. 1. General description. 2. Features. 5 V, 3.3 V and 2.5 V dual UART, 5 Mbit/s (max.), with 16-byte FIFOs

This document is designed to be used in conjunction with the CMX869A data sheet.

SC28L General description. 3.3 V, 5 V UART, Mbit/s, with 256-byte FIFO

MODES AND PROTOCOL HANDLING

RFID. LF Training. Installation and Set-up. J.A.G 7 March, 2008 Texas Instruments Proprietary Information 1

Instruction Sheet UPS SERIES. Serial Control Protocol. I Rev E

Interface converter G kbit/sec

Kongsberg Mesotech Ltd.

Ultrasonic Multiplexer OPMUX v12.0

CP485-4 User s Manual

AccessCON-N64 INTERFACE CONVERTER E1/FRACTIONAL E1 TO N64 INSTALLATION AND OPERATION MANUAL. Version 1

Transcription:

Serial Input/Output Lecturer: Sri Parameswaran Notes by: Annie Guo 1

Serial communication Concepts Standards USART in AVR Lecture overview 2

Why Serial I/O? Problems with Parallel I/O: Needs a wire for each bit. When the source and destination are more than a few feet the parallel cable can be bulky and expensive. Susceptible to reflections and induced noises for long distance communication. Serial I/O overcomes these problems. 3

Serial Communication System Structure Data From Source n Data To Destination n Transmit Data Buffer Received Data Buffer T clock Parallel In/Serial Out Shift Register Serial Data Serial In/Parallel Out Shift Register R clock TRANSMITTER RECEIVER 4

Serial Communication System Structure (cont.) At the communication source: The parallel interface transfers data from the source to the transmit data buffer. The data is loaded into the Parallel In Serial Out (PISO) register and T clock shifts the data bits out from the shift register to the receiver. 5

Serial Communication System Structure (cont.) At the communication destination: R clock shifts each bit received into the Serial In Parallel Out (SIPO) register. After all data bits have been shifted, they are transferred to the received data buffer. The data in the received data buffer can be read by an input operation via the parallel interface. 6

Serial Communication There are two basic types of serial communications synchronous asynchronous 7

Synchronous VS Asynchronous Synchronous Transmitter and receiver are synchronized Need extra hardware for clock synchronization Having faster data transfer rate Asynchronous Transmitter and receiver use different clocks. No clock synchronization is required. Used in many applications such as keyboards, mice, modems The rest of this lecture focuses on Asynchronous communication 8

UART The device that implements both transmitter and receiver in a single integrated circuit is called a UART (Universal Asynchronous Receiver/Transmitter). UART uses least significant first order The least significant bit of data is transferred first Data are transmitted asynchronously. Clocks on both sides are not synchronized But receivers have a way to synchronise the data receiving operation with data transmission operation UART is the basis for most serial communication hardware. 9

UART Structure Data Bus T clock1 Transmitter Receiver R clock2 Data Bus R clock1 Receiver Transmitter T clock2 UART UART 10

UART Data Formats Before transmission, data should be encoded Many encoding schemes, such as ASCII Each encoded data is encapsulated with two bits Start bit and stop bit Mark and space: the logic one and zero levels are called mark and space. When the transmitter is not sending anything, it holds the line at mark level, also called idle level. Least Significant Optional Stop Bit Parity Bit Bit Mark Space Start Bit Data Bits 11

UART Data Formats (cont.) Typical bits in data transmission: Start bit: When the transmitter has data to send, it first changes the line from the mark to the space level for one bit time. This synchronises the receiver with transmitter. When the receiver detects the start bit, it knows to start clocking in the serial data bits. Data bits: representing a data, such as a character Parity bit: used to detect errors in the data For odd parity: the bit makes the total number of 1s in the data odd For even parity: the bit makes the total number of 1s in the data even. Stop bit: added at the end of data bits. It gives one bit-time between successive data. Some systems require more than one stop bit. 12

Data Transmission Rate The rate at which bits are transmitted is called baud rate. It is given in bits per second Standard data rates Baud: 110, 150, 300, 600, 900, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57800 13

Communication System Types Three ways that data can be sent in serial communication system: Simplex system Full-duplex (FDX) system Half-duplex (HDX) system 14

Simplex System Data are sent in one direction only For example, computer to a serial printer. Simple If the computer does not send data faster than the printer can accept it, no handshaking signals are required. Two signal wires are needed for this system. Computer Printer 15

Full-Duplex (FDX) System Data are transmitted in two directions. It is called four-wire system, although only two signal wires and a common ground are sufficient. Terminal Computer 16

Half-Duplex (HDX) System Data are transmitted in two directions with only one pair of signal lines. Additional hardware and handshaking signals must be added to an HDX system. Computer Computer 17

DTE & DCE There are two typical devices in communications DCE Data Communications Equipment DTE Provides a path for communication Data Terminal Equipment Connects to a communication line 18

DTE & DCE example A communication system for internet access PC and Internet provider server are DTEs Two modems are DCEs phone line PC modem modem Provider server 19

Modem A modulator/demodulator It converts logic levels into tones to be sent over a telephone line. At the other end of the telephone line, a demodulator converts the tones back to logic levels. 20

Standards for the Serial I/O Interface Interface standards are needed to allow different manufacturers equipment to be interconnected Must define the following elements: Handshaking signals Direction of data flow Types of communication devices. Connectors and interface mechanical considerations. Electrical signal levels. 21

Standards for the Serial I/O Interface (cont.) Popular standards include RS-232-C, RS- 422, RS-423 and RS-485. RS-232-C standard is used in most serial interface. If the signals must be transmitted farther than 50 feet or greater than 20 Kbits/second, another electrical interface standard such as RS-422, RS- 423 or RS-485 should be chosen. For RS-422, RS-423 and RS-485, handshaking, direction of signal flow, and the types of communication devices are based on the RS-232- C standard. 22

Two RS232-C Connectors DE9 pin assignments DB25 pin assignments 23

RS-232-C Signal Definitions DE9 DB25 Signal Purpose 1 PG Protective ground: this is actually the shield in a shielded cable. It is designed to be connected to the equipment frame and may be connected to external grounds. 3 2 TxD Transmitted data: Sourced by DTE and received by DCE. Data terminal equipment cannot send unless RTS, CTS, DSR and DTR are asserted. 2 3 RxD Received data: Received by DTE, sourced by DCE. 7 4 RTS Request to send: Sourced by DTE, received by DCE. RTS is asserted by the DTE when it wants to send data. The DCE responds by asserting CTS. 24

RS-232-C Signal Definitions (cont.) DE9 DB25 Signal Purpose 8 5 CTS Clear to send: Sourced by DCE, received by DTE. CTS must be asserted before the DTE can transmit data. 6 6 DSR Data set ready: Sourced by DCE and received by DTE. Indicates that the DCE has made a connection on the telephone line and is ready to receive data from the terminal. The DTE must see this asserted before it can transmit data. 5 7 SG Signal ground: Ground reference for this signal is separate from pin 1, protective ground. 25

RS-232-C Signal Definitions (cont.) DE9 DB25 Signal Purpose 1 8 DCD Data carrier detect: Sourced by DCE, received by DTE. Indicates that a DCE has detected the carrier on the telephone line. Originally it was used in half-duplex systems but can be used in full-duplex systems, too. 4 20 DTR Data terminal ready: Sourced by DTE and received by DCE. Indicates that DTE is ready to send or receive data. 9 22 RI Ring indicator: Sourced by DCE and received by DTE. Indicates that a ringing signal is detected. 26

RS-232-C Interconnections When two serial ports are connected, the data rate, the number of data bits, whether parity is used, the type of parity, and the number of stop bits must be set properly and identically on each UART. Proper cables must be used. There are four kinds of cables from which to choose, depending on the types of devices to be interconnected. Full DTE DCE cable Minimal DTE DCE cable DTE DTE null modem cable Minimal null modem cable 27

RS-232-C Interconnections (cont.) DE9 DB25 DB25 DE9 DTE DTE DCE DCE TxD 3 2 2 3 TxD RxD 2 3 3 2 RxD SG 5 7 7 5 SG RTS 7 4 4 7 RTS CTS 8 5 5 8 CTS DCD 1 8 8 1 DCD DSR 6 6 6 6 DSR DTR 4 20 20 4 DTR Full DTE DCE cable 28

RS-232-C Interconnections (cont.) DE9 DB25 DB25 DE9 DTE DTE DCE DCE TxD 3 2 2 3 TxD RxD 2 3 3 2 RxD SG 5 7 7 5 SG RTS 7 4 4 7 RTS CTS 8 5 5 8 CTS DCD 1 8 8 1 DCD DSR 6 6 6 6 DSR DTR 4 20 20 4 DTR Minimal DTE-DCE cable 29

RS-232-C Interconnections (cont.) DE9 DB25 DB25 DE9 DTE DTE DTE DTE TxD 3 2 2 3 TxD RxD 2 3 3 2 RxD SG 5 7 7 5 SG RTS 7 4 4 7 RTS CTS 8 5 5 8 CTS DCD 1 8 8 1 DCD DSR 6 6 6 6 DSR DTR 4 20 20 4 DTR DTE DTE null modem cable 30

RS-232-C Interconnections (cont.) DE9 DB25 DB25 DE9 DTE DTE DTE DTE TxD 3 2 2 3 TxD RxD 2 3 3 2 RxD SG 5 7 7 5 SG RTS 7 4 4 7 RTS CTS 8 5 5 8 CTS DCD 1 8 8 1 DCD DSR 6 6 6 6 DSR DTR 4 20 20 4 DTR Minimal null modem cable 31

RS-232-C Interface RS-232-C Logic levels: Mark -25 to 3 volts Space +25 to +3 volts TTL Logic levels D RS-232-C Logic levels R TTL Logic levels 32

RS-423 Standard Also a single ended system. Allows longer distance and higher data rates than RS-232-C. Allows a driver to broadcast data to 10 receivers. D R Up to 10 receivers R RS-423 Interface 33

RS-422 Standard RS-422 line drivers and receivers operates with differential amplifier. These drivers eliminate much of the commonmode noise experienced with long transmission lines, thus allowing the longer distances and higher data rates. D R Up to 10 receivers R RS-422 Interface 34

RS-485 Standard Similar to RS-422 in that it uses differential line drivers and receivers. Unlike RS-422, RS-485 provides for multiple drivers and receivers in a bussed environment. Up to 32 drivers/receivers pairs can be used together. D Up to 32 drivers R Up to 32 receivers D R RS-485 Interface 35

Summary of Standards Specification RS-232-C RS-423 RS-422 RS-485 Receiver input ±3 to ±15V ±200mV to ±12V ±200mV to ±200mV to voltage ±7V -7 to +12V Driver output signal ±5 to ±15V ±3.6 to ±6V ±2 to ±5V ±1.5 to ±5V Maximum data rate 20 Kb/s 100 Kb/s 10 Mb/s 10 Mb/s Maximum cable 50 ft 4000 ft 4000 ft 4000 ft length Driver source 3-7 KΩ 450 Ω min 100 Ω 54Ω Impedance Receiver input 3 KΩ 4 KΩ min 4 K Ω min 12 KΩ minimum resistance Mode Singled-ended Singled-ended Differential Differential Number of drivers 1 Driver 1 driver 1 Driver 32 Driver and receivers allowed on one line 1 Receivers 10 Receivers 10 Receivers 32 Receivers 36

Two USART units Unit 0 Unit 1 AVR USARTs Each unit can be configured for synchronous or asynchronous serial communication USART unit 0 is used on our lab board to receive program code downloaded from the PC host via USB 37

AVR USARTs (cont.) Support many serial frames. Have transmission error detection Odd or even parity error Framing error Three interrupts on TX Complete TX Data Register Empty RX Complete. 38

USART Block Diagram 39

AVR USART Structure The USART consists of three components: clock generator, transmitter and receiver Clock generator consists of synchronization logic for external clock input used by synchronous slave operation, and the baud rate generator. Transmitter consists of a single write buffer, a serial Shift Register, Parity Generator and Control Logic for handling different serial frame formats. 40

AVR USART Structure (cont.) Receiver The Receiver is the most complex part of the USART module due to its clock and data recovery units. The recovery units are used for asynchronous data reception. In addition to the recovery units, the Receiver includes a Parity Checker, Control Logic, a Shift Register and a Receive Buffer (UDR). The Receiver supports the same frame formats as the Transmitter, and can detect Frame Error, Data Over Run and Parity Error. 41

Up to 30 formats combinations of Example Frame Formats 1 start bit (St) 5, 6, 7, 8, or 9 data bits no, even or odd parity bit (P) 1 or 2 stop bits (Sp) 42

Parity Bit Used to check whether the received data is different from the sending data Two forms of the parity bit Even parity Peven = dn dn 1 d1 d0 Odd parity 0 Podd = dn dn 1 d1 d0 Where d i in the two formulas is a data bit, n is the number of data bits. 1 43

Control Registers Three control registers are used in USART operation: UCSRA for storing the status flags of USART for controlling transmission speed and use of multiple processors UCSRB for enabling interrupts, transmission operations for setting frame formats for bit extension UCSRC For operation configuration 44

UCSRA USART Control and Status Register A 45

UCSRA Bit Descriptions Bit 7 RXC: USART Receive Complete Set when the receive buffer is not empty The RXC flag can be used to generate a Receive Complete interrupt Bit 6 TXC: USART Transmit Complete Set when the entire frame in the Transmit Shift Register has been shifted out and there are no new data present in the transmit butter TXC is automatically cleared when a transmit complete interrupt is executed. TXC can generate a Transmit Complete interrupt 46

UCSRA Bit Descriptions (cont.) Bit 5 UDRE: USART Data Register Empty Set when the transmit buffer (UDR) is empty Can be used to generate a Data Register Empty interrupt Bit 4 FE: Frame Error Set when the next character in the receive buffer had a Frame Error when received. Bit 3 DOR: Data OverRun Set when a Data OverRun condition is detected. A Data OverRun occurs when the receive buffers are all full and a new start bit is detected. 47

UCSRA Bit Descriptions (cont.) Bit 2 UPE: USART Parity Error Set when the next character in the receive buffer had a Parity Error when received and the Parity Checking was enabled Bit 1 U2X: Double the USART Transmission Speed Set for doubling the transfer rate for asynchronous communication Bit 0 MPCM: Multi-processor Communication Mode If set, all the incoming frames received by the USART Receiver that do not contain address information will be ignored. 48

UCSRB USART Control and Status Register B 49

UCSRB Bit Descriptions Bit 7 RXCIE: RX Complete Interrupt Enable Set to enable interrupt on the RXC flag Bit 6 TXCIE: TX Complete Interrupt Enable Set to enable interrupt on the TXC flag Bit 5 UDRIE: USART Data Register Empty Interrupt Enable Set to enable interrupt on the UDRE flag. 50

UCSRB Bit Descriptions (cont.) Bit 4 RXEN: Receiver Enable Set the enable the USART receiver. The Receiver will override normal port operation for the RxD pin when enabled. Disable the Receiver will flush the receive buffer invalidating the FE, DOR and UPE flags. Bit 3 TXEN: Transmitter Enable Set to enable the USART Transmitter The Transmitter will override normal port operations for the TxD pin when enabled. The disabling of the Transmitter will not become effective until transmissions are complete. 51

UCSRB Bit Descriptions (cont.) Bit 2 UCSZ2: Character Size The bit combined with the UCSZ1:0 bits in UCSRC sets the number of data bits in a frame. Bit 1 RXB8: Receive Data Bit 8 The ninth data bit of the received character when operating with serial frames with nine data bits. Must be read before reading the low bits from UDR Bit 0 TXB8: Transmit Data Bit 8 The ninth data bit in the character to be transmitted when operating with serial frames with nine data bits. Must be written before writing the low bits to UDR 52

UCSRC USART Control and Status Register C 53

UCSRC Bit Descriptions Bit 6 UMSEL: USART Mode Select 0: Asynchronous Operation 1: Synchronous Operation Bit 5:4 UPM1:0: Parity Mode Set to enable Parity bit operation UPM1 UPM0 Parity Mode 0 0 Disabled 0 1 Reserved 1 0 Enabled, Even Parity 1 1 Enabled, Odd Parity 54

UCSRC Bit Descriptions (cont.) Bit 3 USBS: Stop Bit Select 0: 1-bit 1: 2-bit Bit 2:1 UCSZ1:0: Character Size Together with UCSZ2 to determine the number of bits for a character UCSZ2 UCSZ1 UCSZ0 Character Size 0 0 0 5-bit 0 0 1 6-bit 0 1 0 7-bit 0 1 1 8-bit 1 0 0 Reserved 1 0 1 Reserved 1 1 0 Reserved 1 1 1 9-bit 55

UCSRC Bit Descriptions (cont.) Bit 0 UCPOL: Clock Polarity UCPOL Transmitted Data Changed Received Data Sampled (Output of TxD Pin) (Input on RxD Pin) 0 Rising XCK Edge Failing XCK Edge 1 Failing XCK Edge Rising XCK Edge 56

USART Initialization Initialization process consists of Setting the baud rate, Setting the frame format; and Enabling the Transmitter or the Receiver For interrupt driven USART operation, the Global Interrupt Flag should be cleared when doing the initialization 57

Initialize USART 1 Sample Code USART_Init: ; Set baud rate, which is stored in r17:r16 sts UBRR1H, r17 sts UBRR1L, r16 ; Enable receiver and transmitter ldi r16, (1<<RXEN1) (1<<TXEN1) sts UCSR1B,r16 ; Set frame format: 8 bit data, 2 stop bits ldi r16, (1<<USBS1) (3<<UCSZ10) sts UCSR1C,r16 58

Data Transmission The USART Transmitter is enabled by setting the Transmit Enable (TXEN) bit in the UCSRB Register. A data transmission is initiated by loading the transmit buffer with the data to be transmitted. The CPU can load the transmit buffer by writing to the UDR I/O location. The buffered data in the transmit buffer will be moved to the Shift Register when the Shift Register is ready to send a new frame. The Shift Register is loaded with new data if it is in idle state (no ongoing transmission) or immediately after the last stop bit of the previous frame is transmitted. When the Shift Register is loaded with new data, it will transfer one complete frame at the rate given by the baud register, U2X bit or by XCK depending on mode of operation. 59

Data Transmission Sample code The code below uses polling of the Data Register Empty (UDRE) flag. When using frames with less than eight bits, the most significant bits written to the UDR are ignored. USART_Transmit: ; Wait for empty transmit buffer lds r15, UCSR1A sbrs r15,udre1 rjmp USART_Transmit ; Put data (r16) into buffer, sends the data sts UDR1,r16 60

Status of Data Transmission The USART Transmitter has two flags that indicate its state: USART Data Register Empty (UDRE) Set: when the transmit buffer is empty and ready to receiver new data Clear: when the transmit butter contains data to be moved into the shift register Transmit Complete (TXC) Set: when data in the Transmit Shift Register has been shifted out and there are no new data currently present in the transmit buffer Clear: otherwise Both flags can be used for generating interrupts. 61

Data Reception The USART Receiver is enabled by writing the Receive Enable (RXEN) bit in the UCSRB Register The baud rate, mode of operation and frame format must be set up once before doing any transmissions. If synchronous operation is used, the clock on the XCK pin will be overridden and used as transmission clock. 62

Data reception Sample code USART_Receive: ; Wait for data to be received lds r10, UCSR1A sbrs r10, RXC1 rjmp USART_Receive ; Get and return received data from buffer lds r16, UDR1 63

Status of Data Reception The Receive Complete (RXC) flag indicates if there are unread data present in the receive buffer. Set: when unread data exist in the receive buffer Clear: otherwise If the receiver is disabled (RXEN = 0), the receive buffer will be flushed and consequently the RXC bit will become zero. 64

Error Detection Three errors are checked on the receiver side: Frame error By checking whether the first stop bit is corrected received Parity error By checking whether the data received has the same (odd or even) number of 1 s as in the data from the tranmistter. Data OveRun error By checking if any data is yet read but is overwritten by incoming data frame. 65

Error Recovery Main sources of errors in asynchronous data transmission Data reception is out sync with transmission Noise added to the data 66

Error Recovery (cont.) AVR includes a clock recovery and a data recovery unit for handling errors in asynchronous transmission. The clock recovery logic is used for synchronizing the internally generated baud rate clock to the incoming asynchronous serial frames at the RxD pin. Based on the start bit The data recovery logic samples and low pass filters each incoming bit, thereby improving the noise immunity of the Receiver Based on multiple sampling and majority policy for each incoming bit 67

Error Recovery (cont.) The following figure gives an illustration The sample rate is 16 times the baud rate When the Clock Recovery logic detects a high (idle) to low (start) transition on the RxD line, it uses the samples 8, 9 and 10 to decide if it is a valid bit (namely, 0) If the majority of the three bits are 0, the bit is valid; data recovery is followed. If the majority of the three bits are 1, the bit is invalid; the circuit looks for next start bit. 68

Error Recovery (cont.) When the receiver clock is synchronized to the start bit, the data recovery can begin for each subsequent data bit. The decision of the logic level of the received bit is taken by doing a majority voting of the logic value to the three samples in the center of the received bit. 69

Reading Material Chapter 7: Computer Buses and Parallel Input and Output. Microcontrollers and Microcomputers by Fredrick M. Cady. Mega64 Data Sheet USART 70