1.8.2.2.2 Workflow

  1. Trigger the first comparison on the comparator channel.
    ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
    
  2. 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;
    
  3. Make the application loop infinitely, while performing triggered comparisons.
    while (true) {
    
  4. Check if a new comparison is complete.
    if (callback_status == true) {
    
  5. 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);
    
  6. 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);
    
  7. 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));
    
  8. After the interrupt is handled, set the software callback flag to false.
    callback_status = false;
    
  9. Trigger the next conversion on the Analog Comparator channel.
    ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);