1.22.15 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

Peripheral library provides the following interfaces:

Functions

Name Description
ICAPx_Initialize Initializes ICAPx module of the device
ICAPx_Enable Enable function for the ICAPx peripheral
ICAPx_Disable Disable function for the ICAPx peripheral
ICAPx_CaptureBufferRead Read buffer function ICAPx peripheral
ICAPx_CaptureStatusGet Reads current state buffer not empty status
ICAPx_ErrorStatusGet Reads current state overflow status ICAPx status
ICAPx_CallbackRegister Sets the callback function for a ICAPx interrupt
ICAPx_Error_CallbackRegister Sets the callback function for a ICAP error interrupt

Data types and constants

Name Type Description
ICAP_STATUS_SOURCE Enum Identifies ICAP status source mask
ICAP_CALLBACK Typedef Defines the function pointer data type and function signature for the ICAPx callback function