Design of SD card main controller based on C8051F020
Source: InternetPublisher:李商隐身 Keywords: Microcontroller SD card control Updated: 2023/11/10
1 Introduction
Aircraft flight condition monitoring and data acquisition instrument. It is used to collect various flight data during aircraft test flights and requires a large amount of storage capacity. After the aircraft returns to the ground, the collected data need to be easily transferred to a general-purpose computer for further data processing and analysis. In order to transfer the collected data from the collection system to the general computer. In the past, the data storage part was designed as a detachable storage board, and the data storage board was designed with a CPU and serial port. Remove the data storage board from the data acquisition system, communicate with the general computer through the serial port, and transmit the data to the general computer. As can be seen. This method has several disadvantages: first, the operation is troublesome, and the storage board must be removed from the acquisition system every time and then reinstalled; second, frequent plugging and unplugging of the storage board reduces the reliability of the entire acquisition system: third , a general-purpose computer that reads data requires the installation of special programs. This program is specially developed for serial communication between a general-purpose computer and a data storage board, which increases the complexity of use. SD/MMC card is a memory card with large capacity (up to 4GB), high cost performance, small size and simple access interface. Moreover, the files in the card can be easily copied to a general-purpose computer through a USB card reader. At present, SD cards have been widely used in digital cameras, MP3s, mobile phones and large-capacity storage devices. In the design of this flight condition monitoring and data acquisition instrument, SD cards are used to store the collected data.
2 Hardware Function Principle and Design
The appearance and interface of the SD card are shown in Figure 1. According to the different communication protocols between the SD card and the main controller, the SD card provides two external access modes: SD mode and SPI mode. Depending on the communication mode used, the functions of the SD card pins are also different. The specific pin functions are shown in Table 1. In the specific communication process, the host can only choose one of the communication modes. The selection of communication mode is transparent to the main controller. The card will automatically detect the communication protocol mode of the reset command. Once the communication mode is selected, the system cannot be changed while the system is powered on. In SD mode, the host controller uses the SD bus to access the SD card. However, the usual microcontroller does not have a hardware SD bus. Although it can be simulated by software with the help of a universal port line, the access speed is low and takes up a lot of CPU time. Most microcontrollers have an SPI bus. This article uses the SPI bus to access the SD card. . The following mainly introduces the SPI bus method.
Note: S--power supply; I--input; O--push-pull output; PP--push-pull I/O.
In SPI bus mode. CS is the chip select signal sent by the main controller to the card, and SCLK is the clock signal sent by the main controller to the card. DI (DataIn) is a one-way data signal sent by the main controller to the card, and DO (DataOut) is a one-way data signal sent by the card to the main controller. The internal structure of the SD card is shown in Figure 2. In addition to having a large number of storage units inside the SD card. It also has a card interface controller, registers, and external interfaces in SD and SPI modes. The external signal line of the external host controller access card is not directly connected to the memory unit, but is connected to the memory unit interface through the card's interface controller. In this way, the main controller does not directly access the memory, but accesses the memory through the card's interface controller. The reading, erasing and writing of the storage unit in the card are automatically processed by the card interface controller according to the command of the main controller. The main controller does not need to know how to operate and manage the storage unit in the card. This will greatly reduce the main controller's burden on the card. memory operation burden. There are 6 information registers inside the SD card, which are used to set and save key information of the operation card. There are two status registers, which are used to record the current status of the operation card.
The main controller of the collector adopts C8051F020 microcontroller. The C8051F series of microcontrollers are fully compatible with 8051 in terms of instructions, and their performance is much higher than that of the standard 8051 microcontroller. In addition to all the peripherals of 8052, the C8051F series microcontrollers have. It has also expanded its rich peripherals and is a veritable SOC (System On Chip) microcontroller. In addition to meeting the data collection function, C8051F020 can also easily access the SD card. Features related to the SD card system are as follows:
- The microcontroller adopts a pipeline structure internally. The instruction execution speed is high. The instruction running speed is about 10 times higher than that of the general 80C51 series microcontroller. The instruction running speed can reach 25MIPS, which meets the functional requirements of high-speed operation of SD cards.
- Has SPI hardware interface. It can interface well with the SPI bus of the SD card.
- Has eight 8-bit I/O ports. In addition to meeting data collection needs. There are enough interface lines for connecting the microcontroller and SD card. and. These port lines can be configured through software to obtain different functions. The SPI interface is obtained by configuring relevant registers.
- Has a scalable interrupt system. Supports 22 interrupt sources. 2 priority levels, including interrupts generated by the SPI interface.
- On-chip memory includes 64KB Flash and 4KB XRAM. No need to expand memory. This can meet the needs of creating a file system and accessing the SD card.
- 3.3V working voltage, compatible with SD card working voltage.
- The clock system is more complete. Internal clock can be used. An external clock can also be used. Can be switched through programming.
- In-system debugging through the JTAG interface can be achieved. Facilitate system development.
The main features of the SPI serial interface of the C8051F series microcontroller are as follows: full-duplex, three-wire synchronous transmission, that is, it can be received while sending; it can work in host mode or slave mode; the maximum data transfer rate of the host (bits/second) It is 1/2 of the system clock frequency. Therefore, it can reach up to 10Mb/s; the SPI bit transmission rate can be selected through programming; the interrupt flag is set when the transmission is completed. No CPU time is occupied during transmission; the polarity and phase of the serial clock can be changed programmably; it has write conflict protection and bus competition protection.
For the SPI system of C8051F020. First, set the I/O port function selection switch control registers XBR0, XBR1, and XBR2. Configure the I/O ports P0.0, P0.1, P0.2, and P0.3 as SPI function pins SCK, MISO, MOSI, and NSS. At this time, the I/0 ports P0.0, P0.1, P0.2, and P0.3 are combined into an SPI interface. The special function registers that set and control the performance of the SPI interface include SPI0CFG, SPI0CKR, SPI0CN, and SPI0DAT. SPI0CFG is the SPI configuration register, used to configure the working mode of SPI. And reflects the data sending status during the communication process. SPI0CN is the SPI control register. Used to control the work of SPI. And reflect some error signs during the communication process. SPI0CKR is the clock rate register of SPI. Used to select the frequency of SCK output. SPI0DAT is the SPI data register, used to send and receive SPI data.
The circuit connection diagram between the main controller C8051F020 and the SD card holder is shown in Figure 3. In addition to the SPI interface connection, there are three control lines. P0.4_SD_INSERT is the I/O port P0.4, which is used to detect whether the SD card is inserted in the card holder. This pin is low when fully inserted and high otherwise. P0.5_SD_WP is the I/O port P0.5, which is used to detect whether the SD card is currently write-protected. This pin is high level when write protected, otherwise it is low level. P0.6_SD_POWER is the I/O port P0.6, which is used for power supply control of the SD card. When this pin outputs low level. Power the card. Otherwise, the power will be cut off. This is so that when the SD card enters an uncertain state, the card can be reset by powering on again without removing the card.
3 Software design
The software design idea is for the portability and ease of use of the program. Divide the program into three levels. The first level is the hardware abstraction layer. The second layer is the command layer that implements access to the SD card, and the third layer is the application layer, which provides interface functions to users or file systems.
The hardware abstraction layer includes the hardware environment configuration for accessing the SD card, the basic functions of the SPI interface for communication, and the processing of SPI interrupts. Hardware environment configuration includes hardware initialization, memory variable initialization, etc. Such as configuring the I/O port of C8051F020 to the relevant settings of the SPI interface, setting the SPI interface performance of C8051F020, powering on and disconnecting the SD card, detecting whether the SD card is inserted in place, setting specific parameters of the SD card internal register, etc. Another example is the SPI interface performance setting of C8051F020, including master/slave working mode setting, SPI communication frequency setting, etc. These contents are all related to the hardware. The SPI interface implements the basic functions of communication, including the basic functions of the main controller sending one byte to the SD card and reading one byte from the SD card. This is the basis of all SPI communication. SPI interrupt processing is when the SPI interface generates an interrupt. The interrupt service routine is automatically called. In the interrupt service routine, the SPI control register is read. Various status flags that generate interrupts are stored here. According to different status flags, different functions are called for processing.
The command layer is to call the basic sending and receiving byte functions of the hardware abstraction layer to implement all the commands specified by the SPI protocol of the SD card. Complete access to SD card. According to the SD card protocol, accessing different functions of the SD card is implemented by different commands. These commands are divided into 0-9 different categories. Each category includes several specific commands, and for the SPI protocol, each command is actively sent by the host. High bit first and low bit last, after the SD card receives the command. Respond differently to different commands. The length of the command sent by the host is always 6 bytes. The format of the command is shown in Table 2. Command represents the command number, which occupies 6 bits. Parameter represents the command parameter, which is 4 bytes in length. Different commands correspond to different command parameter values. There are 4 formats for SD card responses, namely R1, R1B, R2 and R3 formats. The host sends the command and reads the response to form a complete command implementation process.
Table 2 SPI mode command format | |||||
Byte 6 | Bytes 2 to 5 | Byte 1 | |||
0 | 1 | Command | Parameter (high bit first) | CRC | 1 |
The application layer function provides high-level interface functions for client programs to access the SD card. The client does not need to know the internal structure and command content of the SD card, nor does it need to know the SPI interface protocol and other basic contents. The client program only needs to simply call the interface function to understand the current status of the SD card, read or write the content it needs. own content. The application layer program is a higher abstraction based on the command layer and is implemented by calling the command layer functions. These functions include functions such as reading data, writing data, erasing data, returning card status, and returning card storage space usage.
4 Conclusion
The innovation of the author of this article is to apply the SD card storage method to the aircraft flight data collection system, which simplifies the design of the collection system, reduces the system size, improves the reliability of the system, and makes the reading of the collected data easier. Simple and easy. Since the original data acquisition system uses 8051 microcontroller as the main controller, this design uses C8051F020, a high-performance microcontroller that is fully compatible with 8051. It not only inherits the design of the original collection system, but also meets the functional requirements for accessing the SD card, speeding up the design process of the entire system.
- How to choose the right level of integration to meet motor design requirements?
- Car ice warning circuit sharing
- Controlling LEDs with Multithreading via Dual-Core Programming on Raspberry Pi using MicroPython
- How to Build a Gesture-Controlled Elevator Prototype Using Arduino Nano
- A small improvement to the temperature and water level indicator alarm
- Design and principle analysis of practical electric curtain remote control device
- Design and analysis of the principle of automatic power-off switch during power outage
- Design and analysis of a four-bit remote control component capable of remote reset
- Audio interface protection circuit
- Simple disconnection burglar alarm
- Design of temperature detection circuit implemented by single chip microcomputer
- Stepper motor drive circuit composed of LB1836M
- Negative voltage generation circuit diagram
- Minimum system interface circuit based on AT89C52 microcontroller
- Flashing wall light control circuit
- Photocell amplification control circuit
- Photoelectric tracking control circuit
- Signal input circuit a
- Use MC34063 as a 12-volt programming power supply for single-chip microcomputer
- Optoelectronic interface circuit between single-channel DA chip DAC0832 and microcontroller