cancel
Showing results for 
Search instead for 
Did you mean: 

STM8L151K4 - I2C problem

adaszeq
Associate
Posted on June 18, 2012 at 01:09

I want to send 6 Bytes by I2C to slave, but when I send address slave and wait for ACK the is no reaction. I used 10k pull-up resistor.

CLOCK_SPEED = 100000=100kHz

BUFFERSIZE = 6

/********************  main.c **********************/

#include ''stm8l15x.h''

#include ''main.h''

/* -------------------------------- I2C ------------------------------------- */

uint8_t i = 0;

__IO uint8_t Tx_Idx = 0;

__IO uint8_t NumOfBytes = BUFFERSIZE;

extern __IO uint8_t TxBuffer[BUFFERSIZE];

static void I2C_Config(void);

void main(void)

{

 

I2C_Config(); 

  

  /* Infinite loop */

  while (1)

  {}

}

static void I2C_Config(void)

{

        /* TXBuffer initialization */

TxBuffer[0] = 0x00;

TxBuffer[1] = 0x81;

TxBuffer[2] = 0x08;

TxBuffer[3] = 0x50;

TxBuffer[4] = 0x01;

TxBuffer[5] = 0x15;

/* GPIOD configuration */

//GPIO_Init(GPIOC, GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Out_OD_HiZ_Fast);

/* system_clock / 1 */

        CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);

/* I2C  clock Enable*/

        CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);

        /* Initialize I2C peripheral */

        I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);

        /* Enable Buffer and Event Interrupt*/

       I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);

       enableInterrupts();

       I2C_Cmd(I2C1,ENABLE);

       /* Send START condition */

       I2C_GenerateSTART(I2C1, ENABLE);

       while (NumOfBytes);

       //while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

}

/*************** stm8l151x_it.c *****************************/

NTERRUPT_HANDLER(I2C1_SPI2_IRQHandler, 29)

{

  switch (I2C_GetLastEvent(I2C1))

  {

      /* EV5 */

    case I2C_EVENT_MASTER_MODE_SELECT :

     /* Send slave Address for write */

     

I2C_Send7bitAddress(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter);

      break;

      /* EV6 */

    case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:

      if (NumOfBytes != 0)

      {

        /* Send the first Data */

        I2C_SendData(I2C1, TxBuffer[Tx_Idx++]);

        /* Decrement number of bytes */

        NumOfBytes--;

      }

      if (NumOfBytes == 0)

      {

        I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);

      }

      break;

      /* EV8 */

     case I2C_EVENT_MASTER_BYTE_TRANSMITTING:

      /* Transmit Data */

      I2C_SendData(I2C1, TxBuffer[Tx_Idx++]);

      /* Decrement number of bytes */

      NumOfBytes--;

      if (NumOfBytes == 0)

      {

        I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);

      }

      break;

      /* EV8_2 */

      case I2C_EVENT_MASTER_BYTE_TRANSMITTED:

      /* Send STOP condition */

      I2C_GenerateSTOP(I2C1, ENABLE);

      I2C_ITConfig(I2C1, I2C_IT_EVT, DISABLE);

      break;

    default:

      break;

  }

}

The last action, which I observed was in INTERRUPT_HANDLER (I2C_Send7bitAddress(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter)😉

3 REPLIES 3
frankmeyer9
Associate II
Posted on June 18, 2012 at 14:32

I propose to check the I2C bus while you send the address.

If the device is addressed, it will send an ACK, i.e. pull down SDA while the 9.th SCL cycle.

If you don't see this, either the address is not correct, or there is some hardware issue.

Stm32User
Associate II
Posted on June 19, 2012 at 17:01

Read the errata sheet on the device.  There are some I2C issues and workarounds.

juankrygarcia
Associate II
Posted on March 24, 2013 at 20:05

I have got the same problem, did you find the solution?

I would be helpful

regards