AnsweredAssumed Answered

STM8L151K4 - I2C problem

Question asked by p.adm on Jun 18, 2012
Latest reply on Mar 24, 2013 by garcia.juan.002
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);)

Outcomes