STM32单片机对正交编码器的驱动

发布时间:2023-06-20  

STM32正交编码器驱动,引入(突变)带进位的位置环和速度环


#include “stm32f10x.h”

#include “stm32f10x_encoder.h”

#include “sys.h”

#include “usart.h”

#include “led.h”

#define COUNTER_RESET(u16)0

#define ICx_FILTER(u8) 0 // 6《-》 670nsec

#define TIMx_PRE_EMPTION_PRIORITY 1

#define TIMx_SUB_PRIORITY 0

//#define MAX_COUNT ENCODER_TIM_PERIOD/2

volatile s16 Right_hEncoder_Timer_Overflow; //编码器计数值溢出标志

volatile s16 Right_hEncoder_Timer_Overflow_High;//编码器计数值溢出标志高位

volatile s16 Right_hRot_Speed;//当前的速度

volatile s16 Right_hRot_Acceleration;//当前的加速度

s32 Right_CurrentCount = 0;//编码器当前的总计数值

s32 Right_CurrentCount_high = 0;//编码器当前的总计数值高位计算公式,注意结果可能超过32位的表示范围Left_CurrentCount_high*S32_MAX+Left_CurrentCount

volatile s16 Left_hEncoder_Timer_Overflow;

volatile s16 Left_hEncoder_Timer_Overflow_High;

volatile s16 Left_hRot_Speed;

volatile s16 Left_hRot_Acceleration;

s32 Left_CurrentCount = 0;

s32 Left_CurrentCount_high = 0;

void ENC_Right_Init(void)

