TIM2定时器配置函数:
[mw_shl_code=c,true]void TIM2_Interrupt_config(void)
{
TIM2_Config();
TIM2_NVIC();
}
void TIM2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
printf("\r\nTIM2_CONFIG_IN!\n");
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("\r\nTIM2_CONFIG_OK!\n");
}
void TIM2_NVIC(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
printf("\r\nTIM2_NVIC_IN!\n");
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("\r\nTIM2_NVIC_OK!\n");
}[/mw_shl_code]
主函数:
[mw_shl_code=c,true]int main(void)
{
USART1_Config();
printf("\r\nProgram started!\n");
Key_GPIO_Config();
TIM3_PWM_Init();
TIM2_Interrupt_config();
}[/mw_shl_code]
中断函数:
[mw_shl_code=c,true]void TIM2_IRQHandler(void)
{
// printf("\r\ninside the interrupt!\r\n");
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("\r\ninside the scan\r\n");
if(servoz_pulse<=Left_Max)
{servoz_pulse=servoz_pulse;}
else
{
servoz_pulse=servoz_pulse - One_Step;
Tim3_Pwm_Reconfig(servoz_pulse);
printf("\r\nservoz_pulse = %d\r\n",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("\r\nservoz_pulse = %d\r\n",servoz_pulse);
}
}
}
}[/mw_shl_code]
执行后,用串口输出运行情况,如下:
从in the if 的次数来看,总共进了5次中断,然后函数就空转了,最后会进入hardfault的死循环。
请问有什么问题? |