This article mainly introduces the application scheme of SD card in power monitoring and reactive power compensation data collection system.
Design
There are two points to note when using AT89C52 to read and write SD cards. First of all, we need to find a solution to realize the communication between AT89C52 microcontroller and SD card; secondly, the logic level that the SD card can accept does not match the logic level provided by AT89C52, so the level matching problem needs to be solved.
Communication mode
SD cards have two optional communication protocols: SD mode and SPI mode. SD mode is the standard reading and writing method for SD cards. However, when selecting SD mode, you often need to choose an MCU with an SD card controller interface, or an additional SD card control unit must be added to support reading and writing of SD cards. However, the AT89C52 microcontroller does not have an integrated SD card controller interface. If SD mode communication is selected, the hardware cost of the product will be increased. When 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 integrate ready-made SPI interface circuits. Using SPI mode to read and write SD cards can greatly simplify the design of hardware circuits.
Although the AT89C52 does not have an SD card hardware controller and no ready-made SPI interface module, the SPI bus timing can be simulated using software. This article uses 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 safety reasons, 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: First, the minimum voltage value for the output level device to output high level should be greater than the receiving level device to identify as high level. The lowest voltage value of the level; the other is the maximum voltage value of the low level output by the output level device, which should be less than the highest voltage value recognized as low level by the receiving level device.
Generally speaking, a common level conversion solution is to use a dedicated level conversion chip like SN74ALVC4245. This type of chip can not only be used as a boost and a buck, but also allows the power supplies on both sides to be asynchronous. However, this solution is relatively expensive, and general dedicated level conversion chips convert the levels of 8, 16 or more channels at the same time. Compared with this system, which only needs to convert 3 channels, it is a waste of resources. .
Considering that the communication of the SD card is one-way under the working mode of the SPI protocol, 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 connected directly, because the levels between them just meet the above-mentioned level compatibility principles, which is both economical and practical.
This solution requires dual power supplies (one 5V power supply and one 3.3V power supply). The 3.3V power supply can be obtained from the 5V power supply using an AMS1117 voltage regulator tube.
Hardware interface design
The SD card provides a 9-Pin pin interface to facilitate peripheral circuit operation. The 9-Pin pins vary depending on 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. 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 a low-voltage, high-performance CMOS 8-bit microcontroller AT89C52 produced by ATMEL. It 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 SD card data is written in blocks, each block is 512 bytes, so it is necessary to add a piece of RAM to the minimum microcontroller system. The RAM in this system uses the memory chip HM62256, with a capacity of 32K. When reading and writing 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 block operations at a time. The system hardware diagram is shown in Figure 2.
software design
SPI working mode
The SD card automatically enters the SD bus mode at the initial stage of power-on, and sends the reset command CMD0 to the SD card in this mode. If the SD card is active at low level when CS is receiving the reset command, it will enter SPI mode, otherwise it will work in SD bus mode.
For the AT89C52 microcontroller without an SPI serial bus interface, the specific method of using software to simulate the SPI bus operation is: set the initial state of the P1.5 port (simulated CLK line) to 1, and then set it again after allowing reception. P1.5 is 0. In this way, while the MCU outputs a 1-bit SCK clock, it will shift the interface chip serially to the left, thereby outputting 1-bit data to P1.7 (analog MISO line) of the AT89C52 microcontroller, and then set P1.5 to 1, causing the microcontroller to Output 1-bit data (high bit first) from P1.6 (analog 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 to simulate the input and output of 1-bit data. Repeat this cycle 8 times to complete an operation of transmitting 8-bit data through the SPI bus.
The implementation program of this article integrates the SPI bus reading and writing functions. The val variable passed is both data written to SPI and data read from 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;
#define SD_Disable() CS=1 //Chip select off
#define SD_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;
}
SD card initialization
To operate the SD card, you must first initialize the SD card. During the initialization process, set the SD card to work in SPI mode. The flow chart is shown in Figure 3.
After the reset is successful, you can use CMD55 and ACMD41 to determine whether the current voltage is within the working range. The host can also continue to read the CID register of the SD card through CMD10, set the data block length through CMD16, and read the CSD register of the card through CMD9. From the CSD register, the host can learn important parameters such as card capacity and supported command set. The C language program for SD card initialization is as follows:
unsigned char SD_Init(void)
{ unsigned char retry,temp;
unsigned char i;
for (i=0;i<0x0f;i++)
{ SPI_TransferByte(0xff); //Delay more than 74 clocks
}
SD_Enable(); //Open chip select
SPI_TransferByte(SD_RESET); //Send reset command
SPI_TransferByte(0x00);
SPI_TransferByte(0x00);
SPI_TransferByte(0x00);
SPI_TransferByte(0x00);
SPI_TransferByte(0x95);
SPI_TransferByte(0xff);
SPI_TransferByte(0xff);
retry=0;
do{ temp=Write_Command_SD(SD_INIT,0);
//Send initialization command
retry++;
if(retry==100) //Retry 100 times
{SD_Disable(); //Turn off chip selection
return(INIT_CMD1_ERROR);
//If retry fails 100 times, return error number
}
}while(temp!=0);
SD_Disable(); //Turn off chip selection
return(TRUE); //Return successful
}
Reading and writing data blocks
After completing the initialization of the SD card, you can perform its read and write operations. SD card read and write operations are completed by sending SD card commands. The SPI bus mode supports single-block (CMD24) and multi-block (CMD25) write operations. Multi-block operation means writing from the specified position until the SD card receives a stop command CMD12. The data block length of a single block write operation can only be 512 bytes. When writing a single block, the command is CMD24. When the response is 0, it means that data can be written, and the size is 512 bytes. The SD card confirms each data block sent to itself through a response command, which is 1 byte long. When the lower 5 bits are 00101, it indicates that the data block is correctly written to the SD card.
When it is necessary to read the data in the SD card, the command word to read the SD card is CMD17, and the correct first response command byte received is 0xFE, followed by a 512-byte user data block, and finally 2 bytes. CRC verification code.
It can be seen that the operations of reading and writing the SD card are completed based on the SD card commands and responses after initialization. The program flow charts of writing and reading the SD card are shown in Figure 4 and Figure 5.
The experimental results show that when the microcontroller uses a 12MHz crystal oscillator, the read and write speed and power consumption are basically satisfactory, and it can be used in situations where the read and write speed requirements are not high. This article explains in detail the process of operating the SD card using the AT89C52 microcontroller, and proposes a method for MCU to read and write the SD card without an SD card controller, realizing the use of the SD card in the power monitoring and reactive power compensation data collection system. use.
Previous article:Making an eight-way timing circuit based on AT89C2051
Next article:Music doorbell based on AT89C2051
Recommended ReadingLatest update time:2024-11-23 02:53
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- 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
- How to use the address filtering function in the Tri-Speed Ethernet IP core
- TI Wireless Products RF Hardware FAQ - 13 new webinar Q/A summaries
- Quectel BC28 NB module cannot read SIM card
- Why does CCS make a thumping sound and then disconnect when it is running?
- TI: mmWave sensors enable intelligence at the edge
- Digital thermometer circuit composed of LM35 and ICL7107
- Where can I buy the EP3C25Q240C8N development board? How much is it worth?
- Programming Example: DSP Realizes SVPWM Waveform
- FPGA Advanced Timing Synthesis Tutorial
- On the first day of the college entrance examination, do you still remember what the topic of your essay was that year?