The processing and management of data in AT24C512 by single chip microcomputer

Publisher:fnfeecjknqucLatest update time:2014-08-14 Source: 21icKeywords:AT24C512 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the data acquisition system and intelligent instruments composed of single-chip microcomputers, there is often a large amount of data to be saved. With the development of digital measurement and control systems, people have put forward higher requirements for data storage, and the memory capacity used to store data is also increasing. However, while increasing the amount of data storage, people also hope to be able to operate the data in it more conveniently and efficiently (including browsing, adding and deleting, etc.), that is, as simple and easy as managing data on a PC. However, the single-chip microcomputer and the chip used to save data do not provide this function. For this reason, it is necessary to develop an effective method for managing single-chip microcomputer data. Based on the idea of ​​PC file management, this paper takes AT24C512 as an example to construct a method for managing single-chip microcomputer data similar to the file system, which greatly improves the efficiency of data operation.

1 AT24C512 Introduction

AT24C512 is a 64KB serial electrically erasable programmable memory produced by Atmel. It has 512 pages, each page is 128 bytes, the address of any unit is 16 bits, and the address range is 0000~0FFFFH. It uses an 8-pin package, has the characteristics of compact structure and large storage capacity, and can connect 4 chips in parallel on a 2-wire bus. It is particularly suitable for data acquisition systems with large-capacity data storage requirements, so it is widely used in measurement and control systems. The package of AT24C512 is shown in Figure 1. The functions of each pin are as follows: ①A0, A1——address selection input. In the serial bus structure, if 4 AT24C512 chips need to be connected, A0 and A1 can be used to distinguish each chip. A0 and A1 are 0 when they are left floating. ②SDA——bidirectional serial data input and output port. Used for data exchange between memory and microcontroller. ③SCL——serial clock input. Usually, the data on SDA is written into the memory on its rising edge, and the data is read from the memory and sent to SDA on the falling edge. ④WP——Write protection input. When this pin is connected to ground, write operations are allowed; when connected to VCC, all write memory operations are prohibited. If not connected, this pin will be pulled down to ground inside the chip. ⑤VCC——Power supply.

GND is connected to ground. NC is left floating. (1) Interface with microcontroller Since AT24C512 follows the interface characteristics of the AT24C series, the connection with the microcontroller can also follow the traditional method. Generally, A0, A1, and WP are connected to VCC or GND, and SCL and SDA are connected to the P1 port of the microcontroller, so that the microcontroller can operate AT24C512. (2) Device addressing Before starting to operate AT24C512, an 8-bit address word needs to be sent to select the chip for reading and writing. The format of the device address word is shown in Figure 2. Among them, "10100" is a fixed 5-bit binary; A0 and A1 are used to distinguish multiple AT24C512s; R/W is the read and write operation bit, which indicates a read operation when it is 1 and a write operation when it is 0. (3) Write operation The write operation of AT24C512 has two modes: write byte and write page. When writing a byte, usually after sending the device address word to AT24C512 and receiving the response signal, it is necessary to send two 8-bit addresses to select the address to write data. After receiving this address, AT24C512 will respond with a zero signal, then receive 8 bits of data, and return a zero response signal.

In the page write mode, AT24C512 can write a page of 128 bytes at a time. Its initialization process is basically the same as the byte write method. The difference is: after writing a data byte, the microcontroller does not send a stop state, but inputs 127 bytes after the response signal; after each byte is received, AT24C512 still outputs a zero response signal. (4) Read operation There are three types of read operations: current address read, random read, and read string. Its initialization process is basically the same as the write operation, except that the lowest bit in the device selection word must be changed to read. In the current address read operation mode, the address of the internal data will remain at the last read and write operation address plus 1 until the last byte is read and then returns to the beginning. Before the random read operation, a byte address must be written to the AT24C512 before it can be read. The read string operation can be either a current address read or a random address read. When the microcontroller receives a data word, it will respond with an acknowledgement signal. After receiving the acknowledgement signal, the AT24C512 will increase the address by 1 and then output the next byte. When the microcontroller receives the data but does not send an acknowledge signal, the reading process ends.

2 File system composition

In order to effectively manage the data in AT24C512, the author imitated the file management mechanism in PC and constructed a simple file system for AT24C512. Considering the real-time requirements of the measurement and control system and the limited hardware resources, a two-level tree directory organization was adopted.

In order to facilitate data management, the physical space of AT24C512 is divided into 1024 logical pages, each page is 64 bytes. According to the needs of the file system, the entire space of AT24C512 is divided into three parts: data area, which occupies the last 960 pages; page allocation area, which occupies the middle 30 pages; directory area, which occupies the first 24 pages. (1) Data area The last 960 pages of AT24C512 are used as the data area, and their serial numbers start from 0. This area is used as the storage area for file data. When storing file data, several pages are allocated from this area, and one page is stored each time. (2) Page allocation area The page allocation area records the allocation status of each page in the data area. Every 2 bytes in this area constitutes a record item, and there are 960 record items in total (30%64/2). The record items are numbered starting from 0, and each record item corresponds to the usage status of the corresponding page in the data area.

In actual applications, the data of a file is often larger than 64 bytes, so the data area of ​​such a file needs to occupy multiple pages. However, as files are added or deleted, the free space in the data area becomes discontinuous, so the allocation of the data area cannot guarantee continuity. Instead, it is determined on which page a certain part of the file content should be placed based on the current usage of the data area. In order to organically connect these scattered data, the record items are organized in the form of a linked list. Each file corresponds to a linked list. Each node in the linked list is a record item. The content of the record item is the number of the next record item. The content of the last record item is 0FFFFH, indicating the end of the linked list. Connecting the pages corresponding to these record items constitutes the complete data of a file.

In addition, if the content of the record item is 0EEEEH, it means that the corresponding data page is not allocated. (3) Directory area The directory area is used to store the main information of the file. Based on this information, you can know the file name, the date the file was generated, and the first storage address of the file data in the data area. The structure of the file directory item is as follows: ① File name. The file name consists of 4-byte double BCD code. When the file is generated, the keyboard inputs numbers (the keyboard only provides numeric keys on the hardware) as the file name, and the program automatically converts them into double BCD code. When they need to be displayed, they are decomposed into 8-byte single BCD code for display. ② File generation date. It occupies 4 bytes and is stored in the format of double BCD code, such as 20H, 04H, 03H, 20H means March 20, 2004. The date can be entered by keyboard or obtained through the clock chip. ③ File first address. Indicates the sequence number of the first data page allocated to it when allocating space for the file, that is, the number of the first record item in the corresponding linked list. The directory area occupies a total of 24 pages. Since each file information only occupies 10 bytes, a maximum of 153 (24%; 64/10) files can be stored in this file system. The logical relationship between the directory area, page allocation area and data area is shown in Figure 3.

Taking Figure 3 as an example, it is explained how the file system obtains file data: ① In the directory area, find the directory entry containing the file name according to the file name, so as to obtain the attributes of the file and its first address. For example, the date of the file "00000103" is November 24, 2003, and its first address is 0005H. ② According to the first address of the file, find the record item 0005H in the page allocation area. The value stored in the record item is 0007H, so it can be known that the next record item of the file is 0007H. ③ Similarly, the subsequent record items of the file can be obtained as 0008H, 0009H, 000BH, until 0FFFFH is read from the 000BH record item. At this time, it means that this is the last item, and there is no need to continue looking for the subsequent item. ④ So far, we know that the data of file "00000103" is divided into 5 parts and stored in the data area, which are stored in pages 0005H, 0007H, 0008H, 0009H, and 000BH respectively. As long as the data in these pages are read in order and connected, the whole data of the file is formed. ⑤ Similarly, file "00000001" uses pages 0002H and 0003H in the data area, and file "00015671" only uses page 000AH in the data area.

