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:
- Using MPLAB® Harmony Configurator (MHC) to create the project structure with a Static Driver for Timer1
- Verifying the function DRV_TMR0_Initialize() establishes Timer1's operating parameters
- Implementing the interrupt service routine (ISR) in the file system_interrupt.c
- Modifying the function APP_Init() to enable the MCU to accept Timer1 interrupts
- 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.
/* Initialize Timer Instance0 */
/* Disable Timer */
/* Select clock source */
/* Select prescalar value */
/* Enable 16 bit mode */
/* Clear counter */
h/*Set period */
/* Setup Interrupt */
- 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