初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-7-25
|
只能读取一次数据寄存器
u8
u8g_i2c_read_buf( u8 u8t_i2c_slaveaddr, u8 u8t_reg_addr, u8 *u8t_buf, u8 u8t_buf_len )
{
u8 u8t_ret = 0;
u8 i = 0;
/*------------------------- send read register address -------------------------*/
/* wait i2c idle */
while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) == SET );
/* start signal */
I2C_GenerateSTART( I2C1, ENABLE );
/* check EV5 , read register SR1->clear EV5 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) == ERROR );
/* send slave address */
I2C_Send7bitAddress( I2C1, (u8t_i2c_slaveaddr << 1), I2C_Direction_Transmitter );
/* check EV6 , read register SR1 and SR2->clear EV6 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR );
/* check EV8_2 , read register SR1 and SR2->clear EV8_2 */
//while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) == ERROR );
/* check EV8 , read register SR1 and SR2->clear EV8 */
//while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) == ERROR );
/* send register address */
I2C_SendData( I2C1, u8t_reg_addr );
/* check EV8_2 , read register SR1 and SR2->clear EV8_2 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) == ERROR );
/* check EV8 , read register SR1 and SR2->clear EV8 */
//while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) == ERROR );
/*----------------------------- read register data -----------------------------*/
/* start signal */
I2C_GenerateSTART( I2C1, ENABLE );
/* check EV5 , read register SR1->clear EV5 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) == ERROR );
/* send slave address */
I2C_Send7bitAddress( I2C1, (u8t_i2c_slaveaddr << 1), I2C_Direction_Receiver );
/* check EV6 , read register SR1 and SR2->clear EV6 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) == ERROR );
if ( u8t_buf_len == 1 )
{
/* if external process */
}else
{
for( i=0; i<(u8t_buf_len-2); i++ )
{
/* check EV7 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED ) == ERROR ); //第2次,卡死在这
u8t_buf = I2C_ReceiveData( I2C1 );
}
/* check EV7 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED ) == ERROR );
u8t_buf = I2C_ReceiveData( I2C1 );
}
/* Disable Acknowledgment */
I2C_AcknowledgeConfig( I2C1, DISABLE );
/* stop signal */
I2C_GenerateSTOP( I2C1, ENABLE );
/* check EV7 */
while( I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED ) == ERROR );
u8t_buf = I2C_ReceiveData( I2C1 );
return u8t_ret;
}
****************************************************************
另外一个问题,温度的解析不太对
代码:
u8 u8t_mpu6050_buf[14];
/* Temperature in degrees C = (TEMP_OUT_Register_Value as a signed quantity)/340 + 36.53 */
f32g_mpu6050_temp = (f32)( ( (u16)u8t_mpu6050_buf[6] << 8) | (u16)u8t_mpu6050_buf[7] );
f32g_mpu6050_temp = ( f32g_mpu6050_temp/340.0) + 36.53 ;
现象:
|
|