⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 stm32-SDIO+FatFS文件系统txt-int-ascii
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (result != FR_OK)
	{
		printf("挂载文件系统失败 (%d)\r\n", result);
	}

	/* 打开根文件夹 */
	result = f_opendir(&DirInf, "/"); /* 如果不带参数,则从当前目录开始 */
	if (result != FR_OK) 
	{
		printf("打开根目录失败 (%d)\r\n", result);
		return;
	}

	/* 打开文件 */
	result = f_open(&file, "armfly.txt", FA_CREATE_ALWAYS | FA_WRITE);
	
	/* 写一串数据 */
	result = f_write(&file, "FatFS Write Demo \r\n www.armfly.com \r\n", 34, &bw);////34 ge?????	
	if (result == FR_OK)
	{
		printf("armfly.txt 文件写入成功\r\n");
	}
	else
	{
		printf("armfly.txt 文件写入失败\r\n");
	}
	
	/* 关闭文件*/
	f_close(&file);
 	
	/* 卸载文件系统 */
	f_mount(0, NULL);	
}

/*
*********************************************************************************************************
*	函 数 名: ReadFileData
*	功能说明: 读取文件armfly.txt前128个字符,并打印到串口
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
static void ReadFileData(void)
{
	/* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
	FRESULT result;
	FATFS fs;
	FIL file;
	DIR DirInf;  
	uint32_t bw;
	char buf[60];

	
 	/* 挂载文件系统 */
	result = f_mount(0, &fs);			/* Mount a logical drive */
	if (result != FR_OK)
	{
		printf("挂载文件系统失败(%d)\r\n", result);
	}

	/* 打开根文件夹 */
	result = f_opendir(&DirInf, "/"); /* 如果不带参数,则从当前目录开始armfly.txt */
	if (result != FR_OK) 
	{
		printf("打开根目录失败(%d)\r\n", result);
		return;
	}

	/* 打开文件 */
	result = f_open(&file, "Speed11.txt", FA_OPEN_EXISTING | FA_READ);
	if (result !=  FR_OK)
	{
		printf("Don't Find File : armfly.txt\r\n");
		return;		
	}

	/* 读取文件 */
	// while(!feof(&file))
	// 	result = f_read(&file, &buf, sizeof(buf), &bw);	 //&buf is aim reg , sizeof is number 
	//       //
	result = f_read(&file, &buf, sizeof(buf) - 1, &bw);	 //&buf is aim reg , sizeof is number 
	if (bw > 0)
	{
		buf[bw] = 0;
		printf("\r\narmfly.txt 文件内容 : \r\n%s\r\n", buf);
			ascii_int(buf);	  ////int to  string	ascii_int	  	uint receivedtxt
			printf("%s\r\n", receivedtxt[0]);
			printf("%s\r\n", receivedtxt[1]);
			printf("%s\r\n", receivedtxt[2]);
	}
	else
	{
		printf("\r\narmfly.txt 文件内容 : \r\n");		
	}
	
	/* 关闭文件*/
	f_close(&file);
 	
	/* 卸载文件系统 */
	f_mount(0, NULL);
}

/*
*********************************************************************************************************
*	函 数 名: CreateDir
*	功能说明: 在SD卡根目录创建Dir1和Dir2目录,在Dir1目录下创建子目录Dir1_1
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
static void CreateDir(void)
{
	/* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
	FRESULT result;
	FATFS fs;
	
 	/* 挂载文件系统 */
	result = f_mount(0, &fs);			/* Mount a logical drive */
	if (result != FR_OK)
	{
		printf("挂载文件系统失败 (%d)\r\n", result);
	}

	/* 创建目录/Dir1 */
	result = f_mkdir("/Dir1"); 
	if (result == FR_OK) 
	{
		printf("f_mkdir Dir1 Ok\r\n");
	}
	else if (result == FR_EXIST)
	{
		printf("Dir1 目录已经存在(%d)\r\n", result);
	}
	else
	{
		printf("f_mkdir Dir1 失败 (%d)\r\n", result);
		return;
	}

	/* 创建目录/Dir2 */
	result = f_mkdir("/Dir2"); 
	if (result == FR_OK) 
	{
		printf("f_mkdir Dir2 Ok\r\n");
	}
	else if (result == FR_EXIST)
	{
		printf("Dir2 目录已经存在(%d)\r\n", result);
	}
	else
	{
		printf("f_mkdir Dir2 失败 (%d)\r\n", result);
		return;
	}			

	/* 创建子目录 /Dir1/Dir1_1	   注意:创建子目录Dir1_1时,必须先创建好Dir1 */
	result = f_mkdir("/Dir1/Dir1_1"); /* */
	if (result == FR_OK) 
	{
		printf("f_mkdir Dir1_1 成功\r\n");
	}
	else if (result == FR_EXIST)
	{
		printf("Dir1_1 目录已经存在 (%d)\r\n", result);
	}
	else
	{
		printf("f_mkdir Dir1_1 失败 (%d)\r\n", result);
		return;
	}
 	
	/* 卸载文件系统 */
	f_mount(0, NULL);	
}

