本帖最后由 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脚则,灯亮了,我松开又灭了。
没产生比较时:
产生比较时:
以下是程序:
- /**
- * \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>
- bool volatile callback_status = false;
- void configure_usart(void);
- void configure_ac(void);
- void configure_ac_channel(void);
- void callback_function_ac(struct ac_module *const module_inst);
- void configure_ac_callback(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 config_ac_chan;
- ac_chan_get_config_defaults(&config_ac_chan);
- /* Set the Analog Comparator channel configuration settings */
- config_ac_chan.sample_mode = AC_CHAN_MODE_SINGLE_SHOT;
- config_ac_chan.positive_input = AC_CHAN_POS_MUX_PIN0;
- config_ac_chan.negative_input = AC_CHAN_NEG_MUX_SCALED_VCC;
- config_ac_chan.vcc_scale_factor = 32;
- config_ac_chan.interrupt_selection = AC_CHAN_INTERRUPT_SELECTION_END_OF_COMPARE;
- /* 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, &config_ac_chan);
- ac_chan_enable(&ac_instance, AC_COMPARATOR_CHANNEL);
- }
- void callback_function_ac(struct ac_module *const module_inst)
- {
- callback_status = true;
- }
- void configure_ac_callback(void)
- {
- ac_register_callback(&ac_instance, callback_function_ac, AC_CALLBACK_COMPARATOR_0);
- ac_enable_callback(&ac_instance, AC_CALLBACK_COMPARATOR_0);
- }
- int main (void)
- {
- system_init();
- configure_usart();
- configure_ac();
- configure_ac_channel();
- configure_ac_callback();
- 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)
- // { }
- // }
- //}
- system_interrupt_enable_global();
- ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
- uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN;
- port_pin_set_output_level(LED_0_PIN, false);
- while (true)
- {
- if (callback_status == true)
- {
- port_pin_set_output_level(LED_0_PIN, true);
- 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));
- callback_status = false;
- ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL);
- }
- }
- }
|