这个是利用回调函数把一个状态变量置1,而主程序根据这个,来点一个LED0。这个同上个程序的区别只在一个回调函数。
跟据快指导建立程序:
我在编译的时候提示没有定义如下变量:
我看是一个状态,而指导上绝对没有定义,我就定义一个uint8_t 的那个变量
编译通过:
程序如下:
#include <asf.h>
/* 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);
void configure_ac_channel(void);
void callback_function_ac(struct ac_module *const module_inst);
void configure_ac_callback(void);
uint8_t callback_status;
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_ac();
configure_ac_channel();
configure_ac_callback();
ac_enable(&ac_instance);
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, true);
while (true)
{
if (callback_status == 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);
}
}
}
当然相应的AC模块得添到ASF中
|