Lecture_17_Serial Communication
Lecture_17_Serial Communication
Lecture 17
Serial Communication
Md Rakibul Hasan
Lecturer, Dept. of EEE, BRAC University
❑ Double-speed asynchronous
❑ Master synchronous
❑ Slave synchronous
Registers used for Serial
Communication
❑ UDR (USART Data Register)
❑ UCSRx (USART Control and Status Register);
[x=A, B or C]
❑ UBRR (USART Baud Rate Register)
UBRR Register and Baud Rate in
AVR
❑ Some of the baud rates supported by PC are 1200, 2400,
4800, 9600, 19200, 38400, 57600, and 115200
❑ AVR transfers and receives data in different baud rate
and its baud rate is programmable.
❑ This is done by USART Baud Rate Register (UBRR).
❑ For a given crystal frequency, the value of UBRR decides
the baud rate.
The Relationship Between Baud Rate
and the Value in UBRR register
❑ The relationship between the baud rate and the UBRR
register value is given by:
Fosc
Desired Baud Rate =
16 ( X + 1)
Where, X=Value in UBRR Register
Fosc
X = −1
16 ( Desired Baud Rate)
UBRR Values corresponding to Baud
Rate (Fosc=16MHz)
Prescaling
f osc /2 /4 /2 0
Down Counter
1
UCSRA
RXC TXC UDRE FE DOR PE U2X MPCM
UCSRA Register
UCSRA Register
❑ FE – Frame Error
❑ This bit is set if a frame error has occurred in receiving
next character in the receive buffer.
❑ A frame error is detected when the first stop bit of
the character in the receive buffer is zero.
DOR and PE bits of UCSRA register
❑ DOR – Data Overrun
❑ This bit is set if data overrun is detected.
❑ 1 = Synchronous
UPM1:0 and USBS bits in UCSRC
Register
❑ UPM 1:0 – Parity Mode
00 = Disabled
01 = Reserved
10 = Even parity
11 = Odd parity
❑ USBS – Stop bit select
0 = 1 bit
1 = 2 bits
UCSZ1:0 and UCPOL bits in UCSRC
Register
❑ UCSZ1:0 – Character Size
❑ These two bits along with UCSZ2 in USCRB register are
used for setting the character size.
URSEL -- -- --
UCSRA Register
RXC TXC UDRE FE DOR PE U2X MPCM
Control
UCSRB Register
RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
UCSRC Register
URSEL UMSEL UPM1 UPM0 USBS UCSZ1UCSZ0 UCPOL
An Example Program for Receiving
and Transmitting data (using USART’s
Transmit/Receive)
Write a C program for the AVR to receive data at baud rate of
9600 baud continuously and transmit back the data three
times. Use 8-bit data, 1 stop bit and no parity. Assume
XTAL=16 MHz.
8-bit data. So, UCSZ2:0=011
1 stop bit. So, USBS=0
No parity. So, UPM1:0=00;
Baud rate=9600. So, for fosc=16MHz, X=0x0067; So,
UBRRH=0x00 and UBRRL=0x67
We have to enable Receive. So, RXEN=1
We have to enable Transmit. So TXEN=1
Interfacing Circuit
Settings in the Proteus Virtual
Terminal
#include <mega32.h> void main()
{
unsigned char data; // to set the Baud rate to 9600
UBRRH=0x00;
//function to receive data UBRRL=0x67;
unsigned char uart_receive () UCSRB= (1<<RXEN) |
{ (1<<TXEN);
while(!(UCSRA&(1<<RXC))); UCSRC=(1<<URSEL) |
// wait until 8-bit of a character is (1<<UCSZ1) | (1<<UCSZ0) ;
The Code
received while(1){
return UDR; data=uart_receive();
} // uC will wait infinitely to
get a 8-bit character
//function to transmit data // the functions below will
void uart_transmit (unsigned char data) print every character three
{ times
while (!( UCSRA & (1<<UDRE))); uart_transmit(data);
// wait for the UDR to be empty uart_transmit(data);
UDR = data; uart_transmit(data);
} }
}
Thanks
12 December 2022 37