【问题标题】:switching between on, off and toggle LED STM32F303RE在打开、关闭和切换 LED 之间切换 STM32F303RE
【发布时间】:2021-01-23 13:28:09
【问题描述】:

我有一个按钮 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:

降低延迟功能时效果更好,但并不理想。我猜,我的中断和延迟函数冲突了。

【问题讨论】:

  • 您是不是在考虑按键不够完善,信号会一时不稳定?
  • @Jose 在这个例子中没有
  • @Jose 我添加了延迟功能,但仍然没有任何变化

标签: c stm32 led


【解决方案1】:

问题是我在第二次编辑中怀疑我的中断与切换模式下的延迟功能发生冲突。这是由于 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 */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多