- int file_delete(DIR_tag *file_tag){
- u16 k,clust_buf,offset1,offset= file_tag.StartCluster*2; //FAT16用16位表示1個(gè)簇
- file_tag.FileName[7]=0xe5;//FDT中文件被刪除標(biāo)志
- write_fdt(file_tag);//寫修改過的目錄登記項(xiàng)寫回MMC卡
- k= file_tag.FileLength/512+1;//總循環(huán)次數(shù)
- mmc_read_block(&sde,fat1_addr+offset/512,mmc_buffer);
- //讀取起始簇號(hào)所在的塊
- do {
- clust_buf= mmc_buffer[offset%512]&0x00ff(mmc_buffer[offset%512+1]&0x00ff)<<8;
- if(clust_buf!= 0xffff){ //不是文件終止
- mmc_buffer[offset%512]=0; //空簇標(biāo)志00 00
- mmc_buffer[offset%512+1]=0;
- offset1= clust_buf*2; //偏移地址
- }
- else{ //文件終止,完成返回1
- mmc_buffer[offset%512]=0;
- mmc_buffer[offset%512+1]=0;
- mmc_write_block(&sde,fat1_addr+ offset/512,mmc_buffer); //寫FAT1
- mmc_write_block(&sdc,fat2_addr+ offset/512,mmc_buffer); //寫FAT2,內(nèi)容與FAT1同
- return 1;
- }
- if(offset1/512 !=offset/512){
- mmc_write_block(&sdc,fat1_addr+ offset/512,mmc_buffer); //寫FAT1
- mmc_write_block(&sdc,fat2_addr+ offset/512,mmc_buffer); //寫FAT2,內(nèi)容與FAT1同
- mmc_read_block(&sdc,fat1_addr+ offset1/512,mmc_buffer); //讀取下一簇號(hào)所在的塊
- }
- offset= offset1; k--;
- }while(k>=0);
- return 0; //未找到文件結(jié)束標(biāo)志,返回0
4 小結(jié)
通過對(duì)SPI模式下MMC卡讀寫和文件系統(tǒng)的研究,實(shí)現(xiàn)了單片機(jī)對(duì)MMC卡FAT16文件的管理,包括文件的創(chuàng)建、讀寫、刪除等操作。該方法為數(shù)據(jù)采集系統(tǒng)提供了一種非易失性存儲(chǔ)器的解決方案,采集到的數(shù)據(jù)會(huì)以標(biāo)準(zhǔn)文件的格式記錄到MMC卡上,數(shù)據(jù)文件可在Windows下用讀卡器讀取,在保證高性價(jià)比的同時(shí),又方便了數(shù)據(jù)的進(jìn)一步分析、處理。通過在長時(shí)間心電圖監(jiān)測(cè)系統(tǒng)中的實(shí)際應(yīng)用,證明了該方法的可行性。該方案也可應(yīng)用到諸如MP3等與Windows有交互的移動(dòng)存儲(chǔ)設(shè)備中,便于文件的統(tǒng)一管理。
系統(tǒng)采用SPI接口,占用較少的I/O資源。由于SD卡也支持SPI模式,故文中的MMC卡也可以用SD卡替代。





