saving I2C data into matrices

06 Sep 2019

hi,

I have a setup of 1 master and 2 slaves communicating in I2C. The data received from slave 1 is saved into array 'b' and slave 2 into array 'c', since array b and array c are the same size, I would like to make them into a matrics of 2 rows instead. It'd great to have some insight in how to do it. Here's my code using array b and array c

saving slave 1 data and slave 2 data into array b and array c respectively

static float DAC[2], thermistor[2];
char b[2*sizeof(float)];
char c[2*sizeof(float)];

int main() {
        i2c.read(slave_addr[0],(char*) c,sizeof(c));
        DAC[0] = *(float*)(c);  // convert bytes back to float
        thermistor[0] = *(float*)(c+sizeof(float));
        i2c.read(slave_addr[1],(char*) b,sizeof(b));
        DAC[1] = *(float*)(b);  // convert bytes back to float
        thermistor[1] = *(float*)(b+sizeof(float));
        printf("DAC0:%.6f  ,", DAC[0]);
        printf("%.6f  ,", DAC[1]);
        printf("%.6f  ,", thermistor[1]);
        printf("%.6f \n", thermistor[0]);
}

my try to save data into a matrics instead, but seems like it's not working

save data into matrics

static float DAC[2], thermistor[2];
char b[2][2*sizeof(float)];

int main() {
        i2c.read(slave_addr[0],(char*) b[0][0],sizeof(b[0]));
        DAC[0] = *(float*)(b[0]);  // convert bytes back to float
        thermistor[0] = *(float*)(b[0]+sizeof(float));
        i2c.read(slave_addr[1][0],(char*) b,sizeof(b[1][0]));
        DAC[1] = *(float*)(b[1]);  // convert bytes back to float
        thermistor[1] = *(float*)(b[1]+sizeof(float));
        printf("DAC0:%.6f  ,", DAC[0]);
        printf("%.6f  ,", DAC[1]);
        printf("%.6f  ,", thermistor[1]);
        printf("%.6f \n", thermistor[0]);
}

the second question is I'm using 4 lines to print the data, if I make it one line or add any words before %f, the program only prints 0s.

code only print zeros

         //this will print all 0s
        //like this
       //DAC0:0.000000,   DAC1:0.000000,    thermistor0: 0.000000,     thermistor1:  0.000000
        printf("DAC0:%.6f,   DAC1: %.6f,    thermistor0: %.6f,     thermistor1:  %.6f", DAC[0], DAC[1], thermistor[0], thermistor[1]);

08 Sep 2019

Hi Conrad,

Thank you for your question!

I am confused a bit with your code for reading into the matricx. specifically in the two lines:

i2c.read(slave_addr[0],(char*) b[0][0],sizeof(b[0]));

If I understand correct, here you are trying to read into b[0][0], but b[0][0] is not an address, but the byte located in that location. I believe you should change this either to b[0] or &b[0][0] and it should work

and

i2c.read(slave_addr[1][0],(char*) b,sizeof(b[1][0]));

I believe you have a typo and you meant to put the [1][0] for b instead of slave_addr. In addition, you should also give the address of the location:

i2c.read(slave_addr[1], b[1],sizeof(b[1]));

Regards,

Mbed Support

Ron

10 Sep 2019

thank you, it works!

although I used pointer instead

code

char b[2][2*sizeof(float)];
while(1) {
        if(millis() - _time > 1000) {
            _time = millis();
            i2c.read(slave_addr[0],(char*) *(b + 1),sizeof(*b));
            DAC[0] = *(float*)*(b + 1);  // convert bytes back to float
            thermistor[0] = *(float*)(*(b + 1) + sizeof(float));
            i2c.read(slave_addr[1],(char*) b,sizeof(*b));
            DAC[1] = *(float*)(*b);  // convert bytes back to float
            thermistor[1] = *(float*)(*b + sizeof(float));
            //pc.printf("\r\n%f", DACout);
            printf("\r\n%f,%f,%f,%f,", DAC[0],DAC[1],thermistor[0],thermistor[1]);

        }
        //serial_read();
    }