STM32正交编码器测速

  • Post author:
  • Post category:其他



一、正交编码解码原理


参考链接:

https://blog.csdn.net/as480133937/article/details/98750922


二、解码思路


(1)方法一:使用定时器的输入捕获功能,配置好TIM_ICInitTypeDef即可获取一定时间内的脉冲个数,并可根据B相上升沿时A相所处的电平判断电机正转还是反转。中断类型配置为更新事件和上升沿触发,更新事件的时间由定时器配置的参数计算出,在更新事件中断中计算速度;在上升沿中断中计脉冲数。

void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);

配置示例代码:(后续添加注释)

#include "dcmotor.h"		
struct dc_motor DC_information;
char temp[10];

void dcmotor_Init(u16 per,u16 psc){ 
	TIM_ICInitTypeDef TIM_icinitStruct; 
	TIM_TimeBaseInitTypeDef TIM_InitStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;
 	GPIO_InitTypeDef GPIO_InitStructure; 

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

	TIM_icinitStruct.TIM_Channel = TIM_Channel_1;
	TIM_icinitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM_icinitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; 
	TIM_icinitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM_icinitStruct.TIM_ICFilter = 0x00; 
	TIM_ICInit(TIM3,&TIM_icinitStruct);

	TIM_InitStructure.TIM_Period = per;
	TIM_InitStructure.TIM_Prescaler = psc;
	TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM3,&TIM_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
	TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1,ENABLE);
	TIM_Cmd(TIM3,ENABLE);

	 DC_information.dc_angle = 0;
	 DC_information.dc_speed = 0;
}

void TIM3_IRQHandler(){
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)){
		DC_information.dc_speed = DC_information.dc_angle*PI/256/64;
		DC_information.dc_angle = 0;
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
	}

	if(TIM_GetITStatus(TIM3,TIM_IT_CC1)){
		if(DC_information.dc_angle<99999&&DC_A == 1){
			DC_information.dc_angle ++;
		}else if(DC_information.dc_angle>=99999){
			DC_information.dc_angle = 0;
		}
		TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
	}	
}
	

(2)方法二:使用定时器的编码器模式

 TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);	//编码器模式

编码器模式下,中断事件的产生条件为计数达到,TIM_InitStructure.TIM_Period的值。需要注意的是,若计数模式选为,TIM_EncoderMode_TI12,则对正交编码器的A相和B相的上升和下降沿都会计数,实际脉冲数应为TIM_InitStructure.TIM_Period的值除以4;而其他的两种模式只对单相的上升和下降沿计数,实际脉冲数应为TIM_InitStructure.TIM_Period的值除以2。


编码器模式下未解决问题

:暂时不知道中断的周期如何确定,尝试过再开一个定时器,设定中断周期,在这个定时器的中断事件中计算速度,编码器模式的定时器负责计脉冲,但是效果很差。(求解)

问题未解决,暂无代码。



版权声明:本文为weixin_44762713原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。