/*
*********************************************************************************************************
*	函 数 名: WriteFileTest
*	功能说明: 测试文件读写速度
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
static void WriteFileTest(void)
{
	/* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
	FRESULT result;
	FATFS fs;
	FIL file;
	DIR DirInf;  
	uint32_t bw;
	uint32_t i;
	uint32_t runtime1,runtime2,timelen;
	uint8_t err = 0;
	 int len=0;  
//	for (i = 0; i < sizeof(g_TestBuf); i++)	  ////512
//	{
	//	g_TestBuf[i] = (i % 10) + '0';	   /////yu shu   0 ascii 48  bian zi  fu 
//	}																	 
   g_TestBuf[0] = 200;	//uint8_t g_TestBuf[BUF_SIZE]; 255/10=25 yu 5   只能shi  0----9 de zifu 
   g_TestBuf[1] = 0;
   g_TestBuf[2] = 100;	 ///jia 0  bu wei   100
   
  // len=format_data(str, g_TestBuf[i]);	   ////int to  string
  
  	/* 挂载文件系统 */
	result = f_mount(0, &fs);			/* Mount a logical drive */
	if (result != FR_OK)
	{
		printf("挂载文件系统失败 (%d)\r\n", result);
	}

	/* 打开根文件夹 */
	result = f_opendir(&DirInf, "/"); /* 如果不带参数,则从当前目录开始 */
	if (result != FR_OK) 
	{
		printf("打开根目录失败 (%d)\r\n", result);
		return;
	}

	/* 打开文件 */
	result = f_open(&file, "Speed11.txt", FA_CREATE_ALWAYS | FA_WRITE);
	
	/* 写一串数据 */
	//	TEST_FILE_LEN			(2*1024*1024)	/* 用于测试的文件长度 */
//#define BUF_SIZE				512				/* 每次读写SD卡的最大数据长度 */
//uint8_t g_TestBuf[BUF_SIZE];		
	   //
	printf("开始写文件 %dKB ...\r\n", TEST_FILE_LEN / 1024);
	runtime1 = bsp_GetRunTime();	/* 读取系统运行时间 */
	for (i = 0; i <3 ; i++)	  ////4096 TEST_FILE_LEN / BUF_SIZE
	{
		//result = f_write(&file, g_TestBuf[i], sizeof(g_TestBuf), &bw);
		len=format_data(str, g_TestBuf[i]);	  ////int to  string
		result = f_write(&file, str, 5, &bw);
		
		if (result == FR_OK)
		{
			if ((i % 100) == 0)
			{
				bsp_LedToggle(1);	/* 闪烁LED---change0---1  ????? */
			}
		}
		else
		{
			err = 1;
			printf("Speed1.txt 文件写失败\r\n");
			break;
		}
	}
	runtime2 = bsp_GetRunTime();	/* 读取系统运行时间 */
	
	if (err == 0)
	{
		timelen = (runtime2 - runtime1);
		printf("  写耗长 : %dms   平均写速度 : %dB/S (%dKB/S)\r\n", timelen, 
			(TEST_FILE_LEN * 1000) / timelen, ((TEST_FILE_LEN / 1024) * 1000) / timelen);
	}
	
	f_close(&file);		/* 关闭文件*/

	/* 开始读文件测试 ////////////////////////////////////////////////////////////////////////////////*/
	result = f_open(&file, "Speed11.txt", FA_OPEN_EXISTING | FA_READ);
	if (result !=  FR_OK)
	{
		printf("没有找到文件: Speed1.txtt\r\n");
		return;		
	}

	printf("开始读文件 %dKB ...\r\n", TEST_FILE_LEN / 1024);
	runtime1 = bsp_GetRunTime();	/* 读取系统运行时间 */
	for (i = 0; i < TEST_FILE_LEN / BUF_SIZE; i++)
	{
		result = f_read(&file, g_TestBuf, sizeof(g_TestBuf), &bw);	
		if (result == FR_OK)
		{
			if ((i % 100) == 0)
			{
				bsp_LedToggle(1);	/* 闪烁LED */
			}
		}
		else
		{
			err = 1;
			printf("Speed1.txt 文件读失败\r\n");
			break;
		}
	}
	runtime2 = bsp_GetRunTime();	/* 读取系统运行时间 */
	
	if (err == 0)
	{
		timelen = (runtime2 - runtime1);
		printf("  读耗时 : %dms   平均读速度 : %dB/S (%dKB/S)\r\n", timelen, 
			(TEST_FILE_LEN * 1000) / timelen, ((TEST_FILE_LEN / 1024) * 1000) / timelen);
	}

	/* 关闭文件*/
	f_close(&file);
 	
	/* 卸载文件系统 */
	f_mount(0, NULL);	
}

/*
*********************************************************************************************************
*	函 数 名: InitBoard
*	功能说明: 初始化硬件设备
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
static void InitBoard(void)
{
	/* 配置串口,用于printf输出 */
	bsp_InitUart();

	/* 配置LED指示灯GPIO */
	bsp_InitLed();

	/* 配置按键GPIO, 必须在bsp_InitTimer之前调用 */
	bsp_InitButton();

	/* 初始化systick定时器,并启动定时中断 */
	bsp_InitTimer();
}

/*
*********************************************************************************************************
*	函 数 名: PrintfLogo
*	功能说明: 打印例程名称和例程发布日期, 接上串口线后,打开PC机的超级终端软件可以观察结果
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
static void PrintfLogo(void)
{
	printf("*************************************************************\n\r");
	printf("* 例程名称   : %s\r\n", EXAMPLE_NAME);	/* 打印例程名称 */
	printf("* 例程版本   : %s\r\n", DEMO_VER);		/* 打印例程版本 */
	printf("* 发布日期   : %s\r\n", EXAMPLE_DATE);	/* 打印例程日期 */

	/* 打印ST固件库版本,这3个定义宏在stm32f10x.h文件中 */
	printf("* 固件库版本 : %d.%d.%d\r\n", __STM32F10X_STDPERIPH_VERSION_MAIN,
			__STM32F10X_STDPERIPH_VERSION_SUB1,__STM32F10X_STDPERIPH_VERSION_SUB2);
	printf("* \n\r");	/* 打印一行空格 */
	printf("* QQ    : 1295744630 \r\n");
	printf("* Email : armfly@qq.com \r\n");
	printf("* Copyright www.armfly.com 安富莱电子\r\n");
	printf("*************************************************************\n\r");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -