News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Shut down circuit for PIC 18F27K42

Started by JohnB, Feb 04, 2024, 05:50 PM

Previous topic - Next topic

JohnB

I need to save a bunch of values onto the MPU's EEPROM at power off and to do this I have added a 0.22F super cap to sustain the MPU while it saves the values.  The problem I get is that there is sufficient energy left in the supercap that it either/both restarts repeatedly during shutdown or if power returns too early it does not execute a full restart.

I need to dissipate the energy in the super cap after successfully saving all the values in the EEPROM. Attached is my revised circuit. I have added a N channel MOSFET to discharge the Super Cap on setting the Discharge input to low. I have a spare pin on the MPU to do this which I have configured as open drain.  The MOSFET I am using is a FDV305N which is the highest current rated MOSFET I could find in an SO-23 package.

When power is removed the 5V drops virtually immediately, the Power Monitoring line is compared against the MPS's FVR using the internal comparator which triggers an Interrupt.  The MPU saves the EEPROM then sets the Discharge line low firing the MOSFET to discharge the super cap.  I disable interrupts before saving so I hopefully won't get any more interrupts before the MPU finally shuts down.

Can anyone see any issues with this approach especially on start-up or shut down.
JohnB

trastikata

#1
Hello JohnB,

I am a bit confused - this is a N-channel MOSFET, thus it will conduct while the gate is high and the super-cap will never charge because you are effectively shortening the barrier diode D7 to ground?

Edit: I missed the open drain. But for how long it will keep it before the pin goes into high impedance state? I similar applications I used a  blocking diode and voltage divider with a cap to discharge the capacitor with  a single pulse from the MCU - however the time constant has to be calculated.

tumbleweed

I don't see how that would work at all.
The N channel mosfet will turn on via the pullup resistor on the gate, shorting the input 5V to GND, and eventually destroying the fet.

trastikata

Quote from: tumbleweed on Feb 04, 2024, 06:46 PMI don't see how that would work at all.
The N channel mosfet will turn on via the pullup resistor on the gate, shorting the input 5V to GND, and eventually destroying the fet.

I thought the same, but then I saw he mentioned the gate is connected to an open drain pin, which will hold the gate low until the pin goes high. But for how long before it goes to high impedance state might be a problem and won't discharge fully the cap.

JohnB

That was my concern, it's the fragile nature of the timing.  There must be a simpler way.
JohnB

david

I'm confused.   The FET is mentioned as a FDV305N which is N channel but isn't the schematic showing a P channel FET?

David

rick.curl

I think I'd rather stay away from discharging the supercap every time the power goes down.  Instead, use a voltage supervisor chip, like a TI TLV803 to hold the PIC in reset until the voltage is within spec. This will ensure that you get a clean start every time the power returns.

-Rick

david

Yes indeed.   If you get a brown-out on the mains supply the 5V input can flutter about a mid voltage then restore so you really want to save to EEPROM when the input voltage drops below say 3.5V then hold in reset until the voltage has risen to above 4.5V and preferably a time delay - i.e. hysteresis on the trigger point followed by delay.  I don't think you need to discharge the supercap.
As Rick has indicated - there's heaps of little chips which have this hysteresis and delays if required.  Years back we used Seiko chips by the bucket and they came in 100mV increments and with or without time delays.  Sorry I can't find a link but that division may have been sold on.

David

top204

#8
You can also use the HLVD peripheral to stop intermittent starts by setting its voltage so if it does not reach a particular voltage, the device stays in a loop, or goes to sleep for a while and the watchdog wakes it up to try the voltage again etc... I've used this method when working with solar panels, so when the solar panel is not quite giving enough voltage, the device does not start.

On a PIC18F26K40 device, the HLVD peripheral can use procedures such as shown below, and they should be the same for most devices that contain an HLVD peripheral. I say should, but we all know microchip by now, so there are bound to be pointless differences somewhere in different devices designed by a different uni student that microchip employed for a short while. :-)

