Tutorials to .com

Tutorials to .com » Mechine » Mcu » Based on AT89C52 Microcontroller Design SD card reader

Based on AT89C52 Microcontroller Design SD card reader

Print View , by: iSee ,Total views: 60 ,Word Count: 2110 ,Date: Mon, 24 Aug 2009 Time: 10:03 AM

Preface

For a long time to flash Memory for storage of SD Cain body with small size and low power consumption, erasable and non-volatile memory is widely used in consumer electronics products. Especially in recent years, with prices falling and storage capacity rising, increasing the scope of its application to broaden. When the data acquisition system takes a long time to capture and record vast amounts of data, select the SD card as storage media is the development of those who have a very good choice. In the energy monitoring and reactive power compensation system, should be a continuous record of a large number of voltage, current, active power, reactive power and time parameters, the data collected when the mcu can use SD as a storage medium. This paper describes the SD card in the power monitoring and data acquisition system reactive power compensation in the applications.

Design

Applications read and write AT89C52 There are two SD cards that require attention. First of all, the need to find a realization of AT89C52 microcontroller with the SD card communication solutions; secondly, SD card can accept logic level and logic level AT89C52 provided does not match the need to address the level matching.

Communication mode

There are two optional SD card communication protocols: SD mode and SPI mode. SD model is a standard SD card read-write mode, but in the selection of SD mode, they often need to choose with the SD card controller interface MCU, or must be added an extra SD card control unit to support the SD card reading and writing. However, AT89C52 microcontrollers without an integrated SD card controller interface, if the choice of SD-mode communication would in effect increase the hardware cost of the product. In the SD card data read and write time is not very demanding circumstances, the choice of SPI model can be said is the best solution. Because in the SPI mode, the four lines can be completed by all of the data exchange, and is currently on the market a lot of MCU are integrated with existing SPI interface circuit, using SPI mode on the SD card to read and write operation can greatly simplify the hardware circuit design.

Although the AT89C52 without the hardware controller, SD card, there is no ready-made SPI interface module, but you can use the software to simulate SPI Bus Timing. In this paper, to read and write SD card SPI bus mode.

Level Matching

SD Card 3.3V TTL logic level is equivalent to the standard level, while the control chip AT89C52 the logic level of 5V CMOS-level standards. Thus, between them can not be directly connected to, or SD card may be burned. Out of security considerations, there is need to address the level of matching.

To solve this problem, the most fundamental is to resolve the interface logic level compatibility problems, in principle, are mainly two: one for high output level device outputs the minimum voltage value, it should be greater than the receiving level of the device identified as The minimum high voltage value; the other one for the output device outputs low level the maximum voltage value, it should be less than the receiving device is identified as a low level of the maximum voltage value.

In general, the general level conversion program is similar SN74ALVC4245 dedicated level converter chip, these chips can be used not only to boost and buck, but also allows both sides of the power supplies are not synchronized. However, this solution is relatively expensive price, and generally a dedicated level converter chips are also converted 8-way, 16 or more large ones of the level, relative to this system only need to convert 3-way is a waste of resources .

Taking into account the SD card in SPI mode protocol, the communications are one-way, so the microcontroller to the SD card to transfer data using transistors with pull-up resistor when the law program, the basic circuit shown in Figure 1. In the SD card to transfer data to the microcontroller can be connected directly, because they just meet the level between the level compatible with the above-mentioned principles, both economical and practical.

Based on AT89C52 Microcontroller Design SD card reader

This program will require a dual power supply (a 5V power supply, a 3.3V power supply), 3.3V power supply can be AMS1117 regulator from the 5V voltage regulator tube to obtain.

Hardware Interface Design

SD cards 9Pin pin interface to facilitate its peripheral circuits operate, 9Pin pin with the different work Patterns differ. In the SPI mode, pins 1 (DAT3) as the SPI chip select line CS use, pin 2 (CMD) as a SPI bus data output lines MOSI, and pin 7 (DAT0) for the data input line MISO, cited pin 5 used as a clock line (CLK). In addition to power and ground, retaining pin can be left vacant.

