cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4Discovery i2c using

salperogan9
Associate
Posted on January 22, 2013 at 12:39

I need to use I2C1 ports for communication with LG3D20(0xD6 adress) gyro sensor.Accordingly, i need i2c1 initial,i2c1 write data 1 byte,i2c1 read data 1 byte functions.I am using these functions but i couldnt run this function in program flow.The program always lockups at I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY).Please give me an tried example for stm32f4discovery i2c1.My codes are here:

/*  I2C1  */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  GPIO_PinAFConfig(GPIOB,GPIO_PinSource6, GPIO_AF_I2C1);

  GPIO_PinAFConfig(GPIOB,GPIO_PinSource7, GPIO_AF_I2C1);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

I2C_DeInit(I2C1);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

    I2C_init.I2C_Mode = I2C_Mode_I2C;

    I2C_init.I2C_ClockSpeed = 100000;

    I2C_init.I2C_DutyCycle = I2C_DutyCycle_2;

    I2C_init.I2C_OwnAddress1 = 0;

    I2C_init.I2C_Ack = I2C_Ack_Enable;

    I2C_init.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

 I2C_Cmd(I2C1,ENABLE);

    I2C_Init(I2C1,&I2C_init);

   unsigned short int I2C1_read(uint8_t adress, uint8_t index)

   { 

   uint8_t data;

   while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

    I2C_GenerateSTART(I2C1, ENABLE);

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(I2C1, adress, I2C_Direction_Transmitter);

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    I2C_SendData(I2C1, index);

    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, adress, I2C_Direction_Receiver);

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));

    data=I2C_ReceiveData(I2C1);

    I2C_GenerateSTOP(I2C1, ENABLE);

return data;

   }

void I2C1_write(uint8_t adress, uint8_t * data) {

    uint8_t i=0;

    while (i < 7) 

{

        while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

        I2C_GenerateSTART(I2C1, ENABLE);

        while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

        I2C_Send7bitAddress(I2C1, adress, I2C_Direction_Transmitter);

        while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

        I2C_SendData(I2C1, data[i]);

        while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

        I2C_SendData(I2C1,i + 1);

        while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

        I2C_GenerateSTOP(I2C2, ENABLE);

        i+=2;

    }

}

int main(void)

{

uint8_t timer_value,flag=0,i=0;

char buffer[8];

uint8_t deger;

I2C1_Initialize();

  while (1)

  {

deger=I2C2_read(0x3C,0x20);

sprintf(buffer,''%d'',deger);

USART3_Write_Text(buffer);

USART3_Write_Text(''\n\r'');

}

}

I need read,write,initial functions for stm32f4discovery kit's I2C1!!!

#stm32f4discovery-i2c1-problem!!!
0 REPLIES 0