[Atmel] SAML21走起9:AC模拟比较器

[复制链接]
1053|1
 楼主| ddllxxrr 发表于 2015-7-20 14:30 | 显示全部楼层 |阅读模式
用上次的工程,不用建工程。新建也可,只不过选ASF工程。
把上次的TC模块移走加入AC模块:





然后打开AC的快速指导,按照里边的内容完成程序。


但有一点,AC管脚哪个呢,我打开了数据手册,一看就是PA04

运行结果,我用镊子,连接VCC与PA04灯就亮我抬起灯就灭。


具体程序如下:

  1. #include <asf.h>

  2. /* AC module software instance (must not go out of scope while in use) */
  3. static struct ac_module ac_instance;

  4. #define AC_COMPARATOR_CHANNEL   AC_CHAN_CHANNEL_0
  5. void configure_ac(void);
  6. void configure_ac_channel(void);



  7. void configure_ac(void)
  8. {    /* Create a new configuration structure for the Analog Comparator settings     * and fill with the default module settings. */
  9.            struct ac_config config_ac;
  10.            ac_get_config_defaults(&config_ac);
  11.            /* Alter any Analog Comparator configuration settings here if required */
  12.            /* Initialize and enable the Analog Comparator with the user settings */
  13.            ac_init(&ac_instance, AC, &config_ac);
  14.            }
  15. void configure_ac_channel(void)
  16. {    /* Create a new configuration structure for the Analog Comparator channel     * settings and fill with the default module channel settings. */
  17.             struct ac_chan_config ac_chan_conf;
  18.                 ac_chan_get_config_defaults(&ac_chan_conf);
  19.                  /* Set the Analog Comparator channel configuration settings */
  20.                 ac_chan_conf.sample_mode      = AC_CHAN_MODE_SINGLE_SHOT;
  21.                 ac_chan_conf.positive_input   = AC_CHAN_POS_MUX_PIN0;
  22.                 ac_chan_conf.negative_input   = AC_CHAN_NEG_MUX_SCALED_VCC;
  23.                 ac_chan_conf.vcc_scale_factor = 32;
  24.                  /* Set up a pin as an AC channel input */
  25.                 struct system_pinmux_config ac0_pin_conf;
  26.                 system_pinmux_get_config_defaults(&ac0_pin_conf);
  27.                 ac0_pin_conf.direction    = SYSTEM_PINMUX_PIN_DIR_INPUT;
  28.                 ac0_pin_conf.mux_position = MUX_PA04B_AC_AIN0;
  29.                 system_pinmux_pin_set_config(PIN_PA04B_AC_AIN0, &ac0_pin_conf);
  30.                  /* Initialize and enable the Analog Comparator channel with the user     * settings */
  31.                 ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &ac_chan_conf);
  32.                 ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);
  33.                 }


  34. int main (void)
  35. {
  36.         system_init();
  37.         configure_ac();
  38.         configure_ac_channel();
  39.         ac_enable(&ac_instance);
  40.        
  41.           ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  42.           uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
  43.           while (true)
  44.           {
  45.                    if (ac_chan_is_ready(&ac_instance, AC_COMPARATOR_CHANNEL))
  46.                     {
  47.                                             do {
  48.                                                                                last_comparison = ac_chan_get_status(&ac_instance,                        AC_COMPARATOR_CHANNEL);
  49.                                                            }
  50.                                                                                            while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
  51.                                                                                            port_pin_set_output_level(LED_0_PIN,                    (last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS));
  52.                                                                                            ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  53.                                                        }
  54.                   }

  55.        
  56. }



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| ddllxxrr 发表于 2015-7-20 21:14 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7002

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部