I try to figure out how I can make sure, that my controller (STM32F030K6) is always able to save a 16 bit integer to the flash when the power is breaking down rapidly.

This controller has an Analog Watchdog that fires an interrupt when it detects a value below a certain threshold.

The ADC runs in continious mode and multiplexes 4 channels of which one is the critical and is used for the Analog Watchdog. So I figured I get a new value for this channel every 350 cycles ( 87.5*4, to scan and convert 1 channel the adc needs 71+12.5 = 87.5 cycles). When the IRQ fires it runs this function:

uint16_t data=...;

void ADC1_IRQHandler(void)
    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD,0x8007C00, data);


According to the data sheet, it takes this chip ~50µs to program a 16 bit value to the flash.

My set up is a PCB with a 24V supply, an LM317 generating 3,3V and a charge pump for a MOSFET, which takes the clock from the controller. I thought about shutting everything down before writing to flash, to save some power, but shutting down costs cycles...

I've got 100nF on the 24V input and 1µ on the input and output of the LM317 voltage regulator.

How can I make sure I can always save that value, even if the voltage on the input breaks down immidiately?


  • 1
    \$\begingroup\$ Store some charge behind a rectifier, and detect the power failure separately. \$\endgroup\$ – Ignacio Vazquez-Abrams Jul 2 '15 at 17:32
  • \$\begingroup\$ @IgnacioVazquez-Abrams Sorry, took me a bit to upload a schematic. Imgur seems to have problems today. I only have DC voltages, no rectifying anywhere. \$\endgroup\$ – JavaForStarters Jul 2 '15 at 17:35
  • 2
    \$\begingroup\$ You can't add one before the regulator? Because all you'd need is two diodes and a large cap. \$\endgroup\$ – Ignacio Vazquez-Abrams Jul 2 '15 at 17:35
  • \$\begingroup\$ Oh, now I get what you mean. A diode before the input cap for the regulator? \$\endgroup\$ – JavaForStarters Jul 2 '15 at 17:39
  • \$\begingroup\$ I could increase the capacity, but only if they stay SMD. But how big would they have to be if the controller dies at around 2,4V? \$\endgroup\$ – JavaForStarters Jul 2 '15 at 17:41

Something like this should work.

Since C1 only supplies the current for the LM317 + controller, you know it is discharging with ~35mA. If you set the threshold at (say) 10V, then you have at least 5V delta-V for C1 before U1 starts to drop out.

Say you want it to stay up for 1ms after power fail is detected, then

C1 = (0.001 * 0.035)/5 = 7uF. So you could use a 10uF cap.


simulate this circuit – Schematic created using CircuitLab

  • 1
    \$\begingroup\$ That's exactly what I needed! Thank you very much. The Threshold is at 18V actually, so even more time.. or smaller capacitance. \$\endgroup\$ – JavaForStarters Jul 2 '15 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.