?? test1_main.#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 + -