stm32f103测脉冲数用于带霍尔编码器的电机测速

    在使用带霍尔编码器的电机时,需要捕获脉冲,来达到测电机的转速的目的,捕获脉冲的方式有很多,我刚开始选用的是定时器捕获,后因需要测度多个电机的速度,STM32F103的定时器感觉不够用,所以选择EXTI以达到捕获高电平的目的,再用TIM定时器中断计时。
   以下为EXTI配置
/* Data defien ---------------------------------------------------------------------------------------*/
uint16_t motor_1_At = 0;	//电机A相的脉冲数
uint16_t motor_1_Bt = 0;   //电机B相的脉冲数
uint16_t speed_motor_1;         //电机的转速

/***
  * @name speed_measure()
  * @brief
  * @param NONE
  * @retval NONE
  */
void speed_measure_extix(void) 
{
     
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOD, ENABLE);     //使能PORTA,PORTE时钟 
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                  //motor_1_A-->PG9
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(GPIOG, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                  //motor_1_B-->PD8
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    
   
    
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOG,GPIO_PinSource9);
    EXTI_InitStructure.EXTI_Line = EXTI_Line9;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource8);
    EXTI_InitStructure.EXTI_Line = EXTI_Line8;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0X00;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);    
}

void EXTI9_5_IRQHandler(void)
{
     
    if(EXTI_GetITStatus(EXTI_Line8)!=RESET)//判断某个线上的中断是否发生
    {
         
    motor_1_Bt++;
    EXTI_ClearITPendingBit(EXTI_Line8);
    }
    if(EXTI_GetITStatus(EXTI_Line9)!=RESET)//判断某个线上的中断是否发生
    {
         
    motor_1_At++;
    EXTI_ClearITPendingBit(EXTI_Line9);
    }
    
}

因为unsigned char类型整数表示的范围为: 0~2^8-1,所以需要用定时器进行置0,以下为代码:

/***
  * @name speed_measure_time()
  * @brief  TIM2
  * @param NONE
  * @retval NONE
  */
void speed_measure_time(u16 arr, u16 psc)
{
     
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    
    TIM_TimeBaseStructure.TIM_Period = arr;
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    TIM_Cmd(TIM2, ENABLE);
    
}

void TIM2_IRQHandler(void)
{
     
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
     
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        speed_motor_1 = (motor_1_At + motor_1_Bt);
        motor_1_At = 0;
        motor_1_Bt = 0;
        
    }
}

代码中的speed_motor_1为设置时间内的电机A相与B相的脉冲数之和。
定时器时间的设置可以通过(u16 arr, u16 psc)实现,附上计算公式
T =[ (arr+1) + (psc+1)]/72000000

使用时直接调用这两个函数:

void speed_measure_extix(void)void speed_measure_time(u16 arr, u16 psc)

仅为个人拙见…如有不当请指正。谢谢!!

若有疑问,欢迎私信提问

链接:https://pan.baidu.com/s/1iCFA-xKWU6-fxbiv-TsR5Q
提取码:zqb3
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享

你可能感兴趣的:(stm32,单片机,嵌入式)