For my data logger project I'm logging data to a SD card. Here I'm storing the data in binary format. I'm logging accelerometer and temp&humidity sensors. I could read data byte by byte. Hence to print ADXL data (two bytes), I combine each two byte data. Hence four bytes has to be combined to get temparature data. Unfortunately for temparature I failed to print actual data. There is some conversion problem. I'm looking some help regarding how to convert data to floats.
Code to read and print binary to uint16_t ( accelerometer data): working
while ((nr = fgetc(logFile)) != EOF){
pc.printf(" \r\n %d ",nr);
acc_con[i] = nr;
if (i == 1){
acc = (acc_con[1]<<8) | acc_con[0];
pc.printf(" \r\n %i ",acc);
i = 0;}
else i++;
}
Code to read and print binary to float ( temparature data): Not working
while ((nr = fgetc(logFile)) != EOF){
pc.printf(" \r\n %d ",nr);
humicon[i] = nr;
if (i == 3){
hum = (humicon[0] << 24) | (humicon[1] << 16) | (humicon[2] << 8) | humicon[3];
pc.printf(" \r\n %f ",hum);
i = 0;}
else i++;
}
The byte by byte raw data from variable nr is: 24, 58, 46, 66, and the output from variable hum is: 406466112.000000.
I could read humidity or temparature (floats) with fread. Unfortunatly to use fread I must know th no of bytes stored in the file. With below code I could print floats also. int main() {
int nr;
FILE *fp = fopen("/sd/PCE000.bin", "rb");
float read[1];
fread(read,sizeof(float),1,fp);
pc.printf("\r\n %f",read[0]);
fclose(fp);
}
I would like to know the formula for the conversion like (acc_con[1]<<8) | acc_con[0]. Thank you.
struct