1.8.1.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 the comparator is ready for the last triggered comparison result to be read.
    if (ac_chan_is_ready(&ac_instance, AC_COMPARATOR_CHANNEL)) {
    
  5. 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);
    
  6. 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));
    
  7. Trigger the next conversion on the Analog Comparator channel.
    ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);