Design
There are two points to note when using AT89C52 to read and write SD cards. First, you need to find a solution to achieve communication between the AT89C52 microcontroller and the SD card. Second, the logic level that the SD card can accept does not match the logic level provided by the AT89C52, so the level matching problem needs to be solved.
Communication Mode
There are two optional communication protocols for SD cards: SD mode and SPI mode. SD mode is the standard reading and writing method for SD cards, but when using SD mode, you often need to choose an MCU with an SD card controller interface, or you must add an additional SD card control unit to support the reading and writing of SD cards. However, the AT89C52 microcontroller does not have an integrated SD card controller interface. If you choose SD mode communication, the hardware cost of the product will be increased invisibly. In the case where the SD card data reading and writing time requirements are not very strict, choosing SPI mode can be said to be the best solution. Because in SPI mode, all data exchanges can be completed through four lines, and many MCUs on the market currently have integrated ready-made SPI interface circuits . Using SPI mode to read and write SD cards can greatly simplify the design of hardware circuits.
Although AT89C52 does not have an SD card hardware controller and there is no ready-made SPI interface module, the SPI bus timing can be simulated by software. This article uses the SPI bus mode to read and write SD cards.
Level Matching
The logic level of the SD card is equivalent to the 3.3V TTL level standard, while the logic level of the control chip AT89C52 is the 5V CMOS level standard. Therefore, they cannot be directly connected, otherwise the SD card may be burned. For the sake of safety, it is necessary to solve the level matching problem.
To solve this problem, the most fundamental thing is to solve the level compatibility problem of the logic device interface. There are two main principles: one is that the minimum voltage value of the output level device outputting a high level should be greater than the lowest voltage value recognized as a high level by the receiving level device; the other is that the maximum voltage value of the output level device outputting a low level should be less than the highest voltage value recognized as a low level by the receiving level device.
Generally speaking, the universal level conversion scheme is to use a dedicated level conversion chip like SN74ALVC4245, which can not only be used for boosting and bucking, but also allows the two power supplies to be asynchronous. However, this scheme is relatively expensive, and the general dedicated level conversion chip converts 8, 16 or more levels at the same time, which is a waste of resources compared to the system that only needs to convert 3 levels.
Considering that the SD card has unidirectional communication in the SPI protocol working mode, the transistor plus pull-up resistor method is used when the microcontroller transmits data to the SD card. The basic circuit is shown in Figure 1. When the SD card transmits data to the microcontroller, it can be directly connected because the electrical level between them just meets the above-mentioned level compatibility principle, which is both economical and practical.
This solution requires dual power supplies (a 5V power supply and a 3.3V power supply). The 3.3V power supply can be obtained from the 5V power supply using the AMS1117 voltage regulator .
Hardware interface design
SD card provides a 9- pin interface for the peripheral circuit to operate it. The 9-pin pins vary with the working mode. In SPI mode, pin 1 (DAT3) is used as the SPI chip select line CS, pin 2 (CMD) is used as the data output line MOSI of the SPI bus, and pin 7 (DAT0) is the data input line MISO, and pin 5 is used as the clock line (CLK). Except for power and ground, the reserved pins can be left floating.
The MCU that controls the SD card in this article is the low-voltage, high-performance CMOS 8-bit microcontroller AT89C52 produced by ATMEL, which contains 8K bytes of repeatedly erasable read-only program memory and 256 bytes of random access data memory. Since the AT89C52 only has 256 bytes of data memory, and the data writing of the SD card is in blocks, each block is 512 bytes, so it is necessary to add a RAM to the minimum system of the microcontroller. The RAM in this system uses the memory chip HM62256 with a capacity of 32K. When reading and writing to the RAM, the latch latches the lower 8-bit address and forms a 16-bit address space with the 8-bit address data of the P2 port, so that the SD card can read and write 512-byte blocks at a time. The system hardware diagram is shown in Figure 2.
software design
SPI Operating Mode
The SD card automatically enters the SD bus mode at the beginning of power-on. In this mode, a reset command CMD0 is sent to the SD card. If the CS level is low and effective during the process of receiving the reset command, the SD card enters the SPI mode, otherwise it works in the SD bus mode.
For the AT89C52 microcontroller without SPI serial bus interface, the specific method of using software to simulate SPI bus operation is: set the initial state of P1.5 port (simulated CLK line) to 1, and then set P1.5 to 0 after receiving is allowed. In this way, while the MCU outputs 1-bit SCK clock, it will shift the interface chip serially to the left, thereby outputting 1-bit data to P1.7 (simulated MISO line) of the AT89C52 microcontroller, and then set P1.5 to 1, so that the microcontroller outputs 1-bit data (high first) from P1.6 (simulated MOSI line) to the serial interface chip. At this point, the simulation of 1-bit data input and output is completed. After that, set P1.5 to 0 again to simulate the input and output of the next 1-bit data, and repeat this cycle 8 times to complete the operation of transmitting 8 bits of data through the SPI bus.
The implementation program of this article integrates the SPI bus read and write functions. The val variable passed is both the data written to the SPI and the data read from the SPI. The specific program is as follows: (The program is written in the compilation environment of Keil uVision2)
sbit CS=P3^5;
sbit CLK= P1^5;
sbit DataI=P1^7;
sbit DataO=P1^6;
#defineSD_D ISA ble() CS=1 //chip select off
#defineSD_Enable() CS=0 //Chip select on
unsigned char SPI_TransferByte(unsigned char val)
{
unsigned char BitCounter;
for(BitCounter=8; BICounter!=0; BitCounter--)
{ CLK=0;
DataI=0; // write
if(val&0x80) DataI=1;
val<<=1;
CLK=1;
if(DataO)val|=1; // read
}
CLK=0;
return val;
}
Previous article:Principle and design of intelligent controller for lighting lamps
Next article:Principle and Analysis of Wireless Burglar Alarm Designed by 89C2051
Recommended ReadingLatest update time:2024-11-16 14:44
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Traffic Light Control System Based on Fuzzy Control (Single-Chip Microcomputer Implementation)
- Principles and Applications of Single Chip Microcomputers (Second Edition) (Wanlong)
- MCU Principles and C51 Programming Tutorial (2nd Edition)
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
- How to use arrays in C language for microcontrollers
- Implementation of booting Linux from TF card on i.MX6UL
- Can you help me transfer the Ambarella A2S HD camera PCB + schematic diagram to PDF?
- In-depth analysis of T-BOX system solution: wireless connection unit
- Water level measurement voice alarm device
- Application of wireless pressure transmitter in the field of Internet of Things!
- I want to know the specific communication process between the external 24C02 EEPROM and the microcontroller. Which forum friend can tell me? Thank you.
- Failed to run Verilog with Cypress PSoC
- The problem of overcurrent capability of two MOS tubes when turned on
- TI Power