uint16_t PrescalerValue = 0;
TMR_TimerBaseInitType TMR_TimeBaseStructure;
NVIC_InitType NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //优先级分组0
/* TMR2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR2, ENABLE);
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 13107200) - 1; //时基5MS 13107200
/* TMRe base configuration */
TMR_TimeBaseStructInit(&TMR_TimeBaseStructure);
TMR_TimeBaseStructure.TMR_Period = 65535;
TMR_TimeBaseStructure.TMR_DIV = 0;
TMR_TimeBaseStructure.TMR_ClockDivision = 0;
TMR_TimeBaseStructure.TMR_CounterMode = TMR_CounterDIR_Up;
TMR_TimeBaseInit(TMR2, &TMR_TimeBaseStructure);
/* Prescaler configuration */
TMR_DIVConfig(TMR2, PrescalerValue, TMR_DIVReloadMode_Immediate);
/* TMR IT enable */
TMR_INTConfig(TMR2, TMR_INT_CC2, ENABLE);
/* TMR2 enable counter */
TMR_Cmd(TMR2, ENABLE);
/* Enable the TMR2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TMR2_GLOBAL_IRQn; //优先级00
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
老哥,我这个是5ms中断一次的。向上计数模式。需要不同时间的话,改13107200这个地方 |