Here is a library with which you can connect a keypad that is built as an X / Y matrix to the STM32F4.
It does not matter what the arrangement of the key matrix is (3 × 3, 4 × 3, 2 × 5, etc.).
No external diodes or pull-ups are needed.
In the H-file the size of the keyboard matrix is set (in the example 3 × 4)
and in the C-File the GPIO pins are assigned to the X and Y direction.
There is a function to read out the pressed key, which returns "0" if no key is pressed. If one or more keys are pressed, the key number of the first key found is returned. (In Example 1 to 12)
To debounce the buttons no timer but only a loop counter is used.
(this event needs to be adapted for you)
Note: if the return value is> 0 even if no keyboard is connected, external pull-ups (4k7) must be attached to the Y-lines.
Images :
//--------------------------------------------------------------
// File : main.c
// Datum : 02.05.2014
// Version : 1.0
// Autor : UB
// EMail : mc-4u(@)t-online.de
// CPU : STM32F4
// IDE : CooCox CoIDE 1.7.4
// GCC : 4.7 2012q4
// Module : CMSIS_BOOT, M4_CMSIS_CORE
// Funktion : Demo der Key-Matrix-Library
// Hinweis : Diese zwei Files muessen auf 8MHz stehen
// "cmsis_boot/stm32f4xx.h"
// "cmsis_boot/system_stm32f4xx.c"
//--------------------------------------------------------------