编码器正反转
正转的时候信号线A先输出信号,信号线B后输出 A相超前B相90度 证明是正转
反转的时候信号线B先输出信号,信号线A后输出 B相超前A相90度 证明是反转
STM32编码器模式
三种模式
1.仅在TL1计数(A相)
2.仅在TL2计数(B相)
3.在TL1和TL2都计数(A相和B相都计数)
计数
一个脉冲信号周期完成4次跳变。
1时刻:TI2为低电平,TI1上升沿跳变,计数器向上/向下计数;
2时刻:TI1为高电平,TI2上升沿跳变,计数器仍然向上/向下计数;
3时刻:TI2为高电平,TI1下降沿跳变,计数器仍然向上/向下计数;
4时刻:TI1为低电平,TI2下降沿跳变,计数器仍然向上/向下计数。
接线方式
编码器代码
/**********
初始化TIM2 编码器1
**********/
void Encoder_TIM2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //初始化PA0|PA1
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct); //初始化TIM2
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising); //配置编码器模式
TIM_ICStructInit(&TIM_ICInitStruct); //初始化输入捕获
TIM_ICInitStruct.TIM_ICFilter = 10;
TIM_ICInit(TIM2,&TIM_ICInitStruct);
TIM_ClearFlag(TIM2,TIM_FLAG_Update); //清除溢出更新中断标志位
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //配置溢出更新中断标志位
TIM_SetCounter(TIM2,0); //清零TIM2计数值
TIM_Cmd(TIM2,ENABLE); //使能
}
/**********
初始化TIM4 编码器2
**********/
void Encoder_TIM4_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //初始化PB6|PB7
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOB,&GPIO_InitStruct);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct); //初始化TIM4
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStruct);
TIM_ICInitStruct.TIM_ICFilter = 10;
TIM_ICInit(TIM4,&TIM_ICInitStruct);
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4,ENABLE);
}
/***************
编码器速度读取函数
入口参数:TIMx定时器
***************/
int Read_Speed(int TIMx)
{
int value_1;
switch(TIMx)
{
case 2:value_1 = (short)TIM_GetCounter(TIM2);TIM_SetCounter(TIM2,0);break; //TIM2:采集TIM2编码器计数值并保存;TIM2清零;break。
case 4:value_1 = (short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4,0);break; //TIM4:同上
default:value_1 = 0;
}
return value_1;
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=0)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=0)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}
}