Project is an AVR128DB48 lab covering UART communications
 
 
 
Go to file
Jason Layton c763ac37e7 MPAE-16135 - readme.md editorial updates 2023-08-21 10:41:08 -07:00
.citd MPAE-16135 added CI support and readme 2023-07-26 03:34:27 +00:00
.main-meta MPAE-16135: Moving project files to new rebase branch 2023-08-17 11:20:50 -07:00
basicUARTcomms_AVRDB.X MPAE-16135: Moving project files to new rebase branch 2023-08-17 11:20:50 -07:00
images MPAE-16135: Moving project files to new rebase branch 2023-08-17 11:20:50 -07:00
.gitignore MPAE-16135: Moving project files to new rebase branch 2023-08-17 11:20:50 -07:00
LICENSE.txt MPAE-16135 added CI support and readme 2023-07-26 03:34:27 +00:00
README.md MPAE-16135 - readme.md editorial updates 2023-08-21 10:41:08 -07:00

README.md

MCHP

Basic Bidirectional UART Communication Using the AVR128DB48

Overview

This project uses the MPLAB® Code Configurator (MCC) Melody graphical programming tool to configure the AVR128DB48 device populating a Curiosity Nano (Cnano) board to receive and output single ASCII characters over the integrated Serial/CDC port. When the switch on the Cnano board is pressed, a pin change interrupt is triggered and the ASCII character 'S' is transmitted over the Universal Asynchronous Receiver Transmitter (UART), converted to the USB protocol and then displayed in the MPLAB Data Visualizer terminal window. When the ASCII character 'T' (note the capitalization) is entered in the Data Visualizer terminal window, the value is transmitted over USB and then converted to the UART protocol. A UART Receive interrupt will then be triggered and the LED on the Cnano board will toggle ON/OFF. Note that any other character will not generate the interrupt.

Basic Bidirectionsl UART Communications

Note: The following project can be used as a reference for most AVR® devices with some modification.

Software Used

Hardware Used

Hardware Configuration Using the MCC Melody

Create a new project inside of MPLAB X IDE and open the MPLAB Code Configurator (MCC) Melody plug-in by either clicking the icon at the top of the IDE or by selecting Tools>Embedded>MPLAB Code Configurator v.x: Open/Close.

Once inside the MCC Melody plug-in, add an instance of the UART peripheral from the Device Resources:

Add UART Peripherals

Once added, configure the UART as follows:

Next, configure the UART by selecting the UART(None) instance in either the Project Resources panel or the Builder tab to open the Easy View window.

Select UART(None)

The AVR128DB48 populating the Cnano board features five USART peripherals. The correct UART must be selected as the hardware that converts the UART signal to and from USB connects to specific pins on the device. Determining which UART and pins to use can be found in the AVR128DB48 Cnano schematic. A link to the schematic is available in the Kit Window which should open whenever a Cnano board is connected to the computer and MPLAB X IDE is open or by selecting Window>Kit Window inside of the IDE.

Curiosity Nano Kit Window

The first page of the schematic features a connection interface diagram including the UART and associated pins needed to interface with the integrated Serial/CDC port. In this instance, connect USART3 to pins RB4 for the transmit signal (TX) and RB5 for the receive signal (RX).

Curiosity Nano Kit Window

Also note that the integrated LED is connected to pin PB3 and the switch is connected to pin PB2.

Inside Easy View select USART3 from the UART PLIB Selector.

Select USART3

The image below presents how the Builder window will look:

USART3 Builder

Select the USART3 driver either from the Project Resources panel or from the Builder window to open the Easy View settings in the USART3 tab.

Open USART3 Easy View

Inside Easy View enable interrupts in the Interrupt Settings section by toggling the Interrupt Driven slider to the ON position and leaving all other settings at their default:

Enable USART3 Interrupts

Select the USART3PLIB instance in the Builder window to open its Easy View and ensure that both Receive and Transmit interrupts have been enabled:

USART3 PLIB Interrupts

