编码器类型是有空闲状态,当没有旋转的时候,AB相都为高电平,而且当旋转旋钮后无动作,AB相会恢复到高电平,所以方向的一个方向的完整波形为(假设A相先,B相后):
- A相高电平 — A相低电平— 持续低电平 — A相高电平
- B相高电平 ----------B相低电平 — 持续低电平 — B相高电平
然后看一下编程识别方向的串口演示视频:
编码器(有空闲状态)编程识别方向串口输出演示
01 - 编码器旋转波形
这种编码不能只判断低电平的先后顺序,因为一个方向的完整波形包括其恢复到高电平的状态,比如下图,黄蓝线为A相,紫线为B相
,按照编码器的规格书说明:
若电平的变化,A相比B相快(一定先是下降沿,然后上升沿)则为顺时针旋转
若电平的变化,B相比A相快(一定先是下降沿,然后上升沿)则为顺时针旋转
02 - 编程思路
直接参考。【应用C】C语言实现基于中断方式的旋转编码器方向识别(编码器没有空闲状态),里面有详细的说明,这里只是多加一个上升沿的判断
03 - 源代码
此代码并没有加入滤波,可以参考上一篇自行添加
Encoder.h
#ifndef _ENCODER_H_
typedef enum {
DIR_IDLE,
DIR_INC,
DIR_DEC,
} Dir_t;
extern void Encoder_Init(void);
extern void Encoder_A_ISR(void);
extern void Encoder_B_ISR(void);
extern void Encoder_EnableInterrupt(void);
extern void Encoder_DisableInterrupt(void);
#define _ENCODER_H_
Encoder.c
#include "Encoder.h"
static Dir_t Encoder_Dir;
static uint8_t Encoder_A_IsFallingEdge;
static uint8_t Encoder_B_IsFallingEdge;
static uint8_t Encoder_A_IsRisingEdge;
static uint8_t Encoder_B_IsRisingEdge;
static uint8_t Encoder_Ready;
void Encoder_Init(void) {
Encoder_Dir = DIR_IDLE;
Encoder_DirBuff = DIR_IDLE;
Encoder_A_IsFallingEdge = 0;
Encoder_B_IsFallingEdge = 0;
Encoder_A_IsRisingEdge = 0;
Encoder_B_IsRisingEdge = 0;
}
void Encoder_EnableInterrupt(void)
{
ExternalInterrupt0_On();
ExternalInterrupt2_On();
Encoder_Ready = 1;
}
void Encoder_DisableInterrupt(void)
{
ExternalInterrupt0_Off();
ExternalInterrupt2_Off();
Encoder_Ready = 0;
}
#define Encoder_Clear_FallingEdge() {Encoder_B_IsFallingEdge = 0;Encoder_A_IsFallingEdge = 0;}
#define Encoder_Clear_RisingEdge() {Encoder_A_IsRisingEdge = 0;Encoder_B_IsRisingEdge = 0;}
#define Encoder_Clear_Edge() {Encoder_Clear_FallingEdge();Encoder_Clear_RisingEdge();Encoder_DirBuff = DIR_IDLE;}
#define Encoder_SetDirBuff_Inc() {Encoder_DirBuff = DIR_INC;}
#define Encoder_SetDirBuff_Dec() {Encoder_DirBuff = DIR_DEC;}
#define Encoder_SetDir_Inc() {Encoder_Dir = DIR_INC;}
#define Encoder_SetDir_Dec() {Encoder_Dir = DIR_DEC;}
void Encoder_A_Falling_ISR(void)
{
if (Encoder_Ready) {
if (Encoder_B_IsFallingEdge) {
Encoder_Ready = 0;
//Wait for Rising, the result will be confirm
Encoder_SetDirBuff_Inc();
} else {
Encoder_A_IsFallingEdge = 1;
}
}
}
void Encoder_A_Rising_ISR(void)
{
//Wait for completion of a cycle
if (Encoder_B_IsRisingEdge) {
Encoder_Ready = 1;
if (Encoder_DirBuff == DIR_INC) {
Encoder_SetDir_Inc();
}
Encoder_Clear_Edge();
} else {
if (!Encoder_Ready) {
Encoder_A_IsRisingEdge = 1;
}
}
}
void Encoder_A_ISR(void) {
if (IO_Hall == 0) {
Encoder_A_Falling_ISR();
} else {
Encoder_A_Rising_ISR();
}
}
void Encoder_B_Falling_ISR(void)
{
if (Encoder_Ready) {
if (Encoder_A_IsFallingEdge) {
Encoder_Ready = 0;
//Wait for Rising, the result will be confirm
Encoder_SetDirBuff_Dec();
} else {
Encoder_B_IsFallingEdge = 1;
}
}
}
void Encoder_B_Rising_ISR(void)
{
//Wait for completion of a cycle
if (Encoder_A_IsRisingEdge) {
Encoder_Ready = 1;
if (Encoder_DirBuff == DIR_DEC) {
Encoder_SetDir_Dec();
}
Encoder_Clear_Edge();
} else {
if (!Encoder_Ready) {
Encoder_B_IsRisingEdge = 1;
}
}
}
void Encoder_B_ISR(void) {
if (IO_Zero == 0) {
Encoder_B_Falling_ISR();
} else {
Encoder_B_Rising_ISR();
}
}
Dir_t Encoder_GetDir(void)
{
Dir_t Dir_Buff = Encoder_Dir;
if (Dir_Buff != DIR_IDLE) {
Encoder_Dir = DIR_IDLE;
Encoder_Clear_Edge();
}
return Dir_Buff;
}