1.1.1 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

Functions
Name Description
CCT_Initialize Initializes given instance of CCT peripheral.
CCT_CaptureChannelxGet Returns the free running timer counter value at the time of the channel's capture edge event occured
CCT_CapturexCallbackRegister Allows application to register a callback with for the CCT capture channel
CCT_CompareChannelxDisable Disables compare channel
CCT_CompareChannelxEnable Enables compare channel
CCT_CompareChannelxInterruptDisable Disables compare channel interrupt
CCT_CompareChannelxInterruptEnable Enables compare channel interrupt
CCT_CompareChannelxOutputClear Sets the compare channel output low state
CCT_CompareChannelxOutputSet Sets the compare channel output high state
CCT_CompareChannelxPeriodGet Returns the compare period value
CCT_CompareChannelxPeriodSet Sets the compare period value
CCT_ComparexCallbackRegister Allows application to register a callback with for the CCT compare channel
CCT_CounterOverflowCallbackRegister Allows application to register a callback with the PLIB
CCT_FreeRunningTimerGet Returns the current value of the free running timer counter
CCT_FreeRunningTimerReset Stops the free running timer and resets the internal counter to 0
CCT_FreeRunningTimerSet Sets the value of the free running timer counter
CCT_FreeRunningTimerStart Starts the free running timer
CCT_FreeRunningTimerStop Stops the free running timer
CCT_FreqDivSet Sets the divisor value for the free running timer source clock
CCT_FrequencyGet Returns the frequency of the clock source of free running timer
CCT_TimerActivate Activates the CCT timer block
CCT_TimerDeActivate De-activates the CCT timer block
Data types and constants
Name Type Description
CCT_CALLBACK Typedef Defines the data type and function signature for the CCT peripheral callback function.