2.55 Input Capture (ICAP)

The Input Capture module is useful in applications requiring frequency (period) and pulse measurement. The Input Capture module captures the 16-bit or 32-bit value of the selected Time Base registers when an event occurs at the ICx pin. Capture events are caused by the following:

  • Capture timer value on every edge (rising and falling), specified edge first

  • Prescaler capture event modes:

    • Capture timer value on every 4th rising edge of input at ICx pin

    • Capture timer value on every 16th rising edge of input at ICx pin Each input capture channel can select between one of six 16-bit timers for the time base, or two of six 16-bit timers together to form a 32-bit timer. The selected timer can use either an internal or external clock.

Using The Library

In this application, a pulse signal is generated using the OCMP peripheral and is fed to the ICAP input. ICAP peripheral captures the time at every edge and displays the pulse width on the serial terminal.

uint16_t capturedValue[2];
volatile uint8_t captureIndex = 0;

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    ICAP1_Enable();
    
    OCMP3_Enable();
    
    TMR2_Start();
    
    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
        
        while(!ICAP1_CaptureStatusGet());

        capturedValue[captureIndex++] = ICAP1_CaptureBufferRead();

        if ( captureIndex > 1){
            printf("Pulse Width Count = %d\r\n",(capturedValue[1] - capturedValue[0]));
            captureIndex = 0;
        }
    }

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

    return ( EXIT_FAILURE );
}

Library Interface

Input Capture peripheral library provides the following interfaces:

Functions

NameDescription
ICAPx_InitializeInitializes ICAPx module of the device
ICAPx_EnableEnable function for the ICAPx peripheral
ICAPx_DisableDisable function for the ICAPx peripheral
ICAPx_CaptureBufferReadRead buffer function ICAPx peripheral
ICAPx_CaptureStatusGetReads current state buffer not empty status
ICAPx_ErrorStatusGetReads current state overflow status ICAPx status
ICAPx_CallbackRegisterSets the callback function for a ICAPx interrupt
ICAPx_Error_CallbackRegisterSets the callback function for a ICAP error interrupt

Data types and constants

NameTypeDescription
ICAP_STATUS_SOURCEEnumIdentifies ICAP status source mask
ICAP_CALLBACKTypedefDefines the function pointer data type and function signature for the ICAPx callback function
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.