AVR EEPROM data loss problem causes and solutions

Publisher:AngelicJourneyLatest update time:2015-07-23 Source: 51heiKeywords:avr  EEPROM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
To summarize the reasons for AVR internal EEPROM data loss: 


    1. Program problem; 
    2. Program runaway; 
    3. EEPROM related registers are written incorrectly due to external interference such as strong magnetic field, high voltage static electricity, etc.; 
    4. The system has a large inductive load, which will generate a reverse high voltage when the power is off, and the EEPROM may be self-erased. 
      ... (If there are any other reasons, please continue to list them so that we can improve and find a solution) 

    Regarding problem 1, the program problem is no longer within the scope of this article. 

    Regarding problem 2, the program runaway should be the main reason for EEPROM data loss. However, there are many reasons for program runaway. 
     
    First, the voltage is abnormal, the work is unstable, and the program runs away. To solve this problem, you can turn on the internal BOD or add an external reset chip to solve it. In low-power occasions, external reset is 

necessary  , after all, the BOD power consumption is too high.

    Second, the crystal oscillation is disturbed, the frequency is unstable, and the program runs away. To solve this problem, it is recommended that the crystal use full-range oscillation, and the routing should be as short as possible, and use ground wire isolation. 

    Third, the system is disturbed by the external environment, and the PC and other registers are modified, and the program runs away. Regarding this interference problem, the possibility of causing the program to run away should be small. If the environment is really bad 

, then you should think of doing electromagnetic shielding, ESD protection, etc. If it still doesn't work, then we can only suggest changing to another microcontroller. 

    Regarding problem 3, we can only optimize the circuit settings and try to avoid it, such as adding a shielding cover, adding ESD protection, adding TVS protection, adding capacitor decoupling to the power supply, etc. 

    Regarding problem 4, if the system really has a large inductive load, then pay attention to adding freewheeling diodes, filter capacitors, etc. for protection, and do not allow this reverse high voltage to be generated. In any case, 

this high voltage generated by the sudden power failure of the inductive load will not only affect the EEPROM, but also pose a threat to the entire system.

================================================== ================================================== ========== 

 

After the above hardware processing, although the possibility of EEPROM data 

loss  is very small, we still cannot guarantee that EEPROM data will not be lost. At this time, the reliability of EEPROM data must be considered from the software aspect. Then we

will continue to discuss from the software aspect. 

    My approach is to block, partition, verify and backup the data. Of course, the processing method mentioned here is just to provide an idea. You can do block with different data lengths, 

partition with different sizes, use different address mapping methods, and use more data backups. Let's continue to discuss with Mega168 as an example. 

 1. Mega168 EEPROM 512 bytes, divide the EEPROM into two areas, each area is 256 bytes, and then use 8 bytes as a segment, so each area has 32 segments.  
    Data area: 0x000 - 0x0FF   
    Segment 0: 0x000 - 0x007  
    Segment 1: 0x008 - 0x00F  
       ...  
    Segment 31: 0x0F8 - 0x0FF  

    Backup area: 0x100-0x1FF 

    Each segment has 8 bytes, of which the first 6 bytes are valid data and the last 2 bytes are CRC16 checksum. The data format is shown in the figure below:

 
 

 2. EEPROM read and write operations 
    EEPROM operations are performed in segments. 
    Segment write function: when writing data to the data area, first calculate the data CRC16 checksum, and then write the data to the data area and the backup area at the same time; 
    segment read function: when reading data, read the data area and the backup area at the same time. If the data area checksum is incorrect and the backup area data checksum is correct, use the backup area data to restore the data area data;   

  if the backup area data is incorrect and the data area data is correct, then write the data to the backup area and back it up again; if both the data area and the backup area data are incorrect, then return to read failure. 

3. Correspondence between the data area and the backup area 
    Data read and write operations are performed in segments. How are the internal data area and the backup area mapped? In order to prevent the data and the backup from being accidentally modified at the same time, the data and backup address spaces cannot be 

too close , and the addresses of the data and the backup should be as different as possible. Assuming the data address is Data_Addr and the backup address is Bakup_Addr, I use the following function to map the address: 
                 Bakup_Addr = (Data_Addr + 0x100) ^ 0x03F  
    plus 0x100 defines the address to the backup area, and XOR with 0x03F is to invert the lower 6 bits. In this way, the data and backup address spaces are far away, and the addresses are 7 bits different. 
    For example, the address of the third segment: 0x018 - 0x01F,  
          the corresponding backup area is: 0x127 - 0x120  
          as shown below:  
 
 

 

4. Add write protection judgment to the read and write functions, turn off the write protection before reading and writing EEPROM, and turn on the write protection immediately after reading and writing. This can effectively prevent accidental modification of EEPROM caused by program running away 



5. It is recommended not to use block 0.

Keywords:avr  EEPROM Reference address:AVR EEPROM data loss problem causes and solutions

Previous article:A balancing system based on Mega128 encoder to control stepper motors
Next article:Design of avr0~5V digital DC voltmeter

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号