Hardware background
The comparator (CMP) module is an analog comparator integrated in MCU.
Features of the CMP module include:
- 8 bit DAC with 2 voltage reference source
- 8 analog inputs from external pins
- Round robin check. In summary, this allow the CMP to operate independently in STOP and VLPS mode, whilst being triggered periodically to sample up to 8 inputs. Only if an input changes state is a full wakeup generated.
- Operational over the entire supply range
- Inputs may range from rail to rail
- Programmable hysteresis control
- Selectable interrupt on rising-edge, falling-edge, or both rising or falling edges of the comparator output
- Selectable inversion on comparator output
- Capability to produce a wide range of outputs such as: sampled, windowed, which is ideal for certain PWM zero-crossing-detection applications and digitally filtered
- A comparison event can be selected to trigger a DMA transfer
- The window and filter functions are not available in STOP modes.
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.
{
{
.dmaTriggerState = false,
.filterSamplePeriod = 0,
.filterSampleCount = 0,
},
{
.positiveInputMux = 0,
.negativeInputMux = 1
},
{
.voltage = 120,
.state = false
},
{
.roundRobinState = false,
.roundRobinInterruptState = false,
.fixedChannel = 0,
.samples = 0,
.initializationDelay = 0,
.roundRobinChannelsState = 255,
.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)
{
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
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.
{
{
.dmaTriggerState = false,
.filterSamplePeriod = 0,
.filterSampleCount = 0,
},
{
.positiveInputMux = 0,
.negativeInputMux = 1
},
{
.voltage = 120,
.state = false,
.fixRefInputMux = false
},
{
.roundRobinState = false,
.roundRobinInterruptState = false,
.fixedChannel = 0,
.samples = 0,
.roundRobinChannelsState = 255,
.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)
{
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
for (;;)
{}
return(0);
}