从覆盆子pi读

时间:2016-01-18 20:39:45

标签: c raspberry-pi2

我试图使用覆盆子pi读取陀螺仪数据并且遇到很多问题。我想我理解了很多代码并且编写得很好。

当将它打印到覆盆子屏幕上时出现问题,我得到的是不变数的常数。

所有内容都基于http://ozzmaker.com/berryimu/我知道代码已提供但我想学习并可能为我的目的进行优化。我正在使用https://www.adafruit.com/datasheets/L3GD20H.pdf陀螺仪。

读取值的程序是:

    #include </home/pi/gyroSetup.c>
    #include <stdio.h>

    int main() {
        setup();
        int i;
        int G[3];
        for(i = 0;i<=1000;i++){
            readGyro(G);
            printf("%d ", G[0]);
            printf("%d ", G[1]);
            printf("%d\n", G[2]);
        }
    }

gyroSetup.c在这里:

#include <stdio.h>
#include <stdint.h>
#include "fcntl.h"
#include "linux/i2c-dev.h"

#define Gyr_adress 0x6b
#define Gyr_ctrl1 0b1110100 
#define Gyr_ctrl4 0b00000000
#define Gyr_odr 0b00000000

int file;

void readBlock(uint8_t command, uint8_t size, uint8_t *data) {
    int result = i2c_smbus_read_i2c_block_data(file, command, size, data);
    if(result != size) {
        printf("Failed to read block from i2c!");
    {
}

void readGyro(int *T) {
     uint8_t block[6];
     if(ioctl(file,I2C_SLAVE, Gyr_adress) < 0) {
         printf("failed to select device!");
     }
     readBlock(0x80 | 0x28, sizeof(block), block);
     *T = (int16_t)(block[0] | block[1] << 8);
     *(T+1) = (int16_t)(block[2] | block[3] << 8);
     *(T+2) = (int16_t)(block[4] | block[4] << 8);
 }

void writeGyro(uint8_t location, uint8_t value, int file) {
    if(ioctl(file, I2C_SLAVE, Gyr_adress) < 0) {
         printf("Failed to select i2c device");
    }
    int result = i2c_smbus_write_byte_data(file, location, value);
    if(result == -1) {
        printf("Failed to write byte!");
    }
}

void setup(){ 
    __u16 block[I2C_SMBUS_BLOCK_MAX];
    file = open("/dev/i2c-1", O_RDWR);
    if( file < 0) {
         printf("Unable to open i2c bus!");
    }
    writeGyro(0x20, Gyr_ctrl1, file);
    writeGyro(0x23, Gyr_ctrl4, file);
    writeGyro(0x39, Gyr_odr, file);
}

我不知道为什么在读取值时不会改变所以请帮忙。编译时没有错误。

0 个答案:

没有答案