cancel
Showing results for 
Search instead for 
Did you mean: 

After LL_I2C_TransmitData8 the TXE flag keeps 0

DDong.0
Associate II

I'm using STM32F103RBT6 to connetc to a OLED with I2C1.

The init code is as below.

static void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  LL_I2C_InitTypeDef I2C_InitStruct = {0};
 
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
  /**I2C1 GPIO Configuration  
  PB6   ------> I2C1_SCL
  PB7   ------> I2C1_SDA 
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  /** I2C Initialization 
  */
  LL_I2C_DisableOwnAddress2(I2C1);
  LL_I2C_DisableGeneralCall(I2C1);
  LL_I2C_EnableClockStretching(I2C1);
  I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
  I2C_InitStruct.ClockSpeed = 400000;
  I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
  I2C_InitStruct.OwnAddress1 = 240;
  I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
  I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  LL_I2C_Init(I2C1, &I2C_InitStruct);
  LL_I2C_SetOwnAddress2(I2C1, 0);
  /* USER CODE BEGIN I2C1_Init 2 */
	LL_I2C_Enable(I2C1);
  /* USER CODE END I2C1_Init 2 */
 
}

The send data code is as below.

/***********************I2C_WriteByte********************************/
void I2C_WriteByte(unsigned char addr,unsigned char data)
{
  /* 等待空闲 */
    while(LL_I2C_IsActiveFlag_BUSY(I2C1));
    
    /* �?�起始�?  EV5,主模�?*/
    LL_I2C_GenerateStartCondition(I2C1);
    while(!LL_I2C_IsActiveFlag_MSL(I2C1)); 
    //I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT); 
    
    /* 设置I2C从器件地�?�,I2C主设备为写模�?*/
    LL_I2C_TransmitData8(I2C1,OLED_ADDRESS);
    while(!LL_I2C_IsActiveFlag_ADDR(I2C1));
    //I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);
    
     /* 寄存器地�?�*/ 
    LL_I2C_TransmitData8(I2C1,addr); 
    while (!LL_I2C_IsActiveFlag_TXE(I2C1));  
    //I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED);  
    
    /* �?��?数�?�*/
    LL_I2C_TransmitData8(I2C1,data); 
    while (!LL_I2C_IsActiveFlag_TXE(I2C1)); 
    //I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED);  
    
    /* �?�结�?��?*/
    LL_I2C_GenerateStopCondition(I2C1);
}

After I debug I found the program stucked at Line19.

Could anyone tell me what problem is it? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
DDong.0
Associate II

I got it. After Line 14 It should add the code as below.

LL_I2C_ClearFlag_ADDR(I2C1);​

View solution in original post

1 REPLY 1
DDong.0
Associate II

I got it. After Line 14 It should add the code as below.

LL_I2C_ClearFlag_ADDR(I2C1);​