cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RB AF bit of I2C1_SR1 don't work

Mykola_Levun
Associate III

Hello,
STM32F103RB microcontroller.

Mykola_Levun_1-1726487576667.png

7-bit master transmitter (see Figure 273).
When I send Address then I receive AF bit of I2C_SR1 register equals "1" (Acknowledge failure). But the diagram taken from the oscilloscope shows that after the address there is a Acknowledge bit equal to "0". Which does not correspond to the AF bit in I2C_SR1 register. Why? I also attach the program code.

 

/**
 * @brief This function must be called before using other functions from this file.
 *  None.
 * @retval None.
 */
void init_i2c1(void)
{
	RCC->APB2ENR |= 0x00000008;
	RCC->APB1ENR |= 0x00200000;
	GPIOB->CRL &= 0x44000000;
	GPIOB->CRL |= 0xDD000000;
	I2C1->CR2 |= 0x0008;
	I2C1->CCR |= 0x0028;
	I2C1->TRISE |= 0x0009;
	I2C1->CR1 |= 0x0401;
}

/**
 * @brief This function must be called to generate START condition.
 *  None.
 * @retval Execution result code:
 * 			- 0: Not successfully.
 * 			- 1: Successfully.
 */
unsigned char start_i2c1(void)
{
	unsigned int temp = 0;
	I2C1->CR1 |= 0x0100;
	/*while ((I2C1->SR1 & 0x0001) == 0x0000)
	 ;*/
	temp = 0;
	while (temp <= 10000)
	{
		if ((I2C1->SR1 & 0x0001) == 0x0001)
		{
			return 0x01;
		}
		else
		{
			temp++;
		}
	}
	return 0x00;
}

/**
 * @brief This function must be called to send address byte.
 *  address: Slave device address.
 *  mode: Operation mode.
 * 				- 0: Write.
 * 				- 1: Read.
 * @retval Execution result code:
 * 			- 0: Not successfully.
 * 			- 1: Successfully.
 */
unsigned char send_address_byte(unsigned char address, unsigned char mode)
{
	unsigned int temp = 0;
	if ((mode & 0x01) == 0x01)
	{
		address |= 0x01;
	}
	else
	{
		address &= ~0x01;
	}
	I2C1->DR = address;
	temp = 0;
	while (temp <= 10000)
	{
		if ((I2C1->SR1 & 0x0400) == 0x0400)
		{
			I2C1->SR1 &= ~0x0400;
			I2C1->CR1 |= 0x0200;
			return 0x00;
		}
		else
		{
			temp++;
		}
	}
	I2C1->CR1 |= 0x0200;
	return 0x01;
}

 



 

int main(void)
{
	int temp = 0;
	init_i2c1();
	while(1)
	{
		if (start_i2c1() == 0x01)
		{
			if (send_address_byte(0x90, 0x00) == 0x01)
			{
				temp++;
			}
			else
			{
				temp++;
			}
		}
		else
		{
			temp++;
		}
	}
}

 

3 REPLIES 3

> When I send Address then I receive AF bit of I2C_SR1 register equals "1" (Acknowledge failure).

How do you know?

> But the diagram taken from the oscilloscope shows that after the address there is a Acknowledge bit equal to "0".

Show.

JW

I'm looking through the debugger.

Mykola_Levun_0-1726493338529.png

This loop constantly returns 0x00 (see above is the full code).

while (temp <= 10000)
	{
		if ((I2C1->SR1 & 0x0400) == 0x0400)
		{
			I2C1->SR1 &= ~0x0400;
			I2C1->CR1 |= 0x0200;
			return 0x00;
		}
		else
		{
			temp++;
		}
	}

 

It's humm for me. Sorry, I have no answers.

JW

PS. Is this a genuine STM32F103, or a clone?