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

📄 test1_main.#3

📁 C8051F340读写SD卡,带文件系统
💻 #3
字号:

#include "c8051f340.h"
#include "spi.h"
#include "uart.h"
#include "sd.h"
#include "mcu_init.h"
#include "diskio.h"
#include "ff.h"

BYTE buff[1024]; /*working buffer*/

DWORD get_fattime (void)
{
	DWORD tmr = 0;
	return tmr;
}

/*测试函数*/
void test_readSingleBlock(unsigned long);
void test_writeSingleBlock(unsigned long);
void test_readMultipleBlock(unsigned long, unsigned char);
void test_writeMultipleBlock(unsigned long, unsigned char);
void test_FileWR();
void test_FileWRTXT();

/*------------------------------------------------------------------------------------*/
/*main*/
int main(void)
{
	PCA0MD &= ~0x40; /*关内部看门狗*/

	Init_Devices();

	CLS;/*windows超级终端清屏*/

	transmitString("***********************************\r\n");
	transmitString(" xzp21st's microSD Card Testing..\r\n");
	transmitString("***********************************\r\n");
	
	if(disk_initialize(0))
		transmitString("\r\nSD card init fail!\r\n");
	else
	{
		transmitString("\r\nSD card init success!\r\n");

		//单块读写和多块读写会破坏文件系统
		/*单块读写测试*/
		//test_writeSingleBlock(0);
		//test_readSingleBlock(0);
		
		
		/*多块读写测试*/
		//test_readMultipleBlock(0, 2);
		//test_writeMultipleBlock(10000, 2);
		
		//读写测试,卡必须是经过格式化,不能经过SD单独操作
		//FA_CREATE_ALWAYS | FA_WRITE | FA_READ  这个f_open模式可写   不可读
   		//FA_OPEN_ALWAYS | FA_WRITE | FA_READ    这个f_open模式可写   不可读
   		//FA_OPEN_EXISTING | FA_WRITE | FA_READ  这个f_open模式可读   不可写
		//文件打开之后最好不要同时读写。
		/*文件读写测试*/
		test_FileWR();   
		//test_FileWRTXT();
	}
	transmitString("|/-\\\r");
	delay(1000);
	transmitString("/-\\|\r");
	delay(1000);
	transmitString("-\\|/\r");
	delay(1000);
	transmitString("\\|/-\r");
	delay(1000);
	transmitString("|/-\\\r");
	delay(1000);
	while(1)
	{	
		;
	}

	return 0;
}

/*------------------------------------------------------------------------------------*/
/*for test*/

/*测试单块读操作*/
void test_readSingleBlock(unsigned long block_addr)
{
	if(disk_read(0, buff, block_addr, 1)) /*读第block_addr块*/
		transmitString("\r\nRead single block operation fail!!!\r\n");
	else
	{
		transmitString("\r\nRead single block operation success~~~\r\n");
		transmitString("\r\nPress any key to display the data~~~\r\n");
		receiveByte();
		CLS;
		displayData(buff, 512);
	}
}

/*测试单块写操作*/
void test_writeSingleBlock(unsigned long block_addr)
{
	int i = 0;
	for(i=0; i<512; i++)
	{
		buff[i] = i+2;
	}
	
	if(disk_write(0, buff, block_addr, 1)) /*向第block_addr块写数据*/
		transmitString("\r\nWrite single block operation fail!!!\r\n");
	else /*写成功*/
	{	
		transmitString("\r\nWrite single block operation success~~~\r\n");
		test_readSingleBlock(block_addr);
	}
}

/*测试多块读操作*/
void test_readMultipleBlock(unsigned long block_addr, unsigned char count)
{
	if(disk_read(0, buff, block_addr, count)) /*读从第num块开始的两个块*/
		transmitString("\r\nRead multiple blocks operation fail!!!\r\n");
	else /*读多块成功*/
	{
		transmitString("\r\nRead multiple blocks operation success~~~\r\n");
		transmitString("\r\nPress any key to display the data~~~\r\n");
		receiveByte();
		CLS;
		displayData(buff, 1024);
	}
}

/*测试多块写操作*/
void test_writeMultipleBlock(unsigned long block_addr, unsigned char count)
{
	int i = 0;
	for(i=0; i<1024; i++)
	{
		buff[i] = i+1;
	}
	
	if(disk_write(0, buff, block_addr, count))
		transmitString("\r\nWrite multiple blocks operation fail!!!\r\n");
	else /*写多块成功*/
	{	
		transmitString("\r\nWrite multiple blocks operation success~~~\r\n");
		test_readMultipleBlock(block_addr, count);
	}
}
/*测试文件TXT读写操作,文件大小为512字节*/

