STM32配置TIM2定时器作为中断,在中断中扫描按键,中断只能执行有限次

2019-07-21 04:37发布

TIM2定时器配置函数:

[mw_shl_code=c,true]void TIM2_Interrupt_config(void) { TIM2_Config(); TIM2_NVIC(); } void TIM2_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; printf(" TIM2_CONFIG_IN! "); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); TIM_DeInit(TIM2); TIM_TimeBaseStructure.TIM_Period = 7199; TIM_TimeBaseStructure.TIM_Prescaler = 9; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); // TIM_ClearITPendingBit(TIM2, TIM_IT_Update); TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_Trigger,ENABLE); // TIM_ARRPreloadConfig(TIM2, DISABLE); TIM_Cmd(TIM2, ENABLE); printf(" TIM2_CONFIG_OK! "); } void TIM2_NVIC(void) { NVIC_InitTypeDef NVIC_InitStructure; printf(" TIM2_NVIC_IN! "); NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=15; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); printf(" TIM2_NVIC_OK! "); }[/mw_shl_code] 主函数:
[mw_shl_code=c,true]int main(void) { USART1_Config(); printf(" Program started! "); Key_GPIO_Config(); TIM3_PWM_Init(); TIM2_Interrupt_config(); }[/mw_shl_code] 中断函数:
[mw_shl_code=c,true]void TIM2_IRQHandler(void) { // printf(" inside the interrupt! "); if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET) { printf("in the if"); TIM_ClearITPendingBit(TIM2,TIM_IT_Update); if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1) == KEY_ON) { printf(" inside the scan "); if(servoz_pulse<=Left_Max) {servoz_pulse=servoz_pulse;} else { servoz_pulse=servoz_pulse - One_Step; Tim3_Pwm_Reconfig(servoz_pulse); printf(" servoz_pulse = %d ",servoz_pulse); } } else if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_2) == KEY_ON) { if(servoz_pulse>=Right_Max) {servoz_pulse=servoz_pulse;} else { servoz_pulse=servoz_pulse + One_Step; Tim3_Pwm_Reconfig(servoz_pulse); printf(" servoz_pulse = %d ",servoz_pulse); } } } }[/mw_shl_code] 执行后,用串口输出运行情况,如下:

从in the if 的次数来看,总共进了5次中断,然后函数就空转了,最后会进入hardfault的死循环。
请问有什么问题?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。