[Atmel] SAML21走起7:TC回调方式

[复制链接]
1284|1
 楼主| ddllxxrr 发表于 2015-7-16 13:24 | 显示全部楼层 |阅读模式
tc, sam, TI, ST, IO
就是中断方式,Atmel把回调函数替代了以前的中断函数。这样的好处我觉昨不用修改中断那个程序了,只是在程序里声明一下哪个是回调函数就行。

以下是快速指导的提示:

  1. In this use case, the TC will be used to generate a PWM signal, with a varying duty cycle.

  2. Here the pulse width is increased each time the timer count matches the set compare value. The TC module will be set up as follows:

  3. GCLK generator 0 (GCLK main) clock source
  4. 16-bit resolution on the counter
  5. No prescaler
  6. Normal PWM wave generation
  7. GCLK reload action
  8. Don't run in standby
  9. No inversion of waveform output
  10. No capture enabled
  11. Count upward
  12. Don't perform one-shot operations
  13. No event input enabled
  14. No event action
  15. No event generation enabled
  16. Counter starts on 0
没有什么可以设置的,因为默认就是发生器0

按照提示形成代码。完后编译运行:
  1. #define PWM_MODULE EXT2_PWM_MODULE
  2. #define PWM_OUT_PIN EXT2_PWM_0_PIN
  3. #define PWM_OUT_MUX EXT2_PWM_0_MUX
  4. Add to the main application source file, outside of any functions:
  5. struct tc_module tc_instance;
  6. Copy-paste the following callback function code to your user application:
  7. void tc_callback_to_change_duty_cycle(
  8. struct tc_module *const module_inst)
  9. {
  10. static uint16_t i = 0;
  11. i += 128;
  12. tc_set_compare_value(module_inst, TC_COMPARE_CAPTURE_CHANNEL_0, i + 1);
  13. }
  14. Copy-paste the following setup code to your user application:
  15. void configure_tc(void)
  16. {
  17. struct tc_config config_tc;
  18. tc_get_config_defaults(&config_tc);
  19. config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
  20. config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
  21. config_tc.counter_16_bit.compare_capture_channel[0] = 0xFFFF;
  22. config_tc.pwm_channel[0].enabled = true;
  23. config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
  24. config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
  25. tc_init(&tc_instance, PWM_MODULE, &config_tc);
  26. tc_enable(&tc_instance);
  27. }
  28. void configure_tc_callbacks(void)
  29. {
  30. tc_register_callback(
  31. &tc_instance,
  32. tc_callback_to_change_duty_cycle,
  33. TC_CALLBACK_CC_CHANNEL0);
  34. tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
  35. }
  36. Add to user application initialization (typically the start of main()):
  37. configure_tc();
  38. configure_tc_callbacks();
最后我用一个LED按到了PWM_PUT管脚;

照片及运行效果如下:




http://v.youku.com/v_show/id_XMTI4NjAwMzUyMA==.html?from=y1.7-1.2

本帖子中包含更多资源

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

×
maybeyou 发表于 2015-7-24 15:54 | 显示全部楼层
很详细
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7001

帖子

68

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