leofok 发表于 2022-2-7 13:28:19

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

本人有一点点编程经验, 电子零件基本是个新手, 想学习使用欧姆龙的绝对值旋转编码器 E6CP-AG5C, 编码器的资料如下:
https://www.fa.omron.com.cn/products/family/494/specification.html
我手上的是1024解释度的版本, 所以它除了两条接电源线之外(外接了八颗1.2V AA电池的电源盒), 其余十条线则输出由个位至十位二进数的格雷码(Grey code), Arduino接脚与旋转编码器接线用接线器直接接上中间并没加上其他电子零件, 由Pin2顺序接到Pin11, 程式码如下:


#define PIN_0 (2) //2^0
#define PIN_1 (3) //2^1
#define PIN_2 (4) //2^2
#define PIN_3 (5) //2^3
#define PIN_4 (6) //2^4
#define PIN_5 (7) //2^5
#define PIN_6 (8) //2^6
#define PIN_7 (9) //2^7
#define PIN_8 (10) //2^8
#define PIN_9 (11) //2^9
#define TO_INVERT_SIG (1) //In my case, I need to invert the signal to get the correct result.
const int offset = 0; //angle offset
const int numPins = 10;
int8_t pinArray = {
PIN_0,
PIN_1,
PIN_2,
PIN_3,
PIN_4,
PIN_5,
PIN_6,
PIN_7,
PIN_8,
PIN_9
};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

for(int i = 0;i < numPins;i++) {
    pinMode(pinArray, INPUT_PULLUP);
}
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(ConvertGrayCode(), DEC);
delay(50);
}

int ConvertGrayCode() {
int finalResult = 0;
int val = 0;
int curXORVal = 0;
int i = numPins - 1; //start from MSB

//assign first value
#if TO_INVERT_SIG == 1
val = 1 - digitalRead(pinArray);
#else
val = digitalRead(pinArray);
#endif

finalResult = ((val & 1) << i);
for(i = numPins - 2;i >= 0;i--) {
    #if TO_INVERT_SIG == 1
    val = 1 - digitalRead(pinArray);
    #else
    val = digitalRead(pinArray);
    #endif
    curXORVal = ((finalResult >> (i + 1)) & 1) ^ (val & 1);
    finalResult |= ((curXORVal & 1) << i);
}

return finalResult - offset;
}

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






求问各位高手这输出正常吗, 还是我连接的方式有问题?
页: [1]
查看完整版本: 关于使用欧姆龙的绝对值旋转编码器 E6CP-AG5C 输出不稳定