SD card combines SanDisk flash memory card control and MLC (Multilevel Cell) technology and Toshiba 0.16u and 0.13u NAND technology in a volume of 24mm×32mm×2.1mm. It is connected to a special drive through a 9-pin interface and does not require additional power to maintain the information stored on it. Moreover, it is an integrated solid medium without any moving parts, so there is no need to worry about damage to mechanical movement.
SD card (Secure Digital Memory Card) is a memory card based on semiconductor flash memory technology. It was jointly developed by Panasonic, Toshiba and SanDisk in the United States in 1999. In 2000, these companies initiated the establishment of the SD Association (Secure Digital Association, SDA for short), which has a strong lineup and attracted a large number of manufacturers to participate. These include IBM, Microsoft, Motorola, NEC, Samsung, etc. Driven by these leading manufacturers, SD cards have become the most widely used storage card in consumer digital devices. SD cards are multifunctional storage cards with large capacity, high performance, and security. Compared with MMC cards, they have an additional cryptographic authentication function for data copyright protection (SDMI specification), and the reading and writing speed is 4 times faster than that of MMC cards.
Okay, since it is so good, we give here the program for the 51 single-chip microcomputer to read and write 2G SD card:
#include
#include
//===============================================================
//Define the 4 signal lines required by the SD card
sbit SD_CLK = P3^7;
sbit SD_DI
sbit SD_DO
sbit SD_CS
//=============================================================
//Define the key port
sbit KEY = P2^7;
//=============================================================
//Define a 512-byte buffer. Note that the xdata keyword needs to be used
unsigned char xdata DATA[512];
void delayms(unsigned int count)
{
}
//=================================================================
//Write one byte to the SD card, simulating the SPI bus mode
void SdWrite(unsigned char n)
{
unsigned char i;
for(i=8;i;i--)
{
SD_CLK=0;
SD_DI=(n&0x80);
n<<=1;
SD_CLK=1;
}
SD_DI=1;
}
//=============================================================
//Read one byte from SD card, simulating SPI bus mode
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i;i--)
{
SD_CLK=0;
SD_CLK=1;
n<<=1;
if(SD_DO) n|=1;
}
return n;
}
//==============================================================
//Detect the response of the SD card
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i<=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//========================================================================
//Send command to SD card
void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
{
SdWrite(command|0x40);
SdWrite(((unsigned char *)&argument)[0]);
SdWrite(((unsigned char *)&argument)[1]);
SdWrite(((unsigned char *)&argument)[2 ]);
SdWrite(((unsigned char *)&argument)[3]);
SdWrite(CRC);
}
//======================== ========================================
//Initialize SD card
unsigned char SdInit(void )
{
int delay=0, trials=0;
unsigned char i;
unsigned char response=0x01;
SD_CS=1;
for(i=0;i<=9;i++)
SdWrite(0xff);
SD_CS=0;
//Send Command 0 to put MMC in SPI mode
SdCommand(0x00,0,0x95);
response=SdResponse();
if( response!=0x01)
{
return 0;
}
while(response==0x01)
{
SD_CS=1;
SdWrite(0xff);
SD_CS=0;
SdCommand(0x01,0x00ffc000,0xff);
response=SdResponse();
}
SD_CS=1 ;
SdWrite(0xff);
return 1;
}[page]
//================================================ ================
//Write data to the specified address of the SD card, up to 512 bytes at a time
//unsigned char SdWriteBlock(unsigned char *Block, unsigned long address, int len)
unsigned char SdWriteBlock(unsigned long address,int len)
{
unsigned int count;
unsigned char dataResp;
//Block size is 512 bytes exactly
//First Lower SS
SD_CS=0;
//Then send write command
SdCommand(0x18,address,0xff) ;
if(SdResponse()==00)
{
SdWrite(0xff);
SdWrite(0xff);
SdWrite(0xff);
//command was a success - now send data
//start with DATA TOKEN = 0xFE
SdWrite(0xfe);
//now send data
//for(count=0;count
//data block sent - now send checksum
SdWrite(0xff); //Two-byte CRC check, 0XFFFF means CRC is not considered
SdWrite(0xff);
//Now read in the DATA RESPONSE token
dataResp=SdRead();
//Following the DATA RESPONSE token
//are a number of BUSY bytes
//a zero byte indicates the MMC is busy
while(SdRead()==0);
dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
SD_CS=1;
SdWrite(0xff);
if(dataResp==0x0b)
{
//printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR ");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token. ");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC. ");
return 0;
}
//==== ================================================== =================
//Read data from the specified address of the SD card, up to 512 bytes at a time
unsigned char SdReadBlock(unsigned char *Block, unsigned long address, int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
SD_CS=0;
//Then send write command
SdCommand(0x11,address,0xff);
if(SdResponse()==00)
{
//command was a success - now send data
//start with DATA TOKEN = 0xFE
while(SdRead()!=0xfe);
for(count=0;count
//data block sent - now send checksum
SdRead();
SdRead();
//Now read in the DATA RESPONSE token
SD_CS=1;
SdRead();
return 1;
}
return 0;
}
void initbaud(void)
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD;
PCON=0X00;
TR1=1;
SCON=0X50;//8-bit baud variable
//SCON=0X52;//8-bit baud variable TI interrupt
}
//================== ============================================
//Main program
main()
{
}
You can see the data written to the SD card in the serial port. The program is developed in MCU-51/AVR The board was experimented. For more information, please visit:
http://item.taobao.com/item.htm?spm=0.0.0.50.xZjZ87&id=21441052281 , http://item.taobao.com/item.htm?spm=1103*oQM.3 -5SusJ.h-2Yh1mq&id=14049701171&
Previous article:Parameter calculation of RC automatic reset circuit of single chip microcomputer
Next article:Detailed explanation of MCS-51 microcontroller special function registers
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Automobile complete vehicle EMC testing, complete vehicle electromagnetic compatibility testing, automobile parts EMC testing
- Motor control video FOC sensorless
- Interrupt controller of C6678 developed by DSP
- Apply for free evaluation: Anxinke WiFi + Bluetooth audio development board ESP32-Audio-Kit!
- [Dry goods sharing] Lithium battery power supply circuit design (boost, charging management, etc.)
- [Register now, see you in Guangzhou on February 25, 2019! ] World Peace Group WPI / GDSM Visual Retail Summit
- Thank you for being there +2019
- What are the high frequency characteristics of capacitors?
- Can the EPCS16SI8N chip be read and written directly? Altera configuration chip
- Installation Case | MODBUS to PROFINET Gateway Connects Smart Low Voltage Motors