当前位置:文档之家› sd卡读写程序(SDcardreadandwriteprogram)

sd卡读写程序(SDcardreadandwriteprogram)

sd 卡读写程序( SD card read and write program )SD card read and write programObjective: To study the SD card / / operationDesign / software1, using SPI communication / / SD card2, go to SD / / in order to 0-255 a total of 256 data, and then read back LCD1602 display/ / hardware requirements:S11 ON / / dial switchJumper J18 / / all connected#include <p30f6014.h> //dsPIC30F6014 standard header file_FOSC( CSW_FSCM_O&F FX T_PLL4); //4 doubler crystal oscillator, Failsafe clock closed_FWDT (WDT_OFF); / / close the watchdog timer_FBORPO(RP BOR_OFF& MCLR_EN);/ / reset prohibited MCLRr eset enable._FGS (CODE_PROT_OFF); / / code protection against #define CS PORTGbits.RG9 / / SD card selection pin definition #define RSL TB4 / / definition LCDc ontrol bits (note here can only register with LATB, you cannot directly use thePORTB register)#define RW TB5#define e TB6Unsigned char __attribute__ ((address (0x900)))lcd[3]={0,0,0};Void (spi_init); / / declaration system initial functionVoid (spi_low); / / that produces low baud rate (using the SD card initialization function)Void (spi_high); / / that produce high baud rate function (SDcard initialization after use)Unsigned char (sd_reset); / / that the SD cardinitialization functionUnsigned char SD_SendComman(udn signed char CMDu nsigned, longARG); / / write SD card command function statementUnsigned char SPI_WriteByte (unsigned char VAL); / / write a byte function statementUnsigned char SPI_ReadByte (void); / / that receive a byte functionUnsigned char SD_WriteSingleBlock (unsigned long sector); / / that single BLOCK data write SD card functionUnsigned char SD_ReadSingleBlock (unsigned long sector); / / read SD card data function single BLOCK statementVoid (lcd_display); / / state results display functionVoid (delay); / / state delay function (shown by)/ / system initialization functionVoid spi_init (){TRISG=0x00d0; / / set the SDI output, C output port for the otherTRISB=0X0000; / / set for the output port BTRISD=0X0000; / / set for the output port DSPI2CON=0x0278; / / idle bus is high, fosc/64SPI2STAT=0x8000; / / the end of sampling the output data of input data, the rising edge of data transmissionWrite a LCD program/ / write a byte of data functionAfter changing the level / / on the need to insert a delay time, otherwise the LCD react.Void write (unsigned char x){PORTD=x; / / PORTD port to send data to be displayedDelay ();Rs=1; / / the byte data, rather than commandDelay ();Rw=0; / / the operation for writing,Instead of readingDelay ();E=0; / / low enable signal(delay); / / keep the enable signal is low for a period of timeDelay ();E=1; / / pull high enable signal needed by the rising edge of the LCD operation}//********************LCD display settings functionAfter changing the level / / on the need to insert a delay time, otherwise the LCD react.Void lcd_enable (){Delay ();Rs=0; / / the byte data for the command, rather than the dataDelay ();Rw=0; / / the operation for writing, instead of readingDelay ();E=0; / / low enable signal(delay); / / keep the enable signal is low for a period of timeE=1; / / pull high enable signal needed by the rising edge of the LCD operationDelay ();}//*********************LCD initialization functionVoid lcd_init (){PORTD=0X1; / / displayLcd_enable ();PORTD=0X38; //8 bit 2 row 5*7 dot matrixLcd_enable ();PORTD=0X0e; / / display, cursor, flashingLcd_enable ();PORTD=0X06/; / the text does not move the cursor to the right.Lcd_enable ();PORTD=0X86; / / time display.Lcd_enable ();//***********************LCD display functionVoid lcd_display (){Unsigned char I, j;Lcd_init ();For (i=0; i<3; / / i++) a total of 3 bytes of data{Write (lcd[i]); / / look-up table for data and call to write a byte of data to the LCD display functionFor (j=0; j<5; / / j++) for a period of time (mainly in order to control the display speed){delay ();}}}write a bytefunctionUnsigned char SPI_WriteByte (unsigned char VAL){SPI2BUF= Val; / / to be sent to the transmittingregister loadingWhile (IFS1bits.SPI2IF!); / / wait for sentIFS1bits.SPI2IF=0; / / clear to send complete flagReturn SPI2BUF; / / read receive register (even invalid data is empty)}functionUnsigned char SPI_ReadByte (void){SPI2BUF= 0xff; / / send register loading data,receiving to start While (IFS1bits.SPI2IF!); / / wait for the completion of receiving data receive a bytedataIFS1bits.SPI2IF=0; / / clear receive complete flagReturn SPI2BUF; / / read the received data}send command functionUnsigned char SD_SendComman(udn signed char CMD,u nsigned long ARG){Unsigned char r1;Unsigned char retry1=0; / / the number of repeat operationCs=0; / / the chip select signalSPI_WriteByte (CMD 0x40 |); / / write command respectively.SPI_WriteByte (arg>>24); / / fourth byte data segmentSPI_WriteByte (arg>>16); / / third byte data segmentSPI_WriteByte (arg>>8); / / second byte data segmentSPI_WriteByte (ARG); / / first byte data segmentSPI_WriteByte (0x95); //CRC checksumWhile ((R1 = SPI_WriteByte (0xff)) = = 0xff) / / wait for a responseIf break (retry1++ > 200); / / exit timeoutCs=1; / / early chip select signalReturn R1; / / return status}//*******************SD initialization function Unsigned char sd_reset (){Unsigned char I, tmp;Unsigned char retry; / / repeatUnsigned char r1=0;Retry=0;Delay ();Delay ();Do{For (i=0; i<100; i++) SPI_WriteByte (0xff);R1 = SD_SendCommand (0,0); / / idle commandRetry++;If (retry>20) return 1; / / exit timeout} while (R1! = 0x01); / / wait for the command to return IDLERetry = 0;Cs=0;Do{For (i=0; i<100; i++) SPI_WriteByte (0xff);R1 = SD_SendCommand (1, 0); / / Active commandRetry++;If (retry>254) return 1; / / exit timeout } while (R1);For (i=0; i<100; i++) SPI_WriteByte (0xff);R1 = SD_SendCommand (59, 0); / / CRCIf (R1 return 1); / / return is not correct, exit initializationFor (i=0; i<100; i++) SPI_WriteByte (0xff);R1 = SD_SendCommand (16, 512); / / set the sector size 512If (R1 =0 return 1!); / / return is not correct, exit initializationReturn 0; / / return to normal}write a sectorUnsigned char SD_WriteSingleBlock (unsigned long sector) { Unsigned char r1;Unsigned int i;Unsigned char retry=0;Do{For (i=0; i<100; i++) SPI_WriteByte (0xff);R1 = SD_SendCommand (24, sector<<9); / / write command Retry++;If (retry>10) return 1; / / exit timeout} while (R1 = = 0x00);Cs=0;SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xFE); / / the start symbolFor (i=0; i<512; / / i++) to send 512 bytes of data If (i<255) SPI_WriteByte (I); / / send 0--255Else SPI_WriteByte (512-i); / / send 255--0}SPI_WriteByte (0x95);SPI_WriteByte (0x95); //16-bits CRCR1 = SPI_WriteByte (0xff); / / read a responseIf (retry++ >10) return 1;Timeout / exitWhile (! ((r1&0x0f) ==5)); / / wait for datainformation receive(while! (SPI_WriteByte (0xff))); / / wait for the internal SD card programmingReturn 0;}//****************** SD card reading a sectorUnsigned char SD_ReadSingleBlock (unsigned long sector){Unsigned char R1, temp;Unsigned int i, j;Unsigned char retry=0;Do{R1 = SD_SendCommand (17, sector<<9); / / read commandRetry++;If (retry>10) return 1; / / exit timeout} while (R1 = = 0x00);Cs=0;While (SPI_WriteByte (0xff)! = 0xFE) / / wait for receiving a start byte{If (retry++ >100) return 1; / / exit timeoutFor (i=0; i<512; i++) / / read 512 data{Temp = SPI_WriteByte (0xff); / / read the received data Lcd[0]= (temp/100) +48;Lcd[1]= ((temp%100) /10) +48;Lcd[2]= ((temp%100)%10) +48;(lcd_display); / / read data sent to the displayFor (j=0; j<500; j++) {delay ();}}SPI_WriteByte (0xff); / / pseudo 16-bits CRCSPI_WriteByte (0xff);Cs=1;Return 0;}delay proceduresVoid (delay) / delay procedure{Int i; / / define integer variable For (i=0x100; i--;); / / delay}The main function ofInt main (void){Unsigned char loop, res;Delay ();Delay ();Delay ();Loop=1;Cs=1;While (loop){(spi_init); / / call system initialization functionRes= (sd_reset); / / call the SD card initialization functionIf (RES break); / / SDc ard initialization is normal, not normal,not out of the loop executes the read and write operationsSD_WriteSingleBlock (1); / / call and write SDc ard single BLOCK function, the sector is 1If (RES) break;SD_ReadSingleBlock (1); / / call read SD card single BLOCK function, the sector number is 1If (RES) break;Loop=0;While (1);}While (1);}。

相关主题