我用的是STM32F030C8
发现定时器进不了中断,查找原因未果,在此求教!
下面的我的代码:
定时器配置:
[mw_shl_code=c,true]void Tim3_Config(uint32_t period, uint16_t PrescalerValue)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Time base configuration */
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = period;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = period;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
/* TIM IT enable */
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
}
[/mw_shl_code]
中断函数:
[mw_shl_code=c,true]void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
printf("timer interrupt!
");
}
}[/mw_shl_code]
使用的库是STM32F0xx_StdPeriph_Lib_V1.5.0
startup_stm32f030.s中关于中断的定义有
TIM3_IRQHandler,如下:
[mw_shl_code=c,true]
[/mw_shl_code]
[mw_shl_code=c,true]工程编译正常,下载正常,串口正常,但是就是进不了定时器中断(串口收不到中断中的字符串),请问问题出在哪?谢谢![/mw_shl_code]
void TIM3_NvicConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM14 ?D????ì×éè??*/
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/* TIM14 for delay */
void TIM3_BaseConfig(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //open TIM3 clock.
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //48000000 / (47999 + 1) = 1000Hz. 1Ms.
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 3199;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //TIM14, ??1ms?D??ò?′?.
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
/*((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+35999)/72M)*(1+2000)=1?? (?á1??as)*/
void TIM3_Init(void)
{
TIM3_NvicConfig();
TIM3_BaseConfig();
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);//??3y?D??±ê??
}
}
这个可以用,你试试。
Tim3_Config(1000, SystemCoreClock/1000 - 1);
其中SystemCoreClock为库文件system_stm32f0xx.c中定义的
uint32_t SystemCoreClock = 48000000;
---------------------------------
你好,谢谢你的回答,为什么我试了你的代码还是不行呢?
---------------------------------
这个无妨,只有你的工程只出现一个void TIM3_IRQHandler(void) 就行了,在哪里看个人习惯。你自己改下进入中断的间隔时间,建议不要太短,中断里面尽量不要加有死循环的代码。
---------------------------------
非常感谢你的热心解答,问题找到了,配置没问题,问题在于,我的工程是作为APP使用的,程序未对中断向量表设置偏移,导致无法中断!
希望看到的网友也要注意此问题哦
一周热门 更多>