1.8.1.2.2 Workflow
- Trigger the first comparison on the comparator channel.
ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
- Create a local variable to maintain the current comparator state. Since no comparison has taken place, it is initialized to AC_CHAN_STATUS_UNKNOWN.
uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
- Make the application loop infinitely, while performing triggered comparisons.
while
(
true
) {
- Check if the comparator is ready for the last triggered comparison result to be read.
if
(ac_chan_is_ready(&ac_instance, AC_COMPARATOR_CHANNEL)) {
- Read the comparator output state into the local variable for application use, re-trying until the comparison state is ready.
do
{
last_comparison = ac_chan_get_status(&ac_instance,
AC_COMPARATOR_CHANNEL);
}
while
(last_comparison & AC_CHAN_STATUS_UNKNOWN);
- Set the board LED state to mirror the last comparison state.
port_pin_set_output_level(LED_0_PIN,
(last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS));
- Trigger the next conversion on the Analog Comparator channel.
ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);