cancel
Showing results for 
Search instead for 
Did you mean: 

I2C - IMU 6DOF

ngohoainguyen
Associate II
Posted on March 11, 2015 at 05:00

Dear all,

I am doing a project that control a quadcopter using STM32F103.

At this moment, I have done about the control of DC motor. However, my quad. is not stable when it takes off.

I am going to use IMU 6DOF ( ADXL345 / ITG3200) to solve this problem. I have read a code of I2C library ( for AN2824 ) but it seems difficult to understand. I don't know clearly in this function:

 

Status I2C_Read(I2C_TypeDef* I2Cx, uint8_t *buf,uint32_t nbyte, uint8_t SlaveAddress)

 

Can you give me some ideas about that ?

Thank you.
2 REPLIES 2
ngohoainguyen
Associate II
Posted on March 13, 2015 at 05:15

I get stuck at

I2C_Send7bitAddress(I2C1,0xA6,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

What problem is that ?
g535343589
Associate II
Posted on March 18, 2015 at 16:44

*�?始化I2C1对外�?�线*/

static void I2C_GPIO_Config(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd  (RCC_APB2Periph_GPIOB, ENABLE );//使能与 I2C1 有关的时钟

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//PB6-I2C1_SCL�?PB7-I2C1_SDA

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

}

/*硬件�?置I2C1*/

static void I2C_Mode_Config(void)

{

    I2C_InitTypeDef I2C_InitStructure;  

    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ; //I2C �?置

    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

    I2C_InitStructure.I2C_OwnAddress1 = MPU_ADRESS;

    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;         

    I2C_Cmd(I2C1,ENABLE);//使能 I2C1

    I2C_Init(I2C1, &I2C_InitStructure);//I2C1 �?始化       

    I2C_AcknowledgeConfig(I2C1, ENABLE);//�?许应答模�?  

}

/*

 * 函数�??:I2C_MPU6050_ByteWrite

 * �??述  :写一个字节到I2C MPU6050寄存器中

 * 输入  :-pBuffer 缓冲区指针

 *         -WriteAddr 接收数�?�的MPU6050寄存器的地�?�

 * 输出  :无

 * 返回  :无

 * 调用  :内部调用

 */    

static void I2C_MPU_ByteWrite(u8 pBuffer, u8 WriteAddr)

{

  while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));/*wait until I2C bus is not busy*///等待iic总线是�?�忙碌

  I2C_GenerateSTART(I2C1, ENABLE);/* Send START condition *///�?��?开始�?�件

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/* Test on EV5 and clear it *///事件

  I2C_Send7bitAddress(I2C1, MPU_ADRESS, I2C_Direction_Transmitter);/* Send MMA address for write *///�?��?设备�?

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));/* Test on EV6 and clear it *///事件    

  I2C_SendData(I2C1, WriteAddr);/* Send the MMA's Register address to write to *///�?��?设备寄存器地�?�

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));/* Test on EV8 and clear it *///事件   

  I2C_SendData(I2C1, pBuffer);/* Send the byte to be written *///�?��?写入设备寄存器的数�?�

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));/* Test on EV8 and clear it *///事件    

  I2C_GenerateSTOP(I2C1, ENABLE);/* Send STOP condition *///�?�止信�?�

}

/*

 * 函数�??:I2C_MMA_ByteRead

 * �??述  :从MMA寄存器里�?�读�?�一�?�数�?�。

 * 输入  : -ReadAddr 接收数�?�的MMA寄存器的地�?�。        

 * 输出  :无

 * 返回  :读�?�出的寄存器数�?�

 * 调用  :内部调用

 */    

void I2C_MPU_ByteRead(uint32_t* pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)//第一个�?�数是指针�?��?,读第一个地�?��?�加1,

                                                                                   //第二个�?�数是寄存器地�?�,�?�需�?填入第一个地�?��?��?�                                                                                    //在读第二个地�?�,第三个�?�数是多少个8�?,指针好累加

{   

   while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));//检测忙碌   

  I2C_GenerateSTART(I2C1, ENABLE);//开始信�?�  

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//应答

  I2C_Send7bitAddress(I2C1, MPU_ADRESS, I2C_Direction_Transmitter);//�?��?设备地�?�

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//应答

  I2C_Cmd(I2C1, ENABLE);//使能iic

  I2C_SendData(I2C1, ReadAddr);//�?��?设备寄存器地�?�  

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//应答    

  I2C_GenerateSTART(I2C1, ENABLE);//开始信�?�  

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//应答  

  I2C_Send7bitAddress(I2C1, MPU_ADRESS, I2C_Direction_Receiver);//�?��?设备地�?��?+1,读信�?�  

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));//应答  

  while(NumByteToRead)  

  {

    if(NumByteToRead == 1)//如果就读一个一节

    {

      I2C_AcknowledgeConfig(I2C1, DISABLE);//�?�应答      

      I2C_GenerateSTOP(I2C1, ENABLE);//�?�止信�?�

    }

    if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))//应答  

    {      

      *pBuffer = I2C_ReceiveData(I2C1);//读出数�?�传给指针�?��?

      pBuffer++;//指针�?��?地�?�+1       

      NumByteToRead--;//�?读的地�?�里�?�的数�?�个数-1        

    }   

  }

  I2C_AcknowledgeConfig(I2C1, ENABLE);//�?�应答

}