The document discusses serial communication using the Universal Synchronous Asynchronous Receiver and Transmitter (USART) on AVR microcontrollers. It describes the registers used for serial communication like UCSRA, UCSRB, UCSRC. It explains baud rate, parity modes, data sizes. It provides code examples to initialize the USART, transmit data, and receive data.
3. SERIAL COMMUNICATION IN AVR
USRT
AVR microcontrollers have a dedicated hardware for serial
communication; this part is called Universal Synchronous
Asynchronous Receiver and Transmitter (USRT).
You just have to supply the data to transmit and it will do the
rest.
The data to be transmitted is written to one of the register of
USRT and USRT transmits it.
Also USART automatically senses the transmission of RX line and
then inputs the whole Byte and when it has the byte it informs
the CPU to read the data from one of its registers.
4. BAUD RATE AND BIT RATE
Baud rate is the number of bits transmitted
in one second.
Bit Rate is the number of data bits
transmitted in one second.
Both of them has the same unit
‘bits/second’.
Baud rate is determined by the number of
bits in one frame while bit rate is
determined by the number of data its in
that frame.
Atmega16 support five different data sizes
5,6,7,8 and 9 bits.
5. RS232 COMMUNICATION
RS232 is a asynchronous serial
communication protocol widely used in
computers and digital systems.
It is called asynchronous because there is no
separate synchronizing clock signal as there
are in other serial protocols like SPI and I2C.
The Protocol is such that it automatically
synchronize itself.
We can use RS232 to easily create a data link
between our MCU based project and
standard PC.
Excellent example is a commercial serial PC
mouse.
6. RS232
In Rs 232 there are two data lines RX and TX.
TX is the wire in which the data is sent out to other device.
RX is the line in which other device put the data it need to
sent to the device.
We know that a HIGH = +5V and LOW= 0V in TTL/MCU
circuits nut in RS232 a HIGH= -12V and a LOW =+12V.This
increases the range and reliability of data transfer.
Thus to interface a RS232 with the MCU we need to use an
IC MAX232.As there is clock line so for synchronization
accurate timing is required so transmissions are carried
out with certain standard speeds.
The speeds are measured in bits per second. Number of
bits transmitted is also known as baud rate. Some standard
baud rates are1200, 2400, 4800, 9600, 19200, 38400,
57600, 115200……..etc
7. UART OF AVR MICROCONTROLLERS
The UART of AVR is versatile and can
be setup for various different mode as
required by your application.
The Universal Synchronous and
Asynchronous serial Receiver and
Transmitter
(UART) is a highly flexible serial
communication device.
The UART of the AVR is Connected to
the CPU by the following Six Registers
8. UCSRA
RXC: Reception Complete: This bit shows the completion of Reception of
a frame by the USART. If the receiver has received a complete byte it sets
this bit as 1 otherwise it remains 0.
TXC: Transmission Complete: This bit shows the completion of
Transmission of a frame by the USART. If the transmitter has transmitted a
complete byte it sets this bit as 1 otherwise it remains 0.
UDRE: USART Data Register Empty: This bit shows the status of UDR.
This bit set if the UDR is empty otherwise reset.
Bit No 7 6 5 4 3 2 1 0
Name RXC TXC UDRE FE DOR PE U2X MPCM
Initial Val 0 0 1 0 0 0 0 0
9. CONTINUE…..
FE: Framing Error: This bit shows any error in the frame transmitted or
received using USART. This bit is high (‘1’) when there is any error in the
frame transmitted or received otherwise it is low (‘0’).
DOR: Data Over Run: This bit shows where they data bit received have
overflowed or not. This bit is high if the data received has overflowed the bit
size of UDR otherwise it is low.
PE: Parity Error: This bit shows any error in the parity bits, if occurred.
This bit is set if there is a parity error in the frame received otherwise it is
reset.
U2X: Double Speed Transmission: This bit when high doubles the speed at
which the frame is being transmitted.
MPCM: Multi Processor Communication Mode: This bit is high when the
mode of operation of the USART is Multi Processor otherwise it is low.
10. UCSRB
RXCIE: Receiver Complete Interrupt Enable: When this bit is set, an
interrupt is generated at the completion of the reception of a byte.
TXCIE: Transmitter Complete Interrupt Enable: When this bit is set,
an interrupt is generated when the transmission gets completed of a
byte.
UDRIE: USART Data Register Interrupt Enable: When this bit is set,
an interrupt is generated whenever the UDR is Empty.
Bit No 7 6 5 4 3 2 1 0
Name RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
Initial Val 0 0 0 0 0 0 0 0
11. CONTINUE…..
RXEN: Receiving Enable: This bit is set whenever we want to receive some byte
using USART.
TXEN: Transmission Enable: This bit is set whenever we want to transmit some
byte using USART.
UCSZ2: USART Character Size Bit 3: This is the MSB bit of the three bits needed
to set the data size in USART communication. The other two bits are available in
UCSRC register.
RXB8: Received Bit 8th: This Bit is used to Store the 8th bit in 9 bit frame size of
data Communication. If a 9 bit frame is received in 9 bit frame size
communication mode, the LSB is stored at this position.
TXB8: transmitted bit 8th : This bit is used to store the 8th bit in 9 bit frame size
of data communication. If a 9 bit frame is to be transmitted, the LSB is stored at
this Position.
12. UCSRC
URSEL: USART Register Select: The UCSRC and the UBRRH register
shares same address so to determine which register user want to write
is decided with the 7th (last) bit of data. If it is 1 then the data is written
to USCRC else it goes to UBRRH.
Bit No 7 6 5 4 3 2 1 0
Name URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
Initial Val 0 0 0 0 0 0 0 0
13. CONTINUE…..
UPM: USART Parity Mode: The parity to be used while communicating
is decided by the combination of 5th and 4th bit of this register.
UPM1 UPM0 Parity Used
0 0 Disabled
0 1 Reserved
1 0 Enabled, Even Parity
1 1 Enabled, Odd Parity
14. CONTINUE…..
USBS: USART Stop Bit Select: This bit is used to select the number of
stop bits in the transfer.
USBS Stop Bit(s)
0 1 BIT
1 2 BIT
15. CONTINUE….
UCSZ: USART Character Size: These bits decide the number of data
bits that will be present in each frame that will be communicated. It is a
combination of 3 bits (UCSZ2, UCSZ1 and UCSZ0). You can select among
5,6,7,8 or 9 bit frame.
UCSZ2 UCSZ1 UCSZ0 Character Size
0 0 0 5Bit
0 0 1 6Bit
0 1 0 7Bit
0 1 1 8Bit
1 0 0 Reserved
1 0 1 Reserved
1 1 0 Reserved
1 1 1 9Bit
16. USART BAUD RATE REGISTER
It is a 16 bit register. The higher byte of the bud rate is stored in UBRRH while the
lower value of the baud rate is stored in UBRRL. This register is used by the
USART to generate the data transmission at specified speed.
The UBRR value is calculated according to following formula
Where fosc is your CPU frequency
UBRR can hold only integer value. So it is better to use the baud rates that give
UBRR value that is purely integer or very close to integer. So if UBRR value comes
to be 7.68 and you decide to use UBRR=8 then it has high error percentage, and
communication is unreliable.