admin管理员组

文章数量:1405514

I am working on an STM32F446RE-based traffic light system using Eclipse IDE. The project uses a host-to-target communication method to control GPIOs remotely.

The code is supposed to turn on and off Red, Yellow, and Green LEDs based on the state of a GPIO input (PB10). However, when I use multiple HAL_GPIO_WritePin() calls in sequence, the behavior is incorrect.

     if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10) == GPIO_PIN_RESET) {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);   // Red ON [Pedestrain crossing]
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // Yellow OFF
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET); // Green OFF
} 
else {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);   // Red ON
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // Yellow OFF
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET); // Green OFF
    HAL_Delay(2000);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // Red OFF
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);  // Green ON
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // Yellow OFF
    HAL_Delay(1000);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET); // Green OFF
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);   // Yellow ON
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // Red OFF
    HAL_Delay(500);
}

If I use only the first two HAL_GPIO_WritePin() calls, the Red and Yellow LEDs work correctly. If I add a third HAL_GPIO_WritePin() call for the Green LED, the code does not behave as expected. Sometimes, the LEDs do not toggle correctly, or they remain in the wrong state.

Adding a delay does not fix the issue. Since I am using host-to-target communication, I may not be seeing the real-time execution flow.

  1. Since multiple HAL_GPIO_WritePin() calls are executed sequentially, the final LED state may be getting overridden?
  2. If the commands are being sent too quickly, the target may not register them correctly?
  3. Why does adding a third HAL_GPIO_WritePin() call disrupt the expected LED behavior?
  4. Is there a limitation in how GPIO writes are processed in STM32 when using host-to-target communication?
  5. What debugging steps should I take to diagnose and fix this issue?

本文标签: eclipseSTM32 Traffic Light Code HALGPIOWritePin api Not Working as ExpectedStack Overflow