megaAVR® USART Example (Polled)

 Objective

This page provides a simple project demonstrating polled operation of the USART peripheral on megaAVR® devices. The code example runs on the ATmega328PB MCU.

The project configures the Timer/Counter1 module to operate in Clear-Timer-On-Compare (CTC) mode, and, on a period match, generates a "tick" interrupt every 100 mS. The main loop monitors these tick signals to implement a time-of-day clock (HH:MM:SS format). LED0 is also toggled at every tick event.

The clock display is sent out USART0 TX every second and is controlled by user input 'control' characters received on USART0 RX:

  • 'u' enables updates to the clock display every second
  • 'f' freezes updates to the display


The clock interface is displayed using a Terminal Emulator program, such as Tera Term.

 Materials

Hardware Tools

Tool About Purchase
ATmega328PB-XplainedMini-50px.png
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment
swtool-28px.png
Tera Term
Terminal Emulator

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Example Project

We recommend extracting the .zip file to your C:\ folder.

You should see the folder C:\MTT\8avr\mega\code-examples\usart-example-polled\8avr-mega-usart-example-polled containing the solution 8avr-mega-usart-example-polled.atsln

 Connection Diagram

The USART0 module on the target ATmega328PB device is connected to the USART interface on the mEDBG chip. The mEDBG chip performs USB-serial conversion by enumerating as a CDC-class virtual COM port on the PC and presenting the target USART data on this interface. Note that the mEDBG also controls the programming/debug interface, as well as supplying a 16MHz clock when the Xplained board is connected via USB cable to a PC.

xplained-mini-connection-diagram-teraterm.png

 Procedure

Attach the ATmega328PB Xplained Mini board to your computer using a USB-A-male-to-Micro-B-male cable. Start Atmel Studio 7. If the board has been successfully enumerated, you should see the board image come up in Studio as shown:

xplained-mini-enumeration-success.png

The Xplained mini board is identified by the last four digits in its serial number (see sticker on bottom of board). In the above example, the last four digits are "3352"

You should also see a mEDBG Virtual COM Port enumerated in your Windows Device Manager viewer. Please note the COM port number assigned to your board:

xplained-com-port-windows.png

1

Open the Solution

In Studio, select File » Open » Project/Solution and navigate to the saved location of the solution:

as7-open-solution.png
as7-open-usart-polled-solution-detail.png

To review the procedures for configuring/using the megaAVR® USART (as implemented in the project's main.c file), please review the megaAVR® USART Configuration page.

2

Rebuild the Solution

as7-rebuild-usart-polled-solution.png

3

Program the Fuses

There are several key hardware configuration settings that need to be configured. The following fuse settings need to be programmed into the device:

  • HIGH: 0xDF
  • LOW: 0xC0
  • EXT: 0xFC

Enter the Device Programming dialog as shown:

as7-programming-fuses-usart-polled-1.png

In the Device Programming dialog box, select the Tool, Device and Interface as shown, then press Apply:

as7-program-fuses-2.png

To verify a connection, select Read and verify that a Device Signature is found:

as7-program-fuses-3.png

Select the Fuses sub-section, Enter the 3 Fuse Byte Values above, then press Program as shown:

as7-program-fuses-4.png

4

Program the Hex File

While still in the Device Programming dialog box, select the Memories sub-section as shown. The path to the solution's hex file should already be listed in the dialog. Press Program as shown:

as7-program-hex-usart-polled-1.png

5

Start the Tera Term application. Select the correct mEDBG COM port in the dialog box that appears:

teraterm-select-com-port.png

Next, set the serial port parameters to match the project defaults: 38400 baud, 8-data, no parity, 1-stop, no flow-control as shown:

teraterm-select-com-parameters.png

 Results

usart-mega-polled-example-results.png

You may see gibberish on the display after resetting the communication parameters. Simply perform a board reset by shorting RST to GND, or by re-programming the hex file into the board again (see step 4 above).

 Conclusions

This project has provided an example of how to setup and use the USART module on the megaAVR® MCU.

 Learn More

 
megaAVR® USART Overview
Learn more >
 
megaAVR® USART Configuration
Learn more >
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.