旋转编码器(rotary encoder)旋转方向的判断

     旋转编码器又称为轴编码器,它是一种将轴的角度位置或运动方向的信息转换为模拟或数字信号的电机械设备。有两种类型的编码器:绝对式的和增量式的。绝对式旋转编码器输出的是轴的精确位置,其被作为一种角度换能器。增量式旋转编码器输出的只是轴的大概旋转方向,但是不能确定其准确的角度位置信息。这一段内容参考于维基百科
     以下要讲的是一种增量式旋转编码器,如图1所示。图1来至于这里。以下的相关原理介绍以及源码等均参考与这里

 
图1.

     对于以上所示的旋转编码器,我们主要关注其并排的三个引脚,其中一个接地,另外两个我们标记为引脚A和引脚B。当转动旋转编码器的轴时引脚A和引脚B的电位的高低会发生变化,并且顺时针转动和逆时针转动时引脚A和引脚B的电位组合的序列有区别,我们可以根据这种差异来辨别旋转的方向。如图2和图3所示。

 
图2.
 
图3.

     从图4和图5可以看出当旋转编码器顺指针和逆时针旋转时脚A和引脚B的电位组合的序列是不同的。顺时针旋转时脚A和引脚B的电位组合的序列是 { { 1 , 1 } , { 0 , 1 } , { 0 , 0 } , { 1 , 0 } } \{\{1,1\},\{0,1\},\{0,0\},\{1,0\}\} {{1,1},{0,1},{0,0},{1,0}}序列的循环,逆时针旋转时脚A和引脚B的电位组合的序列是 { { 1 , 1 } , { 1 , 0 } , { 0 , 0 } , { 0 , 1 } } \{\{1,1\},\{1,0\},\{0,0\},\{0,1\}\} {{1,1},{1,0},{0,0},{0,1}}序列的循环。并且当引脚A从低电平变为高电平时(即引脚A从圆盘上的灰色区域到接触到圆盘上的白色区域时),在顺时针旋转的情况下引脚B为低电平(即此时引脚A和引脚B的电位不相同),在逆时针旋转的情况下引脚B为高电平(即此时引脚A和引脚B的电位相同)。我们可以利用这两种方式实现算法来辨别旋转编码器的旋转方向。实现算法见算法一和算法二。在实际使用时需要不断调用函数 g e t R o t a r y E n c o d e r D i r e c t i o n ( b o o l p i n A C u r r e n t S t a t u s , b o o l p i n B C u r r e n t S t a t u s ) getRotaryEncoderDirection(bool\quad pinACurrentStatus, bool\quad pinBCurrentStatus) getRotaryEncoderDirection(boolpinACurrentStatus,boolpinBCurrentStatus)来获取旋转编码器的旋转方向,其中参数 p i n A C u r r e n t S t a t u s pinACurrentStatus pinACurrentStatus p i n B C u r r e n t S t a t u s pinBCurrentStatus pinBCurrentStatus引脚A和引脚B的高低电平状态(1代表高电平,0代表低电平),引脚A和引脚B的高低电平状态可以通过GPIO口来获取。

 
图4.
 
图5.
//算法一
enum rotaryEncoderDirection { clockWise = 0, antiClockWise, stop };

enum rotaryEncoderDirection getRotaryEncoderDirection(bool pinACurrentStatus, bool pinBCurrentStatus)
{
	enum rotaryEncoderDirection currentDir = stop;
	static bool pinALastStatus = 0;
	static bool pinBLastStatus = 0; 
	static int clockWiseCounter = 0;
	static int counterClockWiseCounter = 0;

	if (pinACurrentStatus != pinALastStatus || pinBCurrentStatus != pinBLastStatus)
	{
		if (pinALastStatus == 0 && pinBLastStatus == 0)
		{
			if (pinACurrentStatus == 0 && pinBCurrentStatus == 1)
			{
				counterClockWiseCounter++;
			}
			else if (pinACurrentStatus == 1 && pinBCurrentStatus == 0)
			{
				clockWiseCounter++;
			}
		}
		else if (pinALastStatus == 0 && pinBLastStatus == 1)
		{
			if (pinACurrentStatus == 1 && pinBCurrentStatus == 1)
			{
				counterClockWiseCounter++;
			}
			else if (pinACurrentStatus == 0 && pinBCurrentStatus == 0)
			{
				clockWiseCounter++;
			}
		}
		else if (pinALastStatus == 1 && pinBLastStatus == 1)
		{
			if (pinACurrentStatus == 1 && pinBCurrentStatus == 0)
			{
				counterClockWiseCounter++;
			}
			else if (pinACurrentStatus == 0 && pinBCurrentStatus == 1)
			{
				clockWiseCounter++;
			}
		}
		else if (pinALastStatus == 1 && pinBLastStatus == 0)
		{
			if (pinACurrentStatus == 0 && pinBCurrentStatus == 0)
			{
				counterClockWiseCounter++;
			}
			else if (pinACurrentStatus == 1 && pinBCurrentStatus == 1)
			{
				clockWiseCounter++;
			}
		}
		pinALastStatus = pinACurrentStatus;
		pinBLastStatus= pinBCurrentStatus;

		if (clockWiseCounter >= 2)
		{
			clockWiseCounter = 0;
			counterClockWiseCounter=0;
			currentDir = clockWise;
		}
		else if (counterClockWiseCounter >= 2)
		{
			counterClockWiseCounter = 0;
			clockWiseCounter = 0;
			currentDir = antiClockWise;
		}
	}

