0% found this document useful (0 votes)
100 views

AVR Serial Port Programming

The document discusses serial communication using the UART on AVR microcontrollers. It describes the UART registers used for sending, receiving, and controlling serial data including the UDR, UBRR, UCSRA, UCSRB, and UCSRC registers. It provides information on setting the baud rate using the UBRR register and programming the UART for both transmitting and receiving serial data. Code examples are given to continuously transmit and receive characters via the UART serial port.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

AVR Serial Port Programming

The document discusses serial communication using the UART on AVR microcontrollers. It describes the UART registers used for sending, receiving, and controlling serial data including the UDR, UBRR, UCSRA, UCSRB, and UCSRC registers. It provides information on setting the baud rate using the UBRR register and programming the UART for both transmitting and receiving serial data. Code examples are given to continuously transmit and receive characters via the UART serial port.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Advanced Embedded

Systems

AVR Serial Port Programming

Lecture 0801

The AVR Microcontroller and Embedded Systems using Assembly and C


by
M. A. Mazidi, S. Naimi and S. Naimi

1
UART Registers in AVR
 Data Send/Receive register
 UDR
 Baud Rate Control register
 UBRR
 Control and Status register
 UCSRA, UCSRB, and UCSRC

2
Data Transfer Rate
 The rate of data transfer in serial data communication is
stated in bps (bits per second), which is also called baud
rate.

 To allow data transfer between the devices and a


Microcontroller system without any error, we must make sure
that the baud rate of microcontroller system must match the
baud rate of the device.

 A microcontroller can transfer and receives data serially at


many different baud rates.

3
UBRR Register and Baud Rate in AVR
 The Baud rate of AVR microcontroller is
programmable that can be set with the
help of register UBRR

Assume 16 MHz Crystal, Find the UBRR value for 9600bps.

4
UBRR Register and Baud Rate in AVR

System
clock
UART
baud rate

5
UBRR Register (16-Bit Register)

12 bits are used to set the USART baud rate

6
Data Register (UDR)
In AVR, There are 2 shift registers that have directly connected buffers TXB and
RXB. When we write data to UDR, it is transferred to TXB buffer register and
when read data from UDR, it will return the contents of RXB buffer register

7
UCSR (USART Control and Status) Registers
UCSRA Register

 RXC (Bit 7): USART Receive Complete


 This flag bit is set when there are new data in the receive buffer
that are not read yet. It is cleared when the receive buffer is
empty.
 TXC (Bit 6): USART Transmit Complete
 This flag bit is set when the entire frame in the transmit shift
register has been transmitted and there are no new data
available in the transmit data buffer register (TXB). It can be
cleared by writing a one to its bit location.
 UDRE (Bit 5): USART Data Register Empty
 FE (Bit 4): Frame Error
 DOR (Bit 3): Data OverRun
 PE (Bit 2): Parity Error
 U2X (Bit 1): Double the USART Transmission Speed
 MPCM (Bit 0): Multi-processor Communication Mode

8
UCSR (USART Control and Status) Registers
UCSRB Register

 RXCIE (Bit 7): Receive Complete Interrupt Enable


 TXCIE (Bit 6): Transmit Complete Interrupt Enable
 UDRIE (Bit 5): USART Data Register Empty Interrupt Enable
 RXEN (Bit 4): Receive Enable
 To enable the USART receiver you should set this bit to

one.
 TXEN (Bit 3): Transmit Enable
 To enable the USART transmitter you should set this bit

to one.
 UCSZ2 (Bit 2): Character Size
 This bit combined with the UCSZ1:0 bits in UCSRC sets the

number of data bits (character size) in a frame.


 RXB8 (Bit 1): Receive data bit 8
 TXB8 (Bit 0): Transmit data bit 8

9
UCSR (USART Control and Status) Registers
UCSRC Register

 URSEL (Bit ): Register Select


 UMSEL (Bit 6): USART Mode Select
 UPM1:0 (Bit 5:4): Parity Mode
 USBS (Bit 3): Stop Bit Select
 UCSZ1:0 (Bit 2:1): Character Size
 These bits combined with the UCSZ2 bit in UCSRB set

the character size in a frame.


 UCPOL (Bit 0): Clock Polarity
UCSZ2 UCSZ1 UCSZ0 Char. size
0 0 0 5-bit
0 0 1 6-bit
0 1 0 7-bit
0 1 1 8-bit
1 1 1 9-bit

10
Programming AVR to Transfer Data Serially
1. Load UCSRB register with value 08H.
(Enabling the USART Transmitter, TXEN)
2. Load UCSRC register with value 06H.
(Indicating 8-bit data frame, no parity and 1 stop bit)
3. Load UBRR register with value of baud rate.

4. Write character to be transmitted in UDR register.

5. Check TXC (Transmit Complete) bit in UCSRA Reg.


If 0, Return to step 5
Else Step 6
6. To transmit next character, go to step 4

11
Write a C program for AVR to transfer character ‘G’ continuously at
9600 baud rate. Use 8-bit data and 1 stop bit. XTAL = 8MHz
#include <avr/io.h>
#include <avr/io.h> void usart_init (void)
int main (void) {
UCSRB = 0x08;
{ UCSRC = 0x06;
UCSRB = 0x08; UBRRL = 0x33;
}
UCSRC = 0x06;
UBRRL = 0x33; void usart_send (unsigned char ch)
{
UDR = ch;
while(1) while ((UCSRA & (0x40)) == 0);
{ }
UDR = 'G’; int main (void)
while ((UCSRA & (0x40)) == 0); {
} usart_init();
return 0; while(1)
} usart_send ('G’);

return 0;
}

12 12
Problem
Write a program for AVR to transfer message ‘Hello
World’ to serial port continuously at 9600 baud
rate. Use 8-bit data and 1 stop bit. XTAL = 8MHz

13 13
Programming AVR to Receive Data Serially
1. Load UCSRB register with value 10H.
(Enabling the USART Receiver, RXEN)
2. Load UCSRC register with value 06H.
(Indicating 8-bit data frame, no parity and 1 stop bit)
3. Load UBRR register with value of baud rate.

4. Check RXC (Receive Complete) bit in UCSRA Reg.


If 0, Return to step 4
Else Step 5
5. Move received character from UDR register.

6. To receive next character, go to step 4

14
Write program to receive bytes of data serially and puts them
on Port B. Set 9600 baud rate, 8-bit data and 1 stop bit
#include <avr/io.h>
int main (void)
{
DDRB = 0xFF; //Port B is output
UCSRB = 0x10; //initialize USART0
UCSRC = 0x06;
UBRRL = 103;

while(1)
{
while ((UCSRA & (0x80)) == 0); //wait until new data
PORTB = UDR;
}
return 0;
}

15 15

You might also like