关于使用欧姆龙的绝对值旋转编码器 E6CP-AG5C 输出不稳定

2022-02-078383
AI 快速预览详细 收起
本文讨论了使用欧姆龙绝对值旋转编码器 E6CP-AG5C 时遇到的输出不稳定问题。作者详细描述了连接方式和代码实现,并提供了具体的故障排查方法。通过调整信号反转和输入上拉配置,解决了旋转过程中偶尔出现0的问题。文章还提供了详细的接脚配置和转换格雷码的代码示例,帮助读者更好地理解和解决问题。
本人有一点点编程经验, 电子零件基本是个新手, 想学习使用欧姆龙的绝对值旋转编码器 E6CP-AG5C, 编码器的资料如下:
https://www.fa.omron.com.cn/products/family/494/specification.html
我手上的是1024解释度的版本, 所以它除了两条接电源线之外(外接了八颗1.2V AA电池的电源盒), 其余十条线则输出由个位至十位二进数的格雷码(Grey code), Arduino接脚与旋转编码器接线用接线器直接接上中间并没加上其他电子零件, 由Pin2顺序接到Pin11, 程式码如下:


  1. #define PIN_0 (2) //2^0
  2. #define PIN_1 (3) //2^1
  3. #define PIN_2 (4) //2^2
  4. #define PIN_3 (5) //2^3
  5. #define PIN_4 (6) //2^4
  6. #define PIN_5 (7) //2^5
  7. #define PIN_6 (8) //2^6
  8. #define PIN_7 (9) //2^7
  9. #define PIN_8 (10) //2^8
  10. #define PIN_9 (11) //2^9
  11. #define TO_INVERT_SIG (1) //In my case, I need to invert the signal to get the correct result.
  12. const int offset = 0; //angle offset
  13. const int numPins = 10;
  14. int8_t pinArray[numPins] = {
  15.   PIN_0,
  16.   PIN_1,
  17.   PIN_2,
  18.   PIN_3,
  19.   PIN_4,
  20.   PIN_5,
  21.   PIN_6,
  22.   PIN_7,
  23.   PIN_8,
  24.   PIN_9
  25. };
  26. void setup() {
  27.   // put your setup code here, to run once:
  28.   Serial.begin(9600);
  29.   for(int i = 0;i < numPins;i++) {
  30.     pinMode(pinArray[i], INPUT_PULLUP);
  31.   }
  32. }
  33. void loop() {
  34.   // put your main code here, to run repeatedly:
  35.   Serial.println(ConvertGrayCode(), DEC);
  36.   delay(50);
  37. }
  38. int ConvertGrayCode() {
  39.   int finalResult = 0;
  40.   int val = 0;
  41.   int curXORVal = 0;
  42.   int i = numPins - 1; //start from MSB
  43.   //assign first value
  44.   #if TO_INVERT_SIG == 1
  45.   val = 1 - digitalRead(pinArray[i]);
  46.   #else
  47.   val = digitalRead(pinArray[i]);
  48.   #endif
  49.   
  50.   finalResult = ((val & 1) << i);
  51.   for(i = numPins - 2;i >= 0;i--) {
  52.     #if TO_INVERT_SIG == 1
  53.     val = 1 - digitalRead(pinArray[i]);
  54.     #else
  55.     val = digitalRead(pinArray[i]);
  56.     #endif
  57.     curXORVal = ((finalResult >> (i + 1)) & 1) ^ (val & 1);
  58.     finalResult |= ((curXORVal & 1) << i);
  59.   }
  60.   
  61.   return finalResult - offset;
  62. }
复制代码


大致使用没问题, 但旋转途中会突然出现 0 的结果:


关于使用欧姆龙的绝对值旋转编码器 E6CP-AG5C 输出不稳定图1

关于使用欧姆龙的绝对值旋转编码器 E6CP-AG5C 输出不稳定图2

求问各位高手这输出正常吗, 还是我连接的方式有问题?

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -