STM32F407I关于定时器方面的
时间:10-02
整理:3721RD
点击:
开发板是Open407I的。我在使用定时器2的时候居然什么都没有,定时器完全没有运行,我只是想用这个定时器做一个一毫秒的中断来测试一下。AHB1的时钟是168M,APB1时钟是42M。
下面是代码:
void STM_Tim2Configuration(void)
{
TIM_TimeBaseInitTypeDef TimeBaseInitTypeDef;
Timx_cnt = 0;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TimeBaseInitTypeDef.TIM_Period = 10;
TimeBaseInitTypeDef.TIM_Prescaler = (4200-1);
TimeBaseInitTypeDef.TIM_ClockDivision = TIM_CKD_div1;
TimeBaseInitTypeDef.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TimeBaseInitTypeDef);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_FLAG_Update, ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void STM_Tim2NVICConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
下面是代码:
void STM_Tim2Configuration(void)
{
TIM_TimeBaseInitTypeDef TimeBaseInitTypeDef;
Timx_cnt = 0;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TimeBaseInitTypeDef.TIM_Period = 10;
TimeBaseInitTypeDef.TIM_Prescaler = (4200-1);
TimeBaseInitTypeDef.TIM_ClockDivision = TIM_CKD_div1;
TimeBaseInitTypeDef.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TimeBaseInitTypeDef);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_FLAG_Update, ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void STM_Tim2NVICConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
哦哦哦,学习学习,谢谢