|
這個(gè)程序是我8年前無(wú)聊時(shí)做的,SD卡鏡像文件可用uiso打開(kāi),編輯后保存鏡像文件后可在Proteus中看到效果。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
微信圖片_20181009214741.png (62.17 KB, 下載次數(shù): 75)
下載附件
2018-10-10 19:04 上傳
0.png (42.77 KB, 下載次數(shù): 65)
下載附件
2018-10-10 19:35 上傳
AVR32單片機(jī)源程序如下:
- /*************************************************************************
- *文 件: fs/fat32.c
- *作 用: SD/MMC卡的相關(guān)操作
- *作 者: 宋
- *************************************************************************/
- #include "../config.h" //配置文件
- #include "../drivers/mmc.h" //MMC卡驅(qū)動(dòng)
- #include "fat32.h"
- #include "../gui/font.h"
- DBRInfo dbrInfo;
- buf_t dataBuffer;
- File *fileItem;
- LongDir *longItem;
- uint8 getFileInfo(void);
- uint8 delSpace(buf_t string,buf_t stringd,uint8 len);
- uint32 currentPath;
- uint16 currentLong; //當(dāng)前路徑所在簇
- /*************************************************************************
- *函 數(shù): chedkNameSum
- *作 用: 計(jì)算短文件名的效驗(yàn)和
- *入口參數(shù): FileName(短文件名)
- *返 回 值: Sum(短文件名的效驗(yàn)和)
- *備 注: 這個(gè)函數(shù)參考了Microsoft Extensible Firmware Initiative FAT32 File System Specification.pdf
- *************************************************************************/
- uint8 checkNameSum(buf_t FileName)
- {
- uint8 FileNameLen;
- uint8 Sum;
- Sum = 0;
- for(FileNameLen = 11;FileNameLen != 0;FileNameLen --) //共11個(gè)字符
- {
- Sum = ((Sum & 1) ? 0x80 : 0) + (Sum >> 1) + *FileName ++; //計(jì)算效驗(yàn)和
- }
- return Sum;
- }
- /*************************************************************************
- *函 數(shù): bigToSmallending
- *作 用: 大小端轉(zhuǎn)換
- *入口參數(shù): big(大端數(shù)據(jù))
- *返 回 值: len(big的長(zhǎng)度)
- *備 注: 這個(gè)函數(shù)參考了一個(gè)帖子 忘記是哪個(gè)帖子了
- *************************************************************************/
- /*
- uint32 bigToSmallEnding(uint32 big,uint8 len)
- {
- uint32 result = 0;
- uint32 fact = 1;
- int8 i;
- for(i = 0;i < len;i ++)
- {
- result += ((uint8 *)&(big))[i] *fact;
- fact *= 0x100;
- }
- return big;
- }
- */
- /*************************************************************************
- *函 數(shù): stringCmp
- *作 用: 比較字符串
- *入口參數(shù): str1(字符串1),str2(字符串2),len(長(zhǎng)度)
- *返 回 值: 0不同 1相同
- *************************************************************************/
- uint8 stringCmp(buf_t str1,buf_t str2,uint8 len)
- {
- uint8 i;
- for(i = 0;i < len; i ++)
- {
- if(str1[i] != str2[i]) return 0;
- }
- return 1;
- }
- /*************************************************************************
- *函 數(shù): readSector
- *作 用: 讀一扇區(qū)數(shù)據(jù)
- *入口參數(shù): add(扇區(qū)地址),buf(數(shù)據(jù)緩沖區(qū))
- *返 回 值: TRUE(成功),FALSE(失敗)
- *************************************************************************/
- uint8 readSector(uint32 add,buf_t buf)
- {
- return MMC_ReadSector(add,buf);
- }
- /*************************************************************************
- *函 數(shù): getDBRInfo
- *作 用: 獲取FAT的基本信息
- *入口參數(shù): dbrsector(DBR所在的扇區(qū))
- *返 回 值: TRUE(成功),FALSE(失敗)
- *************************************************************************/
- uint8 FAT_Initialize(uint32 dbrSector)
- {
- buf_t tmpString = malloc(11);
- dataBuffer = malloc(512);
- FAT32_DBR *dbr = (FAT32_DBR *)dataBuffer;
- if(readSector(dbrSector,dataBuffer) == FALSE)
- {
- free(tmpString);
- free(dataBuffer);
- return FALSE;
- }
- dbrInfo.bytePerSector = dbr->BPB_BytsPerSec; //每扇區(qū)字節(jié)數(shù)
- dbrInfo.rootDirClust = dbr->BPB_RootClus; //根目錄所在簇
- dbrInfo.sectorsPerClust = dbr->BPB_SecPerClus; //每簇扇區(qū)數(shù)
- dbrInfo.FATSector = dbr->BPB_FATSz32; //FAT表所占扇區(qū)數(shù)
- dbrInfo.firstFATSector = dbr->BPB_RsvdSecCnt; //第一個(gè)FAT表的位置
- dbrInfo.rootDirSector = dbrInfo.firstFATSector \
- +(dbrInfo.FATSector * dbr->BPB_NumFATs); //根目錄所在扇區(qū)
- currentPath = dbrInfo.rootDirClust;
- free(tmpString);
- free(dataBuffer);
- return TRUE;
- }
- /*************************************************************************
- *函 數(shù): getNextClust
- *作 用: 根據(jù)當(dāng)前簇獲取下一簇
- *入口參數(shù): currentClust(當(dāng)前簇)
- *返 回 值: nextClust(下一簇)
- *************************************************************************/
- uint32 getNextClust(uint32 currentClust)
- {
- uint32 nextClust; //定義下一個(gè)簇
- readSector(
- dbrInfo.firstFATSector //FAT表項(xiàng)的首扇區(qū)
- + (uint32)((currentClust << 2) >> 9) //一個(gè)FAT表占4個(gè)字節(jié)
- //比如currentClust = 200 那么它所在位置就是800 而一個(gè)dataBuffer是512
- //字節(jié)這樣的話(huà)800就溢出了所以用800/512=1(整型)這樣就讀出了它在哪個(gè)扇區(qū)中
- ,dataBuffer);
- currentClust = (currentClust << 2) >= 512 ?(currentClust * 4) % 512:currentClust << 2;
- //如上所說(shuō)下一簇就等于第800/512扇區(qū)中的第的第800%512偏移量處
- nextClust = (uint32)(dataBuffer[currentClust] //合并4個(gè)字節(jié)為1個(gè)32位數(shù)
- |(dataBuffer[currentClust + 1] << 8)
- |(dataBuffer[currentClust + 2] << 16)
- |(dataBuffer[currentClust + 3] << 24));
- return nextClust; //返回下一簇的數(shù)值
- }
- /*************************************************************************
- *函 數(shù): delSpace
- *作 用: 刪除字符串兩端的空格
- *入口參數(shù): source(源字符串),string(目地字符串),len(字符個(gè)串長(zhǎng)度)
- *返 回 值: 去掉空格后的字符串長(zhǎng)度
- *************************************************************************/
- uint8 delSpace(buf_t source,buf_t string,uint8 len)
- {
- uint8 i;
- uint8 handspace = 0;
- uint8 lastspace = 0;
- for(i = 0;i < len;i ++) //從頭部查詢(xún)
- {
- if(source[i] == ' ') //發(fā)現(xiàn)空格
- handspace ++; //字符串頭部空格計(jì)數(shù)器加1
- else break; //如發(fā)現(xiàn)非空格則退出循環(huán)
- }
- for(i = len - 1;i > 0;i --) //從尾部查詢(xún)
- {
- if(source[i] == ' ') //發(fā)現(xiàn)空格
- lastspace ++; //字符串尾部空格計(jì)數(shù)器加1
- else break; //如發(fā)現(xiàn)非空格則退出循環(huán)
- }
- memcpy(string,&source[handspace],len - handspace); //把有字符串的位置向頭部移動(dòng)
- if(lastspace != 0)string[len - handspace - lastspace] = 0; //向字符串尾部寫(xiě)0意為字符串到此結(jié)束
- return len-handspace-lastspace; //返回去掉空格后的字符串長(zhǎng)度
- }
- /*************************************************************************
- *函 數(shù): getFileDate
- *作 用: 獲取文件日期
- *入口參數(shù): date(從File里讀到的文件日期信息),filedate(讀出的日期)
- *返 回 值: 無(wú)
- *************************************************************************/
- /*
- void getFileDate(uint16 date,Date *filedate)
- {
- filedate->year = (date >> 9) + 1980; //計(jì)算年
- filedate->month = (date & 0x1e0) >> 5; //計(jì)算月
- filedate->day = date & 0x1f; //計(jì)算日
- }
- /*************************************************************************
- *函 數(shù): getFileTime
- *作 用: 獲取文件日期
- *入口參數(shù): time(從File里讀到的文件時(shí)間信息),filetime(讀出的時(shí)間)
- *返 回 值: 無(wú)
- *************************************************************************/
- /*
- void getFileTime(uint16 time,Time *filetime)
- {
- filetime->hour = (time &0xf800) >> 11; //計(jì)算小時(shí)
- filetime->minute = (time & 0x7e0) >> 5; //計(jì)算分鐘
- filetime->second = time & 0x1f; //計(jì)算秒
- }
- /*************************************************************************
- *函 數(shù): bigToSmall
- *作 用: 字符串大小寫(xiě)轉(zhuǎn)換
- *入口參數(shù): string(源字符串),stringd(目地字符串),flag(大寫(xiě)到小是為1 小寫(xiě)到大寫(xiě)為0),len(字符個(gè)串長(zhǎng)度)
- *返 回 值: 無(wú)
- *************************************************************************/
- void bigToSmall(buf_t string,buf_t stringd,flag_t flag,uint8 len)
- {
- uint8 i;
- if(flag) //判斷大(小) -> 小(大)
- {
- for(i = 0;i < len;i ++)
- {
- if((string[i] >= 'A') && (string[i] <= 'Z')) //大寫(xiě)到小寫(xiě)的字符范圍是'A'-'Z'
- stringd[i] = string[i] + 0x20; //大寫(xiě)到小寫(xiě)的轉(zhuǎn)換
- else
- stringd[i] = string[i]; //不在大寫(xiě)字符范圍內(nèi)的不進(jìn)行轉(zhuǎn)換
- }
- }else{
- for(i = 0;i < len;i ++)
- {
- if((string[i] >= 'a') && (string[i] <= 'z')) //小寫(xiě)到大寫(xiě)的字符范圍是'a'-'z'
- stringd[i] = string[i] - 0x20; //小寫(xiě)到大寫(xiě)的轉(zhuǎn)換
- else
- stringd[i] = string[i]; //不在小寫(xiě)字符范圍內(nèi)的不進(jìn)行轉(zhuǎn)換
- }
- }
- }
- /*************************************************************************
- *函 數(shù): sectorToClust
- *作 用: 扇區(qū)和簇之間的換算
- *入口參數(shù): 扇區(qū)
- *返 回 值: 簇
- *************************************************************************/
- uint32 sectorToClust(uint32 sector)
- {
- return sector / dbrInfo.sectorsPerClust; //單位換算
- }
- /*************************************************************************
- *函 數(shù): clustToSector
- *作 用: 簇和扇區(qū)之間的換算
- *入口參數(shù): 簇
- *返 回 值: 扇區(qū)
- *************************************************************************/
- uint32 clustToSector(uint32 clust)
- {
- return clust * dbrInfo.sectorsPerClust; //單位換算
- }
- /*************************************************************************
- *函 數(shù): dispFileSize
- *作 用: 顯示文件的大小信息
- *入口參數(shù): size(文件的大小)
- *返 回 值: 無(wú)
- *************************************************************************/
- /*
- void dispFileSize(uint32 size)
- {
- #if DEBUG_TYPE == EN //英文終端
- if(size < 1024) //不到1K 的
- printf("size:%dB\r\n",size); //直接顯示XXB
- if((size > 1024) && (size <0x100000)) //大于1K小于1M的
- printf("size:%dKB\r\n",size/1024); //顯示XXKB
- if((size > 0x100000) && (size < 0x40000000)) //大于1M小于1G的
- printf("size:%dMB\r\n",size/0x100000); //顯示XXMB
- if(size > 0x40000000) //大于1G小于1T的
- printf("size:%dGB\r\n",size/0x40000000); //顯示XXGB
- #elif DEBUG_TYPE ==CN
- if(size < 1024)
- printf("文件大小:%dB\r\n",size);
- if((size > 1024) && (size <0x100000))
- printf("文件大小:%dKB\r\n",size/1024);
- if((size > 0x100000) && (size < 0x40000000))
- printf("文件大小:%dMB\r\n",size/0x100000);
- if(size > 0x40000000)
- printf("文件大小:%dGB\r\n",size/0x40000000);
- #endif
- }
- /*************************************************************************
- *函 數(shù): getFileItem
- *作 用: 從當(dāng)前緩沖區(qū)中獲取文件信息
- *入口參數(shù): offset(當(dāng)前緩沖區(qū)中的位置 最大值:512)
- *返 回 值: 無(wú)
- *************************************************************************/
- void getFileItem(uint16 offset)
- {
- fileItem = (File *)&dataBuffer[offset]; //獲取文件結(jié)構(gòu)
- }
- /*************************************************************************
- *函 數(shù): getFileItem
- *作 用: 從當(dāng)前緩沖區(qū)中獲取文件信息
- *入口參數(shù): offset(當(dāng)前緩沖區(qū)中的位置 最大值:512)
- *返 回 值: 無(wú)
- *************************************************************************/
- void getFileLongDirItem(uint16 offset)
- {
- longItem = (LongDir *)&dataBuffer[offset]; //獲取長(zhǎng)目錄項(xiàng)結(jié)構(gòu)
- }
- /*************************************************************************
- *函 數(shù): fileNameProcess
- *作 用: 普通文件名轉(zhuǎn)換成短文件名例如:"mp3.txt" >> "MP3 TXT"
- *入口參數(shù): dir(普通文件名),dird(轉(zhuǎn)換好的文件名)
- *返 回 值: 無(wú)
- *************************************************************************/
- void fileNameProcess(buf_t dir,buf_t dird)
- {
- uint8 i;
- uint8 point = 12; //"."在文件名中的位置
- for(i = 0;i < 8;i ++) //主文件名
- {
- if(dir[i] == 0)goto space;
- if(dir[i] != '.') //沒(méi)有發(fā)現(xiàn)"."
- dird[i] = dir[i]; //獲取短文件名
- else
- { //發(fā)現(xiàn)"."
- point = i + 1; //記錄"."的位置
- space:
- for(;i < 8;i++)
- dird[i] = ' '; //從"."的位位置填充空格
- }
- }
- if(dir[8] == '.')point = 9; //如第9個(gè)字符是"."
- if(point != 12) //如果有"."(為12就是沒(méi)有".")
- {
- for(i = 0;i < 3;i ++) //獲取3個(gè)擴(kuò)展名
- {
- if(dir[point + i] != 0) //如果沒(méi)有結(jié)束
- dird[8 + i] = dir[point + i]; //從"."后面獲取擴(kuò)展名
- else //如果結(jié)束了
- for(;i < 3;i ++) //剩下的字符填充空格
- dird[8 + i] = ' ';
- }
- }
- else //如果沒(méi)有"."
- for(i = 8;i < 11;i ++)
- dird[i] = ' '; //全部填充空格
- bigToSmall(dird,dird,SMALL_TO_BIG,11); //轉(zhuǎn)換成大寫(xiě)
- }
- /*************************************************************************
- *函 數(shù): openDir
- *作 用: 在當(dāng)前目錄下查找文件 如果是文件夾則打開(kāi) 是文件則返回文件首簇
- *入口參數(shù): dir(文件或文件夾名)
- *返 回 值: 文件首簇
- *************************************************************************/
- uint32 openDir(buf_t dir)
- {
- uint16 i;
- uint8 sector;
- uint32 currentClust;
- buf_t tmpString = malloc(11);
- currentClust = currentPath; //設(shè)置當(dāng)前簇
- fileNameProcess(dir,tmpString);
- while(1)
- {
- for(sector = 0;sector < dbrInfo.sectorsPerClust;sector ++) //掃描1整簇
- {
- readSector( //讀一扇區(qū)到緩沖區(qū)
- clustToSector(currentClust - dbrInfo.rootDirClust) //扇區(qū)和簇之間的單位轉(zhuǎn)換
- //當(dāng)前簇 - 根目錄首簇 + 根目錄首扇區(qū) = 當(dāng)前簇首扇區(qū)
- + sector //第"sector"個(gè)扇區(qū)
- + dbrInfo.rootDirSector, //以根目錄首扇區(qū)為參考
- dataBuffer); //讀到這里
- for(i = 0;i < 16;i ++) //每個(gè)目錄項(xiàng)占32個(gè)字節(jié)512/32 = 16所以要掃描16次
- {
- getFileItem(i *32); //從i*32偏移量獲取目錄項(xiàng)信息
- if(stringCmp(tmpString,fileItem->DIR_Name,11)) //如果找到文件
- {
- currentLong = i * 32; //獲取長(zhǎng)目錄項(xiàng)用
- if(fileItem->DIR_Attr == ATTR_DIRECTORY) //如果是文件夾
- {
- currentPath = (uint32)(fileItem->DIR_FstClusHI << 16)|(fileItem->DIR_FstClusLO); //設(shè)置當(dāng)前目錄首簇(相當(dāng)于進(jìn)入一個(gè)目錄)
- free(tmpString);
- return TRUE; //返回成功 可以用if(openDir("xxx")) == TRUE
- //來(lái)判讀這個(gè)文件是文件還是文件夾
- }
- else
- {
- free(tmpString);
- return (uint32)(fileItem -> DIR_FstClusHI << 16)|(fileItem -> DIR_FstClusLO); //如果是文件則返回文件首簇
- }
- }
- }
- }
- currentClust = getNextClust(currentClust); //根據(jù)當(dāng)前簇獲取下一簇
- if((currentClust == ENDCLUST)||(currentClust == BADCLUST)||(currentClust == FIRSTCLUS)) //如果掃描完所有簇還是沒(méi)有發(fā)現(xiàn)要找的文件則返回失敗
- {
- free(tmpString);
- return FALSE;
- }
- }
- free(tmpString);
- return FALSE;
- }
-
- /*************************************************************************
- *函 數(shù): open
- *作 用: 返回一個(gè)文件所在的簇
- *入口參數(shù): path(文件路徑)
- *返 回 值: 文件所在的簇
- *************************************************************************/
- uint32 fileOpen(buf_t path)
- {
- uint32 result;
- uint16 i;
- uint8 j = 0;
- buf_t dir;
- dataBuffer = malloc(512);
- dir = malloc(13); //開(kāi)辟一個(gè)13個(gè)字節(jié)的數(shù)組
- if(path[0] != '/') //判斷文件路徑
- {
- i = 0; //非根目錄 表示是當(dāng)前目錄
- }else
- {
- i = 1; //
- currentPath = dbrInfo.rootDirClust; //是根目錄 切換當(dāng)前目錄至根目錄
- }
- while(1)
- {
- if(path[i] == '/') //一個(gè)文件或目錄名的開(kāi)始例如:"/usr/src/linux/include/asm-generic/page.c"
- {
- dir[j + 1] = '\0';
- openDir(dir); //打開(kāi)目錄
- j = 0; //開(kāi)始一個(gè)文件或目錄名的獲取
- }else
- {
- dir[j] = path[i]; //獲取一個(gè)文件或目錄名
- if(path[i] == 0) //路徑結(jié)束
- {
- result = openDir(dir);
- free(dir);
- free(dataBuffer);
- return result; //返回文件或目錄的起始簇
- }
- j ++; //文件或目錄字符串中的位置
- }
- i++; //路徑字符串中的位置
- }
- }
- /*************************************************************************
- *函 數(shù): read
- *作 用: 讀文件
- *入口參數(shù): fp(文件首簇),buf(讀到的數(shù)據(jù)),llseek(相對(duì)文件首簇的偏移量),len(數(shù)據(jù)長(zhǎng)度 最大不能超過(guò)512),flag
- *返 回 值: 文件讀取結(jié)束信息
- *************************************************************************/
- uint8 fileRead(fp_t fp,buf_t buf,uint32 llseek,uint16 len,flag_t flag)
- {
- uint32 currentClust; //當(dāng)前簇
- uint32 var,var_llseek;
- uint16 i = 0;
- var_llseek = llseek;
- dataBuffer = malloc(512);
- currentClust = fp ;
- var = var_llseek >> 9;
- if(var >= dbrInfo.sectorsPerClust) //計(jì)算偏移量在哪個(gè)扇區(qū)
- if(flag & FONTFILE_MASK == FONTFILE) //判斷是否是字庫(kù)文件 因?yàn)椴患舆@個(gè)讀取字庫(kù)會(huì)很慢 加了這個(gè)如果有碎片會(huì)出意外
- currentClust = var / dbrInfo.sectorsPerClust + currentClust ; //從偏移量中計(jì)算出當(dāng)前簇;
- else
- {
- i = (var / dbrInfo.sectorsPerClust) ; //計(jì)算從文件頭部到偏移量有多少簇
- while(i-- ) //挨個(gè)簇查詢(xún)
- {
- currentClust = getNextClust(currentClust); //獲取下一簇
- if((currentClust == ENDCLUST)\
- || (currentClust == BADCLUST)\
- || (currentClust == FIRSTCLUS)) //有壞簇或到終點(diǎn)返回
- {
- free(dataBuffer);
- return FALSE;
- }
- }
- }
- readSector(clustToSector(currentClust - dbrInfo.rootDirClust)\
- + (var % dbrInfo.sectorsPerClust) + dbrInfo.rootDirSector,dataBuffer); //先從偏移位置讀取一扇區(qū)數(shù)據(jù)
- for(i = 0;i < len;i ++) //把有用數(shù)據(jù)放進(jìn)緩沖區(qū)
- {
- if((i + var_llseek % 512) >= 512) //如果讀取過(guò)程中超出當(dāng)前扇區(qū)
- {
- var_llseek = 0; //下一個(gè)扇區(qū)從頭讀取
- if(((var + 1) % dbrInfo.sectorsPerClust) == 0) //如果讀取過(guò)程中超出當(dāng)前簇
- {
- currentClust = getNextClust(currentClust); //獲取下一簇
- if((currentClust == ENDCLUST)\
- || (currentClust == BADCLUST)\
- || (currentClust == FIRSTCLUS)) //有壞簇或到終點(diǎn)返回
- {
- free(dataBuffer);
- return FALSE;
- }
- readSector(clustToSector(currentClust - dbrInfo.rootDirClust)\
- + ((var + 1) % dbrInfo.sectorsPerClust)\
- + dbrInfo.rootDirSector ,dataBuffer); //讀取下個(gè)簇的第一個(gè)扇區(qū)
- }else
- readSector(clustToSector(currentClust - dbrInfo.rootDirClust)\
- + (var % dbrInfo.sectorsPerClust)\
- + dbrInfo.rootDirSector + 1,dataBuffer); //讀取下個(gè)扇區(qū)
- }
- buf[i] = dataBuffer[i + var_llseek % 512]; //收集有用數(shù)據(jù)
- }
- free(dataBuffer);
- return TRUE;
- }
- /*************************************************************************
- *函 數(shù): getFileLongName
- *作 用: 從當(dāng)前文件中獲取文件長(zhǎng)目錄項(xiàng)信息
- *入口參數(shù): string(獲取到的長(zhǎng)文件名)
- *返 回 值: 無(wú)
- *************************************************************************/
- void getFileLongName(buf_t string)
- {
- uint8 val,val1,i,item;
- item = 0;
- uint32 offset;
- offset = currentLong;
- if(offset > 31)getFileLongDirItem(offset-32); //獲取當(dāng)前文件的長(zhǎng)目錄結(jié)構(gòu)
- if((longItem->LDIR_Attr == ATTR_LONG_NAME) && (offset > 31)) //如里是長(zhǎng)目錄項(xiàng)并
- {
- val = checkNameSum(fileItem->DIR_Name); //獲取文件效驗(yàn)和
- if(val == longItem->LDIR_Chksum) //對(duì)比文件效驗(yàn)和
- {
- do{
- getFileLongDirItem(offset-((item + 1) << 5)); //獲取當(dāng)前文件的長(zhǎng)目錄結(jié)構(gòu)的子項(xiàng)
- for(i = 0;i < 13;i ++) //第個(gè)子項(xiàng)是13個(gè)字符
- {
- if(i < 5) //第1-5個(gè)字符
- {
- if(longItem->LDIR_Name1[i] == 0xffff) //是否結(jié)束
- {
- string[i + (item * 13) + 1] = 0; //設(shè)置結(jié)束
- return; //退出
- }
- if(longItem->LDIR_Name1[i] < 0x100) //是否Unicode
- string[i + (item * 13)] = longItem->LDIR_Name1[i]; //從長(zhǎng)目錄項(xiàng)中獲取字符
- else
- {
- UnitoAsc(longItem -> LDIR_Name1[i]); //把Unicode轉(zhuǎn)換成ASCII
- string[i + (item * 13)] = longItem -> LDIR_Name1[i];
- } //是Unicode
- }
- if((i > 4)&&(i < 11)) //第6-11個(gè)字符
- {
- if(longItem->LDIR_Name2[i - 5] == 0xffff) //是否結(jié)束
- {
- string[i + (item * 13) + 1] = 0; //設(shè)置結(jié)束
- return; //退出
- }
- if(longItem->LDIR_Name2[i - 5] < 0x100) //是否Unicode
- string[i + (item * 13)] = longItem->LDIR_Name2[i - 5]; //從長(zhǎng)目錄項(xiàng)中獲取字符
- else
- {
- UnitoAsc(longItem -> LDIR_Name2[i]);
- string[i + (item * 13)] = longItem -> LDIR_Name2[i]; //把Unicode轉(zhuǎn)換成ASCII
- }
- }
- if (i > 10) //第12-13個(gè)字符
- {
- if(longItem->LDIR_Name3[i - 11] == 0xffff) //是否結(jié)束
- {
- string[i + (item * 13) + 1] = 0; //設(shè)置結(jié)束
- return; //退出
- }
- if(longItem->LDIR_Name3[i - 11] < 0x100) //是否Unicode
- string[i + (item * 13)] = longItem->LDIR_Name3[i - 11];//從長(zhǎng)目錄項(xiàng)中獲取字符
- else
- {
- UnitoAsc(longItem -> LDIR_Name3[i]);
- string[i + (item * 13)] = longItem -> LDIR_Name3[i]; //把Unicode轉(zhuǎn)換成ASCII
- }
- }
- }
- item ++; //子項(xiàng)計(jì)數(shù)
- }
- while(!(longItem->LDIR_Ord & 0x40)); //是否最后一個(gè)子項(xiàng)
- }
- }
- else
- {
- if(fileItem->DIR_LowCase & FILE_NAME_SMALL)
- bigToSmall(fileItem->DIR_Name,string,BIG_TO_SMALL,8); //如果文件名是小寫(xiě)就把短文件名轉(zhuǎn)換成小寫(xiě)
- else
- bigToSmall(fileItem->DIR_Name,string,SMALL_TO_BIG,8); //否則轉(zhuǎn)換成大寫(xiě)
- if(fileItem->DIR_LowCase & FILE_EXT_SMALL)
- bigToSmall(&fileItem->DIR_Name[8],&string[8],BIG_TO_SMALL,3); //如果擴(kuò)展名是小寫(xiě)就把短文件名中的擴(kuò)展名小寫(xiě)
- else
- bigToSmall(&fileItem->DIR_Name[8],&string[8],SMALL_TO_BIG,3); //否則轉(zhuǎn)換成大寫(xiě)
- val = delSpace(string,string,8); //刪除空格
- string[val] = '.'; //設(shè)置'.'
- if(strncmp(&fileItem->DIR_Name[8]," ",3) != 0) //擴(kuò)展名是否為空
- {
- val1 = delSpace(&string[8],&string[val + 1],3); //如不為空刪除空格并連接文件名和擴(kuò)展名
- string[val1 + val + 1] = 0; //設(shè)置結(jié)束
- }else
- string[val] = 0; //為空就不設(shè)置'.'
- }
- }
復(fù)制代碼
主程序:
- #include "config.h"
- #include "drivers/uart.h"
- #include "drivers/mmc.h"
- #include "fs/fat32.h"
- #include "drivers/lcd.h"
- #include "logo.h"
- #include "gui/font.h"
- #include "gui/gui_window.h"
- #include "gui/gui_explorer.h"
- int main()
- {
- uint32 fp;
- uint32 i;
- buf_t dataBuffer;
- dataBuffer = malloc(512);
- PORTA = 0xff;
- PORTB = 0xff;
- DDRA = 0xff;
- DDRB = 0xff;
- Uart_Initialize(9600);
- LCD_Initialize();
- GUI_DrawBitMap(image1,0,0,240,127,PROGRAM|SUN);
- // while(1);
-
- back:
- if(MMC_Initialize() == FALSE){printf("MMC卡錯(cuò)誤\r\n");_delay_ms(1000);goto back;}
- if(FAT_Initialize(0) == FALSE)
- printf("MMC卡錯(cuò)誤\r\n");
- else
- {
- // GUI_DrawBitMap(lkj,0,0,16,16,RAMCODE|SUN);
- Font_Initialize();
- fp = fileOpen("/admin.txt");
- // getFileLongName(dataBuffer);
- // printf("fp = %d\r\n",fp);
- fileRead(fp,dataBuffer,0,512,0);
- // GUI_Explorer("/backup~1.dbk");
- // printf("dataBuffer = %s\r\n",dataBuffer);
- LCD_PutString12(dataBuffer,0,0,RAMCODE|SUN);
- // LCD_PutString16(dataBuffer,0,0,RAMCODE|SUN);
- }
- // LCD_ClearRAM();
- free(dataBuffer);
- while(1);
- }
復(fù)制代碼
所有資料51hei提供下載:
AVR32 SD FAT32.7z
(1.02 MB, 下載次數(shù): 57)
2021-11-6 04:11 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|