LCD Display behaving weird with STM32
If you reset software without turning off power, the display can be in one of the following states:


(State1) 8-bit mode
(State2) 4-bit mode, waiting for the first set of 4 bits
(State3) 4-bit mode, waiting for the second set of 4 bits



There's no way for you to know in which state the display is, but you can always set it into 8-bit mode. Set D7-D4 pins to 0b0011 and toggle the enable pin three times. This ensures that the display is in 8-bit mode. You can now switch to 4-bit mode by setting D7-D4 pins to 0b0010 and toggling enable pin.

More details here: Hitachi HD44780 LCD controller