請問CH376的寫入控制?
请教前辈,小弟在使用CH376这颗U盘控制IC的时候目前遇到一个奇怪的问题就是假设文件内容是空的就可以写入数据,假设文件有数据的话就没办法写入数据,文件扩展名是TXT,以下昰我的程序代码,可否请知道问题的前辈指点,感谢!
主程式
while(1)
{
PORTB = 0X00;
Delay5us(20);
//Test communication interface and work status
UART1_RX_Value_Buffer = 0;
while(UART1_RX_Value_Buffer != 0x00)
{
Delay5us(8);
CHECK_EXIST();
}
PORTBbits.RB0 = 1;
//Setting the USB mode
UART1_RX_Value_Buffer = 0;
while(UART1_RX_Value_Buffer!= 0x51)
{
Delay5us(8);
SET_USB_MODE();
}
PORTBbits.RB1 = 1;
//Initialize the disk and test disk ready
UART1_RX_Value_Buffer = 0;
while(UART1_RX_Value_Buffer !=0x14)
{
Delay5us(8);
DISK_MOUNT();
}
PORTBbits.RB2 = 1;
//Set to operate on the file name
Delay5us(8);
SET_FILE_NAME();
PORTBbits.RB3 = 1;
//Open the file or directory, enumerate files and directories
UART1_RX_Value_Buffer = 0;
while(UART1_RX_Value_Buffer !=0x14)
{
Delay5us(8);
FILE_OPEN();
}
PORTBbits.RB4 = 1;
//Create documents
// while(FILE_CREATE()!=0x14)
// Delay5ms(1);
// PORTBbits.RB4 = 1;
// UART1_RX_Value_Buffer = 0;
//===============================================================
Delay5us(8);
BYTE_LOCATE();
//In bytes of the write data block to the current location
UART1_RX_Value_Buffer = 0;
//while(UART1_RX_Value_Buffer!=0x1E)
//{
Delay5us(8);
BYTE_WRITE();
//}
PORTBbits.RB5 = 1;
//Specified buffer to the internal write request data blocks
UART1_RX_Value_Buffer = 0;
//while(UART1_RX_Value_Buffer!=0x08)
//{
Delay5us(8);
WR_REQ_DATA();
//}
PORTBbits.RB6 = 1;
//-----------------------------------------------------------
Delay5us(8);
UART1_Send_Value(0x41);
UART1_Send_Value(0x41);
UART1_Send_Value(0x42);
UART1_Send_Value(0x42);
UART1_Send_Value(0x46);
UART1_Send_Value(0x0D); // \r
UART1_Send_Value(0x0A); // \n
UART1_Send_Value(0x45);
UART1_Send_Value(0x46);
UART1_Send_Value(0x47);
UART1_Send_Value(0x48);
UART1_Send_Value(0x49);
UART1_Send_Value(0x50);
UART1_Send_Value(0x51);
UART1_Send_Value(0x52);
//Continue byte write
UART1_RX_Value_Buffer = 0;
//while(UART1_RX_Value_Buffer!=0x14)
//{
Delay5us(8);
BYTE_WR_GO();
//}
PORTBbits.RB7 = 1;
//Closes the currently open file or directory
UART1_RX_Value_Buffer = 0;
while(UART1_RX_Value_Buffer!=0xB4)
{
Delay5us(8);
FILE_CLOSE();
}
PORTBbits.RB8 = 1;
Delay5us(100);
}
程式模組
unsigned char CHECK_EXIST()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x06);
UART1_Send_Value(0xFF);
return UART1_RX_Value_Buffer;
}
////Setting the USB mode
unsigned char SET_USB_MODE()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x15);
UART1_Send_Value(0x06);
return UART1_RX_Value_Buffer;
}
//Initialize the disk and test disk ready
unsigned char DISK_MOUNT()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x31);
return UART1_RX_Value_Buffer;
}
//Set to operate on the file name
unsigned char SET_FILE_NAME()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x2F);
char data[] = "TEST.TXT";
UART1_Send_String(data);
//TEST.TXT
//UART1_Send_Value(0x54);
//UART1_Send_Value(0x45);
//UART1_Send_Value(0x53);
//UART1_Send_Value(0x54);
//UART1_Send_Value(0x2E);
//UART1_Send_Value(0x54);
//UART1_Send_Value(0x58);
//UART1_Send_Value(0x54);
return UART1_RX_Value_Buffer;
}
//Create documents
unsigned char FILE_CREATE()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x34);
return UART1_RX_Value_Buffer;
}
unsigned char BYTE_WRITE()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x3C);
UART1_Send_Value(0x0F);
UART1_Send_Value(0x0F>>8);
return UART1_RX_Value_Buffer;
}
unsigned char BYTE_READ()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x3A);
return UART1_RX_Value_Buffer;
}
unsigned char WR_REQ_DATA()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x2D);
return UART1_RX_Value_Buffer;
}
unsigned char BYTE_WR_GO()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x3D);
return UART1_RX_Value_Buffer;
}
unsigned char FILE_CLOSE()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x36);
UART1_Send_Value(0x01);
return UART1_RX_Value_Buffer;
}
unsigned char FILE_OPEN()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x32);
return UART1_RX_Value_Buffer;
}
unsigned char BYTE_LOCATE()
{
UART1_Send_Value(0x57);
UART1_Send_Value(0xAB);
UART1_Send_Value(0x39);
UART1_Send_Value(0xFF);
UART1_Send_Value(0xFF);
UART1_Send_Value(0xFF);
UART1_Send_Value(0xFF);
}