'------------------------------------------------------------------------------
' The voltages to set a trip for the HLVD peripheral
'
$define cHLVD_TRIP_POINT_1_85V 0        ' Trip at 1.85 Volts
$define cHLVD_TRIP_POINT_2_06V 1        ' Trip at 2_06 Volts
$define cHLVD_TRIP_POINT_2_26V 2        ' Trip at 2_26 Volts
$define cHLVD_TRIP_POINT_2_47V 3        ' Trip at 2_47 Volts
$define cHLVD_TRIP_POINT_2_57V 4        ' Trip at 2_57 Volts
$define cHLVD_TRIP_POINT_2_78V 5        ' Trip at 2_78 Volts
$define cHLVD_TRIP_POINT_2_88V 6        ' Trip at 2_88 Volts
$define cHLVD_TRIP_POINT_3_09V 7        ' Trip at 3_09 Volts
$define cHLVD_TRIP_POINT_3_40V 8        ' Trip at 3_40 Volts
$define cHLVD_TRIP_POINT_3_60V 9        ' Trip at 3_60 Volts
$define cHLVD_TRIP_POINT_3_71V 10       ' Trip at 3_71 Volts
$define cHLVD_TRIP_POINT_3_91V 11       ' Trip at 3_91 Volts
$define cHLVD_TRIP_POINT_4_12V 12       ' Trip at 4_12 Volts
$define cHLVD_TRIP_POINT_4_32V 13       ' Trip at 4_32 Volts
$define cHLVD_TRIP_POINT_4_63V 14       ' Trip at 4_63 Volts

'------------------------------------------------------------------------------
' Setup the HLVD peripheral for general voltage monitoring
' Input     : None
' Output    : None
' Notes     : Waits for it to stabilise
'
Proc HLVD_Init_4_63V()
    HLVDCON1 = cHLVD_TRIP_POINT_4_63V               ' Set for a voltage trip of 4.63 volts or less
    HLVDCON0 = %10000000                            ' HLVDEN enabled
    PIR2bits_HLVDIF = 0
    Clrwdt
    While HLVD_BandGapVoltageStable() = 0: Wend     ' Wait for it to become stable
EndProc

'------------------------------------------------------------------------------
' Setup the HLVD peripheral for 4.12 volts
' Input     : None
' Output    : None
' Notes     : Waits for it to stabilise
'
Proc HLVD_Init_4_12V()
    HLVDCON1 = cHLVD_TRIP_POINT_4_12V               ' Set for a voltage trip of 4.12 volts or less
    HLVDCON0 = %10000000                            ' HLVDEN enabled
    PIR2bits_HLVDIF = 0
    Clrwdt
    While HLVD_BandGapVoltageStable() = 0: Wend     ' Wait for it to become stable
EndProc

'------------------------------------------------------------------------------
' Setup the HLVD peripheral for 3.09 volts
' Input     : None
' Output    : None
' Notes     : Waits for it to stabilise
'
Proc HLVD_Init_3_09V()
    HLVDCON1 = cHLVD_TRIP_POINT_3_09V               ' Set for a voltage trip of 3.09 volts or less
    HLVDCON0 = %10000000                            ' HLVDEN enabled
    PIR2bits_HLVDIF = 0
    Clrwdt
    While HLVD_BandGapVoltageStable() = 0: Wend     ' Wait for it to become stable
EndProc

'------------------------------------------------------------------------------
' Return band gap voltage status
'
Proc HLVD_BandGapVoltageStable(), Bit
    Result = HLVDCON0bits_HLVDRDY
EndProc

'------------------------------------------------------------------------------
' Enable the HLVD module
' Input     : None
' Output    : None
' Notes     : None
'
Proc HLVD_Enable()
    HLVDCON0bits_HLVDEN = 1
    PIR2bits_HLVDIF = 0
EndProc

'------------------------------------------------------------------------------
' Disable the HLVD module
' Input     : None
' Output    : None
' Notes     : None
'
Proc HLVD_Disable()
    HLVDCON0bits_HLVDEN = 0
EndProc

'------------------------------------------------------------------------------
' Set the trip point parameters for the HLVD peripheral
' Input     : pNegTrip
'           : pPosTrip
'           : pTripPoints
' Output    : None
' Notes     : None
'
Proc HLVD_TripPointSetup(pNegTrip As Bit, pPosTrip As Bit, pTripPoints As Byte)
   HLVDCON0bits_HLVDINTL = pNegTrip     ' Set Negative trip
   HLVDCON0bits_HLVDINTH = pPosTrip     ' Set Positive trip
   HLVDCON1 = pTripPoints               ' Set trip points
EndProc

'------------------------------------------------------------------------------
' Return HLVD voltage status
' Input     : None
' Output    : Returns 1 if the voltage level has tripped
' Notes     : None
'
Proc HLVD_OutputStatus(), Bit
    Result = HLVDCON0bits_HLVDOUT
EndProc

