📄 main.c
字号:
#include "C8051F060.h"
#include "stdio.h"
#include "string.h"
#include "integer.h"
#include "ff.h"
#include "diskio.h"
unsigned char send_cmd(unsigned char cmd, /* Command byte */unsigned long arg /* Argument */) ;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;
}
void SPI_Init()
{
SFRPAGE = SPI0_PAGE;
SPI0CFG = 0x70;
SPI0CN = 0x01;
SPI0CKR = 0x6D; //100KHz
}
void EMI_Init()
{
SFRPAGE = EMI0_PAGE;
EMI0CF = 0x33;
}
void Port_IO_Init()
{
// P0.0 - TX0 (UART0), Open-Drain, Digital
// P0.1 - RX0 (UART0), Open-Drain, Digital
// P0.2 - SCK (SPI0), Open-Drain, Digital
// P0.3 - MISO (SPI0), Open-Drain, Digital
// P0.4 - MOSI (SPI0), Open-Drain, Digital
// P0.5 - TX1 (UART1), Open-Drain, Digital
// P0.6 - RX1 (UART1), Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Open-Drain, Digital
// P1.1 - Unassigned, Open-Drain, Digital
// P1.2 - Unassigned, Open-Drain, Digital
// P1.3 - Unassigned, Open-Drain, Digital
// P1.4 - Unassigned, Open-Drain, Digital
// P1.5 - Unassigned, Open-Drain, Digital
// P1.6 - Unassigned, Open-Drain, Digital
// P1.7 - Unassigned, Open-Drain, Digital
// P2.0 - Unassigned, Open-Drain, Digital
// P2.1 - Unassigned, Open-Drain, Digital
// P2.2 - Unassigned, Open-Drain, Digital
// P2.3 - Unassigned, Open-Drain, Digital
// P2.4 - Unassigned, Open-Drain, Digital
// P2.5 - Unassigned, Open-Drain, Digital
// P2.6 - Unassigned, Open-Drain, Digital
// P2.7 - Unassigned, Open-Drain, Digital
// P3.0 - Unassigned, Open-Drain, Digital
// P3.1 - Unassigned, Open-Drain, Digital
// P3.2 - Unassigned, Open-Drain, Digital
// P3.3 - Unassigned, Open-Drain, Digital
// P3.4 - Unassigned, Open-Drain, Digital
// P3.5 - Unassigned, Open-Drain, Digital
// P3.6 - Unassigned, Open-Drain, Digital
// P3.7 - Unassigned, Open-Drain, Digital
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x06;
XBR2 = 0x44;
}
void Oscillator_Init()
{
int data i = 0;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++)
; // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01;
OSCICN = 0x00;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Reset_Sources_Init();
SPI_Init();
EMI_Init();
Port_IO_Init();
Oscillator_Init();
}
//************************
char code FileName[] = "Jnny.txt";
char code FileNameDir[] = "Jnny_CN/Jnny.txt";
char code DirName[] = "Jnny_CN";
char code OldName[] = "Jnny.txt";
char code NewName[] = "kapaku.txt";
char code NewFileName[] = "data.txt";
void main(void)
{
char data i;
unsigned int data lenRead;
char *ptr1, *ptr2,*ptr3;
long data p1, p2;
BYTE data res;
BYTE xdata Buff[256] ={"Jnny_CN is a genius! :-)"}; /* Working buffer */
BYTE xdata BuffRead[256] ={0};
WORD data blen = strlen(Buff),readlen = sizeof(BuffRead);
unsigned int data s2;
DWORD data sect = 0;
#ifdef WITH_TIME
struct tm *tmr = gmtime(0);
#endif
//DWORD data word1,word2;
FILINFO data finfo;
UINT data br, bw; // File R/W count
FATFS *fs,fsmount; /* File system object */
DIR dir; /* Directory object */
FIL file1,file2; /* File object */
Init_Device();
memset(&fs, 0, sizeof(FATFS)); /* Invalidate file system */
for (; ;)
{
if (disk_initialize(1) == 0x0)
{
break ;
}
}
if (disk_ioctl(1, GET_SECTOR_COUNT, &p2) == RES_OK)
{
;
}
if (disk_ioctl(1, GET_SECTOR_SIZE, &p2) == RES_OK)
{
;
}
if (disk_ioctl(1, GET_BLOCK_SIZE, &p2) == RES_OK)
{
;
}
if (disk_ioctl(1, MMC_GET_TYPE, &p1) == RES_OK)
{
;
}
if (f_mount(0, &fsmount) == FR_OK)
{
;
}
if (f_mkfs(0, 1, 32768) == FR_OK)
{
;
}
//Creat a directory
ptr1 = DirName;
if (f_mkdir(ptr1) == FR_OK)
{
;
}
f_mkdir("Hello");
// Create destination file
ptr2 = FileNameDir;
if (f_open(&file1, ptr2, FA_CREATE_ALWAYS | FA_WRITE | FA_READ) == FR_OK)
{
;
}
// Open source file
if (f_open(&file1, ptr2, FA_OPEN_EXISTING | FA_READ | FA_WRITE) == FR_OK)
{
;
}
//write a file
if (f_write(&file1, Buff, blen, &s2) == FR_OK)
{
;
}
//flushe the cached information of a wriiting file
if (f_sync(&file1) == FR_OK)
{
;
}
//moves the file read/write pointer of an open file object
if (f_lseek(&file1, 0) == FR_OK)
{
;
}
//read data from a file
if (f_read(&file1, BuffRead, 2048, &lenRead) == FR_OK)
{
;
}
//copy the data to a newfile
ptr3 = NewFileName;
if (f_open(&file1, ptr3, FA_CREATE_ALWAYS | FA_WRITE | FA_READ) == FR_OK)
{
;
}
if (f_write(&file1, BuffRead, blen, &lenRead) == FR_OK)
{
;
}
if (f_sync(&file1) == FR_OK)
{
;
}
//copy a file
// Open source file
f_open(&file1, ptr2, FA_OPEN_EXISTING | FA_READ);
// Create destination file
f_open(&file2, "kapaku.txt", FA_CREATE_ALWAYS | FA_WRITE);
// Copy source to destination
for (; ;)
{
res = f_read(&file1, Buff, sizeof(Buff), &br);
if (res || br == 0)
{
break;
} // error or eof
res = f_write(&file2, Buff, br, &bw);
if (res || bw < br)
{
break;
} // error or disk full
}
//get number of the free clusters
//if (f_getfree("", &p1, &fs) == FR_OK)
//{
// word1 = (DWORD) (fs->max_clust - 2) * fs->sects_clust / 2;
// word2 = p1 * fs->sects_clust / 2;
//printf("%lu KB total disk space.\n"
// "%lu KB available on the disk.\n",
// (DWORD) (fs->max_clust - 2) * fs->sects_clust / 2,
// clust * fs->sects_clust / 2);
//}
//read directory
if (f_opendir(&dir, DirName) == FR_OK)
{
i = strlen(DirName);
;
}
if (f_readdir(&dir, &finfo) == FR_OK)
{
;
}
while ((f_readdir(&dir, &finfo) == FR_OK) && finfo.fname[0])
{
if (finfo.fattrib & AM_DIR)
{
//sprintf(DirName + i, "/%s", &finfo.fname[0]);
//scanf(DirName);
//*(DirName + i) = '\0';
}
else
{
//printf("%s/%s\n", DirName, &finfo.fname[0]);
}
}
// Close all files
f_close(&file1);
f_close(&file2);
// Unregister a work area before discard it
f_mount(0, NULL);
while (1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -