cancel
Showing results for 
Search instead for 
Did you mean: 

I2C problems with STM32L Discovery

marcogrossi89
Associate II
Posted on July 10, 2015 at 18:25

I am trying to communicate with a ADT7410 I2C temperature sensor using a STM32L Discovery Board (PB6 for SCL and PB7 for SDA).

I configure the I2C with the following code:

void I2C_initialization(void)

{

  I2C_InitTypeDef  I2C_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable I2C and GPIO clocks */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  /* Configure I2C pins: SCL and SDA */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);

  /* I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = 0x00;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = 100000;

  /* I2C Peripheral Enable */

  I2C_Cmd(I2C1, ENABLE);

  /* Apply I2C configuration after enabling it */

  I2C_Init(I2C1, &I2C_InitStructure);

}

To read data from the sensor I use the following procedure call (with parameter reg_adrs 0x04):

uint16_t I2C_read2bytes(uint8_t reg_adrs)

  {

      uint8_t I2C_MSB, I2C_LSB;

      uint16_t I2C_total;

  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

  /* Send START condition */

  I2C_GenerateSTART(I2C1, ENABLE);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1, 0x48 << 1, I2C_Direction_Transmitter);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  I2C_SendData(I2C1, reg_adrs);

  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, (0x48 << 1) | (0x01), I2C_Direction_Receiver);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  I2C_MSB = I2C_ReceiveData(I2C1);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));

  I2C_GenerateSTOP(I2C1, ENABLE);

  I2C_total = (uint16_t)(I2C_MSB);

  return I2C_total;

  }

However what I get in return is the decimal number 145 (0b10010001) that is the address byte I send with the second  I2C_Send7bitAddress function and not the byte the slave device sends (that is however wront, that is not the byte it should return).

0 REPLIES 0