AnsweredAssumed Answered

STM32F103RB I2C problem

Question asked by carbonera.felipe on Mar 19, 2013
Latest reply on Mar 21, 2013 by carbonera.felipe
Hi,
I am trying to comunicate with MCP23016 by i2c and i am using STM32F103RB with uVision 4.
The problem is that if I send 0x40 is the same if I send 0x41. Its the red mark on the code.

My code is:

void I2C_Configuration(void)
{    
 
  I2C_InitTypeDef  I2C_InitStructure;
  GPIO_InitTypeDef  GPIO_InitStructure;
 
  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_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  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 = I2C1_SLAVE_ADDRESS7;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = 100000;
  /* Apply I2C configuration after enabling it */
  I2C_Cmd(I2C1,ENABLE);
  I2C_Init(I2C1, &I2C_InitStructure); 
 
}

unsigned int read_MCP(u8 WriteAddress, u8 cmdByte, u8 ReadAddress, u8 NumByteToRead) {
  
  int lsbdata, msbdata;
  unsigned int ldata;
    int timeout=0;
    int indice;
      
  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, 0x40, 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, GP0);       //CONFIGURA PARA LEITURA
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
               
  I2C_GenerateSTOP(I2C1, ENABLE);
  DelayUs(100);
 
  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, 0x41, 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);
 
  while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE)); 
  lsbdata = I2C_ReceiveData(I2C2);
  I2C_ClearITPendingBit(I2C1, I2C_FLAG_RXNE);
 
  while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE)); 
  msbdata = I2C_ReceiveData(I2C2);
  I2C_ClearITPendingBit(I2C1, I2C_FLAG_RXNE);
 
 
  I2C_GenerateSTOP(I2C1, ENABLE);
 
  ldata = (msbdata<<8);
  ldata = ldata + lsbdata;
 
  return(ldata);
}

Thats what mcp23016 needs.

i2c%20mcp23016.png

And here the screen shot of 0x40:

scope_0.png

And here the screen shot of 0x41: (nothing changes)

scope_1.png

I also tried another number like 0x48 and 0x49 and its the same too. I dont know what to do.
Bye bye









Outcomes