本实验是I2C测试模式,没有配置初始化就简单的测试6轴数据。
1、 CS接高电平。
2、 要看看板子上的AD0脚
经过测试AD0为低电平。
AD0=0 1101000
AD0=1 1101001
I2C配置代码
//===========================================================
Pad_Config(I2C0_SCL_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_ENABLE, PAD_OUT_HIGH);
Pad_Config(I2C0_SDA_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_ENABLE, PAD_OUT_HIGH);
Pinmux_Config(I2C0_SCL_PIN, I2C0_CLK);
Pinmux_Config(I2C0_SDA_PIN, I2C0_DAT);
//============================================================
RCC_PeriphClockCmd(APBPeriph_I2C0, APBPeriph_I2C0_CLOCK, ENABLE);
I2C_InitTypeDef I2C_InitStruct;
I2C_StructInit(&I2C_InitStruct);
I2C_InitStruct.I2C_ClockSpeed = 10000;
I2C_InitStruct.I2C_DeviveMode = I2C_DeviveMode_Master;
I2C_InitStruct.I2C_AddressMode = I2C_AddressMode_7BIT;
I2C_InitStruct.I2C_SlaveAddress = ICM_20618_ADDR;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_Init(I2C0, &I2C_InitStruct);
I2C_Cmd(I2C0, ENABLE);
配置ICM20618
uint8_t i2c_writeBuf[16] = {0x00, 0x00, 0x66, 0x68, 0x77, 0x88};
uint8_t i2c_readBuf[16] = {0};
i2c_writeBuf[0] = ICM20618_WHO_AM_I;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
DBG_DIRECT("icm 20618 who am i :0x%x",i2c_readBuf[0]);
i2c_writeBuf[0] = ICM20618_REG_BANK_SEL;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
DBG_DIRECT("ICM20618_REG_BANK_SEL:0x%x",i2c_readBuf[0]);
i2c_writeBuf[0] = ICM20618_LP_CONFIG;
i2c_writeBuf[1] = 0xff;
I2C_MasterWrite(I2C0, i2c_writeBuf, 2);
i2c_writeBuf[0] = ICM20618_LP_CONFIG;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
DBG_DIRECT("ICM20618_LP_CONFIG:0x%x",i2c_readBuf[0]);
i2c_writeBuf[0] = ICM20618_PWR_MGMT_1;
i2c_writeBuf[1] = 0x01;
I2C_MasterWrite(I2C0, i2c_writeBuf, 2);
i2c_writeBuf[0] = ICM20618_PWR_MGMT_1;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
DBG_DIRECT("ICM20618_PWR_MGMT_1:0x%x",i2c_readBuf[0]);
测试代码
int16_t xaccel,yaccel,zaccel;
int16_t xgyros,ygyros,zgyros;
#define DELAY_TIME (100000*100)
while(1)
{
#if 1
//x=============================================================
i2c_writeBuf[0] = ACCEL_XOUT_H;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
xaccel = (i2c_readBuf[0]<<8);
i2c_writeBuf[0] = ACCEL_XOUT_L;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
xaccel |= i2c_readBuf[0];
//y=============================================================
i2c_writeBuf[0] = ACCEL_YOUT_H;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
yaccel = (i2c_readBuf[0]<<8);
i2c_writeBuf[0] = ACCEL_YOUT_L;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
yaccel |= i2c_readBuf[0];
//z=============================================================
i2c_writeBuf[0] = ACCEL_ZOUT_H;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
zaccel = (i2c_readBuf[0]<<8);
i2c_writeBuf[0] = ACCEL_ZOUT_L;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
zaccel |= i2c_readBuf[0];
//x g=============================================================
i2c_writeBuf[0] = GYRO_XOUT_H;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
xgyros = (i2c_readBuf[0]<<8);
i2c_writeBuf[0] = GYRO_XOUT_L;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
xgyros |= i2c_readBuf[0];
//y g=============================================================
i2c_writeBuf[0] = GYRO_YOUT_H;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
ygyros = (i2c_readBuf[0]<<8);
i2c_writeBuf[0] = GYRO_YOUT_L;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
ygyros |= i2c_readBuf[0];
//z g=============================================================
i2c_writeBuf[0] = GYRO_ZOUT_H;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
zgyros = (i2c_readBuf[0]<<8);
i2c_writeBuf[0] = GYRO_ZOUT_L;
I2C_RepeatRead(I2C0, i2c_writeBuf, 1, i2c_readBuf, 1);
zgyros |= i2c_readBuf[0];
DBG_DIRECT("accel:%d-%d-%d====gyros:%d-%d-%d",xaccel,yaccel,zaccel,xgyros,ygyros,zgyros);
for (uint32_t i = 0; i < DELAY_TIME; i++);
#endif
}
配置文件
#define ICM20618_WHO_AM_I 0x00
#define ICM20618_LP_CONFIG 0x05
#define ICM20618_PWR_MGMT_1 0x06
#define ICM20618_REG_BANK_SEL 0x7F
#define ACCEL_XOUT_H 0X2D
#define ACCEL_XOUT_L 0X2E
#define ACCEL_YOUT_H 0X2F
#define ACCEL_YOUT_L 0X30
#define ACCEL_ZOUT_H 0X31
#define ACCEL_ZOUT_L 0X32
#define GYRO_XOUT_H 0X33
#define GYRO_XOUT_L 0X34
#define GYRO_YOUT_H 0X35
#define GYRO_YOUT_L 0X36
#define GYRO_ZOUT_H 0X37
#define GYRO_ZOUT_L 0X38