在线时间1809 小时
UID3327992
ROHM金币4850
注册时间2018-9-28
TA的每日心情 | 奋斗 2022-9-1 17:50 |
---|
签到天数: 2 天 [LV.1]初来乍到
论坛元老
- 最后登录
- 2024-4-18
|
ROHM的BM1422地磁传感器是检测地磁三轴的传感器,该芯片供电电压范围为1.7V-3.6V,支持标准2线制IIC串行数字总线接口。BM1422AGMV芯片内部集成了三轴MI检测元件、高精度ADC转换器、电源稳压器及逻辑控制器等单元组成,采用2.00mm x 2.00mm LGA封装。
其应用电路如下:
寄存器功能表如下:
下面就是BM1422地磁传感器模块:
下面使用STM32F410-Nucleo开发板配合ROHM推出的SensorShield-EVK-001套件进行测试BM1422。因为测试的软件包是基于Arduino的,我自己改了一下适应STM32开发。
驱动程序:
- /*=============================================================================
- // File Name:
- // Author:
- // Date:
- // Version:
- =============================================================================*/
- /* Includes -----------------------------------------------------------------*/
- #include "BM1422AGMV.h"
- static unsigned char _sens;
- //
- uint8_t BM1422AGMV_Init(void)
- {
- uint8_t temp;
- uint8_t buf[2];
- //读ID
- temp = I2C_ReadByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_WIA);
- if(temp != BM1422AGMV_WIA_VAL)
- {
- return 1;
- }
-
- //
- I2C_WriteByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_CNTL1, BM1422AGMV_CNTL1_VAL);
- // Check 12bit or 14bit
- buf[0] = (BM1422AGMV_CNTL1_VAL & BM1422AGMV_CNTL1_OUT_BIT);
- if (buf[0] == BM1422AGMV_CNTL1_OUT_BIT) {
- _sens = BM1422AGMV_14BIT_SENS;
- } else {
- _sens = BM1422AGMV_12BIT_SENS;
- }
- Delay_ms(1);
- buf[0] = (BM1422AGMV_CNTL4_VAL >> 8) & 0xFF;
- buf[1] = (BM1422AGMV_CNTL4_VAL & 0xFF);
- I2C_PageWrite(BM1422AGMV_DEVICE_ADDRESS_0E,BM1422AGMV_CNTL4,buf,2);
- I2C_WriteByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_CNTL2, BM1422AGMV_CNTL2_VAL);
- I2C_WriteByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_AVE_A, BM1422AGMV_AVE_A_VAL);
- return 0;
- }
- void BM1422AGMV_get_rawdata(uint8_t *buff)
- {
- uint8_t temp;
- I2C_WriteByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_CNTL3, BM1422AGMV_CNTL3_VAL);
- while(1)
- {
- temp = I2C_ReadByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_STA1);
- if(temp == BM1422AGMV_STA1_RD_DRDY)
- {
- break;
- }
- Delay_ms(1);
- }
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_DATAX,
- buff, 6);
- }
- void BM1422AGMV_convert_uT(signed short *rawdata, float *data)
- {
- // LSB to uT
- data[0] = (float)rawdata[0] / _sens;
- data[1] = (float)rawdata[1] / _sens;
- data[2] = (float)rawdata[2] / _sens;
- }
- void BM1422AGMV_get_val(float *data)
- {
- unsigned char val[6];
- signed short mag[3];
- BM1422AGMV_get_rawdata(val);
- mag[0] = ((signed short)val[1] << 8) | (val[0]);
- mag[1] = ((signed short)val[3] << 8) | (val[2]);
- mag[2] = ((signed short)val[5] << 8) | (val[4]);
- BM1422AGMV_convert_uT(mag, data);
-
- }
- void BM1422AGMV_get_temperature(uint8_t *buff)
- {
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_TEMP,
- buff, 2);
- }
- ///////////////////////////////////////////////////////////
- uint16_t BM1422AGMV_get_offx(void)
- {
- uint16_t temp;
- uint8_t buff[2];
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_OFF_X,
- buff, 2);
- temp = (buff[1]<<8)|buff[0];
- return temp;
- }
- uint16_t BM1422AGMV_get_offy(void)
- {
- uint16_t temp;
- uint8_t buff[2];
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_OFF_Y,
- buff, 2);
- temp = (buff[1]<<8)|buff[0];
- return temp;
- }
- uint16_t BM1422AGMV_get_offz(void)
- {
- uint16_t temp;
- uint8_t buff[2];
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_OFF_Z,
- buff, 2);
- temp = (buff[1]<<8)|buff[0];
- return temp;
- }
- //////////////
- void BM1422AGMV_set_offx(uint16_t offx)
- {
- uint8_t buff[2];
- buff[0] = offx&0xff;
- buff[1] = (offx>>8)&0xff;
- I2C_PageWrite(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_OFF_X,
- buff, 2);
- }
- void BM1422AGMV_set_offy(uint16_t offy)
- {
- uint8_t buff[2];
- buff[0] = offy&0xff;
- buff[1] = (offy>>8)&0xff;
- I2C_PageWrite(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_OFF_Y,
- buff, 2);
- }
- void BM1422AGMV_set_offz(uint16_t offz)
- {
- uint8_t buff[2];
- buff[0] = offz&0xff;
- buff[1] = (offz>>8)&0xff;
-
- I2C_PageWrite(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_OFF_Z,
- buff, 2);
- }
- void BM1422AGMV_get_fineoutput_xyz(uint8_t *buff)
- {
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_FINEOUTPUT_XYZ,
- buff, 6);
- }
- void BM1422AGMV_get_gainpara_xyz(uint8_t *buff)
- {
- I2C_PageRead(BM1422AGMV_DEVICE_ADDRESS_0E,
- BM1422AGMV_GAINPARA_XYZ,
- buff, 6);
- }
- void BM1422AGMV_power_down(void)
- {
- I2C_WriteByte(BM1422AGMV_DEVICE_ADDRESS_0E, BM1422AGMV_CNTL1, 0x00);
- }
复制代码- /*=============================================================================
- // File Name:
- // Author:
- // Date:
- // Version:
- =============================================================================*/
- #ifndef _BM1422AGMV_H_
- #define _BM1422AGMV_H_
- #include "gpio_i2c.h"
- #define BM1422AGMV_DEVICE_ADDRESS_0E (0x0E) // 7bit Address ADDR = L
- #define BM1422AGMV_DEVICE_ADDRESS_0F (0x0F) // 7bit Address ADDR = H
- #define BM1422AGMV_WIA_VAL (0x41)
- #define BM1422AGMV_WIA (0x0F) //ID = 0X41
- #define BM1422AGMV_DATAX (0x10) //XYZ: 0X10-0X15
- #define BM1422AGMV_STA1 (0x18) //STATUS: 0X00 = NG,0X40= OK
- #define BM1422AGMV_CNTL1 (0x1B)
- #define BM1422AGMV_CNTL2 (0x1C)
- #define BM1422AGMV_CNTL3 (0x1D)
- #define BM1422AGMV_AVE_A (0x40) //Average Time:000:4times, 001:1times, 010:2times, 011:8times, 100:16times
- #define BM1422AGMV_CNTL4 (0x5C)
- #define BM1422AGMV_TEMP (0x60) //Temperature value L_H
- #define BM1422AGMV_OFF_X (0x6C) //
- #define BM1422AGMV_OFF_Y (0x72)
- #define BM1422AGMV_OFF_Z (0x78)
- #define BM1422AGMV_FINEOUTPUT_XYZ (0x90) //
- #define BM1422AGMV_GAINPARA_XYZ (0x9C) //
- #define BM1422AGMV_STA1_RD_DRDY (1 << 6) //0 = NG,1= OK
- #define BM1422AGMV_CNTL1_FS1 (1 << 1) //0=CONTINU MODE,1=SINGLE MODE
- #define BM1422AGMV_CNTL1_ODR_10Hz (0 << 3) //00=10Hz,01=100Hz,10=20Hz,11=1KHz
- #define BM1422AGMV_CNTL1_RST_LV (1 << 5) //0=Reset Release,1=Reset
- #define BM1422AGMV_CNTL1_OUT_BIT (1 << 6) //0=12BIT,1=14BIT
- #define BM1422AGMV_CNTL1_PC1 (1 << 7) //0=POWERDOWN,1=ACTIVE
- #define BM1422AGMV_CNTL2_DRP (1 << 2) //DRDY :0=LOW ACTIVE,1=HIGH ACTIVE
- #define BM1422AGMV_CNTL2_DREN (1 << 3) //DRDYENABLE:0=DISABLE,1=ENABLE
- #define BM1422AGMV_CNTL3_FORCE (1 << 6) //AD start measurement trigger at continuous OR single.Set 1: Start measurement
- #define BM1422AGMV_AVE_A_AVE4 (0 << 2) //000:4times
- #define BM1422AGMV_CNTL1_VAL (BM1422AGMV_CNTL1_FS1 | BM1422AGMV_CNTL1_OUT_BIT | BM1422AGMV_CNTL1_PC1)
- #define BM1422AGMV_CNTL2_VAL (BM1422AGMV_CNTL2_DREN)
- #define BM1422AGMV_CNTL3_VAL (BM1422AGMV_CNTL3_FORCE)
- #define BM1422AGMV_CNTL4_VAL (0x0000)
- #define BM1422AGMV_AVE_A_VAL (BM1422AGMV_AVE_A_AVE4)
- #define BM1422AGMV_14BIT_SENS (24)
- #define BM1422AGMV_12BIT_SENS (6)
- extern uint8_t BM1422AGMV_Init(void);
- extern void BM1422AGMV_get_rawdata(uint8_t *buff);
- extern void BM1422AGMV_get_val(float *data);
- void BM1422AGMV_get_temperature(uint8_t *buff);
- uint16_t BM1422AGMV_get_offx(void);
- uint16_t BM1422AGMV_get_offy(void);
- uint16_t BM1422AGMV_get_offz(void);
- void BM1422AGMV_set_offx(uint16_t offx);
- void BM1422AGMV_set_offy(uint16_t offy);
- void BM1422AGMV_set_offz(uint16_t offz);
- void BM1422AGMV_get_fineoutput_xyz(uint8_t *buff);
- void BM1422AGMV_get_gainpara_xyz(uint8_t *buff);
- extern void BM1422AGMV_power_down(void);
- #endif // _BM1422AGMV_H_
复制代码 下面是串口输出的3轴地磁数据:
BM1422的软件包和PDF芯片文档
BM1422AGMV.zip
(4.2 KB, 下载次数: 1)
|
|