hardware design
programming
#include #define F_OSC 11059200 // crystal oscillator frequency Hz #define F_BAUD 9600 #define RELOAD 256-F_OSC/12/32/F_BAUD #define CR 0x0D //Enter //Define the 4 signal lines required by the SD card sbit SD_CLK = P1^4; sbit SD_DI = P1^6; sbit SD_DO = P1^5; sbit SD_CS = P1^7; unsigned char xdata DATA[512]; //Define a 512-byte buffer. Note that you need to use the xdata keyword. //=========================================================== //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 the SD card, simulating the 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 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; } //================================================================ //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 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 for(;count<512;count++) SdWrite(0); //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 ERRORn"); return 0; } if(dataResp==0x05) return 1; //printf("Invalid data Response token.n"); return 0; } //printf("Command 0x18 (Write) was not received by the MMC.n"); return 0; } Documents are for reference only Link: https://pan.baidu.com/s/1h3ZnGUPP5o_8_NsVMwVQuA Extraction code: 4ebs
Previous article:Multi-point 8-channel temperature acquisition system based on 51 single-chip microcomputer
Next article:Intelligent temperature control fan based on 51 single chip microcomputer
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Finally, TMS320F28379D is dead
- Help, how to find LVDS7:1 IP in Gaoyun Cloud Source Software
- Product Show + Years of Evaluation and Use of Development Board
- New: TI's code-free, sensorless FOC 70W BLDC motor drive solution
- How can the ST BlueNRG-LP, as a slave, prohibit pairing requests initiated by the Bluetooth host and only allow the slave to initiate pairing requests?
- Hey guys.. I'm here to ask questions again
- Studying the road to electric motor driving-3: The size of "torque"
- Wireless charging technology? Xiaomi has really developed it!
- Analysis of the voltage following circuit of the operational amplifier
- How does the Allwinner V853 chip support the ssh function?