void test_FileWRTXT()
{
	FATFS fs;      //Work area (file system object) for logical drive
    FIL file;      //file objects
	//UINT bw, br;   //File R/W count


	int i = 0;
	for(i=0; i<512; i++)
	{
		buff[i] = i+1;//i-16位,buff-8位
	}	
	//Register a work area for logical drive 0
    f_mount(0, &fs);

   //Create file
   
    if(f_open(&file, "test2.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ))	
		transmitString("\r\nFile creat fail!\r\n");
	else
	{	
		transmitString("\r\nFile creat success!\r\n");

		//f_puts(buff, &file);
		
		
			transmitString("\r\nFile write success!\r\n");
			transmitString("\r\nPress any key to read the file!\r\n");
			receiveByte();
			for(i=0; i<512; i++)
			{
				buff[i] = 0;//i-16位,buff-8位
			}	
			f_gets(buff, sizeof(buff), &file);
			
			transmitString("\r\nFile read success!\r\n");
			displayData(buff, sizeof(buff));
			
			
		
		//Close all files
   	 	f_close(&file);
	}
	
    //Unregister a work area before discard it
    f_mount(0, 0);
}
/*测试文件读写操作,文件大小为512字节*/
void test_FileWR()
{
	FATFS fs;       /*Work area (file system object) for logical drive*/
    FIL file;      /*file objects*/
	UINT bw, br;    /*File R/W count*/


	int i = 0;
	for(i=0; i<512; i++)
	{
		buff[i] = i+5;//i-16位,buff-8位
	}	
	//写文件测试
	/*Register a work area for logical drive 0*/
    f_mount(0, &fs);

    /*Create file*/
    if(f_open(&file, "test4.dat", FA_CREATE_ALWAYS | FA_WRITE))	
		transmitString("\r\nFile creat fail!\r\n");
	else
	{	
		transmitString("\r\nFile creat success!\r\n");

		if(f_write(&file, buff, 512, &bw))
			transmitString("\r\nFile write fail!\r\n");
		else
		{	
			transmitString("\r\nFile write success!\r\n");
		}
		/*Close all files*/
   	 	f_close(&file);
	}
	
    /*Unregister a work area before discard it*/
    f_mount(0, 0);
//读文件测试
	f_mount(0, &fs);

    /*Create file*/
    if(f_open(&file, "test4.dat", FA_OPEN_EXISTING | FA_READ))	
		transmitString("\r\nFile creat fail!\r\n");
	else
	{	
		transmitString("\r\nFile creat success!\r\n");
			transmitString("\r\nPress any key to read the file!\r\n");
			receiveByte();
			for(i=0; i<512; i++)
			{
				buff[i] = 0;//i-16位,buff-8位
			}	
			if(f_read(&file, buff, 512, &br))
				transmitString("\r\nFile read fail!\r\n");
			else
			{	
				transmitString("\r\nFile read success!\r\n");
				transmitString("\r\nPress any key to display the data!\r\n");
				receiveByte();
				displayData(buff, 512);
			}
		
		/*Close all files*/
   	 	f_close(&file);
	}
	
    /*Unregister a work area before discard it*/
    f_mount(0, 0);
}
/*void test_FileWR()
{
	FATFS fs;       //Work area (file system object) for logical drive
    FIL file;      //file objects
	UINT bw, br;    //File R/W count
	BYTE ress;

	int i = 0;
	for(i=0; i<512; i++)
	{
		buff[i] = i+1;//i-16位,buff-8位
	}	
	//Register a work area for logical drive 0
    f_mount(0, &fs);

    //Create file
    if(f_open(&file, "test1.bmp", FA_OPEN_EXISTING | FA_WRITE | FA_READ))	
		transmitString("\r\nFile creat fail!\r\n");
	else
	{	
		transmitString("\r\nFile creat success!\r\n");

		//if(f_write(&file, buff, 512, &bw))
		//	transmitString("\r\nFile write fail!\r\n");
		//else
		//{	
		//	transmitString("\r\nFile write success!\r\n");
		//	transmitString("\r\nPress any key to read the file!\r\n");
		//	receiveByte();
		//	for(i=0; i<512; i++)
		//	{
		//		buff[i] = 0;//i-16位,buff-8位
		//	}
		for(;;)//读大文件
		{
			if(ress=f_read(&file, buff, 512, &br))
				transmitString("\r\nFile read fail!\r\n");
			else
			{	
				transmitString("\r\nFile read success!\r\n");					
			}
			if (ress || br == 0) break;   // error or end file 
			transmitString("\r\nPress any key to display the data!\r\n");
			receiveByte();
			displayData(buff, 512);
         }
		//}
		//f_puts ("hello word!", &file);
		//Close all files
   	 	f_close(&file);
	}
	
    //Unregister a work area before discard it
    f_mount(0, 0);
}*/

⌨️ 快捷键说明

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