2.6 Capture Mode Configuration

Capture Mode Initialization and Interrupt Service Routines shows the CCP configured in Capture mode. The example contains the CCP Initialization and Capture Interrupt Service Routines, as well as the Timer1 Initialization routine. The software demonstrates how the CCP module can be configured to measure a continuous series of pulses using Timer1 as the time base. The measurement occurs on every edge of the incoming signal. Timer1 is configured to use the ‘FOSC/4’ clock source.

Capture Mode Initialization and Interrupt Service Routines

static void (*CCP1_CallBack)(uint16_t);
static void CCP1_DefaultCallBack(uint16_t capturedValue)
{
    // Add your code here
}

void CCP1_Initialize(void)
{
    CCP1CON = 0x83;                     // MODE Every edge; FMT right;
    CCP1CAP = 0x00;                     // CCP1CTS CCP1 pin;
    CCPR1H = 0x00;
    CCPR1L = 0x00;
    CCP1_SetCallBack(CCP1_DefaultCallBack);  // Set call back function
    PIR1bits.CCP1IF = 0;                // Clear CCP1 interrupt flag
    PIE1bits.CCP1IE = 1;                // Enable the CCP1 interrupt
}

void CCP1_CaptureISR(void)
{
    CCP1_PERIOD_REG_T module;
    PIR1bits.CCP1IF = 0;                // Clear interrupt flag
    module.ccpr1l = CCPR1L;             // Copy captured value
    module.ccpr1h = CCPR1H;
    CCP1_CallBack(module.ccpr1_16Bit);  // Return 16bit captured value
}

void CCP1_SetCallBack(void (*customCallBack)(uint16_t))
{
    CCP1_CallBack = customCallBack;
}

void TMR1_Initialize(void)
{
    T1GCON = 0x00;                    // T1GE disabled;
    T1GATE = 0x00;
    T1CLK = 0x01;                     // CS FOSC/4;
    TMR1H = 0x00;
    TMR1L = 0x00;
    PIR1bits.TMR1IF = 0;              // Clear IF flag;
    T1CON = 0x33;                     // CKPS 1:8; ON enabled; RD16 enabled;
}