程序從a.txt中讀取一串?dāng)?shù)據(jù),假如是 Hello,the world!將讀的數(shù)據(jù)存放到DATA_BUF中,再將DATA_BUF中的數(shù)據(jù)寫入創(chuàng)建的文件NEWFILE.txt中,在電腦上打開此文件,看到的卻是亂碼。我始終弄不明白這是為什么,請(qǐng)各位各位哥哥姐姐們幫幫忙?。?/p>
我的程序如下: #include #include #include #include
#define MAX_PATH_LEN 32 #include "CH375HM.H"
/* 電路連接方式 單片機(jī) 模塊 P0 = D0-D7 RD = RD# WR = WR# P26 = CS# P20 = A0 P15 = STA# INT0 = INT# */ #define CH375HM_INDEX XBYTE[0xBCF0]/* CH375模塊的索引端口的I/O地址 */ #define CH375HM_DATA XBYTE[0xBDF1]/* CH375模塊的數(shù)據(jù)端口的I/O地址 */ #define CH375HM_INT_WIRE INT0 /* CH375模塊的INT#引腳連接到單片機(jī)的INT0引腳 */ sbit P15 = P1^5; #define CH375HM_STA P15 /* CH375模塊的STA#引腳連接到單片機(jī)的P15引腳 */
/* 文件數(shù)據(jù)緩沖區(qū): ExtRAM: 0000H-7FFFH */ unsigned char xdata DATA_BUF[ 512 * 64 ] _at_ 0x0000;
CMD_PARAM mCmdParam; unsigned char mIntStatus; /* CH375模塊的中斷狀態(tài)或者操作完成狀態(tài) */
sbit LED_OUT0 = P1^0; sbit LED_OUT1 = P1^1;
sbit KEY = P1^3;
#define CH375HM_INDEX_WR( Index ) { CH375HM_INDEX = (Index); } /* 寫索引地址 */ #define CH375HM_DATA_WR( Data ) { CH375HM_DATA = (Data); } /* 寫數(shù)據(jù) */ #define CH375HM_DATA_RD( ) ( CH375HM_DATA ) /* 讀數(shù)據(jù) */
void mDelaymS( unsigned char delay ) { unsigned char i, j, c; for ( i = delay; i != 0; i -- ) { for ( j = 200; j != 0; j -- ) c += 3; for ( j = 200; j != 0; j -- ) c += 3; } }
void mPressKey() { while ( 1 == KEY ) { LED_OUT0 = 0; LED_OUT1 = 1; mDelaymS(500); LED_OUT0 = 1; LED_OUT1 = 0; mDelaymS(500); } mDelaymS(200); KEY = 1; mDelaymS(200); LED_OUT0 = 0; mDelaymS(200); LED_OUT1 = 1; }
/* 執(zhí)行命令 */ unsigned char ExecCommandBuf( unsigned char cmd, unsigned char len, unsigned ar xdata *bufstart ) { unsigned char i, status; #define DataCount status unsigned char data *buf; unsigned char xdata *CurrentBuf; CH375HM_INDEX_WR( 0 ); /* 索引地址置0 */ CH375HM_DATA_WR( cmd ); /* 向索引地址0寫入命令碼 */ CH375HM_DATA_WR( len ); /* 向索引地址1寫入后續(xù)參數(shù)的長(zhǎng)度 */ if ( len ) /* 有參數(shù) */ { i = len; buf = (unsigned char *)&mCmdParam; /* 指向輸入?yún)?shù)的起始地址 */ do { CH375HM_DATA_WR( *buf ); /* 從索引地址2開始,寫入?yún)?shù) */ buf ++; } while ( -- i ); } CH375HM_STA = 0; /* 產(chǎn)生下降沿通知模塊,說明命令包已經(jīng)寫入,請(qǐng)求開始執(zhí)行命令 */ CurrentBuf = bufstart; while ( 1 ) /* 處理數(shù)據(jù)傳輸,直到操作完成才退出 */ { while ( CH375HM_INT_WIRE ); CH375HM_INDEX_WR( 63 ); status = CH375HM_DATA_RD( ); CH375HM_STA = 1; /* 中斷應(yīng)答,取消來自模塊的中斷請(qǐng)求 */ if ( status == ERR_SUCCESS ) /* 操作成功 */ { CH375HM_INDEX_WR( 1 ); DataCount = CH375HM_DATA_RD( ); /* 從索引地址1讀取返回結(jié)果數(shù)據(jù)的長(zhǎng)度,計(jì)數(shù) */ if ( DataCount ) /* 有結(jié)果數(shù)據(jù) */ { buf = (unsigned char *)&mCmdParam; /* 指向輸出參數(shù)的起始地址 */ i = 2; do { CH375HM_INDEX_WR( i ); i ++; *buf = CH375HM_DATA_RD( ); /* 從索引地址2開始,讀取結(jié)果 */ buf ++; } while ( -- DataCount ); } break; /* 操作成功返回 */ } else if ( status == USB_INT_DISK_READ ) { /* 正在從U盤讀數(shù)據(jù)塊,請(qǐng)求數(shù)據(jù)讀出 */ DataCount = 64; /* 計(jì)數(shù) */ i = 0; do { CH375HM_INDEX_WR( i ); i ++; *CurrentBuf = CH375HM_DATA_RD( ); /* 從索引地址0到63依次讀出64字節(jié)的數(shù)據(jù) */ CurrentBuf ++; /* 讀取的數(shù)據(jù)保存到外部緩沖區(qū) */ } while ( -- DataCount ); CH375HM_STA = 0; /* 產(chǎn)生下降沿通知模塊繼續(xù),說明64字節(jié)數(shù)據(jù)已經(jīng)讀取完成 */ } else if ( status == USB_INT_DISK_WRITE ) /* 正在向U盤寫數(shù)據(jù)塊,請(qǐng)求數(shù)據(jù)寫入 */ { CH375HM_INDEX_WR( 0 ); i = 64; do { CH375HM_DATA_WR( *CurrentBuf ); /* 向索引地址0到63依次寫入64字節(jié)的數(shù)據(jù) */ CurrentBuf ++; /* 寫入的數(shù)據(jù)來自外部緩沖區(qū) */ } while ( -- i ); CH375HM_STA = 0; /* 產(chǎn)生下降沿通知模塊繼續(xù),說明64字節(jié)數(shù)據(jù)已經(jīng)寫入完成 */ } else if ( status == USB_INT_DISK_RETRY ) /* 讀寫數(shù)據(jù)塊失敗重試,應(yīng)該向回修改緩沖區(qū)指針 */ { CH375HM_INDEX_WR( 0 ); i = CH375HM_DATA_RD( ); CH375HM_INDEX_WR( 1 ); DataCount = CH375HM_DATA_RD( ); CurrentBuf -= ( (unsigned short)i << 8 ) + DataCount; CH375HM_STA = 0; /* 產(chǎn)生下降沿通知模塊繼續(xù),說明重試狀態(tài)碼已經(jīng)處理完成 */ } else { /* 操作失敗 */ if ( status == ERR_DISK_DISCON || status == ERR_USB_CONNECT ) mDelaymS( 100 ); /* U盤剛剛連接或者斷開,應(yīng)該延時(shí)幾十毫秒再操作 */ break; /* 操作失敗返回 */ } }
return( status ); }
/* 執(zhí)行命令 */ unsigned char ExecCommand( unsigned char cmd, unsigned char len ) { return( ExecCommandBuf( cmd, len, 0 ) ); }
void mStopIfError( unsigned char iError ) { unsigned char led; if ( iError == ERR_SUCCESS ) return; /* 操作成功 */ led=0; while ( 1 ) { LED_OUT0 = led&1; /* LED閃爍 */ mDelaymS( 1000 ); led^=1; } }
/* 為printf和getkey輸入輸出初始化串口 */ void mInitSTDIO( ) { SCON = 0x50; //串口控制寄存器串口模式為1,8位UART,允許接收 PCON = 0x00; //電源控制寄存器 SMOD1=0 波特率為振蕩器的/64 TMOD = 0x20; //定時(shí)器的模式控制位 使用定時(shí)器1定時(shí),采用8位自載定時(shí)器, TH1 = 0xFD; TL1 = 0xFD; //11.0529MHz晶振, 9600 bps TR1 = 1; //定時(shí)器運(yùn)行 TI = 1; //發(fā)送中斷標(biāo)志位
mDelaymS(200); LED_OUT0 = 1; mDelaymS(200); LED_OUT1 = 1; mDelaymS(200); KEY = 1; mDelaymS(200); }
void main( ) { unsigned char i, c, SecCount; unsigned long OldSize; unsigned short NewSize; LED_OUT0 = 0; /* 開機(jī)后LED亮一下以示工作 */ mDelaymS( 1000 ); LED_OUT0 = 1; mInitSTDIO( );
while ( 1 ) /* 主循環(huán) */ { /* 使用查詢方式看U盤是否連接 */ while ( 1 ) { i = ExecCommand( CMD_QueryStatus, 0 ); mStopIfError( i ); if ( mCmdParam.Status.mDiskStatus >= DISK_CONNECT ) break; /* U盤已經(jīng)連接 */ mDelaymS( 1000 ); } mDelaymS( 1000 ); LED_OUT0 = 0; /* LED亮 */ while ( ExecCommand( CMD_DiskReady, 0 ) != ERR_SUCCESS ) { mDelaymS( 1000 ); /* 查詢磁盤是否準(zhǔn)備好 */ }
mPressKey(); mDelaymS(5000);
/* 讀取原文件 */ memcpy( mCmdParam.Open.mPathName, "\\A.TXT", MAX_PATH_LEN ); i = ExecCommand( CMD_FileOpen, MAX_PATH_LEN ); if ( i == ERR_MISS_DIR || i == ERR_MISS_FILE ) { for ( c = 0; c < 255; c ++ ) { /* 最多搜索前255個(gè)文件 */ memcpy( mCmdParam.Enumer.mPathName, "\\*", MAX_PATH_LEN ); /* 搜索文件名,*為通配符,適用于所有文件或者子目錄 */ for ( i = 0; i < MAX_PATH_LEN - 1; i ++ ) if ( mCmdParam.Enumer.mPathName[ i ] == 0 ) break; /* 指向搜索文件名的結(jié)束符 */ mCmdParam.Enumer.mPathName[ i ] = c; /* 將結(jié)束符替換為搜索的序號(hào),從0到255 */ i = ExecCommand( CMD_FileEnumer, i+1 ); /* 枚舉文件,如果文件名中含有通配符*,則為搜索文件而不打開,輸入?yún)?shù)的長(zhǎng)度很好計(jì)算 */ if ( i == ERR_MISS_FILE ) break; /* 再也搜索不到匹配的文件,已經(jīng)沒有匹配的文件名 */ if ( i == ERR_SUCCESS ) { /* 搜索到與通配符相匹配的文件名,文件名及其完整路徑在命令緩沖區(qū)中 */ continue; /* 繼續(xù)搜索下一個(gè)匹配的文件名,下次搜索時(shí)序號(hào)會(huì)加1 */ } else { /* 出錯(cuò) */ mStopIfError( i ); break; } } strcpy( DATA_BUF, "Note: \xd\n沒找到文件!\xd\n" ); OldSize = 0; NewSize = strlen( DATA_BUF ); SecCount = ( NewSize + 511 ) >> 9; } else { /* 找到文件或者出錯(cuò) */ mStopIfError( i ); i = ExecCommand( CMD_FileQuery, 0 ); /* 查詢當(dāng)前文件的信息,沒有輸入?yún)?shù) */ mStopIfError( i ); OldSize = mCmdParam.Modify.mFileSize; if ( OldSize > (unsigned long)(64*512) ) { SecCount = 64; NewSize = 64*512; } else { SecCount = ( OldSize + 511 ) >> 9; NewSize = (unsigned short)OldSize; } mCmdParam.Read.mSectorCount = SecCount; /* 讀取全部數(shù)據(jù) */ i = ExecCo