1.8.1.1.3 Workflow

  1. Create an AC device instance struct, which will be associated with an Analog Comparator peripheral hardware instance.
    static struct ac_module ac_instance;
    
    Note: Device instance structures shall never go out of scope when in use.
  2. Define a macro to select the comparator channel that will be sampled, for convenience.
    #define AC_COMPARATOR_CHANNEL   AC_CHAN_CHANNEL_0
    
  3. Create a new function configure_ac(), which will be used to configure the overall Analog Comparator peripheral.
    void configure_ac(void)
    
  4. Create an Analog Comparator peripheral configuration structure that will be filled out to set the module configuration.
    struct ac_config config_ac;
    
  5. Fill the Analog Comparator peripheral configuration structure with the default module configuration values.
    ac_get_config_defaults(&config_ac);
    
  6. Initialize the Analog Comparator peripheral and associate it with the software instance structure that was defined previously.
    ac_init(&ac_instance, AC, &config_ac);
    
  7. Create a new function configure_ac_channel(), which will be used to configure the overall Analog Comparator peripheral.
    void configure_ac_channel(void)
    
  8. Create an Analog Comparator channel configuration structure that will be filled out to set the channel configuration.
    struct ac_chan_config ac_chan_conf;
    
  9. Fill the Analog Comparator channel configuration structure with the default channel configuration values.
    ac_chan_get_config_defaults(&ac_chan_conf);
    
  10. Alter the channel configuration parameters to set the channel to one-shot mode, with the correct negative and positive MUX selections and the desired voltage scaler.
    ac_chan_conf.sample_mode      = AC_CHAN_MODE_SINGLE_SHOT;
    ac_chan_conf.positive_input   = AC_CHAN_POS_MUX_PIN0;
    ac_chan_conf.negative_input   = AC_CHAN_NEG_MUX_SCALED_VCC;
    ac_chan_conf.vcc_scale_factor = 32;
    
    Note: The voltage scalar formula is documented in description for ac_chan_config::vcc_scale_factor.
  11. Configure the physical pin that will be routed to the AC module channel 0.
    struct system_pinmux_config ac0_pin_conf;
    system_pinmux_get_config_defaults(&ac0_pin_conf);
    ac0_pin_conf.direction    = SYSTEM_PINMUX_PIN_DIR_INPUT;
    ac0_pin_conf.mux_position = CONF_AC_MUX;
    system_pinmux_pin_set_config(CONF_AC_PIN, &ac0_pin_conf);
    
  12. Initialize the Analog Comparator channel and configure it with the desired settings.
    ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &ac_chan_conf);
    
  13. Enable the now initialized Analog Comparator channel.
    ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);
    
  14. Enable the now initialized Analog Comparator peripheral.
    ac_enable(&ac_instance);