In this article I show a simple C library I wrote for the STM32 to help debouncing keys. It is completely non-blocking and also includes a nice auto-repeat feature with progressive speed! I wrote this library to complete an assignment for professor Rudy Hofer’s Embedded Programming Principles course at the Conestoga College and now I am sharing it with anyone that might need it.

Figure 1

The library is fairly simple and there are only two functions: initKeyAutoRepeat() and readKeyAutoRepeat(). The first one is used to initialize the internal data structure, which includes the GPIO port, GPIO pin, GPIO state for pressed key, debounce time (in ms) and auto repeat start-time multiplier. These are public parameters, but the structure also includes some internal variables used to track key state and timing. The structure is also typedef so we can easily declare variables to store each key structure.

Source code of the initialization function is show below:

And the actual debouncing and repeating function is shown below:

Notice it receives a pointer to the key structure and returns a byte (uint8_t) which represents current key state:

  • 0 for key not pressed;
  • 1 for key pressed;

When key is pressed for more than “n” ms (where n = debounce time * auto repeat multiplier), auto repeat mode is engaged and function toggles its return value (from 0 to 1 to 0 and so on) while the key is pressed. An internal mechanism also increases the toggling speed over time, so in the beginning, repeating is slow and gradually speed-up while key is pressed! This way, the application code does not need to be aware of what is going on!

The library also uses the 32-bit tick timer (which operates as a free-running counter) to track all timing regarding debouncing and auto-repeat, it is non-blocking, so it won’t slow down your code while waiting for a key press or wait for a debounce timer to expire!

It uses ST’s CubeMX HAL functions so you can easily port the code to other microcontroller families.

Usage is pretty simple: declare as many key variables as needed, initialize them with initKeyAutoRepeat() and periodically call readKeyAutoRepeat() to check each key state! You need to configure your key pins as GPIO inputs and provide pulling devices (internal or external ones).

Don’t forget to set targetState parameter accordingly! So, if you are using a pull-up device and your key pulls the GPIO to ground when pressed, your targetState should be GPIO_PIN_RESET. If you are using a pull-down device and your key pulls the GPIO do VDD when pressed, your targetState should be GPIO_PIN_SET!!!

Checkout my GitHub repository for a fully working Nucleo32 board (STM32L432) example!

 

 

Auto-repeat and debouncing
Tagged on:                 

Leave a Reply