Skip to main content
Visitor II
January 17, 2024
Question

How To get GPIO Input data in STM32F103

  • January 17, 2024
  • 2 replies
  • 1003 views

Hi, I want to make I2C By GPIO I/O Ports Using STM32F103RBT6 64 Pins.

image.png

run my source code result is above figure.

When I2C Input Read time, It Can’t read data how can I get input data?

 

 

 

	Start_Setup_Time();
	//	b7 ~ b1 Addr + bo_wr + ack
	Setup_Addr(0xA0);

	//	ucReg Addr
	uint32_t regi_addr = 0x16;
	Setup_Regi_Addr(regi_addr);
	//	Start

	SDA_Up();
	SCL_Up();
	TIMER_us(50);
	Start_Setup_Time();
	//	b7 ~ b1 Addr + bo_wr + ack
	Setup_Addr(0xA1);

	uint32_t temp = 0;


	IO_Change(0);
	TIMER_us(50);

	for(uint32_t idx = 0; idx<8; idx++)
	{
		SCL_Down();
		TIMER_us(t_low);

		temp <<= 1;
		TIMER_us(t_low);

		SCL_Up();
		TIMER_us(t_low);
		TIMER_us(t_low);
		if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_11))
		{
			temp |= 0x1;
		}
		SCL_Down();

		TIMER_us(t_low);
	}
	TIMER_us(60);
	is_End_NACK();
	TIMER_us(150);
	//is_ACK();
	//	Start
	//	b7 ~ b1 Addr + bo_r + ack
	//	Data
	//	Stop
	End_Setup_Time();
void IO_Change(uint8_t io)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	if(io)
	{
		GPIO_InitStruct.Pin = SDA_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	}
	else
	{
		GPIO_InitStruct.Pin = SDA_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	}
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

 

 

    This topic has been closed for replies.

    2 replies

    Super User
    January 17, 2024

     


    @Simon_Sung wrote:

     It Can’t read data

    What, exactly, does that mean?

    What debugging have you done to find out what's going on?

    If you step through the code, does it correctly identify the input level?

     


    @Simon_Sung wrote:

     

    		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

     


    Remember that I2C requires open-drain drivers...

     

     

    Graduate II
    January 17, 2024

    Hello 

    Is your SDA_Pin defined as GPIO_PIN_11 ?

    Br JTP