In the Pin Grid View pane at the bottom of the Melody interface, connect the USART3 RX2 signal to pin RB5 (package pin #17) and the USART3 TX2 signal to pin RB4 (package pin #16) by clicking the lock icon associated with them:

USART3 Pin Grid View Configuration

Next, configure the pins associated with the Curiosity Nano switch and LED.

In the Pin Grid View pane, connect pin PB2 (package pin #6) that is connected to the switch (SW0) on the Curiosity Nano board as a GPIO input, and pin PB3 (package pin #7) connected to the LED (LED0) as an output by clicking the appropriate grid square for the associated signal:

Connecting Switch and LED pins

Go to Project Resources>System>Pins to open the associated Easy View.

Select Pins

In the Custom Name column, rename pin PB2 to "SW0" and pin PB3 to "LED". This will make it easier to navigate and use the generated API.

Rename Pins

Note from the Curiosity Nano schematics that the SW0 is active-LOW and connected directly to pin PB2 through a current limiting resistor. To eliminate unknown states, the switch pin will need to have a pull-up resistor. The AVR128DB48 device family features integrated Weak PullUps which can be enabled by checking the box for the associated pin in the Weak Pullup column of the Easy View:

Enabling Weak Pullups

An Input/Sense Configuration (ISC) will be generated when the voltage on the pin associated with SW0 changes. Since the switch on the Curiosity Nano board is active-LOW, this means that the switch resting state will be HIGH until pressed. Therefore, the Input/Sense Configuration column setting for the SW0 pin is configured to trigger on the Sense Falling Edge transition of the pin voltage using the drop-down menu:

Enabling Sense Falling Edge

Finally, enable global interrupts to use interrupts on the AVR128DB48, by navigating to Project Resources>Interrupt Manager.

Interrupt Manager

Then select 'Global Interrupt Enable'.

Select Global Interrupt Enable

Click the Generate button in the Project Resources to generate the API based on the above configurations:

Generate Code

Using Interrupts in Generated API

Navigate to the Projects tab in MPLAB X IDE. Locate the USART3.c source file by expanding the folders basicUARTcomms_AVRDB>Source Files>MCC Generated Files>uart>src>usart3.c. Double click usart3.c to open and do the following:

Scroll down to USART3_ReceiveISR(). When the UART detects a received value on its receive (RX) pin, an interrupt will be generated and this section of code will be executed:

USART3 Receive ISR

Inside of the Interrupt Service Routine (ISR), locate the section that reads the received value. This is done using the regValue variable to read the contents of the USART3 Receive Register (U2RXB) .

Locating the reg_value Variable

Note: Reading the U2RXB register clears the value inside of the register.

This variable checks the value received. If it is the character "T", it toggles the LED ON/OFF, but if it isn't, nothing happens. To do this, use the regValue variable along a generated macro called LED_Toggle(), based on the unique name given to pin PB3 in the previous section, inside of an if statement, as follows:

if(regValue == 'T')LED_Toggle();

Add the above code immediately below the regValue = U2RXB; in the receive ISR:

Using the LED_Toggle() Macro

Note: The LED_Toggle() macro can be viewed in the pins.h header file located in basicUARTcomms_AVRDB>Header Files>MCC Generated Files>system>pins.h:

Locating the LED_Toggle Macro

Next, configure the SW0 interrupt to transmit the character 'S' over the UART when pressed. Navigate to the pins.c file in basicUARTcomms_AVRDB>Source Files>MCC Generated Files>system>src>pins.c. Scroll through the file to locate the SW0_DefaultInterruptHandler() ISR named after the custom name added to pin PB2 in the previous section:

Locating the SW0 ISR

Inside the ISR, add the following code to transmit the character 'S' using the USART3_Write() when the SW0 is pressed below the commented section:

USART3_Write('S');

Adding the USART3_Write()

To use the USART3_Write(), the USART3.h file must be added. Scroll to the top of the pins.c source file and add the following code immediately below the #include "../pins.h" include:

#include "../../uart/USART3.h"

Including USART3.h

Finally, program the AVR128DB48 by hitting the Make and Program Device Main Project button at the top of the IDE:

Make and Program Device

Operation

The Data Visualizer MPLAB X IDE plugin will verify the operation of the project.

Click the Data Visualizer button at the top of the IDE to open:

Open Data Visualizer

The Data Visualizer displays the AVR128DB48 Curiosity Nano board and associated COM port. Note that your COM port number may differ from what is shown below.

Data Visualizer

The terminal window will be the only one used so the rest can be closed as shown below. Begin communication between the COM port and terminal by hovering the COM port instance and selecting the Display as text in the terminal button:

Select Terminal

Verify that the USART3 receive interrupt is working by clicking inside of the terminal window and entering the character 'T'. This action toggles the LED on the Curiosity Nano board ON/OFF. Entering any other character should have no effect

Verify the SW0 interrupt on change by pressing the switch on the Curiosity Nano board. The character 'S' will show as output to the terminal.

Verify Demo

Summary

This example demonstrates basic UART transmit and receive functionality using an AVR128DB48 microcontroller and MPLAB X with MCC melody.