Universal Asynchronous Receiver Transmitter (Uart) PDF
Universal Asynchronous Receiver Transmitter (Uart) PDF
HIGHLIGHTS
This section of the manual contains the following major topics:
Note: This family reference manual section is meant to serve as a complement to device
data sheets. Depending on the device variant, this manual section may not apply to
all dsPIC33 and PIC24 devices.
Please consult the note at the beginning of the “Universal Asynchronous
Receiver Transmitter (UART)” chapter in the current device data sheet to check
whether this document supports the device you are using.
Device data sheets and family reference manual sections are available for download
from the Microchip worldwide web site at: http://www.microchip.com
This document supersedes the following PIC24 and dsPIC Family Reference Manual sections:
1.0 INTRODUCTION
The Universal Asynchronous Receiver Transmitter (UART) module is one of the serial I/O
modules available in the dsPIC33 and PIC24 device families. The UART is a full-duplex,
asynchronous communication channel that communicates with peripheral devices and personal
computers, using protocols such as RS-232, RS-485, LIN/J2602 and IrDA®. The module also
supports the hardware flow control option with the UxCTS and UxRTS pins and includes the IrDA
encoder and decoder.
The primary features of the UART module are as follows:
• Full-duplex, 8-bit or 9-bit data transmission through the UxTX and UxRX pins
• Even, odd or no parity options (for 8-bit data)
• One or two Stop bits
• Hardware auto-baud feature
• Hardware flow control option with the UxCTS and UxRTS pins (These pins are not
available on all devices. Refer to the “Universal Asynchronous Receiver Transmitter
(UART)” chapter of the specific device data sheet for availability.)
• Fully integrated Baud Rate Generator (BRG) with 16-bit prescaler
• Baud rates up to 17.5 Mbps
• Four-deep First-In First-Out (FIFO) transmit data buffer
• Four-deep FIFO receive data buffer
• Parity, framing and buffer overrun error detection
• Support for 9-bit mode with address detect (9th bit = 1)
• Transmit and receive interrupts
• Loopback mode for diagnostic support
• IrDA encoder and decoder logic
• LIN/J2602 bus support (v1.3 and 2.0)
• 16x baud clock output for external IrDA encoder/decoder support
• Optional ISO 7816 Smart Card support
IrDA® BCLKx
UxRTS
Hardware Flow Control
UxCTS
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Note 1: This feature is only available for Standard Speed mode (BRGH = 0). Refer to the “Universal Asynchronous
Receiver Transmitter (UART)” chapter of the specific device data sheet for availability.
2: These features may not be available on all devices. Refer to the “Universal Asynchronous Receiver
Transmitter (UART)” chapter of the specific device data sheet for availability.
3: Enable this bit before enabling the UTXEN bit (UxSTA<10>).
4: The UARTx module does not recognize the first character received on a wake.
5: The use of this feature may consume the corresponding Input Capture x (ICx) peripheral. See Section 9.2
“Auto-Baud Support” for more information.
Note 1: This feature is only available for Standard Speed mode (BRGH = 0). Refer to the “Universal Asynchronous
Receiver Transmitter (UART)” chapter of the specific device data sheet for availability.
2: These features may not be available on all devices. Refer to the “Universal Asynchronous Receiver
Transmitter (UART)” chapter of the specific device data sheet for availability.
3: Enable this bit before enabling the UTXEN bit (UxSTA<10>).
4: The UARTx module does not recognize the first character received on a wake.
5: The use of this feature may consume the corresponding Input Capture x (ICx) peripheral. See Section 9.2
“Auto-Baud Support” for more information.
Note 1: This bit is only available in devices supporting Smart Card. Refer to the “Universal Asynchronous
Receiver Transmitter (UART)” chapter of the specific device data sheet for availability.
2: Enable the UARTEN bit (UxMODE<15>) before enabling this bit.
3: User software should wait at least one instruction cycle between writing UxTXREG and reading the TRMT bit.
Note 1: This bit is only available in devices supporting Smart Card. Refer to the “Universal Asynchronous
Receiver Transmitter (UART)” chapter of the specific device data sheet for availability.
2: Enable the UARTEN bit (UxMODE<15>) before enabling this bit.
3: User software should wait at least one instruction cycle between writing UxTXREG and reading the TRMT bit.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
bit 15 LAST: Last Byte Indicator for Smart Card Support bit(1)
bit 14-9 Unimplemented: Read as ‘0’
bit 8 UTX8: Data bit 8 of the Transmitted Character (in 9-bit mode)
bit 7-0 UTX<7:0>: Data bits 7-0 of the Transmitted Character
Note 1: This bit is only available in devices supporting Smart Card. Refer to the “Universal Asynchronous
Receiver Transmitter (UART)” chapter of the specific device data sheet for availability.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
FP
- ...... (1)
Baud Rate = ---------------------------------------------
16 UxBRG + 1
FP
UxBRG = ------------------------------------- – 1 ...... (2)
16 Baud Rate
Equation 3-2 shows the calculation of baud rate error for the following conditions:
• FP = 4 MHz
• Desired Baud Rate = 9600
The maximum baud rate (BRGH = 0) possible is FP/16 (for UxBRG = 0) and the minimum baud
rate possible is FP/(16 * 65536).
Equation 3-3 shows the formula for computing the baud rate with BRGH = 1.
The maximum baud rate (BRGH = 1) possible is FP/4 (for UxBRG = 0) and the minimum baud
rate possible is FP/(4 * 65536).
Writing a new value to the UxBRG register causes the BRG timer to reset (cleared). This ensures
the BRG does not wait for a timer overflow before generating the new baud rate.
TP
BCLKx @ BRG = 0
BCLKx @ BRG = 1
BCLKx @ BRG = 2
BCLKx @ BRG = 3
BCLKx @ BRG = 4
BCLKx @ BRG = N
(N + 1)TP
UxTX
UxMODE UxSTA
15 9 8 7 0
– Control UxTSR
Transmit FIFO – Control Buffer
– Generate Flags
– Generate Interrupt
Load UxTSR
UxTXIF
UTXINV UTXBRK
Data
Transmit Shift Register (UxTSR)
(Start)
Control
Signals
UxCTS
The transmission is enabled by setting the UTXEN enable bit (UxSTA<10>). The actual trans-
mission will not occur until the UxTXREG register has been loaded with data and the Baud Rate
Generator (UxBRG) has produced a shift clock (Figure 5-1). Normally, when the first transmis-
sion is started, the UxTSR register is empty, so a transfer to the UxTXREG register will result in
an immediate transfer to UxTSR. Clearing the UTXEN bit during a transmission will cause the
transmission to be aborted and will reset the transmitter. As a result, the UxTX pin will revert to
a high-impedance state.
To select 9-bit transmission, the PDSEL<1:0> bits (UxMODE<2:1>) should be set to ‘11’ and the
ninth bit should be written to the UTX8 bit (UxTXREG<8>). A word write should be performed to
UxTXREG, so that all nine bits are written at the same time.
Note: There is no parity in case of a 9-bit data transmission.
Software
Delay Bit Time = T
UxTX B0 B1 B2 B3 B4 B5 B6 B7
1 2
While the UxTXIF flag bit indicates the status of the UxTXREG register, the TRMT bit
(UxSTA<8>) indicates the status of the UxTSR. The TRMT status bit is a read-only bit, which is
set when the UxTSR is empty. No interrupt logic is tied to this bit, so the user application has to
poll this bit to determine if the UxTSR is empty.
Example 5-1 provides the sample code that sets up the UART for transmission.
UxTX
Start bit bit 0 bit 1 bit 7/8 Stop bit
Character 1
Character 1 to
Transmit Shift Register
TRMT bit
Write to UxTXREG
BCLKx/16 Character 1 Character 2
(Shift Clock)
UxTX Start bit bit 0 bit 1 bit 7/8 Stop bit Start bit bit 0
Character 1 Character 2
UxTXIF
(UTXISEL<1:0> = 00)
UxTXIF Cleared by User in Software
UxTXIF
(UTXISEL<1:0> = 10)
Character 1 to Character 2 to
Transmit Shift Register Transmit Shift Register
TRMT bit
int main(void)
{
while(1)
{
}
}
Write to UxTXREG
Dummy Write
BCLKx/16
(Shift Clock)
Break
UxTXIF
TRMT bit
UTXBRK Sampled Here Auto-Cleared
UTXBRK bit
MD1
Start Bit Detected MD3
16x Clock
MD2
Bit Clock
Note: In 16x Clock mode, each bit is sampled at 7th, 8th and 9th rising edges of the clock.
4x Clock
Sample Point
Bit Clock
RX
Note: In 4x Clock mode, the sampling occurs only at the one-half bit width.
The data on the UxRX pin is sampled multiple times by a Majority Detect circuit to determine if a
high or a low level is present at the UxRX pin.
The Framing Error bit, FERR (UxSTA<2>), is set if a Stop bit is detected at a logic low level.
The Parity Error bit, PERR (UxSTA<3>), is set if a parity error has been detected in the data word
at the top of the buffer (i.e., the current word). For example, a parity error would occur if the parity
is set to be even, but the total number of ‘1’s in the data has been detected to be odd. The PERR
bit is irrelevant in 9-bit mode. The FERR and PERR bits are buffered along with the
corresponding word and should be read before reading the data word.
An interrupt is generated if any of these (OERR, FERR and PERR) errors occur. The user
application needs to enable the corresponding UARTx Interrupt Enable Control bit (UxERIE) in
the IECx register to go to the corresponding interrupt vector location.
Note: After a Reset, the three possible UART interrupts have the same Interrupt Priority
Level (IPL). Natural order allows the (RX/TX) data interrupts to be serviced before
the UART error interrupt.
To allow the UART error interrupt to be serviced before the (RX/TX) data interrupts,
its Interrupt Priority Level (UxEIP) is raised or the data Interrupt Priority Levels are
lowered (UxRXIP and UxTXIP).
Example for UART2 to raise the UART2 error interrupt level:
• IPC7bits.U2RXIP = 4; //UART2 RX interrupt priority, mid-range
• IPC7bits.U2TXIP = 4; //UART2 TX interrupt priority, mid-range
• IPC16bits.U2EIP = 5; //UART2 error priority set higher
Word Word or
Read-Only Byte Read
UxMODE UxSTA
15 9 8 7 0
9 UxRXIF
Load UxRSR
to Buffer
LPBACK
FERR
PERR Signals
Receive Shift Register
(UxRSR)
UxRX 0
• Start bit Detect
• Parity Check
• Stop bit Detect
• Shift Clock Generation 16 Divider
• Wake Logic
16x Baud Clock
UEN1 UEN0 from Baud Rate
Generator
BCLKx
BCLKx/UxRTS
UEN UxRTS
Selection
UxCTS UxCTS
Character 1 Character 2
to UxRXREG to UxRXREG
RIDLE bit
Note 1: This timing diagram shows two characters received on the UxRX input.
2: If the interrupt flag is used by the application software as a basis for disabling the UART transmission, the software should
wait for 1-bit time before disabling the transmission.
RIDLE bit
Note: This diagram shows 6 characters received without the user reading the input buffer. The 5th character received is held in the
Receive Shift register. An overrun error occurs at the start of the 6th character.
int main(void)
{
while(1)
{
char ReceivedChar;
Start Start
UxRX (pin) bit bit 0 bit 1 bit 8 Stop bit bit 0 bit 8 Stop
bit bit
Transfer
to Receive FIFO
Read Rcv Word 1
Buffer Register UxRXREG
UxRXREG
UxRXIF
(Interrupt Flag)
Note: This timing diagram shows a data byte followed by an address byte. The data byte is not read into the UxRXREG (receive buffer)
because ADDEN = 1 and bit 8 = 0.
If an address match is found, software must clear ADDEN = 0 to receive the remainder of the
message; interrupts on the body of the message will occur as specified by URXISEL<1:0>
(UxSTA<7:6>). After the body of the message is received, software must set ADDEN = 1 to
enable Address Detect mode once again.
Note: Depending on the specific device used, the ABAUD feature of a respective UART
may automatically use the corresponding Input Capture x (ICx) peripheral for
UARTx auto-baud measurements. For devices that use the corresponding ICx
peripheral, the user application should not use the respective ICx peripheral for
other purposes. See the specific device data sheet regarding this dependency.
BRG Clock
UxRXIF
While the auto-baud sequence is in progress, the UART state machine is held in an Idle state.
The UxRXIF interrupt is set on the 5th UxRX rising edge, independent of the URXISEL<1:0> bits
setting. The receiver FIFO is not updated.
Figure 10-1: Format of 16-Bit UART Receive Data Word Transferred by DMA to RAM
bit 15 12 11 10 9 8 7 bit 0
The UARTx Error Interrupt Flag bit (UxEIF) gets set if the last UART reception caused a framing
or a parity error. Setting the UxEIE bit causes the CPU to enter the UART error Interrupt Service
Routine (ISR). The user application can then inspect the upper byte of the last transferred word
to check which error condition has caused the interrupt.
Note: For PIC24H, PIC24EP and dsPIC® devices only, alternatively, the UART transmitter
can be enabled before the DMA channel is enabled. In this case, the UART trans-
mitter DMA request will be lost and the user application must issue a DMA request
to start DMA transfers by setting the FORCE bit in the DMAxREQ register.
OSC1
Bit Set by User Auto-Cleared
WAKE bit(1)
UxRX
UxRXIF
Note 1: UARTx state machine is held in Idle while WAKE bit is active.
OSC1
Bit Set by
User Software Auto-Cleared
WAKE bit(2)
UxRX (Note 1)
UxRXIF
Sleep
Note 1: If the wake-up event requires a long oscillator warm-up time, the auto-clear of the WAKE bit can occur while the system
clocks are still active. This sequence should not depend on the presence of FP.
2: The UARTx state machine is held in Idle while the WAKE bit is active.
The UxCTS (Clear-to-Send) and UxRTS (Request-to-Send) pins are the two hardware controlled
pins associated with the UART module. These two pins allow the UART to operate in Flow Con-
trol and Simplex modes, which are explained in detail in Section 12.2 “UxRTS Function in Flow
Control Mode” and Section 12.3 “UxRTS Function in Simplex Mode”, respectively. They are
implemented to control transmission and reception between the UART and Data Terminal
Equipment (DTE).
Figure 12-1: UxRTS/UxCTS Flow Control for DTE-DTE (RTSMD = 0, Flow Control Mode)
DTE DTE
(Typically a PC) (Typically another System or dsPIC33E/PIC24E)
UxRTS UxRTS
UxCTS UxCTS
DTE DCE
(Typically a dsPIC33/PIC24) (Typically a Modem)
UxRTS UxRTS
UxRTS active and receiver ready
UxCTS UxCTS
TTL to RS-485
Transceiver
Integrated CKT
UxTX
D
UxRX
R
DTE
(Typically a dsPIC33/PIC24)
UxRTS
I will transmit if OK
A B
UxCTS
Note: Some devices may have a 16x baud clock output (BCLKx) Idle state as a logic high.
UxTX Data
UxTX
UxTX Data
UxTX
UxTX Data
Start of Start of
8th Period 11th Period
UxTX
UxRX
Decoded Data
Decoded Data
16 Periods 16 Periods
UxRX (rx_in)
Decoded Data
Delimiter
UxRX Start bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 Stop bit
Identifier, Data
Break Character Sync Byte and Checksum
Auto-Cleared
Auto-Cleared
WAKE bit
ABAUD bit
UxRXIF
1 2 3 4 5 6
1. The user application first sets the WAKE bit and then the ABAUD bit.
2. An RX interrupt is generated because the device wakes up on the reception of the Break character.
3. The FERR bit is set.
4. The WAKE bit is cleared.
5. The auto-baud feature detects the first rising edge of the Sync byte.
6. The fifth rising edge of the Sync byte is detected. The ABAUD bit is cleared and the BRG counter is loaded with the
baud rate of the received data.
Terminal
TX
I/O
RX
UART
REFO CLK
GPO RST
Card Detect(1)
GPI
VDD
GND
CGT ≤ t ≤ CWT
BGT ≤ t ≤ BWT
Bit Clock
(5)
I/O Start Bit Bit 0 Bit 1 (1) Parity
Character 1
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Note 1: These bits are applicable to T = 0 only. See the PTRCL bit (UxSCCON<1>.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Note 1: These bits are applicable to T = 0 only. See the PTRCL bit (UxSCCON<1>.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Reception with T = 0:
1. Set UTXEN to ‘0’ and URXEN to ‘1’.
2. Read the UxRXREG as the data becomes available upon the receive interrupt.
3. Take appropriate actions according to the ISO 7816 standard if the WTCIF, GTCIF and/or
RXRPTIF bits are set.
Note: For the last character, the user must ensure that the guard time is satisfied before
transmitting the response. The GTC may be used for this purpose, whereas the
WTC interrupt may be disabled or ignored.
Reception with T = 1:
1. Set UTXEN to ‘0’ and URXEN to ‘1’.
2. Read the UxRXREG as the data becomes available upon the receive interrupt.
3. Program the value of CWT to UxWTCL/H and set WTCIE (UxSCINT<1>) to ‘1’.
4. Take appropriate actions according to the ISO 7816 standard if the WTCIF, GTCIF and/or
RXRPTIF bits are set.
Note: For the last character, the user must ensure the guard time is satisfied before trans-
mitting the response. The GTC may be used for this purpose, whereas the WTC
interrupt may be disabled or ignored.
A summary of the registers associated with the UART module of the dsPIC33 and PIC24 device families is provided in Table 16-1.
UxMODE UARTEN — USIDL IREN RTSMD ALTIO UEN1 UEN0 WAKE LPBACK ABAUD URXINV BRGH PDSEL1 PDSEL0 STSEL 0000
UxSTA UTXISEL1 UTXINV UTXISEL0 URXEN UTXBRK UTXEN UTXBF TRMT URXISEL1 URXISEL0 ADDEN RIDLE PERR FERR OERR URXDA 0110
UxADMD ADM_MASK7 ADM_MASK6 ADM_MASK5 ADM_MASK4 ADM_MASK3 ADM_MASK2 ADM_MASK1 ADM_MASK0 ADM_ADDR7 ADM_ADDR6 ADM_ADDR5 ADM_ADDR4 ADM_ADDR3 ADM_ADDR2 ADM_ADDR1 ADM_ADDR0 0000
UxTXREG LAST — — — — — — UTX8 UARTx Transmit Register xxxx
UxRXREG — — — — — — — URX8 UARTx Receive Register 0000
UxBRG Baud Rate Generator Prescaler Register 0000
UxSCCON — — — — — — — — — — TXRPT1(2) TXRPT0(2) CONV T0PD(2) PTRCL SCEN 0000
UxSCINT — — RXRPTIF(2) TXRPTIF(2) — — WTCIF GTCIF — PARIE RXRPTIE TXRPTIE — — WTCIE GTCIE 0000
UxGTC — — — — — — — GTC8 GTC7 GTC6 GTC5 GTC4 GTC3 GTC2 GTC1 GTC0 0000
UxWTCL WTC15 WTC14 WTC13 WTC12 WTC11 WTC10 WTC9 WTC8 WTC7 WTC6 WTC5 WTC4 WTC3 WTC2 WTC1 WTC0 0000
UxWTCH — — — — — — — — WTC23 WTC22 WTC21 WTC20 WTC19 WTC18 WTC17 WTC16 0000
Legend: x = unknown value on Reset, — = unimplemented, read as ‘0’. Reset values are shown in hexadecimal.
Note 1: The registers associated with UARTx are shown for reference. Refer to the “Universal Asynchronous Receiver Transmitter (UART)” chapter of the specific device data sheet for the registers associated with other UART modules.
2: These bits are applicable to T = 0 only. See the PTRCL bit (UxSCCON<1>).
DS70000582E-page 51
UART
dsPIC33/PIC24 Family Reference Manual
17.0 RELATED APPLICATION NOTES
This section lists application notes that are related to this section of the manual. These applica-
tion notes may not be written specifically for the dsPIC33 and PIC24 device families, but the
concepts are pertinent and could be used with modification and possible limitations. The current
application notes related to the UART module include the following:
Note: Please visit the Microchip web site (www.microchip.com) for additional application
notes and code examples for the dsPIC33 and PIC24 families of devices.
• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
QUALITY MANAGEMENT SYSTEM Microchip received ISO/TS-16949:2009 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
CERTIFIED BY DNV Tempe, Arizona; Gresham, Oregon and design centers in California
and India. The Company’s quality system processes and procedures
== ISO/TS 16949 ==
are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.