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

[复制链接]
1551|1
 楼主| ddllxxrr 发表于 2014-12-29 20:04 | 显示全部楼层 |阅读模式
AC比较器固名思议,就是模拟比较器就是模拟信号来进行比较,根据比较的结果进行动作.本程序里是选VCC/2为参考,用比较器第零脚的值同其比较,比较的结果在LED_0上输出.

至于怎么建立ASF工程请参考前面的贴子.

下面只讨论AC模拟比较器.首先打开上个工程的工程文件,然后打开ASF Wizard 把AC模块添入.然后再打开ASF EXPLOER.打开快速指导.

如下图所示:



然后按照提示建立自己的程序:然后编译,以下是编译通过的截图:



然后根据数据手册找到,PIN0的管脚为PA04


然后用万用表拉高PA04,则LED_0亮了,说明程序通过


以下是程序清单:


  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. void configure_usart(void);
  32. void configure_ac(void);
  33. void configure_ac_channel(void);

  34. struct usart_module usart_instance;

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

  51. /* AC module software instance (must not go out of scope while in use) */
  52. static struct ac_module ac_instance;/* Comparator channel that will be used */
  53. #define AC_COMPARATOR_CHANNEL   AC_CHAN_CHANNEL_0
  54. void configure_ac(void)
  55. {    /* Create a new configuration structure for the Analog Comparator settings     * and fill with the default module settings. */   
  56.          struct ac_config config_ac;
  57.          ac_get_config_defaults(&config_ac);
  58.          /* Alter any Analog Comparator configuration settings here if required */
  59.          /* Initialize and enable the Analog Comparator with the user settings */   
  60.           ac_init(&ac_instance, AC, &config_ac);}
  61.           
  62.           void configure_ac_channel(void)
  63.           {    /* Create a new configuration structure for the Analog Comparator channel     * settings and fill with the default module channel settings. */   
  64.                   struct ac_chan_config ac_chan_conf;
  65.                   ac_chan_get_config_defaults(&ac_chan_conf);
  66.                   /* Set the Analog Comparator channel configuration settings */
  67.                   ac_chan_conf.sample_mode      = AC_CHAN_MODE_SINGLE_SHOT;
  68.                   ac_chan_conf.positive_input   = AC_CHAN_POS_MUX_PIN0;
  69.                   ac_chan_conf.negative_input   = AC_CHAN_NEG_MUX_SCALED_VCC;
  70.                   ac_chan_conf.vcc_scale_factor = 32;
  71.                   /* Set up a pin as an AC channel input */
  72.                   struct system_pinmux_config ac0_pin_conf;
  73.                   system_pinmux_get_config_defaults(&ac0_pin_conf);
  74.                   ac0_pin_conf.direction    = SYSTEM_PINMUX_PIN_DIR_INPUT;
  75.                   ac0_pin_conf.mux_position = MUX_PA04B_AC_AIN0;
  76.                   system_pinmux_pin_set_config(PIN_PA04B_AC_AIN0, &ac0_pin_conf);
  77.                   /* Initialize and enable the Analog Comparator channel with the user     * settings */   
  78.                   ac_chan_set_config(&ac_instance, AC_COMPARATOR_CHANNEL, &ac_chan_conf);
  79.                   ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);}

  80. int main (void)
  81. {
  82.         system_init();
  83.         configure_usart();
  84.         configure_ac();
  85.         configure_ac_channel();
  86.         ac_enable(&ac_instance);
  87.         uint8_t string[] = "Hello World!\r\n";
  88.         uint8_t mystring = 0x55;
  89.         int i=30122121;
  90.         long int i2 = 30122121;
  91.        
  92.        
  93.         usart_write_buffer_wait(&usart_instance, string, sizeof(string));
  94.         uint16_t temp;
  95.         //while (true)
  96.         //{
  97.         //                        usart_write_buffer_wait(&usart_instance, &mystring, sizeof(        mystring));
  98.         //                        printf("\n");
  99.         //                        printf("How are youj!!!! \r\n");
  100.         //                        printf("%d,%ld\n",i,i2);
  101.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint8_t));
  102.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint16_t));
  103.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint32_t));
  104.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint64_t));
  105.         //                        printf("The size of uint8_t is  %d \r\n",sizeof(int8_t));
  106.         //                        if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
  107.         //        {            while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
  108.         //                         {            }
  109.         //        }
  110.         //}
  111.        
  112.         ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
  113.         uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
  114.         while (true) {        
  115.                 if (ac_chan_is_ready(&ac_instance, AC_COMPARATOR_CHANNEL))
  116.                  {
  117.                                      do {
  118.                                                                          last_comparison = ac_chan_get_status(&ac_instance,                        AC_COMPARATOR_CHANNEL);
  119.                                                                                                     } while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
  120.                                                                                                port_pin_set_output_level(LED_0_PIN,                    (last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS));
  121.                                                                                                                  ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);        }    }
  122. }

本帖子中包含更多资源

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

×
86269838 发表于 2017-3-20 12:07 | 显示全部楼层
版主你好,我试了你这个程序,但是我的不能工作。我发现程序卡在 if (ac_chan_is_ready(&ac_instance, AC_COMPARATOR_CHANNEL)) 这一步。 AC_chan一直没有ready。请问有头绪吗?注: 我用的ASF3.32。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7001

帖子

68

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