比如我们需要实现一个1ms的定时器中断功能:
void TC1_Init(void)
{
// Unmask clock for TCC1
tc_enable(&TCC1);
// Enable overflow interrupt
tc_set_overflow_interrupt_level(&TCC1, TC_INT_LVL_LO);
// Configure TC in normal mode
tc_set_wgm(&TCC1, TC_WG_NORMAL);
// Configure call back interrupt
tc_set_overflow_interrupt_callback(&TCC1,
ceb_ovf_interrupt_callback);
// Configure TC period and and resolution
tc_write_period(&TCC1, sysclk_get_main_hz()/1000);
tc_set_resolution(&TCC1, sysclk_get_main_hz());
}
上述就是初始化定时器的过程,tc_enable使能定时器模块,这里使用定时器的TCC1模块;tc_set_overflow_interrupt_level使能定时器溢出中断,和设置定时器溢出中断优先级;tc_set_wgm设置定时器为正常模式,由于定时器有多种操作模式;简单理解就是从0计数到设置的周期值,发生溢出中断,在下一个时钟周期计数器变为0;tc_set_overflow_interrupt_callback设置溢出中断回调函数,当溢出中断时,系统会调用ceb_ovf_interrupt_callback这个回调函数;tc_write_period设置定时器周期;tc_set_resolution设置定时器时钟分频;