在做图片显示实验室FATFS文件系统中f_open()函数的使用问题

2019-07-21 06:57发布

while(font_init()) //检查字库
{    
LCD_ShowString(60,50,16,"Font Error!");
delay_ms(200);  
LCD_Fill(60,50,240,66,WHITE);//清除显示     
delay_ms(200);  
}    
  while(f_opendir(&picdir,"0:/PICTURE"))//打开图片文件夹//这里是能打开的
  {    
Show_Str(60,170,240,16,"PICTURE文件夹错误!",16,0);
delay_ms(200);  
LCD_Fill(60,170,240,186,WHITE);//清除显示     
delay_ms(200);  
}
totpicnum=pic_get_tnum("0:/PICTURE"); //得到总有效文件数/totpicnum=0
  while(totpicnum==NULL)//图片文件为0
  {    
Show_Str(60,170,240,16,"没有图片文件!",16,0);//液晶上最总运行的结果是没有图片显示,也就是说totpicnum=0,即pic_get_tnum("0:/PICTURE")的返回值为0
delay_ms(200);  
LCD_Fill(60,170,240,186,WHITE);//清除显示     
delay_ms(200);  
}

//得到path路径下,目标文件的总个数
//path:路径    
//返回值:总有效文件数
u16 pic_get_tnum(u8 *path)
{  
u8 res=0;
u16 rval=0;
  DIR tdir; //临时目录
FILINFO tfileinfo; //临时文件信息
u8 *fn;       
    res=f_opendir(&tdir,(const TCHAR*)path); //打开目录//即f_opendir(&tdir,(const TCHAR*)path)的返回值不为FR_OK
  tfileinfo.lfsize=_MAX_LFN*2+1; //长文件名最大长度
tfileinfo.lfname=mymalloc(tfileinfo.lfsize);//为长文件缓存区分配内存
if(res==FR_OK&&tfileinfo.lfname!=NULL)//调试的结果是res不等于FR_OK
{
while(1)//查询总的有效文件数
{
       res=f_readdir(&tdir,&tfileinfo);       //读取目录下的一个文件
       if(res!=FR_OK||tfileinfo.fname[0]==0)break; //错误了/到末尾了,退出  
      fn=(u8*)(*tfileinfo.lfname?tfileinfo.lfname:tfileinfo.fname);  
res=f_typetell(fn);
//Show_Str(60,200,240,16,"测试!",16,0);
if((res&0XF0)==0X50)//取高四位,看看是不是图片文件
{
rval++;//有效文件数增加1
}    
}  

return rval;
}

FRESULT f_opendir (
DIR* dp, /* Pointer to directory object to create */
const TCHAR* path /* Pointer to the directory path */
)
{
FRESULT res;
FATFS* fs;
DEF_NAMEBUF;


if (!dp) return FR_INVALID_OBJECT;

/* Get logical drive number */
res = find_volume(&fs, &path, 0);
if (res == FR_OK) {
dp->fs = fs;
INIT_BUF(*dp);
res = follow_path(dp, path); /* Follow the path to the directory */
FREE_BUF();
if (res == FR_OK) { /* Follow completed */
if (dp->dir) { /* It is not the origin directory itself */
if (dp->dir[DIR_Attr] & AM_DIR) /* The object is a sub directory */
dp->sclust = ld_clust(fs, dp->dir);
else /* The object is a file */
res = FR_NO_PATH;
}
if (res == FR_OK) {
dp->id = fs->id;
res = dir_sdi(dp, 0); /* Rewind directory *///调试发现dir_sdir函数的返回值不为FR_OK
#if _FS_LOCK
if (res == FR_OK) {
if (dp->sclust) {
dp->lockid = inc_lock(dp, 0); /* Lock the sub directory */
if (!dp->lockid)
res = FR_TOO_MANY_OPEN_FILES;
} else {
dp->lockid = 0; /* Root directory need not to be locked */
}
}
#endif
}
}
if (res == FR_NO_FILE) res = FR_NO_PATH;
}
if (res != FR_OK) dp->fs = 0; /* Invalidate the directory object if function faild */
LEAVE_FF(fs, res);
}

/*-----------------------------------------------------------------------*/
/* Directory handling - Set directory index                              */
/*-----------------------------------------------------------------------*/
//去掉static方便其他地方调用
FRESULT dir_sdi (
DIR* dp, /* Pointer to directory object */
UINT idx /* Index of directory table */
)
{
DWORD clst, sect;
UINT ic;
dp->index = (WORD)idx; /* Current index */
clst = dp->sclust; /* Table start cluster (0:root) */
if (clst == 1 || clst >= dp->fs->n_fatent) /* Check start cluster range */
return FR_INT_ERR;
if (!clst && dp->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
clst = dp->fs->dirbase;

if (clst == 0) { /* Static table (root-directory in FAT12/16) */
if (idx >= dp->fs->n_rootdir) /* Is index out of range? */
return FR_INT_ERR;
sect = dp->fs->dirbase;
}
else { /* Dynamic table (root-directory in FAT32 or sub-directory) */
ic = SS(dp->fs) / SZ_DIR * dp->fs->csize; /* Entries per cluster */
while (idx >= ic) { /* Follow cluster chain */
clst = get_fat(dp->fs, clst); /* Get next cluster */
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
if (clst < 2 || clst >= dp->fs->n_fatent) /* Reached to end of table or internal error */
return FR_INT_ERR;
idx -= ic;
}
sect = clust2sect(dp->fs, clst);
}
dp->clust = clst; /* Current cluster# */
if (!sect) return FR_INT_ERR;
dp->sect = sect + idx / (SS(dp->fs) / SZ_DIR); /* Sector# of the directory entry */
dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */

return FR_OK;
}



红字是根据实验结果我自己理解的,如果我把

totpicnum=pic_get_tnum("0:/PICTURE"); //得到总有效文件数
  while(totpicnum==NULL)//图片文件为0
  {    
Show_Str(60,170,240,16,"没有图片文件!",16,0);
delay_ms(200);  
LCD_Fill(60,170,240,186,WHITE);//清除显示     
delay_ms(200);  
}
这部分注释掉直接领totpicnum=1;是能够显示SD卡中PICTURE文件夹中的所有图片的。。

原子哥这是为什么啊?


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。