High level interface for interacting with an analog Comparator.
The analog comparator measures one input voltage from the non-inverting pin against a second voltage provided on the inverting pin. The result of this comparison can be used in three ways:
These three abilities can be used in any combination.
Call cyhal_comp_init to initialize a comparator instance by providing the comparator object (obj), non-inverting input pin (vin_p), inverting input pin (vin_m), optional output pin (output), and configuration (cfg).
Use cyhal_comp_read to read the comparator state from firmware.
Use cyhal_comp_register_callback and cyhal_comp_enable_event to configure a callback that will be invoked on a rising and/or falling edge of the comparator output.
The following snippet initializes the comparator and powers it on
The following snippet reads the current comparator value into a variable
The following snippet registers a callback that will be called on either a rising or falling edge of the comparator output.
The following snippet demonstrates temporarily powering-off the comparator without freeing it.
API Reference | |
Comparator HAL Results | |
Comparator specific return codes. | |
Data Structures | |
struct | cyhal_comp_config_t |
Configuration options for the Comparator. More... | |
Typedefs | |
typedef void(* | cyhal_comp_event_callback_t) (void *callback_arg, cyhal_comp_event_t event) |
Handler for Comparator events. More... | |
Enumerations | |
enum | cyhal_comp_event_t { CYHAL_COMP_RISING_EDGE = 0x01 , CYHAL_COMP_FALLING_EDGE = 0x02 } |
Comparator event types. More... | |
Functions | |
cy_rslt_t | cyhal_comp_init (cyhal_comp_t *obj, cyhal_gpio_t vin_p, cyhal_gpio_t vin_m, cyhal_gpio_t output, cyhal_comp_config_t *cfg) |
Initialize the Comparator peripheral. More... | |
cy_rslt_t | cyhal_comp_init_cfg (cyhal_comp_t *obj, const cyhal_comp_configurator_t *cfg) |
Initialize the comparator peripheral using a configurator generated configuration struct. More... | |
void | cyhal_comp_free (cyhal_comp_t *obj) |
Deinitialize the Comparator peripheral. More... | |
cy_rslt_t | cyhal_comp_set_power (cyhal_comp_t *obj, cyhal_power_level_t power) |
Changes the current operating power level of the comparator. More... | |
cy_rslt_t | cyhal_comp_configure (cyhal_comp_t *obj, cyhal_comp_config_t *cfg) |
Reconfigure the Comparator. More... | |
bool | cyhal_comp_read (cyhal_comp_t *obj) |
Reads the Comparator state. More... | |
void | cyhal_comp_register_callback (cyhal_comp_t *obj, cyhal_comp_event_callback_t callback, void *callback_arg) |
Register/clear a callback handler for Comparator events. More... | |
void | cyhal_comp_enable_event (cyhal_comp_t *obj, cyhal_comp_event_t event, uint8_t intr_priority, bool enable) |
Enable or Disable a Comparator event. More... | |
struct cyhal_comp_config_t |
Data Fields | ||
---|---|---|
cyhal_power_level_t | power |
Power mode the comparator should operate in. Lower modes save power but operate at lower speed. |
bool | hysteresis |
Whether hysteresis should be used. See the implementation-specific documentation for the hysteresis value |
typedef void(* cyhal_comp_event_callback_t) (void *callback_arg, cyhal_comp_event_t event) |
Handler for Comparator events.
event
argument will be CYHAL_COMP_RISING_EDGE | CYHAL_COMP_FALLING_EDGE
enum cyhal_comp_event_t |
cy_rslt_t cyhal_comp_init | ( | cyhal_comp_t * | obj, |
cyhal_gpio_t | vin_p, | ||
cyhal_gpio_t | vin_m, | ||
cyhal_gpio_t | output, | ||
cyhal_comp_config_t * | cfg | ||
) |
Initialize the Comparator peripheral.
[out] | obj | Pointer to a Comparator object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | vin_p | Non-inverting input pin |
[in] | vin_m | Inverting input pin |
[in] | output | Comparator output pin. May be NC. |
[in] | cfg | Configuration structure |
cy_rslt_t cyhal_comp_init_cfg | ( | cyhal_comp_t * | obj, |
const cyhal_comp_configurator_t * | cfg | ||
) |
Initialize the comparator peripheral using a configurator generated configuration struct.
[out] | obj | Pointer to a comparator object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | cfg | Configuration structure generated by a configurator. |
void cyhal_comp_free | ( | cyhal_comp_t * | obj | ) |
Deinitialize the Comparator peripheral.
[in] | obj | Comparator object |
cy_rslt_t cyhal_comp_set_power | ( | cyhal_comp_t * | obj, |
cyhal_power_level_t | power | ||
) |
Changes the current operating power level of the comparator.
If the power level is set to CYHAL_POWER_LEVEL_OFF, the comparator will be powered-off but it will retain its configuration, so it is not necessary to reconfigure it when changing the power level from CYHAL_POWER_LEVEL_OFF to any other value.
[in] | obj | Comparator object |
[in] | power | The power level to set |
cy_rslt_t cyhal_comp_configure | ( | cyhal_comp_t * | obj, |
cyhal_comp_config_t * | cfg | ||
) |
Reconfigure the Comparator.
This retains the powered state of the comparator. Depending on the implementation, it may be necessary to temporarily deconfigure and/or power off the comparator in order to apply the new configuration. However, if the comparator is powered-off when this function is called, it will remain powered-off after it returns. Likewise, if the comparator is powered-on when this function is called, it will remain powered-on after it returns.
[in] | obj | Comparator object |
[in] | cfg | New configuration to apply |
bool cyhal_comp_read | ( | cyhal_comp_t * | obj | ) |
Reads the Comparator state.
[in] | obj | Comparator object |
void cyhal_comp_register_callback | ( | cyhal_comp_t * | obj, |
cyhal_comp_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register/clear a callback handler for Comparator events.
This function will be called when one of the events enabled by cyhal_comp_enable_event occurs.
[in] | obj | Comparator object |
[in] | callback | Function to call when the specified event happens |
[in] | callback_arg | Generic argument that will be provided to the handler when called |
void cyhal_comp_enable_event | ( | cyhal_comp_t * | obj, |
cyhal_comp_event_t | event, | ||
uint8_t | intr_priority, | ||
bool | enable | ||
) |
Enable or Disable a Comparator event.
When an enabled event occurs, the function specified by cyhal_comp_register_callback will be called.
[in] | obj | Comparator object |
[in] | event | Comparator event |
[in] | intr_priority | Priority for NVIC interrupt events |
[in] | enable | True to turn on event, False to turn off |