cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with I2C

dnm_es
Associate
Posted on July 15, 2014 at 13:46

Hello my name is Dani, I'm starting with bus I2C and the microcontroler stm32f103rd, I have made the next code, it doesn't work. I would like to control a light sensor.

The program stops in:

  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));    

GPIO_InitTypeDef GPIO_InitStructure;

I2C_InitTypeDef I2C_InitStructure;

int main( void )

{

      SysTick_Setup();

 

 

 I2C_DeInit(I2C1);

 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

 

  /* Configure I2C1 pins: SCL and SDA */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

 

   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;

   GPIO_Init(GPIOB, &GPIO_InitStructure);

 

  /* Enable I2C1 reset state */

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);

  /* Release I2C1 from reset state */

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);

 

  /* I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = 0x01;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = 100000;

 

  I2C_SoftwareResetCmd(I2C1,ENABLE);

  I2C_SoftwareResetCmd(I2C1,DISABLE);

 

  /* Apply I2C configuration after enabling it */

  I2C_Cmd(I2C1,ENABLE);

 

  I2C_Init(I2C1, &I2C_InitStructure);

 

 

      

  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));       

   

  I2C_GenerateSTART(I2C1, ENABLE);                                 /* Send I2C1 START condition */

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

             

  I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS7  , I2C_Direction_Transmitter);

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

  I2C_ClearITPendingBit(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED);

 

              

  I2C_SendData(I2C1, 0x01);      

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

               

  I2C_GenerateSTOP(I2C1, ENABLE);

 

  while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE));

  lsb = I2C_ReceiveData(I2C1);

 

 

  I2C_GenerateSTOP(I2C1, ENABLE);

 

}

This code works with stm32f103RB  but not with stm32f103RD.

Thanks in advance.
0 REPLIES 0