| 
 
| AC比较器固名思议,就是模拟比较器就是模拟信号来进行比较,根据比较的结果进行动作.本程序里是选VCC/2为参考,用比较器第零脚的值同其比较,比较的结果在LED_0上输出. 
 至于怎么建立ASF工程请参考前面的贴子.
 
 下面只讨论AC模拟比较器.首先打开上个工程的工程文件,然后打开ASF Wizard 把AC模块添入.然后再打开ASF EXPLOER.打开快速指导.
 
 如下图所示:
 
 
 
 然后按照提示建立自己的程序:然后编译,以下是编译通过的截图:
 
 
 
 然后根据数据手册找到,PIN0的管脚为PA04
 
 
 然后用万用表拉高PA04,则LED_0亮了,说明程序通过
 
 
 以下是程序清单:
 
 
 
 /**
 * \file
 *
 * \brief Empty user application template
 *
 */
/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * This is a bare minimum user application template.
 *
 * For documentation of the board, go \ref group_common_boards "here" for a link
 * to the board-specific documentation.
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# Minimal main function that starts with a call to system_init()
 * -# Basic usage of on-board LED and button
 * -# "Insert application code here" comment
 *
 */
/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
#include <asf.h>
#include <stdio_serial.h>
void configure_usart(void);
void configure_ac(void);
void configure_ac_channel(void);
struct usart_module usart_instance;
void configure_usart(void)
{   
         struct usart_config config_usart;
         usart_get_config_defaults(&config_usart);
         config_usart.baudrate    = 9600;
         config_usart.mux_setting = EXT3_UART_SERCOM_MUX_SETTING;
         config_usart.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0;
         config_usart.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1;
         config_usart.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2;
         config_usart.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3;
         
         while (usart_init(&usart_instance, EXT3_UART_MODULE, &config_usart) != STATUS_OK)
          {    }
          stdio_serial_init(&usart_instance, EXT3_UART_MODULE, &config_usart);
          usart_enable(&usart_instance);
}
/* AC module software instance (must not go out of scope while in use) */
static struct ac_module ac_instance;/* Comparator channel that will be used */
#define AC_COMPARATOR_CHANNEL   AC_CHAN_CHANNEL_0
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_usart();
        configure_ac();
        configure_ac_channel();
        ac_enable(&ac_instance);
        uint8_t string[] = "Hello World!\r\n";
        uint8_t mystring = 0x55;
        int i=30122121;
        long int i2 = 30122121; 
        
        
        usart_write_buffer_wait(&usart_instance, string, sizeof(string));
        uint16_t temp;
        //while (true) 
        //{ 
        //                        usart_write_buffer_wait(&usart_instance, &mystring, sizeof(        mystring));
        //                        printf("\n");
        //                        printf("How are youj!!!! \r\n");
        //                        printf("%d,%ld\n",i,i2);
        //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint8_t));
        //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint16_t));
        //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint32_t));
        //                        printf("The size of uint8_t is  %d \r\n",sizeof(uint64_t));
        //                        printf("The size of uint8_t is  %d \r\n",sizeof(int8_t));
        //                        if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
        //        {            while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
        //                         {            }
        //        } 
        //}
        
        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);        }    }
}
 | 
 
×本帖子中包含更多资源您需要 登录 才可以下载或查看,没有账号?注册 
  |