本帖最后由 ddllxxrr 于 2014-12-22 22:39 编辑
关于建立ASF的工程:这里不重述,具体请看《用ASF跑SAMD21程序(3)(2)(1)》:
用ASF跑SAMD21程序(1)8位定时器点LED
https://bbs.21ic.com/forum.php?mod=viewthread&tid=850511&extra=page%3D1%26filter%3Dtypeid%
26typeid%3D380%26typeid%3D380
用ASF跑SAMD21程序(2)TCC-BASIC
https://bbs.21ic.com/forum.php?mod=viewthread&tid=850813&extra=page%3D1%26filter%3Dtypeid%
26typeid%3D380%26typeid%3D380
用ASF跑SAMD21程序(3)TCC - Double Buffering & Circular
https://bbs.21ic.com/forum.php?mod=viewthread&tid=851403&extra=page%3D1%26filter%3Dtypeid%
26typeid%3D380%26typeid%3D380
这里打开TCC-CallBack文档:按照文档的提示建立程序,由于本程用的是TC6没有指示灯,我就改成TCC的LED0,这样可以看清运行的情况。
时钟不用设就行。
以下是程序清单:
- /**
- * \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_CHANNEL LED_0_PWM4CTRL_CHANNEL
- #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE
- #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX
- #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN
- #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT
- //#define CONF_PWM_MODULE EXT1_PWM_MODULE
- //#define CONF_PWM_OUT_PIN EXT1_PWM_0_PIN
- //#define CONF_PWM_OUT_MUX EXT1_PWM_0_MUX
- //#define CONF_PWM_CHANNEL EXT1_PWM_0_CHANNEL
- //#define CONF_PWM_OUTPUT 0
- 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 uint32_t i = 0;
- if (--delay) {
- return;
- }
- delay = 10;
- i = (i + 0x0800) & 0xFFFF;
- tcc_set_compare_value(module_inst,
- (enum tcc_match_capture_channel)
- (TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL),
- i + 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();
- // Insert application code here, after the board has been initialized.
- // This skeleton code simply sets the LED to the state of the button.
- while (1) {
-
- }
- }
运行结果,只见LED0亮后慢慢地灭。
|