1.8.2.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 a new comparison is complete.
if
(callback_status ==
true
) {
- Check if the comparator is ready for the last triggered comparison result to be read.
do
{
last_comparison = ac_chan_get_status(&ac_instance,
AC_COMPARATOR_CHANNEL);
}
while
(last_comparison & AC_CHAN_STATUS_UNKNOWN);
- 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));
- After the interrupt is handled, set the software callback flag to false.
callback_status =
false
;
- Trigger the next conversion on the Analog Comparator channel.
ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);