1 Introduction
With the rapid development of computer application technology, mobile storage devices have been widely used. Among them, CF (Compact Flash) card is widely used in many mobile storage devices such as digital cameras, PDAs, notebook computers and other currently very popular consumer electronic products due to its advantages such as low price, small size, large storage capacity and high speed. . And CF cards are also widely used in other fields. The content described in this article is to read and write the CF card through the 8051 microcontroller.
2. How to operate the CF card
The operation of the CF card is similar to that of the computer's hard disk. There are two ways of addressing sectors: physical addressing (CHS) and logical addressing (LBA). Physical addressing uses columns. The surface, head and sector number represent a specific sector. The starting sector is track 0, head 0, area 1, followed by area 2, etc. Logical addressing uniformly addresses the entire CF card. The relationship between logical block addressing and physical addressing is: LBA address = (cylinder number × number of heads + number of heads) × number of sectors + number of sectors - 1
In actual use, using LBA addresses for addressing can greatly simplify the programming workload, avoid conversion and switching between cylinders, heads, and sectors, and break through the CHS's limitation of large-capacity inability to be addressed.
3. CF card file management
The CF card file management system is the same as the hard disk file management. It divides the CF card storage space into five parts: Master Boot Record Sector (MBR), DOS Boot Sector (DBR) , file allocation table (FAT), file directory table (FDT) and data.
Master Boot Record Sector (MBR)
The Master Boot Record Sector (MBR) is on cylinder 0, head 0, sector 1. The master boot record records the most important structural information of the disk. The master boot record is created when the hard disk is partitioned (Fdisk). The master boot record includes a small piece of execution code (master boot code), disk characteristics and hard disk partition table. The two bytes at the end of the master boot record must be the boot boot flag 0x55AA. The disk signature is located at 0x01B8, which specifies the disk operating system.
DOS boot sector (DBR)
The starting sector of the DOS boot sector (DBR) is cylinder 0, head 1, sector 1. The logical 0 sector of the disk is called the DOS boot sector, also known as the BOOT area. It consists of the following 5 parts:
Jump instruction. The jump instruction occupying 3 bytes will jump to the boot code, and its content changes with the DOS version.
Manufacturer identification and DOS version number. This part occupies a total of 8 bytes, and its content varies with the DOS version.
BPB (BIOS Paramenter Block, BIOS parameter block). BPB occupies 19 bytes starting from the 12th byte.
DOS bootloader. The DOS boot program is a BOOT code that occupies 480 bytes and is responsible for loading the three system files of DOS. The contents of this section vary with DOS versions.
End flag word, the end flag occupies 2 bytes, and its value is AA55.
The relevant parameters recorded in the BPB table can help programmers determine the disk capacity, the location and size of the file allocation table FAT, and the location and size of the file directory table FDT. Therefore, the FAT32 partition format is used as an example to discuss the structure of the BPB table.
The offset is 0BH, 0CH records the number of bytes in each sector, and 0DH records the number of sectors in each cluster. 0EH and OFH record the number of reserved sectors, 10H records the number of FAT tables, 15H disk media descriptors, 18H the number of sectors in each track, 24H the number of sectors in each FAT table, 2CH~2FH the starting number of the root directory.
File Allocation Table (FATl)
File Allocation Table FAT (File Allocation Tabel) is a table used by the file management system to allocate disk physical space to each file. It tells the operating system where the file is stored on the disk. A file allocation table (FAT) consists of a collection of table identifiers and cluster maps (formerly known as entries). An identical mirror copy is stored continuously behind the main FAT table. The table identifier includes the disk media descriptor (1 byte) and padding bytes, which occupies the cluster mapping area of two clusters in total (this is probably why the cluster number of the FAT file system starts with 2). The padding bytes are generally FF. ,The padding bytes of FAT32 also contain "dirty bits", that is, a disk check (FAT-ChkDsk/NTFS-AutoChk) is performed when the system starts. In FAT cluster mapping, 0000 represents an empty cluster, FFFF represents the end of the cluster chain, FFF7 represents a bad cluster, and the remaining values represent the cluster number of its subsequent cluster. The storage strategies of FAT12 and FAT32 are similar to FAT16. For example, the end cluster of FAT12 is FFF, the bad cluster of FAT32 is 0FFFFFF7, etc. (note that the high 4 bits of FAT32 are reserved). File Allocation Table (FAT2) is a full backup of FAT1.
File Directory Table (FDT) In the file directory table, each file occupies 32 bytes (occupies two lines when read with PC-TOOLS or DEBUB). The meaning of each byte is as follows: 0-7 bytes: File name; 8-10 bytes: extension; 11 bytes, indicating file attributes; 12-21 A total of 10 bytes are reserved fields, not used; 22-23 is the time when the file was created; 24-25 is the file creation time The date; 26-27 is the first cluster number occupied by the file, that is, the starting cluster; 28-31 is the length of the file with a total of 4 bytes.
3. Interface design between AT89S52 and CF card.
CF card has 3 working modes to choose from: I/0 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/0 mode and memory mode, 8-bit access mode can be used, or 16-bit access mode can be used. This article uses an 8-bit memory mode, and its interface circuit schematic is shown in Figure 1.
The P0 port of the AT89S52 is connected to the DO-D7 of the CF for the data line, the A0-A10 of the CF card are the address lines, and P3.6 and P3.7 are the read (RD) and write (WE) lines respectively. Since AT89S52 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--D7 of the CF card. And -CEl can be used as the chip select signal of the CF card. By setting -CEl to '0' to select the CF card, that is, -CEl is connected to P2.6 of AT89S52 to select the CF card. 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. RDY/BSY is the CF card status pin. When it is "0", the CF card is busy. When it is "1", the CF card can be operated. The schematic is shown in the figure:
Due to space limitations, only the code for writing one sector will be written (the code for reading a sector is basically the same) as follows:
;Write one sector
WRITE: MOV DPTR, #SETR_CNT
MOV A, #1
MOVX @DPTR, A
MOV DPTR, #SETR_NO
MOV A, SECTRNO
MOVX @DPTR, A
MOV DPTR, #CYLINDER_LOW
MOV A, CYLINDERL
MOVX @DPTR, A
MOV DPTR, #CYLINDER_HI
MOV A, CYLINDERH
MOVX @DPTR, A
MOV DPTR, #DRV_HEAD
MOV A, DRVHEAD
ANL A, #0FH
ORL A, #0E0H
MOVX @DPTR, A
MOV DPTR, #COMMAND
MOV A, 30H
MOVX @DPTR, A
WRITE512:MOV R6,#0
MOV R7,#2
WRITE: MOV DPTR,#DATA_REG
MOV A,#31H
MOVX @ DPTR,A
DJNZ R6,WRITE
DJNZ R7,WRITE
RET
4. Conclusion
The interface circuit designed in this paper explains the reading and writing methods of CF card, and uses the general MCS-51 series microcontroller as the system controller, which has good portability. According to this circuit, it can be applied to other microcontrollers to read and write CF cards. CF cards are used as storage media. They have large capacity, small size and low price. They can be used in MP3 players, digital cameras, mobile storage and other fields. This test is in The circuit board has passed the debugging, and cooperates with the computer serial port to read and write the CF card, and can perform data recovery on the CF card.
Previous article:Design of AT89S8252 microcontroller to realize contact IC card reading and writing control
Next article:Based on 51 microcontroller driving A4988 to realize counterclockwise rotation of stepper motor
Recommended ReadingLatest update time:2024-11-23 04:36
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs