SD card reading and writing design principle of AT89C52 microcontroller

Publisher:静静思索Latest update time:2023-08-21 Source: elecfansKeywords:AT89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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.

[1] [2]
Keywords:AT89C52 Reference address:SD card reading and writing design principle of AT89C52 microcontroller

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

Design of ultrasonic ranging and reverse collision avoidance alarm system based on AT89C52
Abstract: Using AT89C52 single-chip microcomputer as the main controller, combined with the principle of ultrasonic ranging, the reverse collision avoidance alarm function is realized. The system hardware and software are designed. The detection receiving circuit and temperature compensation circuit use two integra
[Automotive Electronics]
Design of ultrasonic ranging and reverse collision avoidance alarm system based on AT89C52
Design of automatic detection system for four parameters of battery based on AT89C52
At present, in the factory inspection of ordinary dry batteries, manufacturers mainly conduct quality inspections on the open circuit voltage, load voltage, and short circuit current of the battery. However, due to the destructive damage caused by the battery capacity, there is no special inspection equipment, and o
[Test Measurement]
Design of automatic detection system for four parameters of battery based on AT89C52
AT89C52+ADC0809 composed of 0-5V range voltmeter
This is a 0-5V voltage meter electronic production that uses ADC0809 as A/D conversion, four-digit common cathode digital tube for display, and AT89C52 as the single-chip computer . This ADC0809 (in proteus, ADC0808 is used for simulation, which is the same as ADC0809) can basically make a relatively correct voltage m
[Microcontroller]
AT89C52+ADC0809 composed of 0-5V range voltmeter
Design of frequency characteristic tester based on single chip microcomputer AT89C52
introduction Frequency characteristic tester is also called scanner. Early frequency characteristic tester was completed by manually changing the frequency point by point. Later, a special scanner was designed according to this method for measuring frequency characteristics. Most of the early measuring instruments u
[Microcontroller]
Design of frequency characteristic tester based on single chip microcomputer AT89C52
What is the communication principle between the microcontroller with AT89C52 as the core and the PC?
With the widespread application of systems and the popularization of computer network technology, the communication function of single-chip microcomputers is becoming more and more important. Single-chip microcomputer communication refers to the information exchange between single-chip microcomputers and computers or
[Microcontroller]
What is the communication principle between the microcontroller with AT89C52 as the core and the PC?
51 single chip microcomputer (AT89C52) controls dual relays
#include reg52.h   #define uchar unsigned char   #define uint unsigned int   #define DELAY 500   sbit relay=P2^0;   void delay(uint z)  //1ms   {       uint x,y;       for(x=z;x 0;x--)           for(y=111;y 0;y--);   }   void main()   {       while(1)       {           relay=1;           delay(DELAY);           relay=
[Microcontroller]
51 single chip microcomputer (AT89C52) controls dual relays
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号