View previous topic :: View next topic |
Author |
Message |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
How do I know if the power is cut off? |
Posted: Sun Dec 17, 2023 2:21 am |
|
|
Hello,
I am using the MCU PIC18F47Q83. I checked the write and erase times from the datasheet and found them to be 11ms. I want to create a delay using an RC circuit connected to the VDD and VSS lines when the +5V of the PIC is cut off. I would like to write data to EEPROM when the power is cut off using an ISR. How can I achieve this? Is there a peripheral device for this purpose? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sun Dec 17, 2023 6:44 am |
|
|
Several ways...
In the beginning, I fed 1/2 wave rectified low voltage AC into a MC1489(RS232 receiver) that fed an I/O pin. Used it both as a 'clock' ( 60hz ) as well as 'power off' signal
You could also use a spare ADC pin, measure the raw VDD ( before the big filter cap ! ). When it drops say after 4-8 line freq cycles , that would say 'loss of AC power.
Others probably will post their favourite way.
The 'trick' is be be sure you have at least 2-3X the energy needed for the EEPROM write AFTER 'power failed' so the write is sucessful ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Sun Dec 17, 2023 11:45 am |
|
|
This is very standard hardware and code.
First thing you need to do is calculate how many bytes you want to write.
This then determines how long the supply must remain at a safe voltage
to write the EEPPROM after the fail. This then determines the size of
the capacitor needed on the supply. Ensure you allow at least a 50%
margin on this calculation. On your chip VddMIN is 1.8v, so this is the
voltage the supply must be above at the end of all the writes.
Add the time for your detector circuit to trigger, and the time needed to
get into the ISR.
Assume the worst case time for the writes. 11mSec/byte.
Then you just use a convenient interrupt. Your chip supports three
standard interrupt pins. These are the easiest to use.
Sequence is simple. On the interrupt, perform the write, and then delay
in the interrupt for enough time for the power to fail. Have the code then
test the input pin for the ISR. if the interrupt has gone off, exit the
interrupt, otherwise loop and re-test.
The actual detection depends upon the nature to the supply. If AC
a simple missing cycle detector on the AC from the transformer. If
DC, then a comparator on the 5v line. This though means the supply
will already have to start falling before it can be detected. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Sun Dec 17, 2023 11:53 pm |
|
|
What I would do is set the hardware to power the board long enough to detect when the power is removed and then write the EEPROM locations. For example, a blocking diode and a large capacitor between the power input and Vdd, which powers the PIC. Then use an external interrupt to detect when power is removed from the board. I plan to use pin B0 as the EXT ISR.
I plan to use a 470uf capacitor with a 100k resistor. The byte I will write is 8 bytes. _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Mon Dec 18, 2023 2:45 am |
|
|
How much power does the board draw???. Critical.
Also can you reduce this by switching things off?. If so do this at the
start of the interrupt.
Remember as an important extra, that most PIC's have a minimum
supply 'rise time' that is allowed for the reset to work properly. You need
to make sure that this will still work correctly with the decoupling capacitor
present, or provide your own reset circuit.
Now the discharge formula for a capacitor is:
Vt=V0*(e^(-t/rc))
Now the real time will be worse than this, since though you can estimate
the discharge resistance from the current the circuit draws when the power
is on, many circuits do not maintain a linear relationship with voltage as
the voltage drops.
Lets assume the circuit draws 10mA. For your 8 bytes you need to allow
88mSec. Add an extra mSec to read the data and get into the ISR, so lets
say 90mSec. Assume detection at 4.5v. That would give an end voltage just
fractionally over 3V. However a 'nominal' 470uF capacitor will normally
have a specification of +/-20%, so could be as small as 390uF. Also this
will reduce as the capacitor ages. So as I said, allow 50%, so just 313uF.
With this the voltage will fall to just 2.56V. Still 'OK', but not a huge margin.
If the supply consumption is more than 10mA, you would have problems. |
|
|
|