在开、关和切换 LED STM32F303RE 之间切换

switching between on, off and toggle LED STM32F303RE

我有一个按钮 PC13 和我的 LED PA5。我想按下我的按钮,以便当前状态(打开、关闭和切换)切换到下一个状态。例如:LED 当前点亮,我按下一个按钮,LED 关闭,我再次按下按钮,它切换,我再次按下按钮,它保持点亮,等等。当我调试它时,计数器变量切换到我当前所在的所需数字,并且在我调试它时它工作得很好。然而,一旦我将它上传到板上,它就不会像在调试状态下那样工作。它打开然后切换,每次额外的按钮按下都不会改变。

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  int counter = 0;
  while (1)
  {
      int stateOfPushButton = 0;
      if (!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1){                          //Check if button pressed
          HAL_Delay(5);                                                         //check for bounce
          if (!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1){
              stateOfPushButton = 1;
          }
      }
      counter = (counter + stateOfPushButton)%3;
      if(counter == 0){
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,GPIO_PIN_SET);          //Led Switch On
      }
      else if(counter == 1){
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,GPIO_PIN_RESET);    //Led Switch Off
      }
      else{
          HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);                    // Led toggled
          HAL_Delay(1000);
      }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

编辑:

我添加了一个延迟功能来检查按钮中的反弹,由@Jose 提出,但问题仍然存在。

编辑 2:

降低延迟函数效果更好,虽然不是很理想。我想,我的中断与延迟功能发生冲突。

问题正如我在第二次编辑中所怀疑的那样,我的中断与切换模式下的延迟功能发生冲突。这是由于while循环不断循环,总是陷入第二个延迟函数的情况。这使得中断非常困难,因为您需要在切换模式下始终为中断计时,它不会被延迟功能捕获。

为了避免这种情况,只需增加第一个延迟功能,它负责避免信号反弹,使其在切换模式下比第二个延迟功能更长。

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  int counter = 0;
  while (1)
  {
      int stateOfPushButton = 0;
      if (!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1){                          //Check if button pressed
          HAL_Delay(1000);                                                          //check for bounce
          if (!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1){
              stateOfPushButton = 1;
          }
      }
      counter = (counter + stateOfPushButton)%3;
      if(counter == 0){
          HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,GPIO_PIN_RESET);  //Led Switch Off
      }
      else if(counter == 1){
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,GPIO_PIN_SET);          //Led Switch On
      }
      else{
          HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);                    // Led toggled
          HAL_Delay(20);
      }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}