定时器通过ab相采集,并测速

定时器5通道ab相采集功能配置,亲测可用;

void TIM5_Mode_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;      

    //PA0 ch1 A,PA1 ch2 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;         
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);                           

    TIM_DeInit(TIM5);
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = 60000;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);              

    TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
    TIM_ICStructInit(&TIM_ICInitStructure);
    TIM_ICInitStructure.TIM_ICFilter = 6;
    TIM_ICInit(TIM5, &TIM_ICInitStructure);

    TIM_ClearFlag(TIM5, TIM_FLAG_Update);
    TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
    //Reset counter
    //TIM5->CNT = 10;
        TIM_SetCounter(TIM5,0);

    TIM_Cmd(TIM5, ENABLE);
}

以上为ab相采集电机转数。

void Timer1_Init(u16 arr,u16 psc)  
{  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  NVIC_InitTypeDef NVIC_InitStructure;  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //时钟使能  
  TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期值  
  TIM_TimeBaseStructure.TIM_Prescaler =(psc-1);//设置预分频值  
  TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割  
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式  
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//重复计数设置设置重复计算次数  达到次数后出发 
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //参数初始化  
  TIM_ClearFlag(TIM1, TIM_FLAG_Update);//清中断标志位  
  TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);  //|TIM_IT_Trigger
  NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;    
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//先占优先级0级  
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;       //从优先级0级  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);   
  TIM_Cmd(TIM1, ENABLE);  //使能TIMx外设  
}  
void TIM1_UP_IRQHandler(void)   
{    
   // OSIntEnter();    
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
    {  
        TIM_ClearITPendingBit(TIM1, TIM_IT_Update)
              Brige.spt++;
              if(Brige.spt==0)
                {
                  TIM5->CNT = 0;
                }
                 if(Brige.spt>=9)
                {
                  Brige.speed=TIM_GetCounter(TIM5);
                    Brige.spt=0;
                }
    } 
   //OSIntExit();        
}  

该中断周期1ms
通过采集10ms内的ab相采集的脉冲数,可以求出速度

测试稳定   ,留个记忆,不足之处多多指教。

发布了3 篇原创文章 · 获赞 1 · 访问量 206

猜你喜欢

转载自blog.csdn.net/qq_21489487/article/details/103234634
今日推荐