Learn, Implement and Share

Note

This article is a part of Arduino / ATmega328p Embedded C Firmware Programming Tutorial. Consider exploring the course home page for articles on similar topics.

Arduino Tutorial Embedded C Register Level Arduino Master Class

Arduino Tutorial Embedded C Register Level Arduino Master Class

Also visit the Release Page for Register Level Embedded C Hardware Abstraction Library and Code for AVR.

Introduction

The USART (Universal Synchronous and Asynchronous Serial Receiver and Transmitter) hardware in the ATmega48A/PA/88A/PA/168A/PA/328/P microcontroller uses registers UDRn, UCSRnA, UCSRnB, UCSRnC, UBRRnL, and UBRRnH to configure the hardware and to transmit and receive data. Below are the register descriptions. The variable ‘n’ = 0 for USART0 ‘n’ = 1 for USART1 etc. The ATmega328p has only USART0.

AVR USART Register Configuration
AVR USART Register Configuration

What You Will Learn

  • What are the USART/UART Registers in Arduino?
  • What are the USART/UART Registers in AVR ATmega328p?
  • Which AVR Registers control and configures USART/UART?
  • What is the function of the AVR USART/UART Registers?

UDR0 – USART I/O Data Register

Bit (0xC6)76543210
UDR (Read)RXB7RXB6RXB5RXB4RXB3RXB2RXB1RXB0
UDR (Write)TXB7TXB6TXB5TXB4TXB3TXB2TXB1TXB0
Read/WriteR/WR/WR/WR/WR/WR/WR/WR/W
Initial Value00000000

The USART Transmit Data Buffer Register and USART Receive Data Buffer Registers share the same I/O address referred to as USART Data Register or UDRn. The Transmit Data Buffer Register (TXB) will be the destination for data written to the UDRn Register location. Reading the UDR Register location will return the contents of the Receive Data Buffer Register (RXB).

When data is written to the transmit buffer, and the Transmitter is enabled, the Transmitter will load the data into the Transmit Shift Register when the Shift Register is empty. Then the data will be serially transmitted on the TxD pin.

UCSR0A – USART Control and Status Register A

Bit76543210
(0xC0)RXC0TXC0UDRE0FE0DOR0UPE0U2X0MPCM0
Read/WriteRR/WRRRRRR
Initial Value00100000

Bit 7 – RXC0: USART Receive Complete
This flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty. The RXC flag can be used to generate a receive complete interrupt.

• Bit 6 – TXC0: USART Transmit Complete
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 can generate a transmit complete interrupt.

• Bit 5 – UDRE0: USART Data Register Empty
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 (see description of the UDRIE bit). UDRE is set after a reset to indicate that the Transmitter is ready.

• Bit 4 – FE0: Frame Error
This bit is set if the next character in the receive buffer had a Frame Error when received. I.e., 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. Always set this bit to zero when writing to UCSRA.

• Bit 3 – DOR0: Data OverRun
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 – UPE0: USART Parity Error
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 (UPM01 = 1). This bit is valid until the receive buffer (UDR) is read. Always set this bit to zero when writing to UCSRA.

• Bit 1 – U2X0: Double the USART Transmission Speed
This bit only has an effect on 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 – MPCM0: Multi-processor Communication Mode
This bit enables the Multi-processor Communication mode. When the MPCMn bit is written to one, all the incoming frames received by the USART Receiver that do not contain address information will be ignored. The Transmitter is unaffected by the MPCM setting.

UCSR0B – USART Control and Status Register B

Bit76543210
(0xC1)RXCIE0TXCIE0UDRIE0RXEN0TXEN0UCSZ02RXB80TXB80
Read/WriteR/WR/WR/WR/WR/WR/WRR/W
Initial Value00000000

• Bit 7 – RXCIE0: RX Complete Interrupt Enable
Writing this bit to one enables interrupt on the RXC Flag. A USART Receive Complete interrupt will be generated only if the RXCIE bit is written to one, the Global Interrupt Flag in SREG is written to one, and the RXC bit in
UCSRA is set.

• Bit 6 – TXCIE0: TX Complete Interrupt Enable
Writing this bit to one enables interrupt on the TXC Flag. A USART Transmit Complete interrupt will be generated only if the TXCIE bit is written to one, the Global Interrupt Flag in SREG is written to one and the TXC bit in UCSRA is set.

