PIC32 UART Interrupt example Project using MPLAB Harmony

A step by step PIC32 UART interrupt example guide to learn how to use MPLAB Harmony

Microchip provides MPLAB Harmony for solution to complicated program requirement with PIC32 Family of microcontrollers. MIcrochip offers MPLAB Harmony for two PIC32 microcontroller families called PIC32MX and PIC32MZ. Here we will use the PIC32MX470F512L microcontroller and illustrate how to use the MPLAB Harmony to enable UART communication with interrupt with a PC. Although we have used PIC32MX470F512L  any PIC32MX or PIC32MZ will work.

In this pic32 uart interrupt example you can learn how to use PIC32 UART using the MPLAB Harmony framework. In this project example, you connect computer to PIC32 UART and using TeraTerm hyperterminal program running on your computer, you will send some character to PIC32 microcontroller and the microcontroller will send it back(echo) the same character to the hyper-terminal application.

Tera Term hyper terminal is a free program that you can download. An example of using Tera Term port emulator is the Arduino Bluetooth Module HC-05 Tutorial.

Creating the Project

The first step using MPLAB Harmony for your application is to start a new MPLAB Harmony project. This was already explained in details in the tutorial PIC32MX MPLAB Harmony Tutorial. So this step will not be explained here. The only thing you will need is the following configuration setup.

Once you have clicked on Finish, project will be created within the specified folder above and you will find empty folder directory in MPLAB X IDE. Also MPLAB Harmony Configurator(MHC) will launch.

Application Configuration and Coding using MHC

Configure Clock and features of the Microcontroller

The next step is to use MHC to configure first the Microcontroller clock. This clock configuration is done via the Clock Diagram tab in MHC and is same as in the tutorial PIC32MX MPLAB Harmony Tutorial. Also follow the same tutorial to configure features of the Microcontroller such as watchdog timer, code protection, brownout, disable JTAG etc.

After the configurations, generate and save the codes.

UART Driver with interrupt configuration from MHC

The next step is to enable UART module from MHC. Go to the Options tab in MHC, then expand the Harmony Framework Configuration tree, then the Drivers tree then the UART tree. Then configure the UART driver as follows.

The click on Generate Code button.

Port and Pin Configuration

In this tutorial only one LED is required. This will be used to indicate that the USART has received the character that will send via PC terminal later on. We use the Pin Setting tab to configure the Port B pin 0(RB0) as output and as digital pin. See below picture.

Also we need to configure two other pins for USART 2 TX and RX of the PIC32MX470F512L microcontroller. In this tutorial we have selected the Port B pin 1(physical pin 24) as the TX and the Port B pin 5(physical pin 20) as the RX.

To configure these two pins we have to do two things. First in Pin Setting tab, we have to make both as digital and make Port B pin 1 as output.

Secondly, we have to assign the pins as USART 2 TX and RX from the Pin Diagram tab. To do this click on the RB1 and select U2TX.

Configure UART pin of PIC32 microcontroller

Similarly configure the RB5 as U2RX as shown below.

Configure UART pin of PIC32 microcontroller

After the configurations, generate and save the codes.

Application Coding

Create States and States variables

Create states and states variables for the application in the app.h file.

The states are:


which should be put in the APP_STATES enum structure as follows,

And the states variables are:

stringPointer, char data and bool InterruptFlag

which should be put inside the APP_DATA structure as follows,

See the following picture how to put these.

Finally declare extern APP_DATA appData just after the tyepdef struct  to be able to use the appData variable across the project.

See below for example.


Initialize State and State Variables

The above state and state variables we created needs to be initialized to some initial state and some values. This is done inside the APP_Initialize() function which is inside the app.c file.

We initialize state machine initial state as USART_ENABLE and initialize the state variable InterruptFlag as false. Open app.c file and change the APP_Initialize() function as follows.

The final state variables need for our application are two strings. At the top of the app.c file declare two strings are follows.

See where in app.c file below.

Create Application Finite State Machine

The next step is to work on the application state machine. The state machine in MPLAB Harmony is implemented inside APP_Tasks() function which is in app.c file.

In the APP_Tasks() function write the following.

As you can see we have used two new functions called WriteString() and PutCharacter() within the state machines. The WriteString() function is called in two states: USART_TRANSMIT_FIRST_STRING and USART_TRANSMIT_SECOND_STRING. The PutCharacter() function is called in the USART_RECEIVE_DONE state.

Now we have to write these functions. These functions should be written also in the same app.c file as the APP_Tasks() function. You can write these two functions just above the APP_Tasks() function or where the comment says “Add any necessary local functions.” in the app.c file.

The function WriteString() is as follows.

See below where it is written.

Similarly add the PutCharacter() function just below the WriteCharacter() function.

See below.

Declare Prototype functions

We have used the functions WriteCharacter() and PutCharacter() and so we should declare their prototype in the app.h file. So reopen the app.h and at the file declare the following functions.

See below.

ISR(Interrupt Service Routine) and Interrupt Enable

In the ISR function which is located in system_interrupt.c, echo back the received character if the receive buffer is full and enable the interrupt flag. The code to do this is as follows.

The ISR function will look like the following after adding the above code.

Then in the system_init.c add the two functions below to set multi-vector mode and enable interrupt.

See below.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

pic32MX break out board assembly

PIC32MX microcontroller LED blinking C program

Fabricating Printed Circuit Boards

Fabricating Printed Circuit Boards