This problem is not actually reflected in your code,becausethe code is only used to arrange the chip to work,that is to say your output value is because your STM32 reads this value.
The reason why the STM32 reading keeps changing or causing errors is because of electromagnetic interfence between the lines.These problems come down to your hardware design capabilities.
If you want your readings to be more precise and stable,I recommed that you can parallel a 1uF elecreolytic capacitor in series wity your VREF pin.(If the stability is not high,you can continue to increase it ,but it is best not to exceed 10uF.) -> Suppressing power supply noise
(You can also reduce the distance between the pins and the chip to suppress noise ->ambient noise
However,if our hardware is already finish,I just want to enhance the stability through the code level.You can read the value multiple times and then average the values to eltiminate sudden errors.