Using Harmony Static Drivers to Control Timers


This page illustrates the steps needed to configure a timer and associated interrupt vector using Static Drive function calls. When a timer is configured to use a static driver a unique interrupt vector is created for the timer.
This example uses a Timer1 generated interrupt.

Steps demonstrated on this page include:

  1. Using MPLAB® Harmony Configurator (MHC) to create the project structure with a Static Driver for Timer1
  2. Verifying the function DRV_TMR0_Initialize() establishes Timer1's operating parameters
  3. Implementing the interrupt service routine (ISR) in the file system_interrupt.c
  4. Modifying the function APP_Init() to enable the MCU to accept Timer1 interrupts
  5. Modifying the main application program, APP_Tasks() to start Timer1

Using MHC to Create a Project

Under the "Driver" section check the "User Timer Driver?" box. Ensure a STATIC timer driver is used. When STATIC is selected you will have the option of configuring the timer settings as well as the interrupt priority levels.


Under the System Service section of MHC, you should observe the "Clock" and "Ports" system services have been selected by default. Leave the "Timer" system services UNCHECKed.

Below is the project directory and file structure generated by MHC.
The illustration shows the source file (or file directory) of the functions needed
to be modified in order to use a Static Timer Driver.


Configuring the Timer

  • The code for the Timer's configuration parameters and Interrupt levels are generated by MHC, and placed in the function DRV_TMR_Initialize.
  • For a system using a STATIC driver, the driver initialization function is placed in the file drv-tmr-static.c.
  • The drv_tmr_static.c file is placed within the system configuration directory structure as shown in the picture below.


void DRV_TMR0_Initialize(void)
/* Initialize Timer Instance0 */
/* Disable Timer */

/* Select clock source */

/* Select prescalar value */
/* Enable 16 bit mode */
/* Clear counter */
h/*Set period */
PLIB_TMR_Period16BitSet(TMR_ID_1, 0);

/* Setup Interrupt */
PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_T1,
PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_T1,


  • You should examine DRV_TMR_Initialize() to ensure the Timer is configured to your specification.
  • During the applications start-up phase DRV_TMR_Initialize() is called by the system initialization function SYS_Initialize().
  • Static Driver functions are implemented using Peripheral Libraries.

Adding Code to the Interrupt Service Routine

  • The MHC will generate separate interrupt vectors for each driver instance defined.
  • The drivers will be placed in system_interrupt.c
  • Each interrupt vector will contain code which clears the interrupt flag (PLIB_INt_Sourceflagclear(..))
  • You are responsible for adding code to execute the task required of the ISR. In this example, you are asked to enter your own ISR functionality.


void __ISR(_TIMER_1_VECTOR, ipl5) _IntHandlerDrvTmrInstance0(void)
// User-Written ISR code goes here!

Enabling Timer Interrupts

  • The function to initialize the application APP_Initialize() is located in app.c.
  • In this example, a line of code was added to enable interrupts from Timer1.


void APP_Initialize (void)
appData.state = APP_STATE_INIT;


Starting the Timers

  • The main application function APP_Tasks() is located in app.c.
  • In this example, during the initial state of the application ( APP_STATE_INIT), the timer is started.


void APP_Tasks (void)
switch ( appData.state )
DRV_TMR0_Start(); // Start the Timers

 Learn More

Example Code and Projects
Learn more >
Harmony Timer System Services Tutorial
Learn more >
Harmony Timer Driver Tutorial
Learn more >

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.