本文主要是介绍ICM-42607六轴传感器WOM模式配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ICM-42607支持配置WOM模式,检测到加速度变化后在中断引脚触发跳沿信号。具体配置方式如下:
1、首先实现几个读写寄存器的函数:
#define ONE_G 9.807f#define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值:0x07(125Hz)
#define CONFIG 0x1A //低通滤波频率,典型值:0x06(5Hz)
#define GYRO_CONFIG 0x1B //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
#define ACCEL_CONFIG 0x1C //加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)
#define ACCEL_XOUT_H 0x0B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT0 0x1f //电源管理,典型值:0x00(正常启用)
#define WHO_AM_I 0x75 //IIC地址寄存器(默认数值0x68,只读)
#define SlaveAddress 0x68 //IIC写入时的地址字节数据,+1为读取
#define SlaveAddress2 0x69 //IIC写入时的地址字节数据,+1为读取//六轴IIC地址
static unsigned char IMU_slave_addr = SlaveAddress2;//写寄存器
unsigned char IMU_write_reg(unsigned char reg, unsigned char value)
{unsigned char ret = 0;unsigned int retry = 0;while((ret == 0) && (retry++ < 5)){ret = i2c1_write(IMU_slave_addr<<1, reg, value);}return ret;
}//读寄存器
unsigned char IMU_read_reg(unsigned char reg, unsigned char* buf, unsigned short len)
{unsigned char ret = 0;unsigned int retry = 0;while((ret == 0) && (retry++ < 5)){ret = i2c1_read(IMU_slave_addr<<1, reg, buf, len);}return ret;
}//读取MREG1的寄存器
uint8_t read_MREG1_reg(uint8_t addr)
{uint8_t ret;IMU_write_reg(0x7C, 0x00); //BLK_SEL_RIMU_write_reg(0x7D, addr); //MADDR_Rdelay_us(10);IMU_read_reg(0x7E,&ret,1); //M_Rdelay_us(10);return ret;
}//写入MREG1的寄存器
static void write_MREG1_reg(uint8_t addr,uint8_t val)
{IMU_write_reg(0x79, 0x00); //BLK_SEL_WIMU_write_reg(0x7A, addr); //MADDR_WIMU_write_reg(0x7B, val); //M_Wdelay_us(10);
}//读取加速度和角速度
uint8_t my_IMU_read_asix6(float acc[3],float gyro[3])
{unsigned char buf_reg[12];short raw_acc_xyz[3];short raw_gyro_xyz[3];if(0 == IMU_read_reg(ACCEL_XOUT_H,buf_reg,12)){return 0;}raw_acc_xyz[0] = (short)((unsigned short)(buf_reg[0]<<8) |( buf_reg[1]));raw_acc_xyz[1] = (short)((unsigned short)(buf_reg[2]<<8) |( buf_reg[3]));raw_acc_xyz[2] = (short)((unsigned short)(buf_reg[4]<<8) |( buf_reg[5]));// m/s2acc[0] = (float)(raw_acc_xyz[0]*ONE_G)/acc_lsb_div;acc[1] = (float)(raw_acc_xyz[1]*ONE_G)/acc_lsb_div;acc[2] = (float)(raw_acc_xyz[2]*ONE_G)/acc_lsb_div;raw_gyro_xyz[0] = (short)((unsigned short)(buf_reg[6]<<8) |( buf_reg[7]));raw_gyro_xyz[1] = (short)((unsigned short)(buf_reg[8]<<8) |( buf_reg[9]));raw_gyro_xyz[2] = (short)((unsigned short)(buf_reg[10]<<8) |( buf_reg[11]));// dpsgyro[0] = (float)(raw_gyro_xyz[0]*1.0f)/gyro_lsb_div;gyro[1] = (float)(raw_gyro_xyz[1]*1.0f)/gyro_lsb_div;gyro[2] = (float)(raw_gyro_xyz[2]*1.0f)/gyro_lsb_div;return 1;
}
2、配置WOM模式的寄存器函数
//震动检测模式配置(WOM)
void IMU_WOM_mode_config(void)
{uint8_t IMU_threshold_temp = 0;//先配置为LOW NOISE模式,配置MREG1寄存器在LP模式下会出问题//PWR_MGMT0 bit7 ACCEL_LP_CLK_SEL bit23 GYRO_MODE bit01 ACCEL_MODEIMU_write_reg(PWR_MGMT0, 0x8f);/*配置震动检测阈值(单位 2G/256-3.9mg)六轴震动检测阈值:1Lsb = 4mg*/IMU_threshold_temp = 8;write_MREG1_reg(0x4B,IMU_threshold_temp);write_MREG1_reg(0x4C,IMU_threshold_temp);write_MREG1_reg(0x4D,IMU_threshold_temp);//关闭INT1映射INT_SOURCE6write_MREG1_reg(0x2F,0x00);delay_ms(1);//配置加速度寄存器采样滤波系数//ACCCEL_UI_AVG(bit4-6)=2X:000, 4X:001,16X:011, 32X:100,64X:101//设置低通滤波器带宽//ACCEL_UI_FILT_BW(bit0-2)=180Hz:001,010: 121 Hz,25hz:110,16hz:111IMU_write_reg(0x24, 0x21);delay_ms(1);//配置ACCODR/*0101: 1.6 kHz (LN mode)0110: 800 Hz (LN mode)0111: 400 Hz (LP or LN mode)1000: 200 Hz (LP or LN mode)1001: 100 Hz (LP or LN mode)1010: 50 Hz (LP or LN mode)1011: 25 Hz (LP or LN mode)1100: 12.5 Hz (LP or LN mode)1101: 6.25 Hz (LP mode)1110: 3.125 Hz (LP mode)1111: 1.5625 Hz (LP mode)*/IMU_write_reg(0x21, 0x0B);delay_ms(1);//配置LP模式:关闭角速度,加速度低功耗模式//PWR_MGMT0 bit7 ACCEL_LP_CLK_SEL bit23 GYRO_MODE bit01 ACCEL_MODEIMU_write_reg(PWR_MGMT0, 0x02);delay_ms(1);//配置INT1中断类型:Push pull,Active high,Pulsed mode//INT1_POLARITY(bit0):1Active high//INT1_DRIVE_CIRCUIT(bit1):1: Push pull//INT1_MODE(bit2):0: Pulsed modeIMU_write_reg(0x06,0x03);//关闭其他INT1中断映射IMU_write_reg(0x2B,0x00);//WOM中断映射到INT1(X\Y\Z轴:bit0-2)IMU_write_reg(0x2C,0x07);//映射INT2
// //关闭其他中断映射
// IMU_write_reg(0x2D,0x00);
// IMU_read_reg(0x2D,&ret,1);
// //WOM中断映射到INT2
// IMU_write_reg(0x2E,0x07);
// IMU_read_reg(0x2E,&ret,1);//开启WOM功能//WOM_INT_DUR(bit3-4):01: WoM interrupt asserted at second overthreshold event//WOM_INT_MODE(bit2):0:Set WoM interrupt on the OR of all enabled accelerometer thresholds//WOM_MODE(bit1)1: Compare current sample to previous sample//WOM_EN(bit0)1: WOM enabledIMU_write_reg(0x27,0x07);
}
3、尝试初始化芯片,并调用函数配置芯片进入WOM模式
//六轴寄存器初始化
uint8_t IMU_reg_init(void)
{//IIC地址IMU_slave_addr = SlaveAddress2; //0x69//读取WHOAMI寄存器IMU_read_reg(WHO_AM_I, &IMU_chip_id, 1);if(0x60 != IMU_chip_id){IMU_slave_addr = SlaveAddress; //0x68IMU_read_reg(WHO_AM_I, &IMU_chip_id, 1);if(0x60 != IMU_chip_id){return 0;}}IMU_WOM_mode_config(); //WOM模式return 1;
}
至此完成六轴芯片的WOM(震动检测)模式的初始化配置
这篇关于ICM-42607六轴传感器WOM模式配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!