【应用C】C语言实现基于中断方式的旋转编码器方向识别(编码器有空闲状态)


  编码器类型是有空闲状态,当没有旋转的时候,AB相都为高电平,而且当旋转旋钮后无动作,AB相会恢复到高电平,所以方向的一个方向的完整波形为(假设A相先,B相后):

  1. A相高电平 — A相低电平— 持续低电平 — A相高电平
  2. 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;
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值