AVR Microcontroller (Atmel 8) Serial Communication USART Configuration - LEKULE

Breaking

24 Aug 2016

AVR Microcontroller (Atmel 8) Serial Communication USART Configuration

Microcontroller is a control device that contains a number of peripherals like RAM, ROM TIMERS, serial data communication, etc., which are required to perform some pre-defined tasks. Nowadays, advanced type of microcontrollers are used in a wide variety of applications as per their capability and feasibility to perform some desired tasks and these controllers include 8051, AVR and PIC microcontroller. In this article, we are going to learn about advanced AVR family microcontroller and its programming.

AVR Microcontroller

The AVR is a type of controlling device manufactured by the Atmel Corporation in 1996. The AVR does not stand for anything, it is just a name. The AVR microcontrollers consist of the Harvard architecture, and therefore, the device run very fast with a reduced number of machine level instructions (RISC). The AVR microcontrollers consist of special features compared with other microcontroller such as 6-sleep modes, inbuilt ADC, internal oscillator and serial data communication, etc. The AVR microcontrollers are available in different configurations of 8-bit, 16-bit, and 32-bit to perform various operations.

AVR Microcontroller
AVR Microcontroller

USART Serial Data Communication in AVR Microcontroller

The USART stands for universal synchronous and asynchronous receiver and transmitter. It is a serial communication of two protocols. This protocol is used for transmitting and receiving the data bit by bit with respect to clock pulses on a single wire. The AVR microcontroller has two pins: TXD and RXD, which are specially used for transmitting and receiving the data serially. Any AVR microcontroller consists of USART protocol with its own features.

USART Communication in AVR Microcontroller
USART Communication in AVR Microcontroller


The Main Features of AVR USART
  • The USART protocol supports the full-duplex protocol.
  • It generates high resolution baud rate.
  • It supports transmitting serial data bits from 5 to 9 and it consists of two stop bits.

USART Pin Configuration

The USART of AVR consists of three Pins:
  • RXD: USART receiver pin (ATMega8 PIN 2; ATMega16/32 Pin 14)
  • TXD: USART transmitter pin (ATMega8 PIN 3; ATMega16/32 Pin 15)
  • XCK: USART clock pin (ATMega8 PIN 6; ATMega16/32 Pin 1)

Modes of Operation
The AVR microcontroller of USART protocol operates in three modes which are:
  • Asynchronous Normal Mode
  • Asynchronous Double Speed Mode
  • Synchronous Mode
Modes of Operation
Modes of Operation


Asynchronous Normal Mode

In this mode of communication, the data is transmitted and received bit by bit without clock pulses by the predefined baud rate set by the UBBR register.


Asynchronous Double Speed Mode

In this mode of communication, the data transferred at double the baud rate is set by the UBBR register and set U2X bits in the UCSRA register. This is a high-speed mode for synchronous communication for transmitting and receiving the data quickly. This system is used where accurate baud rate settings and system clock are required.


Synchronous Mode

In this system, transmitting and receiving the data with respect to clock pulse is set UMSEL=1 in the UCSRC register.

USART Configuration In AVR microcontroller

USART can be configured using five registers such as three control registers, one data register and baud-rate-selection register, such as UDR, UCSRA, UCSRB, UCSRC and UBRR.


7 Steps for Composing the Program


Step1: Calculate and Set the Baud Rate


The baud rate of USART/UART is set by the UBRR registrar. This register is used to generate the data transmission at the specific speed. The UBRR is a 16-bit register. Since the AVR is a 8-bit microcontroller and its any register size is 8-bit. Hence, here the 16-bit UBRR register is composed of two 8-bit registers such as UBRR (H), UBRR(L).


The formula of the baud rate is

BAUD= Fosc/(16*(UBBR+1))

The formula of the UBRR register is

UBRR= Fosc/( 16*(BAUD-1))

The frequency of the AVR microcontroller is 16MHz=16000000; Let us assume the baud rate as 19200Bps, then

UBRR= 16000000/(16*(19200-1))
UBRR= 16000000/(16*(19200-1))
UBRR= 51.099

Eventually find the baud rate

BAUD= 16000000/( 16*(51+1))
UBRR= 19230bps


Step2: Data Mode Selection

The data transmission mode, start bit and stop bit and the character size is set by the control and status register UCSRC.

Data Mode Selection
Data Mode Selection


Step3: Data Transmission Mode Selection
The synchronous and asynchronous mode is selected by the UMSEL bit of the control status register. If we give UMSEL=0, then the USART operates in asynchronous mode, otherwise operates in synchronous mode.
Data Transmission Mode Selection
Data Transmission Mode Selection


Step4: Start Bit and Stop Bit

The start bit and stop bits are a way for sending and receiving the data serially. Generally any data fame consists of one stat bit and one stop bit, but the AVR microcontroller has one start bit and two stop bits for processing the data. The extra stop bit can be useful for adding a little extra receive processing time. It is especially useful for high data transfer rates, whereas the data transfer speed is very high, so we don’t get proper data. Thus, we can increase the processing time by using two stop bits to get the proper data.

Start Bit and Stop Bit
Start Bit and Stop Bit

The number of stop bits is selected by the USBS bit of UCSRC – the control status register. The USBS=0, for one stop bit, and USBS=1, for two stop bits.

Step5: Set the Character Size

As in case with the basic microcontrollers sending and receiving the byte of data(8-bits) at a time, whether in a AVR microcontroller, we can choose a data frame format in each frame by the UCSZ bit of the UCSRC register.
Data Frame Format
Data Frame Format


Step6: Store the Received Data


The AVR microcontroller consists of a UDR buffer register for transmitting and receiving data. The UDR is a 16-bit buffer register wherein 8-bits are used for receiving (RXB) the data and other bits are used for transmitting the data (TXB). Transmitting data buffer register will be the destination to UDR register for the written data on its location. Receiving data buffer register will be returning the content of the UDR register.


Step7: Transmitter and Receiver Enabling
The transmitted and received data will be allowed by the RXC and TXC pins of the microcontroller which are set by the UCSRA register of the microcontroller. This flag bit set by the microcontroller for the data is completed by receiving and transmitting (TXC=RXC=1).



Double the Baud Rate


We can double the transfer rate of the USART communication of the AVR microcontroller from 16 bits to 8-bits effectively by the U2X –bit in the UCSRA register. This bit effects only on asynchronous operation. If we can set this bit (U2X=1), it will reduce the baud rate from 16-bit to 8-bit effectively doubling the transfer rate for synchronous communication.


This is an advanced feature of the AVR microcontroller for speedy processing of the data.


USART Program






Every microcontroller is predefined with a specific IDE,  and based on this IDE, microcontrollers are programmed with embedded C or assembly language. The AVR microcontroller programming is developed by the AVR studio. Furthermore, If you want additional information about the steps to build microcontroller based projects, or detailed information on this topic, you can contact us by commenting below.

No comments: