2. How to operate the CF card
The operation of CF card is similar to the operation of computer hard disk. There are two ways to address its sectors: physical addressing (CHS) and logical addressing (LBA). Physical addressing uses cylinder, head and sector number to represent a specific sector. The starting sector is track 0, head 0, zone 1, followed by zone 2, etc. Logical addressing addresses the entire CF card uniformly. The relationship between logical block addressing and physical addressing is:
LBA address = (cylinder number × number of heads + head number) × number of sectors + number of sectors minus 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 limitation of CHS that cannot address large capacities.
3. CF card file management
The file management system of the CF card is the same as that of the hard disk. It divides the storage space of the CF card into five parts: master boot record sector (MBR), DOS boot area (DBR), file allocation table (FAT), file directory table (FDT) and data.
Master Boot Record (MBR)
The master boot record sector (MBR) is located at 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 mark 0x55AA. The disk characteristics are located at 0x01B8, specifying the disk operating system.
DOS Boot Region (DBR)
The starting sector of the DOS boot area (DBR) is at cylinder 0, head 1, sector 1. The logical sector 0 of the disk is called the DOS boot sector, also known as the BOOT area. It consists of the following 5 parts:
Jump instruction, which takes up 3 bytes, will jump to the boot code, and its content varies 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 Parameter Block) occupies 19 bytes starting from the 12th byte.
DOS boot program. The DOS boot program is a 480-byte BOOT code that is responsible for completing the three system files of DOS.
This part varies with different DOS versions.
End flag word, the end flag occupies 2 bytes and its value is AA55.
The parameters recorded in the BPB table can help programmers determine the capacity of the disk, the location and size of the file allocation table FAT, and the location and size of the file directory table FDT. Therefore, the structure of the BPB table is discussed using the FAT32 partition format as an example.
The offset is 0BH, 0CH records the number of bytes in each sector, 0DH records the number of sectors in each cluster, 0EH, OFH records the number of reserved sectors, 10H records the number of FAT tables, 15H disk media descriptor, 18H the number of sectors in each track, 24H the number of sectors in each FAT table, and 2CH~2FH the starting number of the root directory.
File Allocation Table (FATl)
The file allocation table (FAT) is a table used by the file management system to allocate physical disk space to each file. It tells the operating system where the file is stored on the disk. The file allocation table (FAT) consists of a set of table identifiers and cluster mappings (formerly known as table entries). An identical mirror copy is stored continuously after the main FAT table. The table identifier includes the disk media descriptor (1 byte) and the padding byte, which occupies a total of two clusters of cluster mapping area (this is probably why the FAT file system cluster number starts with 2). The padding byte is generally FF. The padding byte of FAT32 also contains a "dirty bit", that is, a disk check (FAT-ChkDsk/NTFS-AutoChk) is performed when the system starts. In the FAT cluster map, 0000 indicates an empty cluster, FFFF indicates the end of the cluster chain, FFF7 indicates a bad cluster, and the remaining values indicate the cluster number of its subsequent clusters. The storage strategy of FAT12 and FAT32 is similar to that of FAT16. For example, the end cluster of FAT12 is FFF, the bad cluster of FAT32 is 0FFFFFF7, etc. (Note that the upper 4 bits of FAT32 are reserved). The file allocation table (FAT2) is a complete backup of FAT1.
File Directory Table (FDT) In the file directory table, each file occupies 32 bytes (two lines when read out with PC-TOOLS or DEBUB), and the meaning of each byte is as follows: 0-7 bytes: file name; 8-10 bytes: extension; 11 byte, indicating file attributes; 12-21, a total of 10 bytes, are reserved fields and are not used; 22-23 are the time when the file is created; 24-25 are the date when the file is created; 26-27 are the first cluster number occupied by the file, that is, the starting cluster; 28-31, a total of 4 bytes, are the length of this file.
3. Interface design between AT89S52 and CF card
There are three working modes for CF cards: I/O mode, memory mode and IDE mode. The default mode of CF cards is memory mode, which is the most commonly used. If you use memory mode, you do not need to configure any registers. The circuit connection of each mode is different. In I/O mode and memory mode, you can use 8-bit access mode or 16-bit access mode. This article uses 8-bit memory mode, and its interface circuit schematic is shown in Figure 1.
The P0 port of AT89S52 is the data line connected to DO-D7 of CF, A0-A10 of CF card is the address line, and P3.6 and P3.7 are 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', you can access data 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', the attribute register of the CF card is accessed; when REG is '1', the CF card reads and writes data in the memory mode. RDY/BSY is the status pin of the CF card. When it is "0", the CF card is busy, and when it is "1", the CF card can be operated. The schematic diagram is shown in the figure: [page]
Due to limited space, only one sector writing code is written (the sector reading code is basically the same) as follows:
; Write a sector
WRITE: MOV DPTR, #SECTR_CNT
MOV A, #1
MOVX @DPTR, A
MOV DPTR, #SECTR_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 describes the reading and writing methods of CF card, and adopts the universal MCS-51 series single-chip microcomputer as the system controller, which has good portability. According to this circuit, it can be applied to other single-chip microcomputers to read and write CF cards. CF cards are used as storage media with large capacity, small size and low price. They can be applied to MP3 players, digital cameras, mobile storage and other fields. This experiment has been debugged on the circuit board, and the computer serial port can read and write CF cards, and data can be recovered from CF cards.
Previous article:Application of single chip microcomputer measurement and control technology in the development of flat plate thermal conductivity meter
Next article:Design of wireless communication device based on 51 single chip microcomputer and nRF905
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- On-policy, its process is the same as the above strategic gradient. Practical analysis
- 【ESP32-Korvo Review】 03 Blinker Lighting
- Answer the questions and win prizes | TDK special reports are waiting for you (Issue 2)
- Today is Chinese Valentine's Day, Alipay has launched an electronic marriage certificate, have you gone to pick up your partner?
- TI MSP430 series microcontroller serial communication baud rate calculation method
- [Atria AT32WB415 Series Bluetooth BLE 5.0 MCU Review] 3.1 MCU generates events and sends commands to Bluetooth APP
- What are the differences in the working principles of military radios and mobile phones? Are their power the same?
- 【Qinheng Trial】Four overall system designs (1/2)——Function integration of ch549 modules
- Share: Solution to BQ40Z80's constant hiccups
- Altium Designer v21.2.0.30