MPLAB® Harmony v2 Timer System Service Example Using PIC32MZ EF Starter Kit
Objective
This example project uses the Timer System Service Library to execute functions after a specified period of time. The MPLAB® Harmony Configurator (MHC) is used to configure the Timer System Service. Three examples of using the Timer System Service are provided:
One periodic delay is created to execute a callback function. This will toggle LED1 every 500 ms.
Another periodic delay is created to execute a different callback function. This will toggle LED2 every 1000 ms.
The third example shows you how to implement delays by reading the Timer System Service values directly. This will toggle LED3 every second.
This project uses the PIC32MZ Embedded Connectivity with FPU (EF) Starter Kit. The three LEDs on the board are connected to PIC32 General Purpose I/O Port pins. The Ports Peripheral Library Pin toggle function is used to change the state of the LEDs.
The "Project and Source Files" download (see below) contains a fully functional project. It can be used as an example and requires no modifications. To gain a deeper understanding of how to use the MPLAB Harmony framework, we recommend you generate the project and source files yourself by following the step-by-step procedure below.
Materials
Hardware Tools
Tool
About
Purchase
PIC32MZ Embedded Connectivity with FPU Starter Kit
This project has been verified to work with the following versions of software tools:
MPLAB X IDE v3.25, MPLAB XC32 Compiler v1.40, MPLAB Harmony v1.07.
Note: Because we regularly update our tools, occasionally you may discover an issue while using newer versions. If you suspect that to be the case, we recommend that you double-check using the same versions that the project was tested with.
Archived versions of our tools can be found on the following Microchip websites: MPLAB Harmony (see "Archived Downloads" tab) MPLAB X IDE and XC32 Compiler (see "Downloads Archive" tab)
Note that multiple versions of all these tools can co-exist on the same computer.
The contents of the following .zip file need to be placed in this directory. If this is not done, the MPLAB X IDE will not be able to find all source files, and the project will not build successfully.
The "Project and Source Files" download provides a fully functional project. It can be used as an example and requires no modifications. The following steps below provide instruction on how this project was created.
1
Create a new MPLAB Harmony project
The Target Device used on the PIC32MZ EC with FPU (EF) Starter Kit is the PIC32MZ2048EFH144.
Before you create a new MPLAB® Harmony project, you must install the MPLAB Harmony Configurator (MHC) graphical user interface. This GUI enables easy addition and configuration of the Harmony library files needed for your application. It also allows MPLAB X IDE to create a new MPLAB Harmony project.
In the MPLAB X IDE, select File > New Project
(or click in the toolbar).
Select the Microchip Embedded category if it is not already selected.
From the Projects list, select MPLAB Harmony Project.
Click Next >.
If you do not see MPLAB Harmony Project option, you have not installed the MHC. Click here for instructions on installing the MHC plugin.
The next window should have the default Harmony Path, Project Location, and Project Path already selected for you. Remember the MPLAB Harmony framework expects to find specific files in specific locations. Do not change these default paths because the project may not build.
After you click Finish, your new project will be opened.
MPLAB® Harmony Project Wizard Details Learn more >
Before creating an MPLAB Harmony Project, the MHC plugin must be installed into the MPLAB X IDE. Click here for instructions on how to install the MHC plugin.
This video will show you how to create an MPLAB Harmony Project in the MPLAB X IDE. For details on how an MPLAB Harmony Project differs from a typical standalone project, click here.
2
Use the MPLAB Harmony Configurator (MHC) to configure the PIC32 core
Click on the Options tab in the MPLAB Harmony Configurator window.
Expand the following sub-menus:
Harmony Framework Configuration
System Services
Timer
Check the Use Timer System Service? box. You will use the default values. No other changes are needed.
The Timer Running Frequency controls the increment rate for the System Timer counter (1000 Hz = 1 System Timer tick every 1 ms).
The Internal Time Units (Hz) controls the decrement rate for the alarm delay period countdown timers (10,000 Hz = 10 decrements per 1 ms).
Click image to enlarge.
Click image to enlarge
Note how the MHC configuration tree is highlighted to show a change from MHC's default value. The Timer System Service Library is a client for the Timer Driver Library. Checking the Use Timer System Service? box will also automatically check the Use Timer Driver? box (found in: Harmony Framework Configuration > Drivers > Timer) for you. This is MHC's default setting when the Timer System Service is enabled, so this change is not highlighted.
Three PIC32 I/O Port pins will be used to blink LEDs on the board. The LED on and off time will correspond to your requested Timer System Service periods (you will create three of them).
Click on the Pin Settings tab in the MPLAB Harmony Configurator window.
The pins are listed by ascending pin number by default. To list the pins by their I/O Port names, change the pin listing order from Pins to Ports.
Scroll down the list to find the pins listed in the table below. These I/O Port pins are connected to the LEDs.
PIC32 Pin
Pin Description
Development Board
Function
RH0
PORTH, Bit 0
LED1
RH1
PORTH, Bit 1
LED2
RH2
PORTH, Bit 2
LED3
Note all I/O Port pins are configured as inputs by default. To use an I/O Port pin as an output, it must be configured as such. Change the pin direction from In to Out by clicking on the button.
Also, note all pins with an analog capability are configured as analog by default. To use an I/O Port pin as a digital pin, it must be configured as such. Change the pin mode from Analog to Digital by clicking on the button.
Open the system_config.h file, and add the following application-specific configuration constants:
file: system_config.h
You could have defined these in the application header file (app.h), but defining them in the system configuration header file (system_config.h) allows these macros to change if you plan on using different hardware platforms. The MPLAB X IDE allows one project to support multiple hardware platforms through the ability to include or exclude source files from the project based on the project configuration. The MPLAB Harmony Framework uses this ability to include all source files in one system_config folder while excluding all others (if others exist).
Connect your PIC32MZ EC with FPU (EF) Starter Kit to your computer with a mini-B USB cable. This single cable is used to power the board and to connect to the on-board programmer/debugger. See "Show Hardware Setup" in the "Materials" section above to see a picture of the connection.
The mini-B USB connector found under the RJ-45 connector is used for USB-to-UART serial communication, not for programming and debugging.
b
Open the Project Properties window and select the PIC32MZ EF Family Starter Kit and XC32 (v1.40) compiler version as shown. If a newer compiler version is available, try using that version first.
After successfully building your project and programming your development board, you will see three LEDs blinking.
LED1 turns on for 500 ms, then off for 500 ms. This corresponds to the first delay period created by our application using the following line of code in app.c:
LED2 turns on for 1 second, then off for 1 second. This corresponds to the second delay period created by our application using the following line of code in app.c:
LED3 blinks at the same rate as LED2. This System Tick counter is read every APP_Task loop to determine if one second has passed. If it has, LED3 is toggled.
Conclusions
You have successfully used two implementations of the Timer System Service. Both create periodic delays. There are other Timer System Service implementations that allow one-shot (single) alarm delay periods.