2026-01-16 1:50 PM
I created standard project in CubeIDE for Nucleo 411RE ( with default settings)
I found some code (which interfaces 4x4 keypad) and try to repeat it with HAL
I setup pins PC0,PC1,PC2,PC3 with Input mode and Pull-up.
PC4,PC5,PC6,PC7 as Output and GPIO output level - High
all this based on my understanding code I try to repeat ( below )
Did I do correct repeating with HAL ? ( As I understand CubeIDE will generate GPIOs the same way as function ?
void keypad_init(void)
{
/*Enable clock access to GPIOC*/
RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN;
/*Set PC0 to PC3 as input*/
GPIOC->MODER &= ~(GPIO_MODER_MODE0|GPIO_MODER_MODE1|GPIO_MODER_MODE2|GPIO_MODER_MODE3);
/* Activate internal pullup resistor for PC0 to PC3*/
GPIOC->PUPDR|=GPIO_PUPDR_PUPD0_0|GPIO_PUPDR_PUPD1_0|GPIO_PUPDR_PUPD2_0|GPIO_PUPDR_PUPD3_0;
/*Set PC4 to PC7 as output*/
GPIOC->MODER |= GPIO_MODER_MODE4_0|GPIO_MODER_MODE5_0|GPIO_MODER_MODE6_0|GPIO_MODER_MODE7_0;
GPIOC->MODER &=~( GPIO_MODER_MODE4_1|GPIO_MODER_MODE5_1|GPIO_MODER_MODE6_1|GPIO_MODER_MODE7_1);
/*Set PC4 to PC7 as high*/
GPIOC->BSRR = GPIO_BSRR_BS4|GPIO_BSRR_BS5|GPIO_BSRR_BS6|GPIO_BSRR_BS7;
}
2026-01-16 11:09 PM - edited 2026-01-16 11:09 PM
> Did I do correct repeating with HAL ?
Your code is not "HAL". It only uses symbols defined in the ST device .h file for the STM32F4 - which is good because helps others understand the code. But it does not use any HAL or "LL" functions.
> As I understand CubeIDE will generate GPIOs the same way as function
No, it calls a HAL function rather than set GPIO registers directly.
By the way, CubeIDE does not generate code any longer. This function has moved to CubeMX.
2026-01-16 11:14 PM - edited 2026-01-16 11:14 PM
For simple matrix scanning, set all pins (rows and columns) as open-drain outputs with pull-ups. Set all row pins low, column pins high.
Then you need to implement a simple state machine in timer interrupt with three states:
DetectColumn (initial state): if any column input low - store column number, set that column state to low and all the other lines to high, switch state to CheckRow.
CheckRow: read row input state, deactivate all columns, activate all rows; if any row input was low, store row number, determine key pressed, switch to WaitForRelease; otherwise switch to DetectColumn.
WaitForRelease: if all column inputs high, switch to DetectColumn.
The state machine processing should be called every 5..10 ms.