1.15.2 Capture/Compare/PWM (CCP)

A CCP module can operate in any one of the below three modes.

  • Timer mode - is used for periodic delay.

  • Output Compare/PWM mode - is used to generate waveforms

  • Input Capture mode - is used to measure delay, pulse width and frequency of the input signal.

Timer mode

The CCP channel in timer mode provides following features:

  • Periodic CPU interrupts

  • Master time base function for synchronizing other CCP modules

  • Triggering periodic ADC conversion

  • Periodic wake from Sleep (if an appropriate clock source is available)

Using The Library

When the timer is enabled, it increments by one on every rising edge of the input clock, and generates an interrupt on a period match. Timer mode is used for periodic interrupt generation.

This example demonstrates how to use CCP channel in timer mode to generate periodic callback.

/* This function is called after period expires */
void CCP0_Callback_InterruptHandler(uint32_t status, uintptr_t context)
{
    /* Toggle LED */
    LED_Toggle();
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    /* Register callback function for CCP0 period interrupt */
    CCP0_TimerCallbackRegister(CCP0_Callback_InterruptHandler, (uintptr_t)NULL);

    /* Start the timer channel 0*/
    CCP0_TimerStart();

    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

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

    return ( EXIT_FAILURE );
}

Compare mode

The CCP channel in compare mode is used for waveform generation. This provides below features:

  • Output Compare mode can generate a single output transition or a train of output pulses, and can generate interrupts on match-on-compare events.

  • Output Compare mode can function as a PWM generator.

  • In MCCP modules, multiple PWM outputs can be used for power or motor control applications.

Using The Library

This peripheral supports below output compare modes:

  • Single edge and dual edge compare modes

  • Center-aligned compare mode

MCCP (Multiple CCP) modules support below extended PWM features:

  • Single Output Steerable mode

  • Brush DC Motor (Forward and Reverse) modes

  • Half-Bridge with Dead-Time Delay mode

  • Push-Pull PWM mode

  • Output Scan mode

  • Center-Aligned Compare mode

  • Variable Frequency Pulse mode

  • Auto-Shutdown with Programmable Source and Shutdown State

  • Programmable Output Polarity

This peripheral library allows to set the period and duty cycle of the output waveform. And supports period overflow interrupt and compare match interrupt.

Capture mode

This mode is used to capture a timer value from an independent timer base on the occurrence of an event on an input pin. This mode is useful in applications requiring frequency (time period) and pulse measurement. Input Capture mode uses the CCPxTMR registers as a dedicated 16/32-bit synchronous, up counting timer used for event capture. This value is written to the FIFO buffer when a capture event occurs. The internal value may also be read with a synchronization delay from the CCPxTMR registers.

Using The Library

CCP peripheral when configured in capture mode, captures the time interval of input signal.

It provides both polling and callback methods to indicate capture event has occurred. With polling, the application will need to continuously poll to check if the capture event has occurred. With callback, the registered callback function will be called when the capture event occurs. This means the application do not have to poll continuously.

Callback method This example demonstrates how to use CCP in capture mode to measure pulse width.

/* This function is called after Capture 0 interrupt */
void CCP0_CaptureInterruptHandler(uintptr_t context)
{
    /* Read Captured values */
    pulse_width = CCP0_Capture16bitBufferRead();
}

int main(void)
{

    /* Register callback function for period interrupt */
    CCP0_CaptureCallbackRegister(CCP0_CaptureInterruptHandler, (uintptr_t)NULL);

    /* Start the Capture */
    CCP0_CaptureStart();
}

Library Interface

Peripheral library provides the following interfaces:

Functions

Name Description
CCPx_TimerInitialize Initializes given instance of CCP
CCPx_TimerStart Starts the timer for given CCP instance
CCPx_TimerStop Stops the timer for given CCP instance
CCPx_Timer16bitPeriodSet Sets the period value of a given timer
CCPx_Timer16bitPeriodGet Reads the period value of given timer
CCPx_Timer16bitCounterGet Reads the timer current count value
CCPx_Timer32bitPeriodSet Sets the period value of a given timer
CCPx_Timer32bitPeriodGet Reads the period value of given timer
CCPx_Timer32bitCounterGet Reads the timer current count value
CCPx_TimerFrequencyGet Provides the given timer's counter-increment frequency
CCPx_TimerCallbackRegister Registers the function to be called from interrupt
CCPx_TimerInterruptEnable Enables timer interrupt
CCPx_TimerInterruptDisable Disables timer interrupt
CCPx_CaptureInitialize Initializes given instance of CCP
CCPx_CaptureStart Starts the timer for given CCP instance
CCPx_CaptureStop Stops the timer for given CCP instance
CCPx_Capture16bitBufferRead Reads 16 bit capture value
CCPx_Capture32bitBufferRead Reads 32 bit capture value
CCPx_CaptureStatusGet Reads status capture operation
CCPx_CaptureCallbackRegister Registers the function to be called from interrupt
CCPx_CompareInitialize Initializes given instance of CCP
CCPx_CompareStart Starts the timer for given CCP instance
CCPx_CompareStop Stops the timer for given CCP instance
CCPx_CompareAutoShutdownClear Clears auto shutdown event status
CCPx_CompareAutoShutdownSet Enable auto shutdown
CCPx_Compare16bitPeriodValueSet Sets the period value of a given timer
CCPx_Compare16bitPeriodValueGet Reads the period value of given timer
CCPx_Compare16bitValueGet Reads the timer current count value
CCPx_Compare16bitValueSet Sets new timer counter value
CCPx_Compare16bitRAValueSet Writes compare value of the given compare channel 0
CCPx_Compare16bitRAValueGet Gets lower 16 bits of compare value
CCPx_Compare16bitRBValueSet Writes higher 16bits of compare value
CCPx_Compare16bitRBValueGet Gets higher 16 bits of compare value
CCPx_Compare32bitPeriodValueSet Sets the period value of a given timer
CCPx_Compare32bitPeriodValueGet Reads the period value of given timer
CCPx_Compare32bitValueGet Reads the 32bit comapre value
CCPx_Compare32bitValueSet Sets 32bit compare value
CCPx_CompareCallbackRegister Registers the function to be called from interrupt

Data types and constants

Name Type Description
CCP_TIMER_CALLBACK Typedef Defines the function pointer data type and function signature for the ccp channel callback function
CCP_COMPARE_CALLBACK Typedef Defines the function pointer data type and function signature for the ccp channel callback function
CCP_CAPTURE_CALLBACK Typedef Defines the function pointer data type and function signature for the ccp channel callback function