Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c - switching between on, off and toggle LED STM32F303RE

I have a push button PC13 and my LED PA5. I want to push my button so that the current state (on, off and toggle) switches to the next one. So for example: The LED is currently on, I push a button, LED switches off, I push the button again and it toggles, I push the button again and it stays on, and so on. When I debug this the counter variable switches to the desired number I am currently at and while I debug this it works perfectly fine. However, as soon as I upload it to the board it doesn't work like in debug state. It is on and then toggles and every additional button push doesn't change.

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 */
}

EDIT:

I added a delay function to check for bounces in the button, proposed by @Jose and the problem persists.

EDIT 2:

It works better, although not ideal, when decreasing the delay function. I guess, my interrupt gets into conflict with the delay function.

question from:https://stackoverflow.com/questions/65859746/switching-between-on-off-and-toggle-led-stm32f303re

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem was as I suspected in the second EDIT that my interrupt gets into conflict with the delay function in toggle mode. This is due to the circumstance that the while loop continues to loop over and always gets caught in the second delay function. This makes interrupting very hard, since you need to time the interrupt always when in toggle mode it is not caught in the delay function.

To avoid this, simply increase the first delay function, which is responsible to avoid signal bounces, so that it gets longer than the second delay function in toggle mode.

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 */
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...