Serial communication (USART) with different frame size using AVR microcontroller

Summary of Serial communication (USART) with different frame size using AVR microcontroller


This article explains configuring AVR microcontroller serial communication for 5, 6, 7, or 9-bit data frames to optimize efficiency. It details setting UCSZ bits in the UCSRC and UCSRB registers. A specific example demonstrates 6-bit transmission between a PC and microcontroller using HyperTerminal. The text highlights that while numeric ASCII codes work correctly in 6-bit mode, alphabetic characters may display incorrectly because their two most significant bits are masked during reception.

Parts used in the AVR Microcontroller Serial Communication Project:

  • AVR Microcontroller
  • PC with COM port
  • HyperTerminal software
  • User keyboard input
  • USART module
  • UCSRC Register
  • UCSRB Register

The previous article explains serial communication using 8-bit data transfer. AVR microcontroller also supports serial data transfer with frame size of 5, 6, 7 and 9 data bits. The size of data frame can be adjusted according to application. For example, consider a system that needs to transmit only ASCII codes as data. In this case data frame size of 7-bits is sufficient because data length of ASCII code is equal to 7-bit. This will makes system more efficient by saving time of one clock period in each data frame transmission. This article explains serial transfer of data with different frame size.

The frame size of data can be selected by configuring the UCSZ (USART Character Size) bits. The Register UCSRC contains UCSZ [1:0] bits and UCSRB contains UCSZ2 bit. The following table shows the bit settings of UCSZ bits corresponding to different data frame size.
Serial communication (USART) with different frame size using AVR microcontroller
Programming:
Case1: Frame size 5, 6 and 7 bits:
These three frame sizes can be selected by programming only UCSZ1 and UCSZ0 bits. There is no need to program UCSZ2 bit because default value of this bit is zero.
For example:             UCSRC= (1<<UCSEL)|(1<<UCSZ0); // Asynchronous mode, frame size=6 bit,
                                                                 // No parity, 1 stop bit.
Programming steps of sending and receiving data will remain same as used in 8-bit data transmission and reception. For more information, readers can refer the previous article on serial communication.
Test program for 6-bit reception and transmission:
A test program is written for 6-bit data communication between Microcontroller and PC.  In this experiment the input is taken from the user through a keyboard. The corresponding data is sent to microcontroller via PC’s COM port. The microcontroller receives data and returns it again to PC’s COM port. The HyperTerminal is used to configure COM port to make it compatible for this experiment.
The following steps are used to configure COM ports: 
1. Set the Baud rate = 9600 bps, data bit=6, Parity=None, Stop bit=1.
2. Tick the check box corresponding to Echo as shown in following picture to observe both transmitting and receiving data.
Output:
The following output is received when 1 2 3 4 5 6 A B C D E F G keys are pressed form keyboard.
Serial communication (USART) with different frame size using AVR microcontroller schematic
 
As shown in above picture, the proper output is received when numeric keys are pressed but when alphabetic keys are pressed some symbol appears. This unexpected output is received because we are working with 6-bit data frame size. If 6-bit data frame is selected, two most significant bits are masked to zero while reading from UDR. The ASCII code of ‘1’ is 0x31 (0011 0001). If the two most significant bits are masked, 11 0001 remain which is also equal to 0x31. The size of ASCII codes for all numeric values is 6-bits so there will no problem in receiving and transmitting numeric values in 6-bit mode. But the ASCII value of ‘A’ is 0x41 (0100 0001). If two most significant bits are masked, 00 0001 remain which is equal to 0x01 so it will print some unusual symbol.

Quick Solutions to Questions related to AVR Microcontroller Serial Communication:

  • How can the data frame size be adjusted?
    The frame size is selected by configuring the UCSZ bits in the UCSRC and UCSRB registers.
  • What is the benefit of using a 7-bit frame for ASCII codes?
    It saves one clock period per transmission, making the system more efficient.
  • Which register bits control 5, 6, and 7-bit frame sizes?
    Only the UCSZ1 and UCSZ0 bits in the UCSRC register need programming for these sizes.
  • Why do alphabetic keys show symbols instead of letters in 6-bit mode?
    The two most significant bits of the character are masked to zero, altering the ASCII value.
  • What COM port settings are required for the 6-bit experiment?
    Set Baud rate to 9600 bps, data bit to 6, Parity to None, and Stop bit to 1.
  • Does the programming logic change for different frame sizes?
    No, the steps for sending and receiving data remain the same as 8-bit transmission.
  • What happens to the most significant bits when reading from UDR in 6-bit mode?
    The two most significant bits are masked to zero.
  • Can this method transmit only ASCII codes efficiently?
    Yes, a 7-bit frame is sufficient and efficient for transmitting standard ASCII codes.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top