Jump to main content

##

# 3.20.12 Output Compare (OCMP)

The Output Compare module is primarily used to generate a single pulse or a series of pulses in response to selected time base events. Each output compare module 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.

Each Output Compare module has the following modes of operation (OCM bit field): Single Compare Match Mode:

- With output drive high - Initial state of the pin is low. OCx pin becomes high on compare match.

- With output drive low - Initial state of the pin is high. OCx pin becomes low on compare match.

- With output drive toggles - OCx pin toggles on compare match.

Dual Compare Match Mode:

- With single output pulse - OCx pin becomes high on compare match and becomes low on secondary compare match

- With continuous output pulses - OCx pin becomes high on compare match and becomes low on secondary compare match continuously. Compare time base will count up to period value defined in the selected timer source module.

Pulse Width Modulation Mode:

- PWM without fault protection input - PWM duty cycle is defined by secondary compare register, OCxRS and PWM period is defined by selected timer period register (PRy)

- PWM with fault protection input - PWM duty cycle is defined by secondary compare register, OCxRS and PWM period is defined by selected timer period register (PRy). PWM outputs are shut asynchronously when fault condition is detected.

## Using The Library

This peripheral library is used to generate a single pulse or continuous pulses. The OCMP module needs the TMR module to provide the time base. The period value is defined in the TMR module and the compare value is defined in the OCMP module.

In this application, three OCMP modules are used to generate waveforms.

Active Low Output: By default output is set as high and it is set as low on the compare match

Active High Output: By default output is set as low and it is set as high on the compare match

Toggled Output: Compare match toggles the output.

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

        /* Active low output */
        OCMP1_Enable();
        /* Active high output */
        OCMP2_Enable();
        /* toggled output */
        OCMP3_Enable();

        TMR2_Start();

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

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

        return ( EXIT_FAILURE );
    }

## Library Interface

Peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
OCMPx_Initialize | Initializes OCMPx module of the device
OCMPx_Enable | Enable function for the OCMPx peripheral
OCMPx_Disable | Disable function for the OCMPx peripheral
OCMPx_FaultStatusGet | Reads the fault status
OCMPx_CompareValueSet | Configures the compare register
OCMPx_CompareValueGet | Reads the compare value
OCMPx_CompareSecondaryValueGet | Reads secondary compare register
OCMPx_CompareSecondaryValueSet | Sets OCMPx Secondary Compare Register
OCMPx_CallbackRegister | Registers the function to be called from interrupt

Data types and constants

Name | Type | Description
--- | --- | ---
OCMP_CALLBACK | Typedef | Defines the function pointer data type and function signature for the OCMP callback function