Search code examples
stm32stm32f4discovery

Timers and push button interface stm32


How to develop a program so that when we press the push button a timer should start, and if the push button is pressed for more than 5 seconds message should get transmitted via UART and if the timer is below 5 seconds an error message should come up.

I need help with how to interface push button and timer in stm.


Solution

  • Regardless of your framework you can implement this algorithm which detects button press and release. To do so, you need to enable the EXTI on both rising and falling edges. The EXTI handler would look like:

      EXTI_Handler ()
      {
        /* resolve button status according GPIO value transition */
        if (button_pushed)
          {
            start_timer ().
          }
        else if (button_released)
          {
            local_seconds_count = get_timer_count_in_seconds ();
            if (local_seconds_count > 5)
              {
                /* 5s have elapsed */
                send_uart (OK_MSG);
              } 
            else
              {
                send_uart (ERR_MSG);
              }
            /* stop timer */
            stop_timer ();
          }
      }
    

    Other ideas may be basing on SysTick timer and HAL_GetTicks: just use one global variable to store button press instant and compare+clear it on release.

    You can find a great tutorial on STM32 timer in visualgdb site. For EXTI refer to this page.