cancel
Showing results for 
Search instead for 
Did you mean: 

Re: Tristating GPIO as an output

ramin110
Visitor

I used the code below but not working , did you find the solution ?

 

void SetPinTristate(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // Switch to input mode
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up, No pull-down
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}

void SetPinOutput(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Push-Pull Output
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}





while (1)
{
SetPinOutput(GPIOA, GPIO_PIN_1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(1000);

SetPinTristate(GPIOA, GPIO_PIN_1); // Switch to Tri-State (Hi-Z)
HAL_Delay(1000);
}

 

 

3 REPLIES 3

>>did you find the solution ?

ELEVEN years later.. probably not going to get the OP back

Which part are you using?

It might be quick just to flip a couple of bits in the GPIO registers.

Not looking unreasonable, perhaps single step or break-point so you can inspect the values and GPIO registers

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

"I attempted to dynamically switch between input and output on both the STM32F401RC and STM32F030F4P6 microcontrollers. However, only the initial configuration takes effect, and subsequent changes do not work as expected. On the ATmega328, this functionality performed flawlessly, but I prefer to implement it on the STM32F030F4P6 due to its lower cost. Unfortunately, I have not yet found a solution."

 

Also tried these methods:

 

HAL_GPIO_DeInit(GPIOx, GPIO_Pin);

 

#define GPIO_MODE_INPUT 0x0U

#define GPIO_MODE_OUTPUT 0x1U

void SetPinMode(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, uint32_t mode)

{

uint32_t position;

uint32_t temp;

 

// Loop through all the pins

for (position = 0U; position < 16U; position++)

{

// Check if the current pin is set in GPIO_Pin

if ((GPIO_Pin & (1U << position)) != 0U)

{

// Calculate the position for MODER register

temp = GPIOx->MODER;

temp &= ~(0x3U << (position * 2U)); // Clear the current mode

temp |= (mode << (position * 2U)); // Set the new mode

GPIOx->MODER = temp;

}

}

}

ramin110
Visitor

The code I wrote was right; I made a *** mistake: the programmer was in connect mode, and I had to disconnect it to operate.