[Atmel] 用ASF跑SAMD21程序(5)TCC - CallBack

[复制链接]
2208|0
 楼主| ddllxxrr 发表于 2014-12-20 15:40 | 显示全部楼层 |阅读模式
本帖最后由 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,这样可以看清运行的情况。


时钟不用设就行。
以下是程序清单:
  1. /**
  2. * \file
  3. *
  4. * \brief Empty user application template
  5. *
  6. */

  7. /**
  8. * \mainpage User Application template doxygen documentation
  9. *
  10. * \par Empty user application template
  11. *
  12. * This is a bare minimum user application template.
  13. *
  14. * For documentation of the board, go \ref group_common_boards "here" for a link
  15. * to the board-specific documentation.
  16. *
  17. * \par Content
  18. *
  19. * -# Include the ASF header files (through asf.h)
  20. * -# Minimal main function that starts with a call to system_init()
  21. * -# Basic usage of on-board LED and button
  22. * -# "Insert application code here" comment
  23. *
  24. */

  25. /*
  26. * Include header files for all drivers that have been imported from
  27. * Atmel Software Framework (ASF).
  28. */
  29. #include <asf.h>

  30. #define         CONF_PWM_CHANNEL   LED_0_PWM4CTRL_CHANNEL
  31. #define         CONF_PWM_MODULE   LED_0_PWM4CTRL_MODULE
  32. #define         CONF_PWM_OUT_MUX   LED_0_PWM4CTRL_MUX
  33. #define         CONF_PWM_OUT_PIN   LED_0_PWM4CTRL_PIN
  34. #define         CONF_PWM_OUTPUT   LED_0_PWM4CTRL_OUTPUT

  35. //#define CONF_PWM_MODULE      EXT1_PWM_MODULE
  36. //#define CONF_PWM_OUT_PIN     EXT1_PWM_0_PIN
  37. //#define CONF_PWM_OUT_MUX     EXT1_PWM_0_MUX
  38. //#define CONF_PWM_CHANNEL     EXT1_PWM_0_CHANNEL
  39. //#define CONF_PWM_OUTPUT      0


  40. struct tcc_module tcc_instance;


  41. static void tcc_callback_to_change_duty_cycle(
  42. struct tcc_module *const module_inst)
  43. {
  44.         static uint32_t delay = 10;
  45.         static uint32_t i = 0;
  46.         if (--delay) {
  47.                 return;
  48.         }
  49.         delay = 10;
  50.         i = (i + 0x0800) & 0xFFFF;
  51.         tcc_set_compare_value(module_inst,
  52.         (enum tcc_match_capture_channel)
  53.         (TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL),
  54.         i + 1);
  55. }

  56. static void configure_tcc(void)
  57. {
  58.         struct tcc_config config_tcc;
  59.         tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE);
  60.         config_tcc.counter.period = 0xFFFF;
  61.         config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
  62.         config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF;
  63.         config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true;
  64.         config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT]        = CONF_PWM_OUT_PIN;
  65.         config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT]    = CONF_PWM_OUT_MUX;
  66.         tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc);
  67.         tcc_enable(&tcc_instance);
  68. }
  69. static void configure_tcc_callbacks(void)
  70. {
  71.         tcc_register_callback(
  72.         &tcc_instance,
  73.         tcc_callback_to_change_duty_cycle,
  74.         (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL));
  75.         tcc_enable_callback(&tcc_instance,
  76.         (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL));
  77. }
  78. int main (void)
  79. {
  80.         system_init();
  81.         configure_tcc();
  82.         configure_tcc_callbacks();
  83.     system_interrupt_enable_global();
  84.         // Insert application code here, after the board has been initialized.

  85.         // This skeleton code simply sets the LED to the state of the button.
  86.         while (1) {
  87.         
  88.         }
  89. }
运行结果,只见LED0亮后慢慢地灭。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部