用上次的工程,不用建工程。新建也可,只不过选ASF工程。
把上次的TC模块移走加入AC模块:
然后打开AC的快速指导,按照里边的内容完成程序。
但有一点,AC管脚哪个呢,我打开了数据手册,一看就是PA04
运行结果,我用镊子,连接VCC与PA04灯就亮我抬起灯就灭。
具体程序如下:
- #include <asf.h>
- /* AC module software instance (must not go out of scope while in use) */
- static struct ac_module ac_instance;
- #define AC_COMPARATOR_CHANNEL AC_CHAN_CHANNEL_0
- void configure_ac(void);
- void configure_ac_channel(void);
- void configure_ac(void)
- { /* Create a new configuration structure for the Analog Comparator settings * and fill with the default module settings. */
- struct ac_config config_ac;
- ac_get_config_defaults(&config_ac);
- /* Alter any Analog Comparator configuration settings here if required */
- /* Initialize and enable the Analog Comparator with the user settings */
- ac_init(&ac_instance, AC, &config_ac);
- }
- void configure_ac_channel(void)
- { /* Create a new configuration structure for the Analog Comparator channel * settings and fill with the default module channel settings. */
- struct ac_chan_config ac_chan_conf;
- ac_chan_get_config_defaults(&ac_chan_conf);
- /* Set the Analog Comparator channel configuration settings */
- 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;
- /* Set up a pin as an AC channel input */
- 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);
- /* Initialize and enable the Analog Comparator channel with the user * settings */
- ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &ac_chan_conf);
- ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);
- }
- int main (void)
- {
- system_init();
- configure_ac();
- configure_ac_channel();
- ac_enable(&ac_instance);
-
- ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
- uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
- while (true)
- {
- if (ac_chan_is_ready(&ac_instance, AC_COMPARATOR_CHANNEL))
- {
- do {
- last_comparison = ac_chan_get_status(&ac_instance, AC_COMPARATOR_CHANNEL);
- }
- while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
- port_pin_set_output_level(LED_0_PIN, (last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS));
- ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
- }
- }
-
- }
|