Then in the main code, you can use something like the code below at the beginning of the program to stop it actually doing anything if the voltage is too low:

    HLVD_Init_4_12V()               ' Enable the HLVD peripheral to see if there is enough voltage for the device to actually do things 
    Repeat                          ' \
        DelayMs 10                  ' | Loop until the HLVP peripheral is not triggered
    Until HLVD_OutputStatus() = 0   ' /
    HLVD_Init_4_63V()               ' Setup the HLVD peripheral for normal operation again

JohnB

thanks for the suggestions I'll give them a try
JohnB

rick.curl

How many writes do you need to do?  While there's nothing wrong with using a supercap, an ordinary aluminum electrolytic may be sufficient.

-Rick

tumbleweed

QuoteI thought the same, but then I saw he mentioned the gate is connected to an open drain pin, which will hold the gate low until the pin goes high.
At reset all IO pins are inputs until your code runs to change them, so at power on/off the gate is controlled solely by the pullup resistor R9.
If it's an N-channel fet that means it'll power up with the fet turned on. Maybe it's supposed to be a P-channel mosfet and not an FDV305N N-channel device.

In either case, if the mosfet is turned on the input 5V is shorted to GND through D7, which probably isn't desirable.

top204

#12
Are you sure you actually need to power off the microcontroller John?

When in sleep mode and all the peripherals switched off and pins brought to their right level for anything attached to them, the 18F K devices draw around 3uA to 5uA in the real world. I created a project recently that has a bunch of components around the microcontroller and the RTC chip not able to be switched off so it always drew current, and with some, unwonted but necessary, pull-up resistors, and an inexpensive LDO regulator, and I got the whole circuit to draw only 10uA by setting up before putting the microcontroller to sleep, so the batteries can last a couple of years.

Waking the microcontroller up is easy, peasy by using an INT pin and a button, and putting it back to sleep can either be with another button press with a time delay while pressed, or a seperate button, or a timeout. Then when the button is pressed to switch off the device, the EEPROM can be saved in case the batteries die while sleeping, then the circuit put to sleep.

RGV250

Hi,
QuoteAre you sure you actually need to power off the microcontroller
I might be wrong but I took this post as a need to save the values when the power to the microcontroller fails?

Bob

JohnB

You are right but there were 2 issues on shut down, 1 saving to EEPROM and 2 Preventing restart when the voltage was close to reset.
AI have not resolved saving, I am getting rubbish save to EEPROM at present.  The same routine works perfectly when the progrwam is running normally but corrupts the data on shut down
JohnB

tumbleweed

1. what FOSC freq?
2. what FVR setting is the comparator using (1V/2V/4V)?
3. do you have the BOR brownout reset enabled, and if so, at what voltage?
4. how many bytes of data are you trying to save?

trastikata

Quote from: JohnB on Feb 05, 2024, 11:07 PMAI have not resolved saving, I am getting rubbish save to EEPROM at present.  The same routine works perfectly when the progrwam is running normally but corrupts the data on shut down

Hello JohnB,

The AMS1117 requires about 1.2v dropout voltage, what is the comparator triggering point?

In your circuit the 5V cap is behind two Schottky diodes, probably with a total forward dropout of about 0.5v?

Can you check for how long after power failure the 3.3v line stays stable?

Is it possible to change the AMS1117 with VLDO regulator and check again?

JohnB

I have the FVR set at 2.048V and the power fail monitoring point sits at 2.14V so a drop of about 0.1V will trigger the comparator.
The processor is running at 64Mhz and I am saving 16 bytes in total to EEPROM.

I am about to set up a timer to measure the actual time it is taking to save the settings when the app is running, will let you know when I have measured it. 

Also, I am using a bench power supply for the 5V and turning it on and off from the bench supply which makes the 5V open circuit, with the real PSU connected it offers a low impedance when off so I would expect the 5V to drop more quickly.  Again, I will lash up the real PSU to see if it makes a difference.  I don't have any VLDO regulators to hand at present, I will add some in to my next order to Farnell.
 
(The reason I have been using the bench supply is to avoid handling the board when its carrying mains voltages)
JohnB

John Lawton

Put a 'scope on the supply line and look how the voltage collapses on power fail. Then you can see if the power holds up long enough to do the EEPROM save.

John L

JohnB

With a very temporary lash up using the real PSU its about 13 seconds after removing mains before there is any detectable drop in 3.3VDD. That must be plenty of time. I'll set up a safer config which uses the intended PSU and do some more testing.
JohnB