This SD card to control the MCU is ATMEL company's low-voltage, high-performance CMOS 8-bit microcontroller AT89C52, contains 8K bytes that can be repeatedly rewritable read-only program memory and 256 bytes of random data memory storage. As the AT89C52 is only 256 bytes of data memory, while the SD card data is written based on block as a unit, each block of 512 bytes, so the need microcontroller added a minimum system RAM. Use this system RAM memory chips HM62256, capacity of 32K. To read and write to the RAM when the latch into the low 8-bit address latch, and P2 port 8-bit address data constitute a 16-bit address space, allowing for an SD card to read and write 512-byte block operation. System hardware diagram shown in Figure 2.

Based on AT89C52 Microcontroller Design SD card reader

Software Design

SPI mode

The initial power-on SD card into the SD Bus mode automatically, in this mode, send a reset command to the SD card CMD0. If the SD card in the receiving CS low during the reset command is valid, enter the SPI mode, or work in the SD Bus mode.

For the non-SPI serial bus interface AT89C52 microcontrollers, the SPI bus with software to simulate the operation of the specific approach is to: P1.5 port (analog CLK line) of the initial state is set to 1, while in allowed to receive and then set P1.5 to 0. This, MCU in the output one SCK clock at the same time, will allow the serial interface chip left, so the output data to a microcontroller AT89C52 the P1.7 (analog MISO line), this then set P1.5 to 1 so that microcontroller from P1.6 (analog MOSI line) output 1-bit data (first as a high) to the serial interface chip. At this point, simulated a data input and output will be completed. This then set P1.5 to 0 to simulate the data under an input and output, and so cycle 8 times, to complete a time through the SPI bus, 8-bit data transfer operation.

The realization of this program to integrate the functions of SPI bus to read and write, passed val variable is written to the SPI data is read from the SPI data. Specific procedures are as follows: (in Keil uVision2 program is to prepare the build environment)

sbit CS=P3^5;
sbit CLK= P1^5;
sbit DataI=P1^7;
sbit DataO=P1^6;
#define SD_Disable() CS=1 //#define SD_Enable() CS=0 //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 on the SD card SD card must first be initialized during the initialization settings SD card work in SPI mode, the flow chart shown in Figure 3.

Based on AT89C52 Microcontroller Design SD card reader

After the success of the reset by CMD55 and ACMD41 to determine whether the current voltage is within the scope of work. The host can continue through the CMD10 to read SD cards CID register is set by CMD16 Data Block length, through the CSD register CMD9 reading cards. From the CSD register, the host should be informed of card capacity, support command set and other important parameters. SD card initialization of c language programs are as follows:

unsigned char SD_Init(void)
{ unsigned char retry,temp;
unsigned char i;
for (i=0;i<0x0f;i++)
{ SPI_TransferByte(0xff); //74}
SD_Enable(); //SPI_TransferByte(SD_RESET); //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);
//retry++;
if(retry==100) //100{SD_Disable(); //return(INIT_CMD1_ERROR);
//100}
}while(temp!=0);
SD_Disable(); //return(TRUE); //}

Read and write data blocks

After the completion of the initialization of SD card can carry out its read and write operations. SD card read and write operations are ordered by sending a SD card completed. SPI Bus mode supports single block (CMD24) and multi-block (CMD25) write operation, multi-block operation is to write down starting from the specified location, until the SD card before receiving a stop command CMD12 to stop. A single block write operation of data block length of only 512 bytes. Single-block write, the command CMD24, when the answer is 0, the data can be written note, the size is 512 bytes. SD card for each block of data sent to their response through a command confirmed that it is 1 byte long, while the low 5 to 00101, the show that data blocks are written to SD card correctly.

SD card is required to read the data when reading SD card command word as the CMD17, the first one to receive the correct response to the command bytes 0xFE, followed by 512 bytes of user data blocks, the last 2 bytes of CRC verification code.

Can be seen, read and write SD card operations are based on the SD card after initialization commands and to respond to the completion of the operation, writing, reading SD card program flow chart shown in Figure 4 and Figure 5 below.

Based on AT89C52 Microcontroller Design SD card reader

Based on AT89C52 Microcontroller Design SD card reader

Concluding remarks

Experimental results show that the use of 12MHz crystal oscillator, microcontroller, the read and write speed and power are generally satisfactory, can be applied to the read and write speed of less demanding circumstances. This paper elaborates on the SD card using AT89C52 microcontroller to operate the process, presents a non-SD card controller, MCU read and write SD card method to achieve the SD card in the power monitoring and data acquisition system reactive power compensation in use.


Micro Controller Unit Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.