21ic问答首页 - FatFS TF卡创建读写文件正常,插电脑上空白
FatFS TF卡创建读写文件正常,插电脑上空白
根本就没好ID了2024-09-30
用的是HC32F4A0,目前做了:
但是把SD卡插到读卡器上,看到的就是空白。有没有大佬知道这是为什么
代码附上
#include "FileEdit.h"
#include "ff.h"
#include "stdio.h"
#include "BSP_Timer_INT_Count.h"
FATFS fsobject;
FIL fp;
BYTE work[FF_MAX_SS];
const char write_buf[] = "abcdefg";
char read_buf[FF_MAX_SS] = "";
UINT bw;
UINT br;
void FatFS_Test(void)
{
FRESULT res;
/* 挂载文件系统 */
res = f_mount(&fsobject, "0:", 1); //挂载fsobject到文件系统、0: 存储器路径,也就是SD卡、1立即挂载
if(res != FR_OK)
{
res = f_mkfs("0:", 0, work, sizeof(work));
res = f_mount(NULL, "0:", 1);
res = f_mount(&fsobject, "0:", 1);
}
/* 打开、创建文件 */
f_mkdir("0:/dir0");
res = f_open(&fp, "0:/dir0/qwerpoiu.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
if(res == LL_OK)
{
res = f_write(&fp, write_buf, sizeof(write_buf), &bw);
f_close(&fp);
res = f_open(&fp, "0:/dir0/qwerpoiu.txt", FA_READ);
if(res==LL_OK)
{
f_lseek(&fp, 0);
res = f_read(&fp, read_buf, f_size(&fp), &br);
__nop();
}
else
f_close(&fp);
}
f_unmount("0:");
}
- 在电脑上对SD卡格式化为Fat32格式
- 为了确定写入了SD卡,而不是读取缓存。写入文件后,将文件关闭,再以READ模式打开,进行读取,也是可以读的。
但是把SD卡插到读卡器上,看到的就是空白。有没有大佬知道这是为什么
代码附上
#include "FileEdit.h"
#include "ff.h"
#include "stdio.h"
#include "BSP_Timer_INT_Count.h"
FATFS fsobject;
FIL fp;
BYTE work[FF_MAX_SS];
const char write_buf[] = "abcdefg";
char read_buf[FF_MAX_SS] = "";
UINT bw;
UINT br;
void FatFS_Test(void)
{
FRESULT res;
/* 挂载文件系统 */
res = f_mount(&fsobject, "0:", 1); //挂载fsobject到文件系统、0: 存储器路径,也就是SD卡、1立即挂载
if(res != FR_OK)
{
res = f_mkfs("0:", 0, work, sizeof(work));
res = f_mount(NULL, "0:", 1);
res = f_mount(&fsobject, "0:", 1);
}
/* 打开、创建文件 */
f_mkdir("0:/dir0");
res = f_open(&fp, "0:/dir0/qwerpoiu.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
if(res == LL_OK)
{
res = f_write(&fp, write_buf, sizeof(write_buf), &bw);
f_close(&fp);
res = f_open(&fp, "0:/dir0/qwerpoiu.txt", FA_READ);
if(res==LL_OK)
{
f_lseek(&fp, 0);
res = f_read(&fp, read_buf, f_size(&fp), &br);
__nop();
}
else
f_close(&fp);
}
f_unmount("0:");
}
赞0
在例程的基础上开发
评论
2024-09-30
赞0
评论
2024-09-30
您需要登录后才可以回复 登录 | 注册