With the rapid development of computer application technology, mobile storage devices have been widely used. Among them, CF (Compact Flash) card was born in 1994. It is the earliest flash memory card launched and has the highest penetration rate among many products. Because CF cards have the characteristics of low price, small size, large storage capacity, and high speed, they are widely used in currently very popular consumer electronic products such as digital cameras, PDAs, and notebook computers. CF cards are also widely used in other fields. As described in this article, CF cards are used in dynamic EEG systems to store EEG signals.
Since the EEG signal stored in the CF card must be read from the PC, the EEG signal must be stored in the corresponding file format. The following will introduce the use of C8051F020 microcontroller to control CF card file storage, and introduce its software and hardware design in detail.
1 Interface circuit between C8051F020 and CF card
C8051F020 is a fully integrated mixed-signal system-level MCU chip. It uses Cygnal's patented high-speed, pipeline structure and the CIP-51 microprocessor core that is fully compatible with the MCS-51 instruction set. It has 64 digital I/O pins. There is 64KB of Flash memory that can be programmed in the system, 4 352B of on-chip RAM, and on-chip watchdog timers, VDD monitors and temperature sensors. The on-chip JTAG debugging circuit allows installation on the final application system. The product MCU performs non-intrusive, full-speed, in-system debugging; its MCU can operate with a voltage of 2.7" 3.6V within the industrial temperature range (-45℃-+85℃); port I/O, RST and JTAG pins are compatible with 5V input signal voltage.
CF card has three working modes to choose from: I/O mode, memory mode and IDE mode. The default mode of CF card is memory mode, which is also the most commonly used. If using memory mode there is no need to configure any registers. The circuit connections are different for each mode. In I/O mode and memory mode, 8-bit access mode or 16-bit access mode can be used. This article uses an 8-bit memory mode, and its interface circuit is shown in Figure 1.
The C8051F020's external data memory interface (EMIF) can be used to access off-chip memory and memory-mapped I/O devices. If the PRTSEL bit of the EMIF configuration register EMIOCF is set to '1', the EMIF can be located at ports 7, 6, 5, and 4; if the EMD2 bit is set to '1', the EMIF can work in a non-multiplexed mode; set EMDl~0 Set to '01' to make EMIF work in fragmentation mode without block selection. In this way, the P7 port is the data line D0"D7, the P6 port is A0"A7, the P5 port is A8"A15, and P4.6 and P4.7 are the read (RD) and write (WE) lines respectively.
Since C8051F020 is an 8-bit microcontroller, it is more convenient to access the CF card in 8-bit mode. By setting -CE2 to '1', data can be accessed by accessing D0 and D7 of the CF card. And -CEl can be used as the chip select signal of the CF card, and the CF card can be strobed by setting -CEl to '0'. That is, -CEl is connected to the high-order address P5.7 of C8051F020 to select the CF card. When the CF card is inserted into its socket, both CDl and CD2 are connected to ground (CND); P1.1 and P1.2 of C8051F020 are connected to CDl and CD2, and can determine whether the CF card is inserted into the CF card socket by detecting P1.1 and P1.2. The CF card works in memory mode, -OE and -WE are connected to the read (P1.6) and write (P1) of C8051F020 respectively. .7) Line. When REG is '0', access the attribute register of the CF card; when REG is '1', the CF card reads and writes data in memory mode.
2 C8051F020 reads and writes data from CF card
Some register addresses related to data reading and writing in the memory mode of the CF card with the circuit shown in Figure 1 are shown in Table 1.
One sector of the CF card is 512B. These data are read from the 0th register in Table 1, and the minimum read and write data is 1 sector each time, but it can also be set to read from the 2nd register. Out, read and write several sectors at a time.
The process of C8051F020 reading sector data from the CF card is: first read the status word of the 7th register. If it is 50H, it means there is no error and the CF card is ready; then it reads the status word of the 2nd, 3rd, 4th, 5th, and 5th registers respectively. Write corresponding data to the 6 registers to specify the sectors that need to be read and written; finally write 20H or 21H to the 7th register, read the data after the read status word is 58H, and the process is shown in Figure 2.
The command for C8051F020 to write data to the designated sector on the CF card is 30H or 31H. The operation is similar to reading data, except that after writing the data, the process cannot be completed until the data in the CF card is stable and enters an idle state.
When reading and writing the CF card, the sector locations that the CF card needs to access are written to the 2nd, 3rd, 4th, 5th, and 6th registers respectively. There are two writing modes: CHS (Cylinder/Head/Sector) mode. and LBA (Logical Block Address) mode. Its mode is determined by setting the 6th bit LBA of the 6th register: that is, if LBA=0, it is CHS mode; if LBA=I, it is LBA mode.
The conversion from CHS mode to LBA mode is as follows:
Where LBA is the logical block address, Cn is the cylinder number, HpC is the number of heads per cylinder, Hn is the number of heads, Sn is the sector number, and SpH is the number of sectors per head (cluster).
3 CF card file storage
The FAT (File Allocation Table) file system was produced from the late 1970s to the early 1980s and is a file system supported by Microsoft's MS-DOS operating system. There are currently three types of FAT file systems: FATl2, FATl6 and FAT32. The difference between them is the number of bits occupied by each record (Entry) in the actual FAT structure on the disk. FATl2 occupies 12 bits (1.5 bytes), FATl6 occupies 16 bits (2 bytes), and FAT32 occupies 32 bits (4 bytes). The data in each record is arranged in order of low byte first and high byte last.
When formatting a CF card, you must first understand a series of information such as the capacity of the CF card. By writing the command ECH to the CF card, you can read out one sector, which contains information about some parameters of the CF card, such as capacity, default number of cylinders, default number of heads, and bytes of each sector. number, the number of sectors per track and the total number of sectors on the CF card, etc. This provides important data for formatting the CF card.
In the formatted CF card, the FAT file system consists of the following four parts:
Area 0: Reserved area (boot area).
Zone 1: FAT zone.
Area 2: Root directory area (a floating FDT table exists in FAT32).
Area 3: File, directory and data area (hereinafter referred to as data area).
The boot area contains important information of the entire FAT file system, namely BPB (BIOS Parameter Block). The following are several important data structures of BPB:
In FAT32 format, BPB﹒ RootEntCnt is '0', so RootDirSec is always '0'.
If BPB. If FATSzl6 is not '0', the first sector address of the data area can be calculated according to the following formula.
FirstDataSec="BPB". ResvdSecCnt+(BPB.NumFATs*BPB.FATSzl6)+RootDirSec (4)
Therefore, the number of sectors occupied by the data area:
DataSec="BPB". TotSecl6-FirstDataSec (5)
The first sector address of the root directory (under FATl2, FATl6):
FirstRootDirSec=”BPB”. ResvdSecCnt+(BPB.NumFATs*BPB.FATSzl6)
If BPB. FATSzl6 is '0', then BPB. FATSz32 must not be '0', then:
FirstDataSec="BPB". ResvdSecCnt+fBPB. NumFATs*BPB. FATSz32)+RootDirSec (6)
DataSec="BPB". TotSec32-FirstDataSec (7)
FirstRootDirSec=”BPB”. ResvdSecCnt+(BPB.NumFATs*BPB.FATSz32) (8)
If the CF card has multiple partitions, the parameters calculated by equations (3) to (8) are only relative to the current partition, and the number of clusters occupied by the data area is:
CntofClus="DataSec"/BPB. SecPerClus (9)
When formatting the CF card, some parameters in the FAT data structure are determined according to the size of the CF card capacity, and then the total number of clusters in the data area is calculated according to equation (9), and the FAT format is determined based on the size of the total number of clusters. Generally speaking, if CntofClus<4 084 clusters, it can be formatted in FAT12 form; if ≥4 084 clusters and <65 524 clusters, it can be formatted in FAT16 form; if ≥65 524 clusters, it can be formatted in FAT32 form. .
If a cluster number N is given (N≥2), the first sector number corresponding to the cluster in the data area is:
FirstSecofClus=((N-2)*BPB.SecPerClus)+FirstDataSec (10)
Equation (10) expresses the corresponding relationship between FAT and data area, as shown in Figure 3. The position of each record in the FAT area corresponds to a cluster of data in the data area. At the same time, this record stores the next cluster serial number, so that a file forms a linked list in the FAT area. As for the storage location of the serial number of each cluster in FAT, it can be determined according to the formatting form (FATl2, FAT16, FAT32). Assume that each record occupies i bytes, then the Nth record is at the position N×i.
The end character of the file is 0xFFF under FATl2, 0xFFFF under FATl6, and 0x0FFFFFFF under FAT32. If a damaged cluster is encountered during writing, the damaged flag is written in the corresponding location (0xFF5 under FATl2, 0xFFF7 under FATl6, 0x0FFFFFF7 under FAT32), and the damaged cluster is skipped in the { table. When deleting a file, write all '0's in the linked list of the file in the FAT area to free up space. In this way, the corresponding data area in the linked list can be allocated by a new file. A FAT directory entry or file entry usually occupies 32 bytes and stores information related to the directory or file. Its data structure is as follows:
Whether the 12-byte name in this data structure is a directory name or a file name is determined by the member Attr. If it is a directory, Attr can be set to 10H; if it is a j file, Attr can be set to 20H, or it can be a combination of several attributes; if it is a long file name, Attr can be set to OFH. Detailed long file names can be found in the literature.
Previous article:Application of Haisuxin 8051 microcontroller TM52F1376 in electronic voting machines
Next article:OLED display implementation design based on C8051F023 microcontroller
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【AT-START-F425 Review】Review Summary
- What does the EAS test of the chip mean and what is its function? Please give me some advice
- gcc linaro cross-compilation tool download address
- Help: RSL10 RAM can only use 24KB, why can't the remaining 6x8KB be used? - Problem Solved
- Synopsys IP Resources: A look at the complete Ethernet PHY IP for high-performance computing SoCs
- What is Common Mode Rejection Ratio (CMRR)
- High Definition Smith Chart Black and White_the_Smith_Chart.
- Is there any difference between these two circuits?
- Help~How can I make the PCB package of this component according to the picture?
- Using R329 for offline face detection