1.28.14 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

Name Description
PIT_TimerInitialize Initialize PIT registers per user configuration
PIT_TimerRestart Restart the PIT counter
PIT_TimerStart Start the PIT counter
PIT_TimerStop Stop the PIT counter
PIT_TimerPeriodSet Set the timer period value
PIT_TimerPeriodGet Get the timer period value
PIT_TimerCounterGet Get the timer counter value
PIT_TimerCompareSet Set the timer comparison value
PIT_TimerFrequencyGet Get the timer clock frequency
PIT_TimerPeriodHasExpired Return whether or not the Timer Period has expired
PIT_DelayMs Delays processing for x milliseconds
PIT_DelayUs Delays processing for x microseconds
PIT_TimerCallbackSet Register callback for PIT interrupt
PIT_ClearInterrupt PIT Clear Interrupt

Data types and constants

Name Type Description
PIT_CALLBACK Typedef PIT Interrupt Callback Function definition