/****************************************************************************** / 函数功能:读取HMC5883的数据,单位国际化 / 修改日期:none / 输入参数:none / 输出参数:none / 使用说明:none ******************************************************************************/ void HMC5883L_Read(HMC5883L_DATA * ptResult,HMC5883L_DATA_Unit * hmc5883_calibration) { static uint8_t tmp[6]; float mx_sum = 0, my_sum = 0, mz_sum = 0; /**************************/ tmp[0]=Single_Read(HMC5883L_Addr,HMC5883L_HX_H);//OUT_X_L_A tmp[1]=Single_Read(HMC5883L_Addr,HMC5883L_HX_L);//OUT_X_H_A tmp[2]=Single_Read(HMC5883L_Addr,HMC5883L_HZ_H);//OUT_Z_L_A tmp[3]=Single_Read(HMC5883L_Addr,HMC5883L_HZ_L);//OUT_Z_H_A tmp[4]=Single_Read(HMC5883L_Addr,HMC5883L_HY_H);//OUT_Y_L_A tmp[5]=Single_Read(HMC5883L_Addr,HMC5883L_HY_L);//OUT_Y_H_A ptResult->hx = (int16_t)((tmp[0] << 8) | tmp[1]); ptResult->hy = (int16_t)((tmp[4] << 8) | tmp[5]); ptResult->hz = (int16_t)((tmp[2] << 8) | tmp[3]); /****************归0归1***************/ if(CMP_OFFSET_OK) { if(cal_cnt) { cal_cnt--; if(mx_min > ptResult->hx) mx_min = ptResult->hx; if(mx_max < ptResult->hx) mx_max = ptResult->hx; if(my_min > ptResult->hy) my_min = ptResult->hy; if(my_max < ptResult->hy) my_max = ptResult->hy; if(mz_min > ptResult->hz) mz_min = ptResult->hz; if(mz_max < ptResult->hz) mz_max = ptResult->hz; if(cal_cnt==0) { static uint8_t j = 0; hmc_offset.hx = (mx_min + mx_max)/2; hmc_offset.hy = (my_min + my_max)/2; hmc_offset.hz = (mz_min + mz_max)/2; mx_sum = mx_max - mx_min; my_sum = my_max - my_min; mz_sum = mz_max - mz_min; My_Gain = mx_sum/my_sum; Mz_Gain = mx_sum/mz_sum; CMP_OFFSET_OK = 0; EE_SAVE_CMP_OFFSET(); for(j=0;j<3;j++) {all_ON(); delay_ms(300);all_OFF();delay_ms(300);} } } } else { hmc5883_calibration->hx = (float)(ptResult->hx - hmc_offset.hx); hmc5883_calibration->hy = (float)((ptResult->hy - hmc_offset.hy) * My_Gain); hmc5883_calibration->hz = (float)((ptResult->hz - hmc_offset.hz) * Mz_Gain); } }
/*====================================================================================================*/ u8 InitMPU6050(void) { u8 ack; ack = Single_Read(MPU6050_ADDRESS, WHO_AM_I); if (!ack) return FALSE; Single_Write(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); //解除休眠状态 Single_Write(MPU6050_ADDRESS, SMPLRT_DIV, 0x07); Single_Write(MPU6050_ADDRESS, CONFIGL, MPU6050_DLPF); //低通滤波 Single_Write(MPU6050_ADDRESS, GYRO_CONFIG, MPU6050_GYRO_FS_1000); //陀螺仪量程 +-1000 Single_Write(MPU6050_ADDRESS, ACCEL_CONFIG, MPU6050_ACCEL_FS_4); //加速度量程 +-4G return TRUE; }
//***************************************************************** //初始化ADXL345,根据需要请参考pdf进行修改************************ int Init_ADXL345() { if(Single_Read(DEVICE_ID)==0XE5) { //读取器件ID Single_Write(DATA_FORMAT,0x2b); //测量范围,正负16g,13位模式 Single_Write(BW_RATE, 0x0a); //速率设定为12.5 Single_Write(POWER_CTL, 0x28); //选择电源模式 Single_Write(INT_ENABLE, 0x00); //使能 DATA_READY 中断 Single_Write(OFSX,0x00); //X 偏移量 Single_Write(OFSY,0x00); //Y 偏移量 Single_Write(OFSZ,0x00); //Z 偏移量 return 0; } else return 1; }
//******读取MPU3050数据**************************************** void READ_MPU3050(void) { BUF[0]=Single_Read(MPU3050_Addr,GX_L); BUF[1]=Single_Read(MPU3050_Addr,GX_H); T_X= (BUF[1]<<8)|BUF[0]; T_X/=16.4; //读取计算X轴数据 BUF[2]=Single_Read(MPU3050_Addr,GY_L); BUF[3]=Single_Read(MPU3050_Addr,GY_H); T_Y= (BUF[3]<<8)|BUF[2]; T_Y/=16.4; //读取计算Y轴数据 BUF[4]=Single_Read(MPU3050_Addr,GZ_L); BUF[5]=Single_Read(MPU3050_Addr,GZ_H); T_Z= (BUF[5]<<8)|BUF[4]; T_Z/=16.4; //读取计算Z轴数据 BUF[6]=Single_Read(MPU3050_Addr,TMP_L); BUF[7]=Single_Read(MPU3050_Addr,TMP_H); T_T=(BUF[7]<<8)|BUF[6]; T_T = 35+ ((double) (T_T + 13200)) / 280;// 读取计算出温度 }
void HMC5883L_Read(tg_HMC5883L_TYPE * ptResult) { uint8_t tmp[6]; int32_t s32Val; i2cWrite(HMC5883L_Addr,HMC5883L_REGA,0x14); //30Hz i2cWrite(HMC5883L_Addr,HMC5883L_MODE,0x00); //连续测量模式 Delayms(10); tmp[0]=Single_Read(HMC5883L_Addr,HMC5883L_HX_H);//OUT_X_L_A tmp[1]=Single_Read(HMC5883L_Addr,HMC5883L_HX_L);//OUT_X_H_A tmp[2]=Single_Read(HMC5883L_Addr,HMC5883L_HZ_H);//OUT_Z_L_A tmp[3]=Single_Read(HMC5883L_Addr,HMC5883L_HZ_L);//OUT_Z_H_A tmp[4]=Single_Read(HMC5883L_Addr,HMC5883L_HY_H);//OUT_Y_L_A tmp[5]=Single_Read(HMC5883L_Addr,HMC5883L_HY_L);//OUT_Y_H_A ptResult->hx = (int16_t)((tmp[0] << 8) | tmp[1])+HMC5883L_OFFSET_X; s32Val = (int16_t)((tmp[4] << 8) | tmp[5])+HMC5883L_OFFSET_Y; s32Val = (s32Val*HMC5883L_GAIN_Y)/10000; ptResult->hy = (int16_t)s32Val; ptResult->hz = (int16_t)((tmp[2] << 8) | tmp[3]); }
//**************************实现函数******************************************** //将iic读取到得数据分拆,放入相应寄存器,更新MPU6050_Last //****************************************************************************** void MPU6050_Read(void) { mpu6050_buffer[0]=Single_Read(MPU6050_ADDRESS, 0x3B); mpu6050_buffer[1]=Single_Read(MPU6050_ADDRESS, 0x3C); mpu6050_buffer[2]=Single_Read(MPU6050_ADDRESS, 0x3D); mpu6050_buffer[3]=Single_Read(MPU6050_ADDRESS, 0x3E); mpu6050_buffer[4]=Single_Read(MPU6050_ADDRESS, 0x3F); mpu6050_buffer[5]=Single_Read(MPU6050_ADDRESS, 0x40); mpu6050_buffer[8]=Single_Read(MPU6050_ADDRESS, 0x43); mpu6050_buffer[9]=Single_Read(MPU6050_ADDRESS, 0x44); mpu6050_buffer[10]=Single_Read(MPU6050_ADDRESS, 0x45); mpu6050_buffer[11]=Single_Read(MPU6050_ADDRESS, 0x46); mpu6050_buffer[12]=Single_Read(MPU6050_ADDRESS, 0x47); mpu6050_buffer[13]=Single_Read(MPU6050_ADDRESS, 0x48); }