旋转编码器又称为轴编码器,它是一种将轴的角度位置或运动方向的信息转换为模拟或数字信号的电机械设备。有两种类型的编码器:绝对式的和增量式的。绝对式旋转编码器输出的是轴的精确位置,其被作为一种角度换能器。增量式旋转编码器输出的只是轴的大概旋转方向,但是不能确定其准确的角度位置信息。这一段内容参考于维基百科。
     以下要讲的是一种增量式旋转编码器,如图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 countif (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 statuspinALastStatus = pinACurrentStatus;if(clockWiseRotaryEncoderCounter >=4){clockWiseRotaryEncoderCounter = 0;antiClockWiseRotaryEncoderCounter = 0;currentDir = clockWise;}else if (antiClockWiseRotaryEncoderCounter>=4){clockWiseRotaryEncoderCounter = 0;antiClockWiseRotaryEncoderCounter = 0;currentDir = antiClockWise;}return currentDir;
}

旋转编码器(rotary encoder)旋转方向的判断相关推荐

  1. STM32 编码器驱动/旋转编码器旋钮encoder

    本文已比较纯粹的方式介绍编码器和驱动的编写 编码器最少有两个输出信号,一种典型的结构如上图所示.AB是编码器的输出引脚.当触点和黄色的金属片接触的时候信号发生跳变沿,可以上上升沿也可以是下降沿,具体根 ...

  2. 11旋转编码器原理_旋转编码器的原理是什么?增量式编码器和绝对式编码器有什么区别?...

    先给出结论,最重要的区别在于:增量式编码器没有记忆,断电重启必须回到参考零位,才能找到需要的位置,而绝对式编码器,有记忆,断电重启不用回到零位,即可知道目标所在的位置. 接下来细说一下,主要包含如下的 ...

  3. 增量型旋转编码器和绝对值旋转编码器

    增量型旋转编码器轴的每圈转动,增量型编码器提供一定数量的脉冲.周期性的测量或者单位时间内的脉冲计数可以用来测量移动的速度.如果在一个参考点后面脉冲数被累加,计算值就代表了转动角度或行程的参数.双通道编 ...

  4. 编码器(rotary encoder)工作原理

  5. 传感器自学笔记第五章——旋转编码器

    作者:GWD 时间:2019.06.23 一:学习要点: 1.手册只讲了原理,连引脚定义也没有,模块上的引脚标号也不正确.好在旋转编码器是开关量类的传感器,解决办法如下,在三个信号输出引脚上分别接上L ...

  6. STM32入门笔记03_EXTI外部中断详解+案例:红外对射计数、旋转编码器计数

    EXTI外部中断 中断的相关概念 中断源: 可以引起中断的事件称为中断源 中断: 在主程序运行过程中,出现了特定的中断触发条件(中断源),使得CPU暂停当前正在运行的程序,转而去处理中断程序,处理完成 ...

  7. 台达编码器型号含义_台达光学式旋转编码器

    台达光学式旋转编码器 光学式旋转编码器 ( Rotary Optical Encoder ) 属于传感器的一种, 主要使用光电讯号转换输出轴上的机械几何位移转换成脉冲或数位 量的感测器,所以可以用于侦 ...

  8. 【Arduino使用旋转编码器模块】

    Arduino使用旋转编码器模块 前言 旋转编码器模块引脚 旋转编码器模块如何工作? 旋转编码器模块电路图 Arduino旋转编码器模块电路连接图 代码说明 完整代码 前言 旋转编码器是一种机电位置传 ...

  9. arduino编码器计数_ARDUINO旋转编码器

    旋转编码器 旋转编码器可通过旋转可以计数正方向和反方向转动过程中输出脉冲的次数,旋转计数不像电位计,这种 转动计数是没有限制的.配合旋转编码器上的按键,可以复位到初始状态,即从 0 开始计数. 工作原 ...

最新文章

  1. 采用Filter的方法解决Servlet的编码问题
  2. 文巾解题 19. 删除链表的倒数第 N 个结点
  3. php简单网站源码包含数组_PHP 数组
  4. SpringMVC学习08之SSM整合(三)
  5. 自研开源框架 Midway Serverless ,让前端提效 50% 背后的故事
  6. Java常用接口与类——Date、Calendar、DateFormat、TimeZone(日期相关)
  7. Apicloud_(问题)P54提示错误:Uncaught SyntaxError: Unexpected token ) at main.html : 117
  8. HaLow技术提升车载Wi-Fi质量 促进车联网发展
  9. 萌新的Python练习实例100例(二)根据企业的利润,计算企业的方法奖金
  10. php中页面静态化技术,在PHP中实现页面静态化的方法有哪些
  11. App Store、Steam、Google Play等出海应用、游戏如何收款到国内账户
  12. Allegro 使用技巧
  13. 主力用计算机吸筹,通达信主力吸筹指标
  14. 单模光纤最大传输距离为多少_单模光纤的最长传输距离有多远?单模,多模光纤有什么不同呢?...
  15. Python如何自动播放视频(XXT)
  16. ae 渲染 计算机内存不足,AE内存不足怎么办? After Effects内存不足【解决方法】...
  17. 如何提升自己的打字速度?
  18. 用python处理excel 数据分析_数据分析---用python处理excel
  19. 谈谈单元测试:为什么要进行单元测试?
  20. 游戏趣史:游戏引擎的发展史

热门文章

  1. 计算机页面添加文字水印在哪,怎么添加水印-Word小技巧-快速添加高大上的水印...
  2. 小孩子初次办身份证需携带什么资料?年龄有要求吗?
  3. 【mac】MacBook使用快捷键
  4. 安装SQLServer2019
  5. 一文教你看懂Fama-French三因子模型
  6. java图片处理---Javax.imageIO包的用法
  7. 华南师大计算机转专业,广西师范大学计算机科学与信息工程学院/软件学院转专业管理规定(试行)...
  8. 最新车载以太网解决方案,你知多少?
  9. HCIP之BGP的选路原则
  10. 税务会计实务【13】