Design and analysis of SD card reading and writing principle based on STC89C52 microcontroller

Publisher:快乐家庭Latest update time:2018-01-02 Source: eefocusKeywords:STC89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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;

  }


Keywords:STC89C52 Reference address:Design and analysis of SD card reading and writing principle based on STC89C52 microcontroller

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

STC89C52 microcontroller uses two timers to control the frequency and output time interval of the buzzer
topic Two timers are used to control the buzzer sound at the same time. Timer 0 controls the frequency, and timer 1 controls the duration of the same frequency. The output is 1, 10, 50, 100, 200, 400, 800, etc. at intervals of 300ms. 1k (hz) square wave. Problems encountered When timer disorder occurs, the priority
[Microcontroller]
STC89C52MCU--System reset by software
MCU system reset (one of the hot boots) The address of the STC MCU ISP/IAP control register in the special function register is E7H, which cannot be addressed. This register is used to manage and set ISP/IAP related functions and whether to reset the software, etc. When the MCU is reset, all registers are cleared. I
[Microcontroller]
Introduction to 51 MCU timer settings (STC89C52RC)
Introduction to 51 MCU Timer Settings (STC89C52RC) STC MCU Timer Setting The use of STC MCU timer can be said to be very simple, as long as you understand the principle and have a basic understanding of C language. The key points are as follows: 1. You must know the original form of the English abbreviation, so
[Microcontroller]
Learning infrared remote control source code with storage function
Very detailed infrared learning remote control, the assembly subroutine stored in 24c64, you only need to modify the IO to call it in your own design, it is my engineering verification that can learn most of the commonly used infrared remote controls on the market The stc89c52 microcontroller is used, and the followi
[Microcontroller]
[51 microcontroller] STC89C52 independent keyboard experiment, including c code (6)
1. Reference tutorial: Qingxiang 51 microcontroller tutorial 2.Achievement effect: Use the keys of an independent keyboard to control changes in digital tube numbers. 3. Basic principles **Features:**Each button occupies an IO port. When the number of buttons is large, the utilization efficiency of the IO port is
[Microcontroller]
[51 microcontroller] STC89C52 independent keyboard experiment, including c code (6)
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号