2.78 Periodic Interval Timer (PIT)

The Periodic Interval Timer (PIT) provides the operating system’s scheduler interrupt. It is designed to offer maximum accuracy and efficient management, even for systems with long response time.

  • 20-bit Programmable Counter plus 12-bit Interval Counter

  • Reset-on-read Feature

  • Both Counters Work on Master Clock/16

Using The Library

PIT peripheral library can be used to poll for a periodic timeout, generate periodic interrupts or generate timed waits. The following examples shows different modes of its usage.

Polling method

int main ( void )
{
    int i;
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    while ( true )
    {
        for (i=0; i<10; i++) {
            while(!PIT_TimerPeriodHasExpired());
            PIT_TimerRestart();
        }
        LED_BLUE_Toggle();
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Interrupt Method

static void pit_callback(uintptr_t context)
{
    (void)context;
    LED_BLUE_Toggle();
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    PIT_TimerCallbackSet(pit_callback, NULL);
    
    PIT_TimerStart();

    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Delay routines

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    PIT_TimerCallbackSet(pit_callback, NULL);
    
    PIT_TimerStart();

    while ( true )
    {
        PIT_DelayMs(1000U);
        LED_BLUE_Toggle();
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Library Interface

Periodic Interval Timer peripheral library provides the following interfaces:

Functions

NameDescription
PIT_TimerInitializeInitialize PIT registers per user configuration
PIT_TimerRestartRestart the PIT counter
PIT_TimerStartStart the PIT counter
PIT_TimerStopStop the PIT counter
PIT_TimerPeriodSetSet the timer period value
PIT_TimerPeriodGetGet the timer period value
PIT_TimerCounterGetGet the timer counter value
PIT_TimerCompareSetSet the timer comparison value
PIT_TimerFrequencyGetGet the timer clock frequency
PIT_TimerPeriodHasExpiredReturn whether or not the Timer Period has expired
PIT_DelayMsDelays processing for x milliseconds
PIT_DelayUsDelays processing for x microseconds
PIT_TimerCallbackSetRegister callback for PIT interrupt
PIT_ClearInterruptPIT Clear Interrupt

Data types and constants

NameTypeDescription
PIT_CALLBACKTypedefPIT Interrupt Callback Function definition
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.