Simplest MPLAB Harmony Project with PIC32 microcontroller

In this PIC32 microcontroller tutorial you will learn the simplest MPLAB Harmony project that turns LED on and off when a switch button is pressed

In this PIC32 microcontroller tutorial we will create the simplest MPLAB Harmony project. It just turns on and off a LED by pressing a push button. If the push button is pressed, the LED turns on and if not the LED is just off.

To accomplish this we will use only the peripheral library  APIs or functions. These are the lowest level API in the Microchip Harmony Framework.

The peripheral library functions that will be used are as follows:

PLIB_PORTS_PinGet (PORTS_ID_0, PORT_CHANNEL_X, PORTS_BIT_POS_x)

PLIB_PORTS_PinSet (PORTS_ID_0, PORT_CHANNEL_X, PORTS_BIT_POS_x)

and,

PLIB_PORTS_PinClear (PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_3);

The first function gets the state of the pin x of Port B. Note that here, PORT_CHANNEL_X is the Port and X is either A, B, C, D, E, F and PORTS_BIT_POS_x where small letter x means pin number which can 1, 2,3… etc

So for example if we have a switch connected to port D pin 2, that is in RD2 then we can read the state of the switch using the following function API.

PLIB_PORTS_PinGet (PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_2)

Similarly, to turn a LED connected to port D pin 3(ie RD3) we use the API function,

PLIB_PORTS_PinSet (PORTS_ID_0, PORT_CHANNEL_X, PORTS_BIT_POS_x)

and to turn off the same LED we would use,

PLIB_PORTS_PinClear (PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_3);

This is basic of programing that will be used in this tutorial.

Now start a new MPLAB Harmony project in MPLABX. See PIC32MX MPLAB Harmony Tutorial for how to start a new project. Then you should configure the clock of your microcontroller. This is also explained in the same tutorial. You should generate the code after you have set the clock.

And that’s it.

We don’t need anything more to accomplish our goal. The various features of the microcontroller such as watch dog timer, code protection, JTAG etc which should be turned off are turned off in the default setting of the MHC. After you generated the code, after the clock is set, all these settings and configuration code are put in the appropriate files.

Now we just need to take care of the state machine of the application. As you might know, when the program runs, the code inside main.c executes, then code inside system_init.c, then codes inside system_task.c gets executed. In the system_tasks.c the SYS_Tasks() function is executed and so APP_Tasks() function is called. This APP_Tasks() is in the app.c file. The APP_Tasks() function implements the state machine of the application.

So what we need to do is to write our application in this APP_Tasks() function or equivalently implement our application as the state machine. The following is the APP_Tasks() function inside app.c file.

Next we just need to insert our application logic into the APP_STATE_SERVICE_TASKS case as follows,

And that’s it.

Now Compile and Upload the program into your PIC32 microcontroller. Then build your circuits with switch and LED connection to PIC32 microcontroller as follows.

Simplest MPLAB Harmony Project with PIC32 microcontroller

When you press the push button the LED will turn ON and will be OFF if not pressed.

So this illustrates how you can make the simplest kind of application in Microchip MPLAB Harmony.

See another MPLAB tutorial in which LED blinking is illustrated- PIC32MX microcontroller LED blinking C program.

Leave a Reply

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