sd卡读写程序(SD card read and write program).doc

上传人:rrsccc 文档编号:9184822 上传时间:2021-02-06 格式:DOC 页数:19 大小:21.58KB
返回 下载 相关 举报
sd卡读写程序(SD card read and write program).doc_第1页
第1页 / 共19页
sd卡读写程序(SD card read and write program).doc_第2页
第2页 / 共19页
sd卡读写程序(SD card read and write program).doc_第3页
第3页 / 共19页
sd卡读写程序(SD card read and write program).doc_第4页
第4页 / 共19页
sd卡读写程序(SD card read and write program).doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《sd卡读写程序(SD card read and write program).doc》由会员分享,可在线阅读,更多相关《sd卡读写程序(SD card read and write program).doc(19页珍藏版)》请在三一文库上搜索。

1、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

2、switchJumper J18 / / all connected#include /dsPIC30F6014 standard header file_FOSC (CSW_FSCM_OFF & XT_PLL4); /4 doubler crystal oscillator, Failsafe clock closed_FWDT (WDT_OFF); / / close the watchdog timer_FBORPOR (PBOR_OFF & MCLR_EN); / / reset prohibited MCLR reset enable._FGS (CODE_PROT_OFF); /

3、/ code protection against#define CS PORTGbits.RG9 / / SD card selection pin definition#define RS LATBbits.LATB4 / / definition LCD control bits (note here can only register with LATB, you cannot directly use the PORTB register)#define RW LATBbits.LATB5#define e LATBbits.LATB6Unsigned char _attribute

4、_ (address (0x900) lcd3=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 (SD card initialization after use)Unsigned char (sd_reset); / / t

5、hat the SD card initialization functionUnsigned char SD_SendCommand (unsigned char CMD unsigned, long ARG); / / 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 funct

6、ionUnsigned 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 de

7、lay 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

8、 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 displa

9、yedDelay ();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 timeE=1; / / pull high enable signal needed by the rising edge of the LCD operationDela

10、y ();/*LCD display settings function *After 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

11、 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 function *Void lcd_init ()PORTD=0X1; / / displayLcd_enable ();PORTD=0X38; /8 bit 2 row 5*7 dot matrixLcd_enable ()

12、;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 function *Void lcd_display ()Unsigned char I, j;Lcd_init ();For (i=0; i3; / / i+) a total of 3 bytes of dataWr

13、ite (lcdi); / / look-up table for data and call to write a byte of data to the LCD display functionFor (j=0; j24); / / fourth byte data segmentSPI_WriteByte (arg16); / / third byte data segmentSPI_WriteByte (arg8); / / second byte data segmentSPI_WriteByte (ARG); / / first byte data segmentSPI_Write

14、Byte (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; / / repea

15、tUnsigned char r1=0;Retry=0;Delay ();Delay ();DoFor (i=0; i20) return 1; / / exit timeout while (R1! = 0x01); / / wait for the command to return IDLERetry = 0;Cs=0;DoFor (i=0; i254) return 1; / / exit timeout while (R1);For (i=0; i100; i+) SPI_WriteByte (0xff);R1 = SD_SendCommand (59, 0); / / CRCIf

16、(R1 return 1); / / return is not correct, exit initializationFor (i=0; i100; 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 sector *Unsigned char SD_WriteS

17、ingleBlock (unsigned long sector)Unsigned char r1;Unsigned int i;Unsigned char retry=0;DoFor (i=0; i100; i+) SPI_WriteByte (0xff);R1 = SD_SendCommand (24, sector10) return 1; / / exit timeout while (R1 = = 0x00);Cs=0;SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xff)

18、;SPI_WriteByte (0xff);SPI_WriteByte (0xff);SPI_WriteByte (0xFE); / / the start symbolFor (i=0; i512; / / i+) to send 512 bytes of dataIf (i10) return 1;Timeout / exitWhile (! (r1&0x0f) =5); / / wait for data receive information(while! (SPI_WriteByte (0xff); / / wait for the internal SD card programm

19、ingReturn 0;/* SD card reading a sector *Unsigned char SD_ReadSingleBlock (unsigned long sector)Unsigned char R1, temp;Unsigned int i, j;Unsigned char retry=0;DoR1 = SD_SendCommand (17, sector10) return 1; / / exit timeout while (R1 = = 0x00);Cs=0;While (SPI_WriteByte (0xff)! = 0xFE) / / wait for re

20、ceiving a start byteIf (retry+ 100) return 1; / / exit timeoutFor (i=0; i512; i+) / / read 512 dataTemp = SPI_WriteByte (0xff); / / read the received dataLcd0= (temp/100) +48;Lcd1= (temp%100) /10) +48;Lcd2= (temp%100)%10) +48;(lcd_display); / / read data sent to the displayFor (j=0; j500; j+) delay

21、();SPI_WriteByte (0xff); / / pseudo 16-bits CRCSPI_WriteByte (0xff);Cs=1;Return 0;/* delay procedures *Void (delay) / delay procedureInt i; / / define integer variableFor (i=0x100; i-;); / / delayThe main function of * /*Int main (void)Unsigned char loop, res;Delay ();Delay ();Delay ();Loop=1;Cs=1;W

22、hile (loop)(spi_init); / / call system initialization functionRes= (sd_reset); / / call the SD card initialization functionIf (RES break); / / SD card initialization is normal, not normal, not out of the loop executes the read and write operationsSD_WriteSingleBlock (1); / / call and write SD card 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);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1