2位补码中的16位值,令人困惑

时间:2013-03-03 22:38:17

标签: c raspberry-pi gyroscope

我正在研究从陀螺仪读取数据,我正在恢复数据,但价值不如预期。我认为这是由于我在寻找解决方案后的编码错误,我想出了一篇帖子HERE,其中指出:

  

确保正确读取输出寄存器,数据是   2的补码中的16位值(即MSB是符号位,然后是15   值的位)

这对我来说非常混乱,我不确定我的编码是否正在读取其应有的值。我正在使用wiringPi I2C库将Arduino的现有代码转换为在Raspberry Pi上运行。我在下面有我的代码我希望有人能告诉我他们是否看到正确的尝试在2的赞美中读取16位值。 getGyroValues函数内部是唯一可以看到这种情况的地方。我的代码是否正确读取了值?

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23


int fd;
int x = 0;
int y = 0;
int z = 0;
int main (){



    fd = wiringPiI2CSetup(0x69); // I2C address of gyro
    wiringPiI2CWriteReg8(fd, CTRL_REG1, 0x1F); //Turn on all axes, disable power down
    wiringPiI2CWriteReg8(fd, CTRL_REG3, 0x08); //Enable control ready signal
    wiringPiI2CWriteReg8(fd, CTRL_REG4, 0x80); // Set scale (500 deg/sec)
    delay(100);                    // Wait to synchronize

void getGyroValues (){
    int MSB, LSB;

    LSB = wiringPiI2CReadReg16(fd, 0x28);
    MSB = wiringPiI2CReadReg16(fd, 0x29);
    x = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg16(fd, 0x2B);
    LSB = wiringPiI2CReadReg16(fd, 0x2A);
    y = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg16(fd, 0x2D);
    LSB = wiringPiI2CReadReg16(fd, 0x2C);
    z = ((MSB << 8) | LSB);
}

    for (int i=0;i<10;i++){
    getGyroValues();
    // In following Divinding by 114 reduces noise
    printf("Value of X is: %d\n", x / 114);
    printf("Value of Y is: %d\n", y / 114);
    printf("Value of Z is: %d\n", z / 114);
    int t = wiringPiI2CReadReg8(fd, 0x26);
    t = (t*1.8)+32;//convert Celcius to Fareinheit
    int a = wiringPiI2CReadReg16(fd,0x2B);
    int b = wiringPiI2CReadReg16(fd,0x2A);
    printf("Y_L equals: %d\n", a);
    printf("Y_H equals: %d\n", b);
    int c = wiringPiI2CReadReg16(fd,0x28);
    int d = wiringPiI2CReadReg16(fd,0x29);
    printf("X_L equals: %d\n", c);
    printf("X_H equals: %d\n", d);
    int e = wiringPiI2CReadReg16(fd,0x2C);
    int f = wiringPiI2CReadReg16(fd,0x2D);
    printf("Z_L equals: %d\n", e);
    printf("Z_H equals: %d\n", f); 

    printf("The temperature is: %d\n\n\n", t); 
    delay(500);
}
};

2 个答案:

答案 0 :(得分:1)

以下是如何轻松组合最高和最低有效字节,将2的补码16位整数的一半表示为int:

int Bytes2Short(unsigned char msb, unsigned char lsb)
{
  long t = msb * 0x100L + lsb;
  if (t >= 32768)
    t -= 65536;
  return (int)t;
}

答案 1 :(得分:1)

为了清晰起见,使用Alexey Frunze的方法,但这里是没有测试的版本(在某些情况下可以编译为更快的内联代码)。此模式也适用于其他大小的无符号位模式 - 例如,处理12位整数,使用0x0800,17位整数使用0x10000,依此类推。

我将它与一个小驱动程序main捆绑在一起以显示输出。

#include <stdio.h>

int comb(unsigned char msb, unsigned char lsb) {
        long t = ((long)msb << 8) | lsb;
        if (t >= 32768)
                t -= 65536;
        return t;
}

int comb2(unsigned char msb, unsigned char lsb) {
        unsigned long t = ((unsigned long)msb << 8) | lsb;
        t ^= 0x8000UL;
        return (int)((long)t - (long)0x8000);
}

int main(void) {
        printf("%6s %6s\n", "comb", "comb2");
        printf("%6d %6d\n", comb(0, 0), comb2(0, 0));
        printf("%6d %6d\n", comb(127, 255), comb2(127, 255));
        printf("%6d %6d\n", comb(128, 0), comb2(128, 0));
        printf("%6d %6d\n", comb(255, 255), comb2(255, 255));
        return 0;
}