[Atmel] 用ASF跑SAMD21程序(11)AC比较器CALLBACK

[复制链接]
1448|0
 楼主| ddllxxrr 发表于 2014-12-31 22:42 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2014-12-31 22:52 编辑

上一篇是AC比较器不用回调函数实现的,这里用回调函数,在我看来回调函数就是一个不让你去写中断函数的机制。

首先ASF EXPLOER查下有AC模块点选在(CallbaCk)

其次打开快速指导的网页,


按照提示替换掉以前的函数。其实换完了就多了个void callback_function_ac(struct ac_module *const module_inst);

但有一点值得注意,网页指导可没有说明callback_status 这个变量是什么类型,编译后提示出错,我就想应该是8位的。

我就用了uint8_t型,没想到,编译通过了是不错,但不好用,我找了老半天程序。后来查到这个变量是bool型。

后来换成bool volatile callback_status = false;

程序就好用了。

程序运行的现象是我用一根导线直接接3.3V和COMP0第PA04脚则,灯亮了,我松开又灭了。

没产生比较时:


产生比较时:


以下是程序:



  1. /**
  2. * \file
  3. *
  4. * \brief Empty user application template
  5. *
  6. */

  7. /**
  8. * \mainpage User Application template doxygen documentation
  9. *
  10. * \par Empty user application template
  11. *
  12. * This is a bare minimum user application template.
  13. *
  14. * For documentation of the board, go \ref group_common_boards "here" for a link
  15. * to the board-specific documentation.
  16. *
  17. * \par Content
  18. *
  19. * -# Include the ASF header files (through asf.h)
  20. * -# Minimal main function that starts with a call to system_init()
  21. * -# Basic usage of on-board LED and button
  22. * -# "Insert application code here" comment
  23. *
  24. */

  25. /*
  26. * Include header files for all drivers that have been imported from
  27. * Atmel Software Framework (ASF).
  28. */
  29. #include <asf.h>
  30. #include <stdio_serial.h>

  31. bool volatile callback_status = false;

  32. void configure_usart(void);
  33. void configure_ac(void);
  34. void configure_ac_channel(void);
  35. void callback_function_ac(struct ac_module *const module_inst);
  36. void configure_ac_callback(void);


  37. struct usart_module usart_instance;

  38. void configure_usart(void)
  39. {   
  40.          struct usart_config config_usart;
  41.          usart_get_config_defaults(&config_usart);
  42.          config_usart.baudrate    = 9600;
  43.          config_usart.mux_setting = EXT3_UART_SERCOM_MUX_SETTING;
  44.          config_usart.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0;
  45.          config_usart.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1;
  46.          config_usart.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2;
  47.          config_usart.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3;
  48.          
  49.          while (usart_init(&usart_instance, EXT3_UART_MODULE, &config_usart) != STATUS_OK)
  50.           {    }
  51.           stdio_serial_init(&usart_instance, EXT3_UART_MODULE, &config_usart);
  52.           usart_enable(&usart_instance);
  53. }

  54. /* AC module software instance (must not go out of scope while in use) */
  55. static struct ac_module ac_instance;
  56. /* Comparator channel that will be used */
  57. #define AC_COMPARATOR_CHANNEL AC_CHAN_CHANNEL_0
  58. void configure_ac(void)
  59. {
  60.             /* Create a new configuration structure for the Analog Comparator settings     * and fill with the default module settings. */
  61.                  struct ac_config config_ac;
  62.                  ac_get_config_defaults(&config_ac);
  63.                  /* Alter any Analog Comparator configuration settings here if required */
  64.                  /* Initialize and enable the Analog Comparator with the user settings */
  65.                  ac_init(&ac_instance, AC, &config_ac);
  66. }
  67. void configure_ac_channel(void)
  68. {
  69.            /* Create a new configuration structure for the Analog Comparator channel     * settings and fill with the default module channel settings. */
  70.              struct ac_chan_config config_ac_chan;
  71.                  ac_chan_get_config_defaults(&config_ac_chan);
  72.                 /* Set the Analog Comparator channel configuration settings */
  73.                  config_ac_chan.sample_mode         = AC_CHAN_MODE_SINGLE_SHOT;
  74.                  config_ac_chan.positive_input      = AC_CHAN_POS_MUX_PIN0;
  75.                  config_ac_chan.negative_input      = AC_CHAN_NEG_MUX_SCALED_VCC;
  76.                  config_ac_chan.vcc_scale_factor    = 32;
  77.                  config_ac_chan.interrupt_selection = AC_CHAN_INTERRUPT_SELECTION_END_OF_COMPARE;
  78.                  /* Set up a pin as an AC channel input */
  79.                  struct system_pinmux_config ac0_pin_conf;
  80.                  system_pinmux_get_config_defaults(&ac0_pin_conf);
  81.                  ac0_pin_conf.direction    = SYSTEM_PINMUX_PIN_DIR_INPUT;
  82.                  ac0_pin_conf.mux_position = MUX_PA04B_AC_AIN0;
  83.                  system_pinmux_pin_set_config(PIN_PA04B_AC_AIN0, &ac0_pin_conf);
  84.                  /* Initialize and enable the Analog Comparator channel with the user     * settings */
  85.                  ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &config_ac_chan);
  86.                  ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);
  87.                  }
  88. void callback_function_ac(struct ac_module *const module_inst)
  89. {   
  90.         callback_status = true;
  91. }
  92.         void configure_ac_callback(void)
  93.         {   
  94.                 ac_register_callback(&ac_instance, callback_function_ac, AC_CALLBACK_COMPARATOR_0);
  95.                 ac_enable_callback(&ac_instance, AC_CALLBACK_COMPARATOR_0);
  96.         }

  97. int main (void)
  98. {
  99.         system_init();
  100.         configure_usart();
  101.          configure_ac();
  102.          configure_ac_channel();
  103.          configure_ac_callback();
  104.          ac_enable(&ac_instance);
  105.         uint8_t string[] = "Hello World!\r\n";
  106.         uint8_t mystring = 0x55;
  107.         int i=30122121;
  108.         long int i2 = 30122121;
  109.         
  110.         
  111.         usart_write_buffer_wait(&usart_instance, string, sizeof(string));
  112.         uint16_t temp;
  113.         //while (true)
  114.         //{
  115.         //                        usart_write_buffer_wait(&usart_instance, &mystring, sizeof(        mystring));
  116.         //                        printf("\n");
  117.         //                        printf("How are youj!!!! \r\n");
  118.         //                        printf("%d,%ld\n",i,i2);
  119.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint8_t));
  120.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint16_t));
  121.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint32_t));
  122.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint64_t));
  123.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(int8_t));
  124.         //                        if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
  125.         //        {            while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
  126.         //                         {            }
  127.         //        }
  128.         //}
  129.            system_interrupt_enable_global();
  130.           ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  131.           uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
  132.           port_pin_set_output_level(LED_0_PIN, false);
  133.           while (true)
  134.           {
  135.                          if (callback_status == true)
  136.                                  {            
  137.                                                   port_pin_set_output_level(LED_0_PIN, true);
  138.                                                          do            
  139.                                                  {
  140.                                                                                  last_comparison = ac_chan_get_status(&ac_instance,AC_COMPARATOR_CHANNEL);
  141.                                                          } while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
  142.                                                          port_pin_set_output_level(LED_0_PIN, (last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS));
  143.                                                          callback_status = false;
  144.                                                          ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  145.                               }
  146.      }
  147. }

本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7001

帖子

68

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