打印
[Atmel]

SAML21走起7:TC回调方式

[复制链接]
1063|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tc, sam, TI, ST, IO
就是中断方式,Atmel把回调函数替代了以前的中断函数。这样的好处我觉昨不用修改中断那个程序了,只是在程序里声明一下哪个是回调函数就行。

以下是快速指导的提示:

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

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:

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

按照提示形成代码。完后编译运行:
#define PWM_MODULE EXT2_PWM_MODULE
#define PWM_OUT_PIN EXT2_PWM_0_PIN
#define PWM_OUT_MUX EXT2_PWM_0_MUX
Add to the main application source file, outside of any functions:
struct tc_module tc_instance;
Copy-paste the following callback function code to your user application:
void tc_callback_to_change_duty_cycle(
struct tc_module *const module_inst)
{
static uint16_t i = 0;
i += 128;
tc_set_compare_value(module_inst, TC_COMPARE_CAPTURE_CHANNEL_0, i + 1);
}
Copy-paste the following setup code to your user application:
void configure_tc(void)
{
struct tc_config config_tc;
tc_get_config_defaults(&config_tc);
config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
config_tc.counter_16_bit.compare_capture_channel[0] = 0xFFFF;
config_tc.pwm_channel[0].enabled = true;
config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
tc_init(&tc_instance, PWM_MODULE, &config_tc);
tc_enable(&tc_instance);
}
void configure_tc_callbacks(void)
{
tc_register_callback(
&tc_instance,
tc_callback_to_change_duty_cycle,
TC_CALLBACK_CC_CHANNEL0);
tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
}
Add to user application initialization (typically the start of main()):
configure_tc();
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

2398

主题

6945

帖子

66

粉丝