static void timer5_irq_handler(void)
{
if (RESET != timer_interrupt_flag_get(TIMER5, TIMER_INT_FLAG_UP)) {
timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
//to do
}
}
void TIMER5_DAC_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
timer5_irq_handler();
/* leave interrupt */
rt_interrupt_leave();
}
/* 描述:基本定时器5初始化
* 备注:TIMER5的时钟源为 TIMER_CK = 100MHz
* 1ms产生一次中断, 单脉冲模式
* 返回值:无 */
void timer5_period_init(void)
{
timer_parameter_struct timer_basestructure;
unsigned int TIMER_CK;
__IO unsigned short timer_prescaler;
int count = 50000;
memset(&timer_basestructure, 0x00, sizeof(timer_parameter_struct));
/* enable the TIMER5 global interrupt */
nvic_irq_enable((uint8_t)TIMER5_DAC_IRQn, 1U, 0U);
rcu_periph_clock_enable(RCU_TIMER5); /* 使能TIMER5时钟 */
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL2);
timer_deinit(TIMER5);
TIMER_CK = rcu_clock_freq_get(CK_APB1) * 2; //100Mhz, 对应1000ms => 0.1Mhz对应1ms
//向上计数到50000,产生一次中断(1ms), 那么分频: 0.1Mhz/50000 = 2
timer_prescaler = (TIMER_CK / 1000) / count - 1;
timer_disable(TIMER5);
timer_interrupt_disable(TIMER5, TIMER_INT_UP);
/* TIMER5 初始化配置 */
timer_basestructure.prescaler = timer_prescaler;
timer_basestructure.alignedmode = TIMER_COUNTER_EDGE;
timer_basestructure.counterdirection = TIMER_COUNTER_UP;
timer_basestructure.period = count;
timer_init(TIMER5,&timer_basestructure);
timer_counter_value_config(TIMER5, 0);
timer_update_source_config(TIMER5, TIMER_UPDATE_SRC_REGULAR);
timer_auto_reload_shadow_disable(TIMER5);
timer_single_pulse_mode_config(TIMER5, TIMER_SP_MODE_SINGLE);
timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER5,TIMER_INT_UP); /* 使能更新中断 */
timer_enable(TIMER5);
//dump_timer5_reg();
return;
}
其中
CK_APB1 : 50Mhz
CK_TIMER5 :100Mhz
这里TIMER_CK等于CK_TIMER ??
代码中预分频设为2 所以PSC_CLK = 100Mhz/2 = 50Mhz 表示计数50 000 000次表示1秒。 所以计数1次代表时间1/50000000秒
代码中计数50000次,所以表示计数50000次后时间为 (1/50000000) *50000秒 = 1/1000秒