Old text backup: AVR read-write EEPROM analysis

Source: Internet
Author: User

Since the EEPROM write cycle of the AVR is longer (generally milliseconds), special attention should be paid to it during programming. There's nothing to say about reading an EEPROM, it takes 4 clock cycles to read a byte of data, it can endure, it's more troublesome to write, Although the data placed in the EEPROM is not frequently accessed, although it is possible to use the read-compare-write mechanism to reduce the EEPROM write operation frequency, but in the process of writing, too long write cycle will cause some problems, the following analysis of several ways EEPROM write operations.

    1. Circular query Type

Write the address and data to the EEPROM-related registers, write the flag after the loop constantly query the write completion flag, until the completion of the write, exit the loop, order to execute other programs. During the time the write flag is written to completion, the program does nothing but keep querying for write-completion flags and responding to hardware interrupts. This time is so wasted, if it is a real-time demand for a relatively high application, waste is not only time, it is likely that the changes in the I/O status can not be timely response, if you want to do multiple bytes of EEPROM write operations, then the situation will be worse. In response to this situation, There is a solution is that this write-completion query in the software's dead loop (no operating system, the system will go into a dead loop after boot), each cycle once query write completion flag, so that you do not have to wait for the completion of the write operation and can do other things, but this will bring two problems. First, If the cycle of death is not guaranteed, then the completion of each EEPROM write operation flag Query is not guaranteed, so that the period of an EEPROM write operation is not guaranteed (the maximum delay time is the maximum time of a dead cycle); During the time of the EEPROM write operation and the completion of the write, the other subroutines in the dead loop cannot perform EEPROM read and write operations. The first question is not a problem if the maximum cycle is guaranteed within the delay range, otherwise it will have to be taken the following two ways; The solution to the second problem is that each EEPROM read and write operation should be added to the EEPROM write completion flag Register, if there is data is written to wait or exit, but wait and exit will cause wait delay and write operation is unsuccessful, the solution is to first put in a data buffer, The last write operation is completed and a byte is dragged from the buffer for the next write cycle, but this involves selecting the appropriate buffer size and the problem, which is discussed in the following interrupt operation.

    1. Timed query type

After writing the EEPROM address and data register write flag to start the timer, timed query write completion flag, this method inherits most of the advantages and disadvantages of mode 1, the only progress is to be able to determine the write operation delay time is the timing period.

    1. Interrupt type

Write EEPROM write completion interrupt subroutine, set a FIFO buffer, the data to be written into the FIFO, if the EEPROM operation is idle, then pull a byte of data from the FIFO to write the EEPROM, the completion of the write complete flag to return, and then to do other things, EEPROM write completion generates an interrupt, in the Interrupt service program and then pull a byte from the FIFO to write the EEPROM, so loop until the FIFO empty, this way although the first time to complete a write operation, but there is a and mode 1 mentioned the same problem, How to choose the size of this FIFO. Small, to write data may overflow lost, big, how big, 1K enough, 10K enough, there is so much RAM space on the hardware, which needs to be based on the actual resources and system needs to be determined. If the EEPROM write operation is very low frequency, Compared to an EEPROM write operation interval is much longer, then a few units of the FIFO is sufficient, if the EEPROM write operation frequency may be very high or a large batch of continuous write data will require a large FIFO, to ensure that the data is not lost, it depends on the bulk data size and write frequency to be determined, Also consider the limitations of available RAM space to find the best value, and FIFO overflow must be developed corresponding measures.

Last note two points:

    1. Before reading and writing, it is necessary to detect whether there is an EEPROM write operation, after the completion of the write must read the data to verify the success of the write operation, the failure of the EEPROM damage, to do a good job to respond to measures.
    2. If you want to save a 16-bit data, make sure that the 16-bit data is divided into two bytes are successfully written to the EEPROM to read the data, or just write a byte to read the data, the resulting data is obviously not what you want, if the data is an important parameter, the consequences can be imagined.

      (2007.11.5)

Old text backup: AVR read-write EEPROM analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.