Comparator (CMP)

Detailed Description

Hardware background

The comparator (CMP) module is an analog comparator integrated in MCU.

Features of the CMP module include:

How to use the CMP driver in your application

The user can configure the CMP in many ways: -CMP_DRV_Init - configures all CMP features -CMP_DRV_ConfigDAC - configures only DAC features -CMP_DRV_ConfigTriggerMode - configures only trigger mode features -CMP_DRV_ConfigComparator - configures only analog comparator features -CMP_DRV_ConfigMUX - configures only MUX features

Also the current configuration can be read using: -CMP_DRV_GetConfigAll - gets all CMP configuration -CMP_DRV_GetDACConfig - gets only DAC configuration -CMP_DRV_GetMUXConfig - gets only MUX configuration -CMP_DRV_GetInitTriggerMode - gets only trigger mode configuration -CMP_DRV_GetComparatorConfig - gets only analog comparator features

When the MCU exits from STOP mode CMP_DRV_GetInputFlags can be used to get the channel which triggered the wakeup. Please use this function only in this use case. CMP_DRV_ClearInputFlags will be used to clear this input change flags.

CMP_DRV_GetOutputFlags can be used to get output flag state and CMP_DRV_GetOutputFlags to clear them.

The main structure used to configure your application is cmp_module_t. This structure includes configuration structures for trigger mode, MUX, DAC and comparator: cmp_comparator_t, cmp_anmux_t, cmp_dac_t and cmp_trigger_mode_t

Example for S32K14x:

The next example will compare 2 external signals (CMP input 0 an CMP input 1). The output can be measured on port E, pin 4.

const cmp_module_t cmp_general_config =
{
{
.dmaTriggerState = false,
.outputInterruptTrigger = CMP_NO_EVENT,
.mode = CMP_CONTINUOUS,
.filterSamplePeriod = 0,
.filterSampleCount = 0,
.powerMode = CMP_LOW_SPEED,
.inverterState = CMP_NORMAL,
.outputSelect = CMP_COUT,
.pinState = CMP_AVAILABLE,
.offsetLevel = CMP_LEVEL_OFFSET_0,
.hysteresisLevel = CMP_LEVEL_HYS_0
},
{
.positivePortMux = CMP_MUX,
.negativePortMux = CMP_MUX,
.positiveInputMux = 0,
.negativeInputMux = 1
},
{
.voltageReferenceSource = CMP_VIN1,
.voltage = 120,
.state = false
},
{
.roundRobinState = false,
.roundRobinInterruptState = false,
.fixedPort = CMP_PLUS_FIXED,
.fixedChannel = 0,
.samples = 0,
.initializationDelay = 0,
/* Channel 0 is enabled for round robin check */
/* Channel 1 is enabled for round robin check */
/* Channel 2 is enabled for round robin check */
/* Channel 3 is enabled for round robin check */
/* Channel 4 is enabled for round robin check */
/* Channel 5 is enabled for round robin check */
/* Channel 6 is enabled for round robin check */
/* Channel 7 is enabled for round robin check */
.roundRobinChannelsState = 255,
/* Initial comparison result for channel 0 is 1 */
/* Initial comparison result for channel 1 is 1 */
/* Initial comparison result for channel 2 is 1 */
/* Initial comparison result for channel 3 is 1 */
/* Initial comparison result for channel 4 is 1 */
/* Initial comparison result for channel 5 is 1 */
/* Initial comparison result for channel 6 is 1 */
/* Initial comparison result for channel 7 is 1 */
.programedState = 255
}
};
#define COMPARATOR_PORT PORTA
#define COMPARATOR_INPUT1_PIN 0UL
#define COMPARATOR_INPUT2_PIN 1UL
#define COMPARATOR_OUTPUT 4UL
#define COMPARATOR_INSTANCE 0UL
int main(void)
{
/* Initialize and configure clocks
* - Setup system clocks
* - Enable clock feed for Ports and Comparator
* - See Clock Manager component for more info
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
/* Set pins used by CMP */
/* The negative port is connected to PTA0 and positive port is connected to PTA1. The
comparator output can be visualized on PTA4 */
/* Initialize pins
* - Setup input pins for Comparator
* - Setup output pins for LEDs
* - See PinSettings component for more info
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
/* Init CMP module */
CMP_DRV_Init(COMPARATOR_INSTANCE, &cmp_general_config);
for (;;)
{}
return(0);
}

Example for MPC574XG:

The next example will compare 2 external signals (CMP input 0 an CMP input 1). The output can be measured on port E, pin 4.

const cmp_module_t cmp_general_config =
{
{
.dmaTriggerState = false,
.outputInterruptTrigger = CMP_NO_EVENT,
.mode = CMP_CONTINUOUS,
.filterSamplePeriod = 0,
.filterSampleCount = 0,
.powerMode = CMP_LOW_SPEED,
.inverterState = CMP_NORMAL,
.outputSelect = CMP_COUT,
.pinState = CMP_AVAILABLE,
.hysteresisLevel = CMP_LEVEL_HYS_0
},
{
.positivePortMux = CMP_MUX,
.negativePortMux = CMP_MUX,
.positiveInputMux = 0,
.negativeInputMux = 1
},
{
.voltageReferenceSource = CMP_VIN1,
.voltage = 120,
.state = false,
.fixRefInputMux = false
},
{
.roundRobinState = false,
.roundRobinInterruptState = false,
.fixedPort = CMP_PLUS_FIXED,
.fixedChannel = 0,
.samples = 0,
/* Channel 0 is enabled for round robin check */
/* Channel 1 is enabled for round robin check */
/* Channel 2 is enabled for round robin check */
/* Channel 3 is enabled for round robin check */
/* Channel 4 is enabled for round robin check */
/* Channel 5 is enabled for round robin check */
/* Channel 6 is enabled for round robin check */
/* Channel 7 is enabled for round robin check */
.roundRobinChannelsState = 255,
/* Initial comparison result for channel 0 is 1 */
/* Initial comparison result for channel 1 is 1 */
/* Initial comparison result for channel 2 is 1 */
/* Initial comparison result for channel 3 is 1 */
/* Initial comparison result for channel 4 is 1 */
/* Initial comparison result for channel 5 is 1 */
/* Initial comparison result for channel 6 is 1 */
/* Initial comparison result for channel 7 is 1 */
.programedState = 255
}
};
#define COMPARATOR_PORT PORTA
#define COMPARATOR_INPUT1_PIN 0UL
#define COMPARATOR_INPUT2_PIN 1UL
#define COMPARATOR_OUTPUT 4UL
#define COMPARATOR_INSTANCE 0UL
int main(void)
{
/* Write your local variable definition here */
/* Initialize and configure clocks
* - Setup system clocks
* - Enable clock feed for Ports and Comparator
* - See Clock Manager component for more info
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
/* Set pins used by CMP */
/* The negative port is connected to PTA0 and positive port is connected to PTA1. The
comparator output can be visualized on PTA4 */
/* Initialize pins
* - Setup input pins for Comparator
* - Setup output pins for LEDs
* - See PinSettings component for more info
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
/* Init CMP module */
CMP_DRV_Init(COMPARATOR_INSTANCE, &cmp_general_config);
for (;;)
{}
return(0);
}

Modules

 Comparator Driver
 Comparator Peripheral Driver.