写了一个stm32的定时器初始化程序
cpu型号是stm32f051c8t6
可以初始化,但是永远进不了中断服务程序,程序在仿真器里面跑的时候每次停下来,都停止在以下红色的部分。
难道我中断部分配置错了?
以下是代码
//-----------------------------------------------------
void TIM2_IRQHandler()
{
static int tic,tid;
if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) ==SET )
{
TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);
tic++;
tid=tic&0x0001;
if(tid==0)
GPIO_SetBits(LED_PORT,LED4); //点亮LED1
else
GPIO_ResetBits(LED_PORT,LED4);//熄灭LED1
}
}
//-----------------------------------------------------
void init_timer2_sub0()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(MOTO_GPIO_CLK, ENABLE);//使能moto所在GPIO的时钟
GPIO_InitStructure.GPIO_Pin = motor0_pluse | motor0_dir;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(motor0_port, &GPIO_InitStructure);
GPIO_ResetBits(motor0_port,motor0_pluse | motor0_dir);
}
//-----------------------------------------------------
void init_timer2_sub1()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseStructure.TIM_Period=2000;//这个值实际上就是TIMX->ARR,延时开始时重新设定即可?
TIM_TimeBaseStructure.TIM_Prescaler=20000;//计数频率为1KHz
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV4;//定时器基准频率8MHz
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//计数溢出时触发中断
TIM_ARRPreloadConfig(TIM2, DISABLE);
TIM_Cmd(TIM2,ENABLE);
}
//-----------------------------------------------------
void init_timer2_sub2()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
///-----------------------------------------------------
void moto_task_init()
{
init_MotorStepCount();
init_timer2_sub0();
init_timer2_sub1();
init_timer2_sub2();
}
//-----------------------------------------------------
void moto_task(void)
{
moto_task_init();
while(1)
{
}
}
|