• Bit 5 – UDRIE0: USART Data Register Empty Interrupt Enable
Writing this bit to one enables interrupt on the UDRE Flag. A Data Register Empty interrupt will be generated only if the UDRIE bit is written to one, the Global Interrupt Flag in SREG is written to one and the UDRE bit in UCSRA is set.

• Bit 4 – RXEN0: Receiver Enable
Writing this bit to one enables the USART Receiver. The Receiver will override normal port operation for the RxD pin when enabled. Disabling the Receiver will flush the receive buffer invalidating the FE, DOR, and UPE Flags.

• Bit 3 – TXEN0: Transmitter Enable
Writing this bit to one enables the USART Transmitter. The Transmitter will override normal port operation for the TxD pin when enabled. The disabling of the Transmitter (writing TXEN to zero) will not become effective until ongoing and pending transmissions are completed, i.e., when the Transmit Shift Register and Transmit Buffer Register do not contain data to be transmitted. When disabled, the Transmitter will no longer override the TxD port.

• Bit 2 – UCSZ02: Character Size
The UCSZ2 bits combined with the UCSZ 1:0 bit in UCSRC sets the number of data bits (Character Size) in a frame the Receiver and Transmitter use.

• Bit 1 – RXB80: Receive Data Bit 8
RXB8 is 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 – TXB80: Transmit Data Bit 8
TXB8 is 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.

UCSR0C – USART Control and Status Register C

Bit76543210
(0xC2)UMSEL01UMSEL00UPM01UPM00USBS0UCSZ01UCSZ0UCPOL0
Read/WriteR/WR/WR/WR/WR/WR/WR/WR/W
Initial Value00000110

• Bits 7:6 – UMSEL0 1:0 USART Mode Select
These bits select the mode of operation of the USART

UMSEL1UMSEL0Mode
00Asynchronous USART / UART
01Synchronous USART
10(Reserved)
11Master SPI (MSPIM)

• Bits 5:4 – UPM0 1:0: Parity Mode
These bits enable and set the type of parity generation and check. If enabled, the Transmitter will automatically generate and send the parity of the transmitted data bits within each frame. The Receiver will generate a parity value for the incoming data and compare it to the UPM setting. If a mismatch is detected, the UPE Flag in UCSRA will be set.

UPM1UPM0Parity Mode
00Disabled
01Reserved
10Enabled, Even Parity
11Enabled, Odd Parity

• Bit 3 – USBS0: Stop Bit Select
This bit selects the number of stop bits to be inserted by the Transmitter. The Receiver ignores this setting.

USBSStop Bit(s)
01-bit
12-bit

• Bit 2:1 – UCSZ0 1:0: Character Size
The UCSZ 1:0 bits combined with the UCSZ 2 bit in UCSRB sets the number of data bits (Character Size) in a frame the Receiver and Transmitter use.

UCSZ2UCSZ1UCSZ0Character Size
0005-bit
0016-bit
0107-bit
0118-bit
100Reserved
101Reserved
110Reserved
1119-bit

• Bit 0 – UCPOL0: Clock Polarity
This bit is used for synchronous mode only. Write this bit to zero when asynchronous mode is used. The UCPOL bit sets the relationship between data output change and data input sample, and the synchronous clock (XCK).

UCPOLTransmitted Data Changed (Output of
TxD Pin)
Received Data Sampled (Input on RxD
Pin)
0Rising XCK EdgeFalling XCK Edge
1Falling XCK EdgeRising XCK Edge

UBRR0L and UBRR0H – USART Baud Rate Registers

(0xC5)111098
(0xC4)76573210

• Bit 15:12 – Reserved
These bits are reserved for future use. For compatibility with future devices, these bits must be written to zero when UBRRH is written.

USART Baud Rate Calculation
USART Baud Rate Calculation


• Bit 11:0 – UBRR[11:0]: USART Baud Rate Register
This is a 12-bit register that contains the USART baud rate. The UBRRH contains the four most significant bits, and the UBRRL contains the eight least significant bits of the USART baud rate. Ongoing transmissions by the transmitter and Receiver will be corrupted if the baud rate is changed. Writing UBRRL will trigger an immediate update of the baud rate Prescaler.


Crazy Engineer

MAKER - ENGINEER - YOUTUBER

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.