[Atmel] SAML21走起10:AC模拟比较器CALLBACK模式

[复制链接]
1106|1
 楼主| ddllxxrr 发表于 2015-7-23 06:05 | 显示全部楼层 |阅读模式
这个是利用回调函数把一个状态变量置1,而主程序根据这个,来点一个LED0。这个同上个程序的区别只在一个回调函数。

跟据快指导建立程序:



我在编译的时候提示没有定义如下变量:
我看是一个状态,而指导上绝对没有定义,我就定义一个uint8_t 的那个变量

编译通过:




程序如下:


  1. <P> </P>
  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. /* Comparator channel that will be used. */
  5. #define AC_COMPARATOR_CHANNEL AC_CHAN_CHANNEL_0

  6. void configure_ac(void);
  7. void configure_ac_channel(void);
  8. void callback_function_ac(struct ac_module *const module_inst);
  9. void configure_ac_callback(void);

  10. uint8_t callback_status;

  11. void configure_ac(void)
  12. {
  13. /* Create a new configuration structure for the Analog Comparator settings * and fill with the default module settings. */
  14. struct ac_config config_ac;
  15. ac_get_config_defaults(&config_ac);
  16. /* Alter any Analog Comparator configuration settings here if required. */
  17. /* Initialize and enable the Analog Comparator with the user settings. */
  18. ac_init(&ac_instance, AC, &config_ac);}
  19. void configure_ac_channel(void)
  20. {
  21. /* Create a new configuration structure for the Analog Comparator channel * settings and fill with the default module channel settings. */
  22. struct ac_chan_config config_ac_chan;
  23. ac_chan_get_config_defaults(&config_ac_chan);
  24. /* Set the Analog Comparator channel configuration settings. */
  25. config_ac_chan.sample_mode = AC_CHAN_MODE_SINGLE_SHOT;
  26. config_ac_chan.positive_input = AC_CHAN_POS_MUX_PIN0;
  27. config_ac_chan.negative_input = AC_CHAN_NEG_MUX_SCALED_VCC;
  28. config_ac_chan.vcc_scale_factor = 32;
  29. config_ac_chan.interrupt_selection = AC_CHAN_INTERRUPT_SELECTION_END_OF_COMPARE;
  30. /* Set up a pin as an AC channel input. */
  31. struct system_pinmux_config ac0_pin_conf;
  32. system_pinmux_get_config_defaults(&ac0_pin_conf);
  33. ac0_pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_INPUT;
  34. ac0_pin_conf.mux_position = MUX_PA04B_AC_AIN0;
  35. system_pinmux_pin_set_config(PIN_PA04B_AC_AIN0, &ac0_pin_conf);
  36. /* Initialize and enable the Analog Comparator channel with the user * settings. */
  37. ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &config_ac_chan);
  38. ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);}
  39. void callback_function_ac(struct ac_module *const module_inst)
  40. { callback_status = true;}
  41. void configure_ac_callback(void)
  42. { ac_register_callback(&ac_instance, callback_function_ac, AC_CALLBACK_COMPARATOR_0);
  43. ac_enable_callback(&ac_instance, AC_CALLBACK_COMPARATOR_0);}






  44. int main (void)
  45. {

  46. system_init();
  47. configure_ac();
  48. configure_ac_channel();
  49. configure_ac_callback();
  50. ac_enable(&ac_instance);
  51. ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  52. uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
  53. port_pin_set_output_level(LED_0_PIN, true);
  54. while (true)
  55. {
  56. if (callback_status == true)
  57. {
  58. do
  59. {
  60. last_comparison = ac_chan_get_status(&ac_instance, AC_COMPARATOR_CHANNEL);
  61. } while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
  62. port_pin_set_output_level(LED_0_PIN, (last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS));
  63. callback_status = false;
  64. ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  65. }
  66. }


  67. }
当然相应的AC模块得添到ASF中

本帖子中包含更多资源

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

×
shanguizhige 发表于 2015-7-24 16:01 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7002

帖子

68

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