用的是PIC18F单片机,可建立TXT文件,放在电脑中也可以打开,但是写不进去数据,CH376_WR_CMD_PORT( CMD_BYTE_WRITE );返回值为0x14,而非0x1E ,定义外部中断中由全局变量sta获取状态值,如下:
void interrupt CH376HMInterrupt ()
{
if(CCP3IF==1)
{
CH376_WR_CMD_PORT(CMD_GET_STATUS);
delay_2us();
sta=CH376_RD_DAT_PORT( );
CCP3IF=0;
}
} 不知道这样写对不对? 还有个问题:xEndCH376Cmd( );这个意思是CS=1吗?这个,程序中我没加,我是在结束后就令CS=1;
unsigned char Set_Mode(unsigned char mode)
{
unsigned char temp;
CH376_WR_CMD_PORT(CMD_SET_USB_MODE);
CH376HM_DATA_WR(mode);
temp=CH376_RD_DAT_PORT( );
return temp;
}
voidCH376SetFileName( unsigned char *name ) /* 设置将要操作的文件的文件名 */
{
UINT8c;
CH376_WR_CMD_PORT( CMD10_SET_FILE_NAME );
c = *name;
CH376HM_DATA_WR( c );
while ( c ) {
name ++;
c = *name;
if ( c == DEF_SEPAR_CHAR1 || c == DEF_SEPAR_CHAR2 ) c = 0; /* 强行将文件名截止 */
CH376HM_DATA_WR( c );
}
}
void CH376FileOpen( unsigned char *name ) /* 在根目录或者当前目录下新建文件,如果文件已经存在那么先删除 */
{
if ( name ) CH376SetFileName( name ); /* 设置将要操作的文件的文件名 */
CH376_WR_CMD_PORT( CMD_FILE_OPEN );
}
void CH376FileCreate( unsigned char *name ) /* 在根目录或者当前目录下新建文件,如果文件已经存在那么先删除 */
{
if ( name ) CH376SetFileName( name ); /* 设置将要操作的文件的文件名 */
CH376_WR_CMD_PORT( CMD0H_FILE_CREATE );
}
unsigned char CH376WriteReqBlock( unsigned char *buf ) /* 向内部指定缓冲区写入请求的数据块,返回长度 */
{
UINT8s, l;
CH376_WR_CMD_PORT( CMD_WR_REQ_DATA );
s = l = CH376_RD_DAT_PORT( ); /* 长度 */
if ( l ) {
do {
CH376HM_DATA_WR( *buf );
buf ++;
} while ( -- l );
}
//xEndCH376Cmd( );
return( s );
}
unsigned charCH376ByteWrite( unsigned char *buf, unsigned short ReqCount, unsigned short *RealCount ) /* 以字节为单位向当前位置写入数据块 */
{
unsigned char s=0;
CH376_WR_CMD_PORT( CMD_BYTE_WRITE );
CH376HM_DATA_WR( (unsigned char)ReqCount );
CH376HM_DATA_WR( (unsigned char)(ReqCount>>8) );
//xEndCH376Cmd( );
if ( RealCount ) *RealCount = 0;
while ( 1 ) { //此处的sta是获取反应的中断状态
s=sta; //在中断中获取状态赋予sta
if ( s== USB_INT_DISK_WRITE) {
s = CH376WriteReqBlock( buf ); /* 向内部指定缓冲区写入请求的数据块,返回长度 */
CH376_WR_CMD_PORT( CMD_BYTE_WR_GO );
//xEndCH376Cmd( );
buf += s;
if ( RealCount ) *RealCount += s;
}
/*else if ( s == USB_INT_SUCCESS ) return( s );*/ /* 结束 */
else return( s ); /* 错误 */
}
}
void main() {
unsigned char buff[]={0x12,0x14,0x15},s=0;
init_ccp3();
init_IO();
mDelay100mS( ); /* 延时100毫秒,CH376模块上电后需要100毫秒左右的复位时间 */
mDelay100mS( );
mDelay100mS( );
mDelay100mS( );
CH376_WR_CMD_PORT(CMD_RESET_ALL);
mDelay100mS( );
mDelay100mS( );
mDelay100mS( );
mDelay100mS( );
mDelay100mS( );
CH376HM_INT_EN=1; //开中断
Set_Mode(0x06); //设置模式
while(sta!=USB_INT_CONNECT);
CH376_WR_CMD_PORT(CMD_DISK_INIT); //初始化
while(sta!=0x14);
delay_2us();
delay_2us();
CH376FileCreate("/MY001.TXT\0");
s=CH376ByteWrite( buff, 3, NULL ) ;
CH376_WR_CMD_PORT(CMD1H_FILE_CLOSE); //CMD1H_FILE_CLOSE 0x36
delay_2us();
delay_2us();
while(1)
{
}
}