定时器2的配置如下:
定时时间是1ms,
/**
\brief configure the TIMER peripheral
\param[in] none
\param[out] none
\retval none
*/
void timer_config(void)
{
/* TIMER2 configuration: generate PWM signals with different duty cycles:
TIMER2CLK = SystemCoreClock / 72 = 1MHz */
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
timer_struct_para_init(&timer_initpara);
/* TIMER2 configuration */
timer_initpara.prescaler = 71; //(71+1)/72000000*(999+1)=1/1000s=1ms
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER2,&timer_initpara);//这里面会自动完成timer_autoreload_value_config的功能;
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER2,TIMER_INT_UP);
nvic_irq_enable(TIMER2_IRQn,0U);//使能更新中断,这一句是必须有的,否则不进中断
timer_auto_reload_shadow_enable(TIMER2);
timer_enable(TIMER2);
}
uint32_t G_TimerFlag = 0;//测试定时时间用
//定时器2的中断处理
void TIMER2_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP)){
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
G_TimerFlag ++;//测试定时时间用
}
}
在main循环中1s打印一次G_TimerFlag的值:
while (1)
{
if(G_TimerFlag%1000 == 0){
printf("timer flag = %d\r\n!",G_TimerFlag);//10s打印一次
}
}
串口可以看到1s打印一次;
原文链接:https://blog.csdn.net/u012523921/article/details/111884878 |