呼吸灯,仿人类吸引,是检验开发板及单片机性能的一种方法。
首先建立ASF工程,然后把TCC(CALLBACK)模块包含进来。
然后点开,ASF EXPLORER中的快速指导。
按照提示形成程序,然后改一下代码如下:
static void tcc_callback_to_change_duty_cycle(
struct tcc_module *const module_inst)
{
static uint32_t delay = 10;
static bool flag = true;
if(flag)
{
delay +=64;
if(delay >= 0xFFE0)
flag = false;
}
else
{
delay-=64;
if(delay<=0x20)
flag = true;
}
tcc_set_compare_value(module_inst,
(enum tcc_match_capture_channel)
(TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL),
delay + 1);
}
总程序清单如下:
/**
* \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>
#define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE
#define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL
#define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT
#define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN
#define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX
static void configure_tcc_callbacks(void);
static void configure_tcc(void);
static void tcc_callback_to_change_duty_cycle(struct tcc_module *const module_inst);
struct tcc_module tcc_instance;
static void tcc_callback_to_change_duty_cycle(
struct tcc_module *const module_inst)
{
static uint32_t delay = 10;
static bool flag = true;
if(flag)
{
delay +=64;
if(delay >= 0xFFE0)
flag = false;
}
else
{
delay-=64;
if(delay<=0x20)
flag = true;
}
tcc_set_compare_value(module_inst,
(enum tcc_match_capture_channel)
(TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL),
delay + 1);
}
static void configure_tcc(void)
{
struct tcc_config config_tcc;
tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE);
config_tcc.counter.period = 0xFFFF;
config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF;
config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true;
config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN;
config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX;
tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc);
tcc_enable(&tcc_instance);
}
static void configure_tcc_callbacks(void)
{
tcc_register_callback(
&tcc_instance,
tcc_callback_to_change_duty_cycle,
(enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL));
tcc_enable_callback(&tcc_instance,
(enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL));
}
int main (void)
{
system_init();
configure_tcc();
configure_tcc_callbacks();
system_interrupt_enable_global();
while (true) {
}
}
运行的结果是,真象呼吸一样。渐亮又渐灭。
|