本帖最后由 whoyzf 于 2015-4-1 15:31 编辑
你的代码中貌似没有对磁力计的输出值进行补码。为了验证正确性
我改了你的代码,让其二进制输出。
signed short int mag[3];
unsigned char tmp[7], data_write[1];
tmp[6]=0x00;
data_write[0]=0x01;
i2cread(Compass_Addr, Compass_ST1, 1, tmp+6);
if(tmp[6] == 1)
{
i2cread(Compass_Addr, Compass_HXL, 6, tmp);
mag[0] = (((signed short int)tmp[1]) << 8) | tmp[0];
mag[1] = (((signed short int)tmp[3]) << 8) | tmp[2];
mag[2] = (((signed short int)tmp[5]) << 8) | tmp[4];
// mag[0] = ((long)mag[0] * mag_sens_adj_val[0]) >> 8; //ÁéÃô¶Èµ÷Õû
// mag[1] = ((long)mag[1] * mag_sens_adj_val[1]) >> 8;
// mag[2] = ((long)mag[2] * mag_sens_adj_val[2]) >> 8;
init_mx =(float)mag[1]; //ת»»×ø±êÖá
init_my =(float)mag[0];
init_mz =(float)-mag[2];
i2cwrite(Compass_Addr, Compass_CNTL, 1, data_write); //¿ªÆôcompass£ºsingle measurement mode
}
printf("my:%d(0x%02x%02x) mx:%d(0x%02x%02x) mz:%d(0x%02x%02x) \r\n",mag[0],tmp[1],tmp[0],mag[1],tmp[3],tmp[2],mag[2],tmp[5],tmp[4]) ;
结果发现
my:-495(0xfe11) mx:225(0x00e1) mz:-727(0xfd29)
my:-501(0xfe0b) mx:219(0x00db) mz:-729(0xfd27)
my:-491(0xfe15) mx:221(0x00dd) mz:-729(0xfd27)
my:-496(0xfe10) mx:216(0x00d8) mz:-726(0xfd2a)
my:-495(0xfe11) mx:225(0x00e1) mz:-729(0xfd27)
my:-487(0xfe19) mx:223(0x00df) mz:-727(0xfd29)
my:-496(0xfe10) mx:218(0x00da) mz:-728(0xfd28)
my:-492(0xfe14) mx:220(0x00dc) mz:-726(0xfd2a)
还真的符合,不知道具体原理。可以在计算机中的存储就是以那个补码形式存在吧 |