[Atmel] SAML21走起6:TC0 定时器

[复制链接]
1890|0
 楼主| ddllxxrr 发表于 2015-7-15 05:48 | 显示全部楼层 |阅读模式
这个我昨天就搞了,本来认为很简单,照着提示一路做就完了。但是后来发现灯怎么也不亮。

我于是掉过头仔细看了下快速指导。


如下:
  1. In this use case, the TC will be used as a timer, to generate overflow and compare match callbacks.

  2. In the callbacks the on-board LED is toggled.

  3. The TC module will be set up as follows:

  4. •GCLK generator 1 (GCLK 32K) clock source
  5. •16-bit resolution on the counter
  6. •Prescaler is divided by 64
  7. •GCLK reload action
  8. •Count upward
  9. •Don't run in standby
  10. •No waveform outputs
  11. •No capture enabled
  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
  17. •Counter top set to 2000 (about 4s) and generate overflow callback
  18. •Channel 0 is set to compare and match value 900 and generate callback
  19. •Channel 1 is set to compare and match value 930 and generate callback

可以看到必须设一下GCLK generator 1 (GCLK 32K) clock source
于是得设两个地方一个是,外部晶振32K 为true,再一个 GCLK generator 1必须是true且为GCLK 32K

再下进程序,则LED0亮了。


以下是程序:
  1. #include <asf.h>
  2. #define CONF_TC_MODULE TC3

  3. struct tc_module tc_instance;
  4. void tc_callback_to_toggle_led(struct tc_module *const module_inst);
  5. void configure_tc(void);
  6. void configure_tc_callbacks(void);

  7. void tc_callback_to_toggle_led( struct tc_module *const module_inst)
  8. { port_pin_toggle_output_level(LED0_PIN);}
  9. void configure_tc(void)
  10. { struct tc_config config_tc;
  11. tc_get_config_defaults(&config_tc);
  12. config_tc.counter_size = TC_COUNTER_SIZE_8BIT;
  13. config_tc.clock_source = GCLK_GENERATOR_1;
  14. config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV1024;
  15. config_tc.counter_8_bit.period = 100;
  16. config_tc.counter_8_bit.compare_capture_channel[0] = 50;
  17. config_tc.counter_8_bit.compare_capture_channel[1] = 54;
  18. tc_init(&tc_instance, CONF_TC_MODULE, &config_tc);
  19. tc_enable(&tc_instance);}
  20. void configure_tc_callbacks(void)
  21. { tc_register_callback(&tc_instance, tc_callback_to_toggle_led, TC_CALLBACK_OVERFLOW);
  22. tc_register_callback(&tc_instance, tc_callback_to_toggle_led, TC_CALLBACK_CC_CHANNEL0);
  23. tc_register_callback(&tc_instance, tc_callback_to_toggle_led, TC_CALLBACK_CC_CHANNEL1);
  24. tc_enable_callback(&tc_instance, TC_CALLBACK_OVERFLOW);
  25. tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
  26. tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL1);}

  27. int main (void)
  28. {
  29. system_init();
  30. configure_tc();
  31. configure_tc_callbacks();
  32. system_interrupt_enable_global();

  33. while (1)

  34. {
  35. }
  36. }


本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7004

帖子

68

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