2.11 Input Capture and Compare Timer

The Input Capture and Compare Timers block contains a 32-bit timer running at the main system clock frequency. The timer is free-running and is associated with six 32-bit capture registers and two compare registers. Each capture register can record the value of the free-running timer based on a programmable edge of its associated input pin. An interrupt can be generated for each capture register each time it acquires a new timer value. The timer can also generate an interrupt when it automatically resets and can additionally generate two more interrupts when the timer matches the value in either of two 32-bit compare registers.

Using the Library

The below example demonstrates how to use the Input Capture and Compare Timer peripheral in capture mode. A PWM signal is generated and fed as input to the input capture module. The input capture module is configured to generate an interrupt on falling edge of the input signal. The demo calculates and displays the frequency of the input signal on to the console.
volatile uint32_t capTimerCnt = 0;
volatile uint32_t diffCnt = 0;
volatile bool capInterrupt = false;
uint32_t freq = 0;

void cct_capture_callback(uintptr_t context)
{
    uint32_t timerCnt = CCT_FreeRunningTimerGet();
    if (timerCnt > capTimerCnt)
    {
        diffCnt = timerCnt - capTimerCnt;
    }
    else
    {
        diffCnt = (0xFFFFFFFF - capTimerCnt) + timerCnt;
    }
    capTimerCnt = timerCnt;
    capInterrupt = true;
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    printf("\n\r---------------------------------------------------------");
    printf("\n\r                    CCT Capture Demo                     ");
    printf("\n\r---------------------------------------------------------\n\r");
    
    CCT_Capture0CallbackRegister(cct_capture_callback, 0);
    
    CCT_FreeRunningTimerStart();
    
    PWM0_Start();

    while ( true )
    {
        if (capInterrupt == true)
        {
            freq = CCT_FrequencyGet()/diffCnt;
            printf("Frequency : %ld Hz\r\n", freq);
            capInterrupt = false;
        }
    }

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

    return ( EXIT_FAILURE );
}

Library Interface

Input Capture and Compare Timer peripheral library provides the following interfaces:

Functions

NameDescription
CCT_InitializeInitializes given instance of CCT peripheral.
CCT_CaptureChannelxGet Returns the free running timer counter value at the time of the channel's capture edge event occurred
CCT_CapturexCallbackRegisterAllows application to register a callback with for the CCT capture channel
CCT_CompareChannelxDisableDisables compare channel
CCT_CompareChannelxEnableEnables compare channel
CCT_CompareChannelxInterruptDisableDisables compare channel interrupt
CCT_CompareChannelxInterruptEnableEnables compare channel interrupt
CCT_CompareChannelxOutputClearSets the compare channel output low state
CCT_CompareChannelxOutputSetSets the compare channel output high state
CCT_CompareChannelxPeriodGetReturns the compare period value
CCT_CompareChannelxPeriodSetSets the compare period value
CCT_ComparexCallbackRegisterAllows application to register a callback with for the CCT compare channel
CCT_CounterOverflowCallbackRegisterAllows application to register a callback with the PLIB
CCT_FreeRunningTimerGetReturns the current value of the free running timer counter
CCT_FreeRunningTimerResetStops the free running timer and resets the internal counter to 0
CCT_FreeRunningTimerSetSets the value of the free running timer counter
CCT_FreeRunningTimerStartStarts the free running timer
CCT_FreeRunningTimerStopStops the free running timer
CCT_FreqDivSetSets the divisor value for the free running timer source clock
CCT_FrequencyGetReturns the frequency of the clock source of free running timer
CCT_TimerActivateActivates the CCT timer block
CCT_TimerDeActivateDe-activates the CCT timer block

Data types and constants

NameTypeDescription
CCT_CALLBACKTypedefDefines the data type and function signature for the CCT peripheral callback function.
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.