{

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

TIM_ICInitTypeDef TIM_ICInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//A7接靠近红色的编码器输入一端

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

TIM_DeInit(ENCODER_Right_TIMER);

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Prescaler = 0x0;// No prescaling

TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(ENCODER_Right_TIMER, &TIM_TimeBaseStructure);

//TIMx_SMCR 寄存器中的 SMS=011 = TIM_EncoderMode_TI12;

TIM_EncoderInterfaceConfig(ENCODER_Right_TIMER, TIM_EncoderMode_TI12,

TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //ICx 不反相输入捕获在上升沿

TIM_ICStructInit(&TIM_ICInitStructure); //TIM_Channel_1 TIM_ICPSC_DIV1 TIM_ICPolarity_Rising

TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;//输入滤波 0110:采样频率fSAMPLING=fDTS/4, N=6

TIM_ICInit(ENCODER_Right_TIMER, &TIM_ICInitStructure);

// Clear all pending interrupts

TIM_ClearFlag(ENCODER_Right_TIMER, TIM_FLAG_Update);

TIM_ITConfig(ENCODER_Right_TIMER, TIM_IT_Update, ENABLE);

//Reset counter

ENCODER_Right_TIMER-》CNT = COUNTER_RESET;

TIM_Cmd(ENCODER_Right_TIMER, ENABLE);

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

Right_hEncoder_Timer_Overflow = 0;

Right_hEncoder_Timer_Overflow_High = 0;

Right_hRot_Speed = 0;

Right_ENC_Get_Electrical_Angle(); //第一次不要这个数据!!lastCount

}

void ENC_Left_Init(void)

{

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

TIM_ICInitTypeDef TIM_ICInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;//A7接靠近红色的编码器输入一端

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY+1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

TIM_DeInit(ENCODER_Left_TIMER);

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Prescaler = 0x0;// No prescaling

TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(ENCODER_Left_TIMER, &TIM_TimeBaseStructure);

//TIMx_SMCR 寄存器中的 SMS=011 = TIM_EncoderMode_TI12;

TIM_EncoderInterfaceConfig(ENCODER_Left_TIMER, TIM_EncoderMode_TI12,

TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //ICx 不反相输入捕获在上升沿

TIM_ICStructInit(&TIM_ICInitStructure); //TIM_Channel_1 TIM_ICPSC_DIV1 TIM_ICPolarity_Rising

TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;//输入滤波 0110:采样频率fSAMPLING=fDTS/4, N=0

TIM_ICInit(ENCODER_Left_TIMER, &TIM_ICInitStructure);

// Clear all pending interrupts

TIM_ClearFlag(ENCODER_Left_TIMER, TIM_FLAG_Update);

TIM_ITConfig(ENCODER_Left_TIMER, TIM_IT_Update, ENABLE);

//Reset counter

ENCODER_Left_TIMER-》CNT = COUNTER_RESET;

TIM_Cmd(ENCODER_Left_TIMER, ENABLE);

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

__nop();

Left_hEncoder_Timer_Overflow = 0;

Left_hEncoder_Timer_Overflow_High = 0;

Left_hRot_Speed = 0;

Left_ENC_Get_Electrical_Angle(); //第一次不要这个数据!!lastCount

}

s16 Right_ENC_Get_Electrical_Angle(void)

{

static u16lastCount = 0;

u16 curCount ;

s16 dAngle ;

static s16 Last_Overflow = 0;

curCount = ENCODER_Right_TIMER-》CNT;

dAngle = curCount - lastCount;

//ensure |Right_hEncoder_Timer_Overflow - Last_Overflow|《=1

if(Right_hEncoder_Timer_Overflow != Last_Overflow)

{

if ( (ENCODER_Right_TIMER-》CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)//反转

{// encoder timer down-counting

dAngle -= ENCODER_TIM_PERIOD;

}

else

{//encoder timer up-counting

dAngle += ENCODER_TIM_PERIOD;

}

}

lastCount = curCount;

Last_Overflow = Right_hEncoder_Timer_Overflow;

return (s16)dAngle;

}

s16 Left_ENC_Get_Electrical_Angle(void)

{

static u16lastCount = 0;

u16 curCount ;

s16 dAngle ;

static s16 Last_Overflow = 0;

curCount = ENCODER_Left_TIMER-》CNT;

dAngle = curCount - lastCount;

//ensure |Left_hEncoder_Timer_Overflow - Last_Overflow|《=1

if(Left_hEncoder_Timer_Overflow != Last_Overflow)

{

if ( (ENCODER_Left_TIMER-》CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)//反转

{// encoder timer down-counting

dAngle -= ENCODER_TIM_PERIOD;

}

else

{//encoder timer up-counting

dAngle += ENCODER_TIM_PERIOD;

}

}

lastCount = curCount;

Last_Overflow = Left_hEncoder_Timer_Overflow;

return (s16)dAngle;

}

void TIM2_IRQHandler(void)

{

TIM_ClearFlag(ENCODER_Left_TIMER, TIM_FLAG_Update);

if ((Left_hEncoder_Timer_Overflow != S16_MAX )&&(Left_hEncoder_Timer_Overflow != S16_MIN ))

{

if ( (ENCODER_Left_TIMER-》CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)//反转

{// encoder timer down-counting

Left_hEncoder_Timer_Overflow--;

}

else

{//encoder timer up-counting

Left_hEncoder_Timer_Overflow++;

}

}

if(Left_hEncoder_Timer_Overflow == S16_MAX)

{

Left_hEncoder_Timer_Overflow = 0;

Left_hEncoder_Timer_Overflow_High++;

}

if(Left_hEncoder_Timer_Overflow == S16_MIN )

{

Left_hEncoder_Timer_Overflow = 0;

Left_hEncoder_Timer_Overflow_High--;

}

}

void TIM3_IRQHandler(void)

{

TIM_ClearFlag(ENCODER_Right_TIMER, TIM_FLAG_Update);

if ((Right_hEncoder_Timer_Overflow != S16_MAX )&&(Right_hEncoder_Timer_Overflow != S16_MIN ))

{

if ( (ENCODER_Right_TIMER-》CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)//反转

{// encoder timer down-counting

Right_hEncoder_Timer_Overflow--;

}

else

{//encoder timer up-counting

Right_hEncoder_Timer_Overflow++;

}

}

if(Right_hEncoder_Timer_Overflow == S16_MAX)

{

Right_hEncoder_Timer_Overflow = 0;

Right_hEncoder_Timer_Overflow_High++;

}

if(Right_hEncoder_Timer_Overflow == S16_MIN )

{

Right_hEncoder_Timer_Overflow = 0;

Right_hEncoder_Timer_Overflow_High--;

}


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>