Low voltage detection in PIC18

Thread Starter

raychar

Joined Nov 8, 2011
82
Hello everybody,

I recalled that some years ago I suceed in doing low voltage detection in PIC16 IC. By putting smaller capacitor with a resistor tied to voltage to a port, so when power being turned off, its voltage drop is faster than the main voltage drop which goes to MCU power that has larger value capacitor. So, MCU still has time to make detection on it and work in the program such as write some data to memory...It went very stable.

Now, in PIC18, there is LVD module, I make use of it. I follow what the data sheet mentioned, it suceed sometime but not every time. That is, sometimes, it don't store the data during turned off and so can't recover data when turned on again, I don't know why this happens, can anyone help and has some examples about it?

Thanks
 

JohnInTX

Joined Jun 26, 2012
4,787
rc, you did not specify which 18F you are using or which memory you are storing to but here are some general things to consider (from the 18F4520 datasheet):

Writing to the 18F EEPROM takes a long time (4ms/byte typ.) Presumably, your 16F setup used an external EEPROM with page-write and lower voltage capabilities which would allow multibyte writes.

Writing EEPROM also consumes additional current (10ms typ). Vdd must remain above Vmin during this additional load.

How fast are you getting to the interrupt? If another IRQ is being serviced at the same priority when the low voltage IRQ happens, it will delay getting with the saving to EE. Consider prioritized interrupts.

Not all PICs run at full speed with reduced voltage. If you are running a fast oscillator, you can drop out of spec at lower voltages.

You should consider using the brownout detector as well. Even if you are successful writing the EE, if the processor continues to run after Vmin, it can go stupid and corrupt things (actually happened to me).

Turn WREN off whenever not using the EE.

Set EEADDRH/L to point to an unused EE byte after use so that a spurious write won't clobber something useful.

Make sure your supporting circuitry also functions throughout the lower voltage range.

You can validate your setup by setting an unused output line when you enter the low voltage handler and clearing it when you are done. Trigger your scope with it. Then you can observe Vdd, the oscillator etc. while in the shutdown routine (don't forget to add 4ms for the last EEbyte which is still being written after the service is 'done'. For test purposes, you can also poll WR after the last byte)

Have fun!
 

JohnInTX

Joined Jun 26, 2012
4,787
The HLVD module notes when voltage is low so that you can take some action.
The brown-out detector halts processing and resets the chip to keep it from running out of spec and possibly running amok.
 

Thread Starter

raychar

Joined Nov 8, 2011
82
Thanks for the information.

It is 18F452. If I enable LVD and BOR, and need only to write bytes to two addresses. What voltage levels should I set in them respectively?

Thanks,
 

JohnInTX

Joined Jun 26, 2012
4,787
.. strictly speaking, that is. Referring to the datasheet secs 18.1, 22.1 and Figs 22-1/2, I see that

1) the minimum Vdd for the F452 is 4.2V.
2) the appropriate BORV1:0 range is 01 (4.16-4.5V) i.e. where the part will reset. Note that the lower limit is BELOW Vdd min. More on this later.
3) the only LVD range that would work is 1110 (4.5V - 4.77v).

Unfortunately, taking worst case, BOR goes off exactly when LVD does and you don't get anything saved. For me, that means that the F452 won't work without a different approach. (Over the years, I've learned that sometimes its difficult to get a PIC to meet specs, let alone bend them. Such transgressions are for hobbyists and those enjoy receiving phone calls in the middle of the night).

Some possible solutions:
Use the LF452. Its spec'd to run at much lower voltage. Then you can add nice spread between LVD and BOR to give you time to store your data. Vdd min will be detrmined by the V-F graph in Fig 22-2.

If you are stuck with the F452, try picking off your power supply sense voltage before the 5V regulator (through a voltage-divider). That way you can get advance notice before the input to the regulator gets to its Vmin.

Consider other ways to do it in firmware. One way I've done it is to have multiple data records in EEPROM. By updating them in sequence i.e. writing #1, then #2, #3 then #1 again you can ensure that you always have a recent record if not the MOST recent. How recent depends on what you are doing of course and you have to respect EEPROM write limits etc. That data records have to be protected by a checksum, CRC or some other way of knowing that you haven't clobbered one during power down but its easy enough to do.

Whatever your approach, avoid the temptation to fudge on the specs even though it seems to work in 'tests'. Microchip has lots more test equipment than we do and if they don't explicitly say it, don't do it.

The 'more-later'. The (L)F4520 is an upgraded replacement for the F452 that you should be using if possible. It has LOTS of improvements and they are better specified. Careful examination of 26.1 in DataSheet 39631E (18F4520) reveals Note 2 which, unlike the 452, says that even though BORVmin is less than Vddmin, full speed operation is guaranteed until BOR. These 'little' details are critical if you are serious about reliable operation. Don't want to switch? Consider this little note from the 18F452 page:
Please consider this device: PIC18F4520
For those of us fluent in Microchip-speak, this loosely translates to "F452? Whaddya, nuts??" What it means is the F452 is done. It will always be available but all improvements will be on the 4520.

As I wrote above, you'll still have to make sure you have enough stored energy after power fail to get the job done. Part of your shutdown routine should include turning off any external stuff and any internal timers, ADC etc to save some power.

I realize this is a lot to chew on just to save a few bytes of info. Unfortunately, the PIC doesn't care about any of that.

Have fun!
 
Last edited:

Thread Starter

raychar

Joined Nov 8, 2011
82
Hello,

Many thanks for your information. Really, it is a bit more than I can chew now. I am facing to learn and work on electronic, computer, mechanical, machining, designing, etc. when doing diy projects. I have limit time to focus on a specialised field.

I perfer to try the voltage divider and detect voltage method, I tried it in 452 but failed, so, I want to switch to use its LVD function. I think the reason of failure is, the voltage before the 5-voltage regulator is not high enough to get more voltage capacity(energy) for detection after turning off supply. I'll try increase it and try again..

I'll definitely switch to 18F4520 when the compiler is tested to suit it.

Thanks,
 
Top