3 System Programming

According to the above file system structure, the system can manage all the data of AT24C512 in real time through the directory area and page allocation area. In the management process, the most important operations are adding and deleting files. (1) Adding files The main task of adding files is to find storage space for new files. The search steps are as follows: ① Find an empty position in the directory area. If the directory area is full (up to 153 files can be stored), report to the user and terminate the program; otherwise, record the position (recorded as MyFile). ② Calculate the number of pages required for file data, recorded as My Page. ③ Find and count the record items marked as empty in the page allocation area, whose content is 0EEEEH. If the number is less than MyPage, report to the user and terminate the program. ④ Fill in the file name and date in the MyFileA position, and fill in the serial number of the first empty record item found as the first address of the file. ⑤ Fill in the serial number of the next empty record item in the empty record item found in turn, and fill in 0FFFFH for the last empty record item. ⑥ Starting from the first address of the file, write the data to the corresponding pages of the data area in sequence according to the file linked list. (2) Deleting a file The main task of deleting a file is to reclaim the space occupied by the file so that it can be allocated to other files in the future. ① Find the file in the directory area, extract its first address, and record First. Then, clear the first byte of the directory area occupied by the file to 0FFH, indicating that the directory entry is free. ② Find the First record item in the page allocation area, extract its content, and record it as Next. Then, rewrite the content of the First record item to 0EEEEH. ③First=Next, repeat ② until Next=0FFFFH.

Figure 3 As can be seen from adding files, when searching for space, only the directory area and page allocation area are operated. Therefore, when deleting files, only the directory and page allocation area need to be released, and there is no need to modify the data area. This greatly improves the efficiency of deletion. (3) System formatting The purpose of system formatting is to initialize AT24C512 according to the format described above to correctly reflect the current usage status. The main tasks of formatting include: ① Write all directory areas to 0FFH to clear all data in the directory area; ② Write all record items in the page allocation area to 0EEEEH to mark them all as unused. Note: The source code of the file system program can be found on the website : www.dpj.com.cn.

4 Performance Comparison

In most systems, data storage in AT24C512 uses sequential storage: each time data is stored, it is written into the data space in order. Compared with the sequential storage method, the method described in this article has the following advantages: ① Simple operation during storage. In sequential storage, finding free space requires reading the stored data one by one until free space is found, which requires a large amount of data operation. The method described in this article only needs to read the directory area and the page allocation area, and the efficiency of searching for free space is high. ② Simple data deletion. In sequential storage, in order to locate the data to be deleted, the stored data must be read one by one until the data to be deleted is found, and then the free space is rewritten to an unused state. The method described in this article only needs to modify the directory area and the page allocation area, which not only locates the data quickly, but also requires little modification. ③ Data space recovery is realized. In the sequential storage method, after a certain data is deleted, the space occupied by the data may not be reclaimed. Because the recovered space will form fragments: data is stored before and after the space, but the length of the space cannot meet the needs of a larger length of data. The method described in this article uses linked lists to allocate storage space, allowing the data of a file to be stored non-contiguously, and the reclaimed space can be used freely. ④ By reading the directory area, the user can roughly know what data is stored in the file, while the sequential storage method cannot provide this information.

5 Conclusion

In order to achieve efficient management of real-time measurement and control system data, a simple file system was designed for AT24C512 in the measurement and control system according to the idea of ​​PC file system, including system formatting, adding files, deleting files and other functions, which has been successfully applied in the measurement and control system with large data volume. The file system can be applied to storage chips of different capacities with slight modification and has wide application value.

Keywords:AT24C512 Reference address:The processing and management of data in AT24C512 by single chip microcomputer

Previous article:Design and Application of Encrypted Storage Chip AT88SC1616
Next article:I2C bus ferroelectric memory FM31256 with RTC

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号