本帖最后由 imfly89 于 2016-8-4 18:09 编辑
想利用TIM10,作中断,我贴出相关的代码,大家帮我看看
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ; //TIM10用做PWM更新中断,更新sinewave的占空比
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM10);
/*TIM10*/
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/42000000)-1; //预分频值,4分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 0xffff ;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM10,&TIM_TimeBaseStructure);
TIM_UpdateRequestConfig(TIM10,TIM_UpdateSource_Global);
TIM_UpdateDisableConfig(TIM10, ENABLE);
TIM_ITConfig(TIM10, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM10, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel= TIM1_UP_TIM10_IRQn; //更新中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 3;
NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
NVIC_Init( &NVIC_InitStructure );
下面的是中断处理
void TIM1_UP_TIM10_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM10, TIM_FLAG_Update) == SET)
{
TIM_ClearFlag(TIM10,TIM_FLAG_Update);
// send_char(0x01);
Delay1mS++;
if(Delay1mS >= 200)
{
LEDRun; //300mS闪1次灯
Delay1mS=0;
}
if (Required_Direction == CW)
{
if (Current_Direction == CW)
Phase += PhaseInc; // Increment Phase if CW to generate the
// sinewave only if both directions are equal
// If Required_Direction is CW (forward) POSITIVE voltage is applied
#ifdef PHASE_ADVANCE
SVM(ControlOutput, Phase + PhaseAdvance); // PhaseAdvance addition
// produces the sinewave
// phase shift
#else
SVM(ControlOutput, Phase);
#endif
}
else
{
if (Current_Direction == CCW)
Phase -= PhaseInc; // Decrement Phase if CCW to generate
// the sinewave only if both
// directions are equal
// If Required_Direction is CCW (reverse) NEGATIVE voltage is applied
#ifdef PHASE_ADVANCE
SVM(-(ControlOutput+1), Phase + PhaseAdvance);// PhaseAdvance addition
// produces the sinewave
// phase shift
#else
SVM(-(ControlOutput+1), Phase);
#endif
}
}
return;
如果进中断LED会闪烁的,我发现LED只处理了一次,也就是中断只进了一此,后面就不处理。应该是没有触发过。哪里有问题,望大家指出
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
中断里的 if(TIM_GetFlagStatus(TIM10, TIM_FLAG_Update) == SET) 改成 if(TIM_GetFlagStatus(TIM10, TIM_IT_Update) == SET),
TIM_ClearFlag(TIM10,TIM_FLAG_Update); 改成 TIM_ClearFlag(TIM10,TIM_IT_Update);
TIM_IT_Update是中断标志位,你没清中断标志位
一周热门 更多>