BlueNRG-1驱动LSM303C通过手机显示磁场拆线图
[复制链接]
下载驱动:LSM303C(加速度+磁力计)驱动例程ver1.1 在BlueNRG-1_2 DK 2.x工程下找到Library新建文件夹BSP\Components,将下载的LSM303C_ver1.1.rar下的LSM303C_Driver复制到Components文件下 在IAR工程的Library下新建目录BSP\Components\LSM303C_Driver将LSM303C_Driver下的*.c文件添加到IARr LSM303C_Driver目录下 打开工程选项下的C/C++ Compler->Preprocessor在Additional include directories下添加 $PROJ_DIR$\..\..\..\..\..\Library\BSP\Components\LSM303C_Driver 在BLE_Beacon_main.c文件里添加头文件 #include "LSM303C_MAG_driver.h" #include "LSM303C_ACC_driver.h" 只使用磁场传感器可以只添加LSM303C_MAG_driver.h文件 打开LSM303C_ver1.1.rar下的LSM303C_basic.c文件 复制下边代码到BLE_Beacon_main.c #define SENSITIVITY_ACC 0.061 /* mg/LSB */ #define SENSITIVITY_MAG 0.580 /* mgauss/LSB */ status_t response; // mems error variable Type3Axis16bit_U data; // accelerometer row data float mgData_X, mgData_Y,mgData_Z; //data in mg float gaussData_X, gaussData_Y, gaussData_Z; //data in Gauss u8_t whoAmI; 将LSM303C_ACC_driver.c和LSM303C_MAG_driver.c引用的头文件i2c.h替换为SDK_EVAL_I2C.h 添加Library\SDK_Eval_BlueNRG1\src\SDK_EVAL_I2C.c到IAR工程的Platform_Layers目录下 找到BlueNRG1_conf.h,去掉#include "BlueNRG1_i2c.h"前边的注释 修改LSM303C_MAG_driver.c的LSM303C_MAG_WriteReg,LSM303C_MAG_ReadReg和LSM303C_ACC_driver.c的LSM303C_ACC_WriteReg,LSM303C_ACC_ReadReg函数里调用的I2C函数 在BLE_Beacon_main.c的main函数里添加I2C初始化函数SdkEvalI2CInit(10000); 复制LSM303C_basic.c里的下边代码到main函数 //this flag is used for reading data only when there are new data u8_t flag_LSM303C_ACC_STATUS_FLAGS; LSM303C_MAG_ZYXDA_t flag_LSM303C_MAG_XYZDA; 新建初始化函数void LSM303C_Init(void) 复制LSM303C_basic.c里的下边代码到LSM303C_Init //Initialize Magnetometer //set ODR (turn ON device) response = LSM303C_MAG_W_OutputDataRate(0, LSM303C_MAG_DO_10Hz); if(response==MEMS_ERROR) while(1); //manage here comunication error //set Fullscale response = LSM303C_MAG_W_FullScale(0, LSM303C_MAG_FS_16Ga); if(response==MEMS_ERROR) while(1); //manage here comunication error //set Block Data Update response = LSM303C_MAG_W_BlockDataUpdate(0, LSM303C_MAG_BDU_ENABLE); if(response==MEMS_ERROR) while(1); //manage here comunication error //set XY Axis Operation Mode response = LSM303C_MAG_W_OperatingModeXY(0, LSM303C_MAG_OM_HIGH); if(response==MEMS_ERROR) while(1) //manage here comunication error //set Z Axis Operation Mode response = LSM303C_MAG_W_OperatingModeZ(0, LSM303C_MAG_OMZ_HIGH); if(response==MEMS_ERROR) while(1); //manage here comunication error //set Continuous Mode response = LSM303C_MAG_W_SystemOperatingMode(0, LSM303C_MAG_MD_CONTINUOUS); if(response==MEMS_ERROR) while(1); //manage here comunication error 在SdkEvalI2CInit(10000);后边添加LSM303C_Init(); 新建一个函数 static void Update_Beaconing(void) { /* Set AD Type Flags at beginning on Advertising packet */ uint8_t adv_data[] = { /* Advertising data: Flags AD Type */ 0x02, 0x01, 0x06, 06, H16(mx), L16(mx), H16(my), L16(my), H16(mz), L16(mz), }; /* Update the ADV data with the BEACON manufacturing data */ hci_le_set_advertising_data (sizeof(adv_data), adv_data); } 在主循环里加入下边代码 response = LSM303C_MAG_R_NewXYZData(0, (LSM303C_MAG_ZYXDA_t*)& flag_LSM303C_MAG_XYZDA); if(response==MEMS_ERROR) while(1); //manage here comunication error //read only if new data are available else if ((LSM303C_MAG_ZYXDA_t) flag_LSM303C_MAG_XYZDA & LSM303C_MAG_ZYXDA_AVAILABLE) { response = LSM303C_MAG_Get_Magnetic(0, data.u8bit); mx=data.i16bit[0]; my=data.i16bit[1]; mz=data.i16bit[2]; //convert from LSB to Gauss gaussData_X=data.i16bit[0]*SENSITIVITY_MAG; gaussData_Y=data.i16bit[1]*SENSITIVITY_MAG; gaussData_Z=data.i16bit[2]*SENSITIVITY_MAG; Update_Beaconing(); } 测试的时候走了一些弯路,LSM303C_ver1.1.rar例程里使用的是8位I2C地址 SDK_EVAL_I2C.c使用的是7位地址,后来直接把原有8位地址右移一位 下边是BlueNRG-1代码 下边是android应用,安装APK文件,解压int8文件夹内的文件复制到手机存储目录下即可
|