简体   繁体   English

结构指针分配

[英]Struct pointer assignment

I'm trying to pass a struct to a function (by reference) and set several values of the struct in that function. 我正在尝试将一个结构传递给一个函数(通过引用),并在该函数中设置该结构的几个值。

Here's the struct: 这是结构:

struct Sensor
{
    BYTE accel_data[6];
    BYTE gyro_data[6];
    int ax;
    int ay;
    int az;
    int gx;
    int gy;
    int gz;
}; 

here's the declaration in main(): 这是main()中的声明:

struct Sensor s;

here's the pass to the function: 这是该函数的传递:

readAccel(&s);

readAccel function: readAccel函数:

void readAccel(struct Sensor* s)
{
int ax, ay, az;
// Read data from Accelerometer Chip

i2c_multiRead(ACCEL_READ_ADDR, ACCEL_DATA_ADDR, s->accel_data, 6);

ax = make16(s->accel_data[1],s->accel_data[0]);
ay = make16(s->accel_data[3],s->accel_data[2]);
az = make16(s->accel_data[5],s->accel_data[4]);

fprintf(COM_A,"x: %d\ty: %d\tz: %d\r\n", ax, ay, az);

s->ax = ax;
s->ay = ay;
s->az = az;

fprintf(COM_A,"x: %d\ty: %d\tz: %d\r\n", s->ax, s->ay, s->az);
}

for some reason, the three assignment lines at the bottom of readAccel() are not working. 由于某种原因,readAccel()底部的三个赋值行不起作用。 The first print statement gives the correct accelerometer values. 第一个打印语句给出了正确的加速度计值。 The second print statement gives the right value for x, but y is a junk value and z is always 0. 第二个打印语句为x给出正确的值,但是y是一个垃圾值,z始终为0。

This is slightly nonstandard c (ccs c compiler) running on a pic microchip. 这是在pic微芯片上运行的稍微不标准的c(ccs c编译器)。 The compiler has a few quirks (ints are 16 bit, all variables must be declared at the beginning of a function, etc), but I don't think it should be the reason why this isn't working (though I supposed it's possible). 编译器有一些怪癖(int是16位,必须在函数的开头声明所有变量,等等),但是我不认为这是不起作用的原因(尽管我认为这是可能的) )。

Thanks for taking your time to help! 感谢您抽出宝贵的时间来帮助您!

EDIT: 编辑:

here's the i2c_multiread function: 这是i2c_multiread函数:

void i2c_multiRead(char deviceAddrR, char registerAddr, BYTE data[], int numBytes)
{
    int x;
    i2c_start();
    i2c_write(deviceAddrR-0b00000001);
    i2c_write(registerAddr);
    i2c_start();
    i2c_write(deviceAddrR);
    for(x=0; x<numBytes-1; x++)
    {
        data[x]=i2c_read();
    }
    data[numBytes-1] = i2c_read(0); //NACK on last read
    i2c_stop();
}

void main ()
{
    struct Sensor s;
    int raw_accel_data[6];
    int i, data;
    char data_ready;
    fprintf(COM_A,"keyspan working \n\r");
    initAccel(&s);
    //initGyro(&s);
    fprintf(COM_A,"ALL SYSTEMS GO (accel initialized successfully)\n\r");

while(true) {

     //data_ready = i2c_singleRead(ACCEL_READ_ADDR,ACCEL_INT_MAP);
     //data_ready = data_ready >> 7;
    readAccel(&s);
    //readGyro(&s);
    //displayGyroData(&s);
    //displayGyroRawData(&s);

    delay_ms(100);
}
// Address of ITG (Gyro Chip)
//  Read:  0xD1
//  Write: 0xD0
}

Could it be that this is just a "feature" of your fprintf implementation? 难道这仅仅是fprintf实现的“功能”?

fprintf(COM_A,"x: %d\ty: %d\tz: %d\r\n", s->ax, s->ay, s->az);

The format string says that you will be providing 3 ints but you're providing 3 BYTEs instead. 格式字符串表示您将提供3个整数,但要提供3个BYTE。 It's possible that the fprintf implementation generates wrong address for the 2nd and 3rd argument. fprintf实现可能会为第二个和第三个参数生成错误的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM