prevent EEPROM corruption when battery runs out - Page 1
Forum section: electronics / general

author: curiousZombie
author_rank: Newbie
timestamp: December 08, 2016, 02:28:05 am
content: Hi all, first post --- be gentleI have a project where a PIC12F1840 with some LEDs are powered by 2 coin cell CR2032 batteries. I'm gathering data and storing to EEPROM every minute. The device will eventually run out of batteries and I notice the EEPROM data often gets corrupted, I'm guessing the voltage gets too low during an EEPROM write.Is there a graceful way to stop EEPROM writing before the power gets too low?The challenge is that I've exhausted all I/O pins for digital input and output so hardware modification is not an option, I need a reliable software routine that stops EEPROM writes when the battery is about run out (battery mileage may vary so a set timer isn't suitable).Any expertise in this matter would be much appreciated.Thank you

author: Gixy
author_rank: Regular Contributor
timestamp: December 08, 2016, 06:35:09 am
content: Each time you have to write data, first write dummy data and read them back. If ok then write real data, if not battery is low. Not 100% safe but should lower the problem.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: December 08, 2016, 07:00:59 am
content: - backup a last known good config- CRC checks over records (parts) or the whole configuration- two phase commit (like Gixy said) - but not with dummy data. Write the real data and set a flag for temp/phase 1. Read back and clear flag if ok. If you read data with this flag set, you know its going downhill. Use the backup value instead. This will wear down your EEPROM twice as fast (2 writes).

author: lwatts666
author_rank: Unknown
timestamp: December 08, 2016, 09:05:07 am
content: The easy approach for the PIC12F1840 is to use the programmable brown-out-reset. This will hold the chip in reset when the supply voltage falls below the programmed limit.

author: tszaboo
author_rank: Super Contributor
timestamp: December 08, 2016, 09:08:44 am
content: The PIC works down to 1.8V. Set up a brownout reset. Also, some EEPROMs will work down to 1.5V, change to one like that.

author: JPortici
author_rank: Super Contributor
timestamp: December 08, 2016, 09:38:15 am
content: you could also add a bigger bypass cap in parallel with the usual 100n ones so the voltage will sag slower during eepom writingthen once in a while you could read vdd and determine if batteries are depleted (hint: read the FVR value. that's what i do in these situations)

author: TheDane
author_rank: Regular Contributor
timestamp: December 08, 2016, 09:56:27 pm
content: It might be possible to read out the supply voltage, without a hardware modification.According to the datasheet, under the ADC section the following is stated:16.1.3 ADC VOLTAGE REFERENCEThe ADPREF bits of the ADCON1 register provides control of the positive voltage reference. The positive voltage reference can be:• VREF + pin• VDD• FVR 2.048V• FVR 4.096V (Not available on LF devices)See Section 14.0 “Fixed Voltage Reference (FVR)” for more details on the fixed voltage reference- so measuring the VDD and a Fixed Voltage Reference (and possibly doing some scaling/comparing if the 2.048FVR drops as well) it might give you an idea software wise, when to stop (or slow down) writing to the EEprom.I have not worked that much with PIC's, so I don't know if it interferes with some of the GPIO pins while sampling VDD/FVR 'registers'.

author: wraper
author_rank: Unknown
timestamp: December 08, 2016, 10:04:59 pm
content: Quote from: Gixy on December 08, 2016, 06:35:09 amNot even remotely safe at all and will wear out EEPROM twice as fast. Another issue is that when you write something at low voltage, it's not guaranteed that write will occur even at the correct address. You may just corrupt some other data.

author: wraper
author_rank: Unknown
timestamp: December 08, 2016, 10:17:20 pm
content: Set brown-out reset (2.4 or 2.7 V). If you are using internal EEPROM, use CODE-PROTECT against accidental writes (in 11.2 chapter in the datasheet). You can also use combination of DAC with ADC or comparator for detecting the power voltage without utilizing any pins.

author: wraper
author_rank: Unknown
timestamp: December 08, 2016, 10:31:14 pm
content: Silabs EFM8UB10 MCUs I work with, unless internal voltage monitor is enabled, won't even try writing into the FLASH (they have area with small page size instead of EEPROM). And MCU will just reset if try doing so. I recall woes about "unreliable" EEPROM in AVR MCUs (people too stupid to enable brown-out detect) and using external EEPROM ICs instead.