Yesterday I tried the comparator and ADC functions of SAM R21 again. Let me talk about the use of the comparator first. From the data sheet, we can see that the comparator uses the PA04-PA07 pins, while the ADC uses the PA04-PA09 pins.
In the comparator example in ASF, we can see that the main settings are in the configure_ac_channel function.
- void configure_ac_channel(void) //! [setup_7] { /* Create a new configuration structure for the Analog Comparator channel * settings and fill with the default module channel settings. */ //! [setup_8] struct ac_chan_config ac_chan_conf; //! [setup_8] //! [setup_9] ac_chan_get_config_defaults(&ac_chan_conf); //! [setup_9] /* Set the Analog Comparator channel configuration settings */ //! [setup_10] 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; //! [setup_10] /* Set up a pin as an AC channel input */ //! [setup_11] 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 = MUX_PA04B_AC_AIN0; system_pinmux_pin_set_config(PIN_PA04B_AC_AIN0, &ac0_pin_conf); //! [setup_11] /* Initialize and enable the Analog Comparator channel with the user * settings */ //! [setup_12] ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &ac_chan_conf); //! [setup_12] //! [setup_13] ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL); //! [setup_13] } //! [setup]
复制代码It uses AIN0 as the positive input of the comparator, corresponding to PA04. Therefore, when doing the comparator experiment, we connect the input of the comparator to PA04 on the EXT1 interface. The negative end of the comparator uses the voltage divider of VCC as input, and the voltage divider coefficient is 32, that is, 32/64=1/2Vcc. If you modify the voltage divider coefficient, you can get different comparison thresholds. If you change the input of the comparator, you can use different GPIOs.
LED0 represents the output of the comparator. You can easily observe the output status of the comparator by adjusting the potentiometer.