For the UART - Serial Communication How-To:
NOTE: Not all the bits in the following registers are discussed - only those that apply to the projects completed in the How To section. Additionally, the Atmega324p is equipped with two USARTs. USART0 and USART1. Here, we are configuring USART0 - as indicated by the example: Register UBRR0 is shown below. If we were using USART1, the appropriate register would be UBRR1. For more information on this see the USART: Register Description section of the Atmega324p's datasheet.
Unless otherwise specified, the bits in the register - from left to right - start at 7 and count down to 0.
UBRR0: USART Baud Rate Registers - Sets the Baud Rate using the USBRR0H and USBRR0L registers. See the UART and Baud Rate for more details.
UBRR0H: Contains the most significant bits (MSB) of the baud rate. Defaults values are 0. Bits 15 - 12 are unused.
15 14 13 12 11 10 9 8
UBRR0L: Contains the least significant (LSB) bits of the baud rate. Default values are 0.
7 6 5 4 3 2 1 0
UCSR0A: USART Control and Status Register A
RXC0: Receive Complete Bit. Set to 1 when there is unread data in the buffer. Set to 0 when buffer is empty.
TXC0: Transmit Complete Bit: Set to 1 when the buffer is empty (no unread data) and 0 when the buffer is not-empty.
UDRE0: Data Register Empty Bit. Set to 1 when the UDR0 is ready to receive data/buffer is empty.
UCSR0B: USART Control and Status Register B
RXEN0: Receiver Enable Bit. Sets the PD0 pin (pin 14) on the Atmega324p to input.
TXEN0: Transmit Enable Bit. Sets the PD1 pin (pin 15) on the Atmega324p to output.
(USBSZ02): Combined with the USBSZ01 & USBSZ00 to set the frame size. ie the binary value 011 (or decimal 3) represents and 8-bit frame.
UCSR0C: USART Control and Status Register C
USBS0: Stop Bit Selection Bit. Setting this to 0 selects a single stop bit and setting it 1 selects 2 stop bits.
USBSZ00: In combination with USBSZ01 and USBSZ02 sets the frame rate. In this case a 3, 011, shifted by the USBSZ00 sets the USBSZ02 to 0, USBSZ01 to 1 and USBSZ00 to 1 = 8 bit frame rate.
(USBSZ01): (See USBZ00)
UDR0: USART Data Register. Contains the Receive and Transmit Registers and operates as FIFO.
UDR0 - RXB7-0 (Read) Default values are 0.
UDR0 - TXB7-0 (Write) Default values are 0.
The transmit (write) buffer can only be written to when the UDRE0 flag is not set in the UCSR0A. For more details on this see the datasheet.
DDRD: Data Direction for Port D. Setting to 1 makes this output, setting to 0 makes this input. 0xff = all 1's
PORTD: Port D pins - setting this to all 1's sets all the pins to output, all 0's sets the pins to input.