uint32_t(*GetDriverVersion)(void);
} ROM_I2CMD_API_T;
2.1.1 定义的ROM_I2CM_XFER_T:typedef PRE_PACK struct POST_PACK {
constvoid *txBuff; /*!< Pointer to array of bytes to betransmitted */
void *rxBuff; /*!< Pointer memory where bytes receivedfrom I2C be stored */
volatileErrorCode_t status; /*!< Packed status of the current I2Ctransfer (ErrorCode_t), must be 32-bits */
uint32_t flags; /*!< Optional transfer flags */
uint16_t txSz; /*!< Number of bytes in transmit array, if 0 only receivetransfer will be performed */
uint16_t rxSz; /*!< Number of bytes to receive, if 0 onlytransmission we be performed */
uint16_t slaveAddr; /*!< 7-bit I2C Slave address */
uint16_t reserved; /*!< Reserved for alignment */
} ROM_I2CM_XFER_T;
/** I2C master callback IDs */
typedef enum {
ROM_I2CM_DATACOMPLETE_CB= 0, /*!< Callback ID forI2C master data transfer complete */
ROM_I2CM_DATATRANSMITSTART_CB, /*!< Callback ID for I2C mastertransmit start */
ROM_I2CM_DATATRECEIVESTART_CB /*!< Callback ID for I2C masterreceive start */
} ROM_I2CM_CALLBACK_T;
2.2 《ROMAPI_I2CM.H》文件说明:
/**
* @brief Get memory size in bytes needed for I2Cmaster driver context
* @return Size in bytes needed for the ROM driver
*/
uint32_t ROM_I2CM_GetMemSize(void);
/**
* @brief InitializeI2C master peripheral
* @param mem : Pointer to memory area used todriver context
* @param pInit : Pointer to I2C master init data
* @return NULLon error, or a pointer to the device context handle
*/
ROM_I2CM_HANDLE_T ROM_I2CM_Init(void *mem, constROM_I2CM_INIT_T *pInit);
/**
* @brief SetI2C master clock rate
* @param pHandle : Pointer to driver context handle
* @param inRate : Base clock rate for the I2C peripheral in Hz
* @param i2cRate : Desired I2C master clock rate
* @return ActualI2C master clock rate
*/
uint32_t ROM_I2CM_SetClockRate(ROM_I2CM_HANDLE_TpHandle, uint32_t inRate, uint32_t i2cRate);
/**
* @brief Registera I2C master callback
* @param pHandle : Pointer to driver context handle
* @param cbIndex : Callback to register
* @param pCB : Pointer to callback function
* @return Nothing
*/
void ROM_I2CM_RegisterCallback(ROM_I2CM_HANDLE_TpHandle, ROM_I2CM_CALLBACK_T cbIndex, void *pCB);
/**
* @brief Starta I2C master transfer
* @param pHandle : Pointer to driver context handle
* @param pXfer : Pointer to master transfer configuration
* @return Errorcode
* @note Thisfunction starts the transfer and returns immediately.
*/
ErrorCode_t ROM_I2CM_Transfer(ROM_I2CM_HANDLE_TpHandle, ROM_I2CM_XFER_T *pXfer);
/**
* @brief I2Cmaster transfer (interrupt) handler
* @param pHandle : Pointer to driver context handle
* @return Nothing
* @note Thisfunction should be called from the I2C interrupt handler and
* is used in interrupt and DMA modes.
*/
void ROM_I2CM_TransferHandler(ROM_I2CM_HANDLE_TpHandle);
/**
* @brief Returnthe I2C master ROM driver version
* @return Driverversion number
* @note Thereturned driver version number consists of a major and minor
* number, with the minor number in the lower 8bits and the major number in
* the upper 8 bits.
*/
uint16_t ROM_I2CM_GetDriverVersion(void);
2.3 其他程序说明:2.3.1 PIN脚设置:/* Initializes pin muxing for I2C interface - notethat SystemInit() may
already setupyour pin muxing at system startup */
static void Init_I2C_PinMux(void)
{
#if defined(BOARD_NXP_LPCXPRESSO_54102)
/* Connectthe I2C_SDA and I2C_SCL signals to port pins */
Chip_IOCON_PinMuxSet(LPC_IOCON,0, 23, (IOCON_FUNC1 | IOCON_DIGITAL_EN)); /*I2C0 */
Chip_IOCON_PinMuxSet(LPC_IOCON,0, 24, (IOCON_FUNC1 | IOCON_DIGITAL_EN)); /*I2C0 */
#else
/*Configure your own I2C pin muxing here if needed */
#warning "No I2C pin muxing defined"
#endif
}
2.3.2 定义数据类型/* Made global so data can be seen in the debuuger */
uint8_t rx[10], tx[4];
ROM_I2CM_XFER_T mXfer;
volatile bool done;
uint32_t actualRate;
2.3.3 数据传输: /* Writeaddress (2), read 8 bytes back, no flags */
mXfer.slaveAddr= I2C_ADDR_7BIT;
tx[0]= 2;
mXfer.txBuff= tx;
mXfer.rxBuff= rx;
mXfer.txSz= 1;
mXfer.rxSz= 8;
mXfer.flags= 0;
/*Start transfer */
done =false;
ROM_I2CM_Transfer(i2cmHandle,&mXfer);
2.4 MPU6050程序设计://****************************************
//定义mpu6050内部地址
//****************************************
#define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值0x07(125Hz)
#define CONFIG 0x1A //低通滤波频率,典型值0x06(5Hz)
#define GYRO_CONFIG 0x1B//陀螺仪自检及测量范围,典型值:0x18
#define ACCEL_CONFIG 0x1C //
#define ACCEL_XOUT_H 0x3B
#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_MGMT_1 0x6B //电源管理
#define WHO_AM_I 0x75 //IIC寄存器地址,默认参数0x75
#define SlaveAddress 0x68 //IIC地址
2.4.1 I2C_ByteWritevoid I2C_ByteWrite(uint8_tSlaveAddress,uint8_t REG_Address,uint8_t REG_data)
{
/* Write address, write 1 btye, read 8 bytes back */
mXfer.slaveAddr= SlaveAddress;
tx[0]= REG_Address;
tx[1]= REG_data;
mXfer.txBuff= tx;
mXfer.rxBuff= rx;
mXfer.txSz= 2;
mXfer.rxSz= 0;
/*I2C master driver will block if blocking flag is used */
mXfer.flags= ROM_I2CM_FLAG_BLOCKING;
/*Start transfer and wait for completion */
ROM_I2CM_Transfer(i2cmHandle,&mXfer);
}
2.4.2 I2C_ByteReaduint8_t I2C_ByteRead(uint8_tSlaveAddress,uint8_t REG_Address)
{
uint8_tREG_data;
/* Write address, write 1 btye, read 8bytes back */
mXfer.slaveAddr = SlaveAddress;
tx[0] = REG_Address;
mXfer.txBuff = tx;
mXfer.rxBuff = rx;
mXfer.txSz = 1;
mXfer.rxSz = 1;
/* I2C master driver will block if blockingflag is used */
mXfer.flags= ROM_I2CM_FLAG_BLOCKING;
/* Start transfer and wait for completion*/
ROM_I2CM_Transfer(i2cmHandle, &mXfer);
REG_data = rx[0];
return REG_data ;
}
算法程序参考@强子00001的分享,在此就不在多说了。谢谢@强子00001的分享。
以下是四元结算的资料:
最后的调试结果:
我的其他心得贴: