用stm32f429 discovery 那块板子,想调一个每1s闪一次的LED,
系统时钟使用自带的system_stm32f4xx.c 文件初始化,
系统频率 180M,
我程序是这么写的。
static void tim_init(void)
{
TIM_DeInit(TIM2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_GetClocksFreq(&RCC_ClockStruct);
RCC_TIMCLKPresConfig(RCC_TIMPrescDesactivated);
TIM_TimeBaseInitStruct.TIM_Period = 32768-1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4;
TIM_TimeBaseInitStruct.TIM_Prescaler = RCC_ClockStruct.PCLK1_Frequency*2/32768;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_UpdateDisableConfig(TIM2,DISABLE);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStruct.NVIC_IRQChannelSubPriority =0x0F;
NVIC_Init(&NVIC_InitStruct);
TIM_Cmd(TIM2, ENABLE);
}
上面32768 就是TIM2 的计数时钟,
我发现这个时钟配置值和实际定时效果又很大关系,
比如设置10000时候,差不多就是1S,但是设成100,可能只有200ms了
当然TIM_Period和TIM_Prescaler 对应值是同时修改的。
有碰到同样问题的伙伴吗? |