	return currentDir;
}
//算法二
enum rotaryEncoderDirection {clockWise=0, antiClockWise,stop};

enum rotaryEncoderDirection getRotaryEncoderDirection(bool pinACurrentStatus, bool pinBCurrentStatus)
{
	enum rotaryEncoderDirection currentDir = stop;
	static bool pinALastStatus = 0;
	static int clockWiseRotaryEncoderCounter=0;
	static int	antiClockWiseRotaryEncoderCounter=0;

	// If last and current state of pinA are different, then pulse occurred
	// React to only 1 state change to avoid double count
	if (pinACurrentStatus != pinALastStatus && pinACurrentStatus == 1) 
	{
		// If the pinB state is different than the pinA state then
		// the encoder is rotating CW 
		if (pinBCurrentStatus != pinACurrentStatus) 
		{
			clockWiseRotaryEncoderCounter++;
		}
		else 
		{
			// Encoder is rotating CCW 
			antiClockWiseRotaryEncoderCounter++;
		}
	}

	// Remember last pinA status
	pinALastStatus = pinACurrentStatus;

	if(clockWiseRotaryEncoderCounter >=4)
	{
		clockWiseRotaryEncoderCounter = 0;
		antiClockWiseRotaryEncoderCounter = 0;
		currentDir = clockWise;
	}
	else if (antiClockWiseRotaryEncoderCounter>=4)
	{
		clockWiseRotaryEncoderCounter = 0;
		antiClockWiseRotaryEncoderCounter = 0;
		currentDir = antiClockWise;
	}
	return currentDir;
}
  • 20
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
旋转编码器Rotary Encoder)是一种用于检测旋转方向和转动距离的装置,它通常由两个输出信号相位差90度的正交脉冲信号组成。在Verilog语言中,我们可以使用状态机来实现旋转编码器的功能。 下面是一个简单的Verilog代码,实现了一个旋转编码器的状态机: ```verilog module rotary_encoder ( input clk, input a, input b, output reg direction, output reg [7:0] count ); reg [1:0] state; always @(posedge clk) begin case (state) 2'b00: if (a == 0 && b == 1) state <= 2'b01; else if (a == 1 && b == 0) state <= 2'b10; 2'b01: if (a == 1 && b == 1) begin direction <= 1; count <= count + 1; state <= 2'b00; end else state <= 2'b11; 2'b10: if (a == 1 && b == 1) begin direction <= 0; count <= count - 1; state <= 2'b00; end else state <= 2'b11; 2'b11: state <= 2'b11; endcase end endmodule ``` 在这个例子中,我们声明了一个名为rotary_encoder的模块,它有三个输入信号(clk、a、b),两个输出信号(direction、count)。其中,clk是时钟信号,a和b是旋转编码器的两个输出信号,direction表示旋转方向,count表示旋转距离。 我们使用一个2位的状态寄存器(state)来实现旋转编码器的状态机。在每个时钟上升沿时,根据当前的状态和a、b的值,更新状态寄存器、direction和count的值。具体地说: 1. 当状态为00时,如果a为0并且b为1,则状态变为01;如果a为1并且b为0,则状态变为10。 2. 当状态为01时,如果a和b都为1,则方向为正,count加1,状态变为00;否则状态变为11。 3. 当状态为10时,如果a和b都为1,则方向为负,count减1,状态变为00;否则状态变为11。 4. 当状态为11时,保持不变。 通过这个代码,我们实现了一个简单的旋转编码器的功能。当旋转编码器旋转时,a和b的输出信号会发生变化,根据这些变化,我们可以检测旋转方向旋转距离,并将结果存储在direction和count中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqssss121dfd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值