Objective
This example project uses the Timer Driver Library (Dynamic implementation) to execute timer interrupts. The period of these interrupts and the number of interrupts to count for a given alarm period are calculated for you. Every 500 ms, a callback function will be executed to toggle LED 3.
You will use the MPLAB® Harmony Configurator (MHC) to create a Timer Driver instance using the PIC32 Timer1 peripheral (16-bit) with interrupts enabled. You will then add the following code to the project:
- Create the timer alarm initialization function.
- Calculates the timer period used to generate interrupts.
- Calculates the number of timer interrupts needed for a specific alarm period.
- Open a Timer Driver, register a callback function with it, then start the Timer Driver.
- Create the callback function to change the state of the I/O Port pin connected to LED 3 at the end of an alarm period.
The hardware for this project uses the chipKIT® WF32 board from Digilent® Inc.
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 |
---|---|---|
| | |
chipKIT® PGM
Programmer/Debugger |
| |
Software Tools
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.
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC32
C/C++ Compiler |
| | | | |
MPLAB® Harmony 1.xx
Integrated Software Framework |
| | | | |
Exercise Files
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.
<Harmony install path>/apps/training
(example Harmony install path = c:/microchip/harmony/v1_07)
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
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 chipKIT WF32 is the PIC32MX695F512L.
2
Use MHC to configure the PIC32 clocks
3
Use MHC to configure the Timer Driver
4
Use MHC to configure the I/O Port Pins
5
Use MHC to generate the code
6
Add application variables to your project in app.h
7
Add application-specific configuration constants to the project in system_config.h.
8
Create the callback function
9
Initialize the Timer1 period and number of interrupts to count to implement the timer alarm period
10
Initialize other application variables
11
Open the Timer Driver, register a timer alarm, and start the timer.
12
Connect the development board to your computer, then select the XC32 compiler version and programmer/debugger for the project
13
Build, program and run your project
Results
After successfully building your project and programming your development board, you will see LED 3 blink (on/off) once a second (on 500 ms, off 500 ms). Change the APP_ALARM_PERIOD macro you created in system_config.h (near line #144) to modify the LED blink rate.
Conclusions
You have successfully used the MPLAB Harmony Timer Driver Library to create a periodic delay. You can experiment by changing this to a single (one-shot) delay, or by creating another Timer Driver to blink a different LED.
Table of Contents
|