参考博文:https://www.cnblogs.com/liaigu/p/17782198.html
在CH32L103EVT开发板的范例TIM_INT中,TIM1_INT_Init函数需要适当修改。
1 主频96MHz,计数时钟预分频器(TIM1_PSC)为16位,取值0~65535,对应TIM_Prescaler。
2 TIM_RepetitionCounter 对应重复计数值寄存器(TIM1_RPTCR)
示例中初始化 TIM1_INT_Init( 200-1, 48000-1); 100ms*TIM_RepetitionCounter(50)=5000ms产生一次中断。
将TIM_RepetitionCounter 改为 0 ,则为100ms产生一次中断。
void TIM1_INT_Init( u16 arr, u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};RCC_PB2PeriphClockCmd(RCC_PB2Periph_TIM1, ENABLE );TIM_TimeBaseInitStructure.TIM_Period = arr; //重新加载寄存器中的周期值
TIM_TimeBaseInitStructure.TIM_Prescaler = psc; //TIM时钟预分频值0~65535
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //无
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数值寄存器。达到该计数值后中断。示例中为50,此处改为0。
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);TIM_ClearITPendingBit( TIM1, TIM_IT_Update );NVIC_InitStructure.NVIC_IRQChannel =TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; //
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1; //
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_Init(&NVIC_InitStructure);TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
}