#include #include #define LIB_CFG_DISK_IO 1 /* 磁盤讀寫(xiě)的數(shù)據(jù)的復(fù)制方式,1為"單DPTR復(fù)制",2為"雙DPTR復(fù)制",3為"單DPTR和P2+R0復(fù)制" */ #define LIB_CFG_FILE_IO 1 /* 文件讀寫(xiě)的數(shù)據(jù)的復(fù)制方式,0為"外部子程序",1為"單DPTR復(fù)制",2為"雙DPTR復(fù)制",3為"單DPTR和P2+R0復(fù)制" */ #define LIB_CFG_INT_EN 0 /* CH375的INT#引腳連接方式,0為"查詢方式",1為"中斷方式" */ #define CH375_CMD_PORT_ADDR 0xBDF1 /* CH375命令端口的I/O地址 */ #define CH375_DAT_PORT_ADDR 0xBCF0 /* CH375數(shù)據(jù)端口的I/O地址 #define MAX_BUFF_SIZE 250 #define COMM_NO_ERROR 0 #define COMM_RX_FULL 1
#include "CH375HF6.H" UINT8 i,c; UINT8 *err; UINT8X CommBuffer[MAX_BUFF_SIZE]; //串口環(huán)形緩沖區(qū) UINT8 ReceiveCharcount; //接收到的字符數(shù) UINT8 StartFlag; //接收開(kāi)始標(biāo)志 UINT8 ReceiveFlag; //接收到字符標(biāo)志 UINT8X *pBufferIn=&CommBuffer[0]; //寫(xiě)入指針 UINT8X *pBufferOut=&CommBuffer[0]; //讀出指針 UINT16 OverTimeCount=0 ;
/* 延時(shí)100毫秒,不精確 */ void mDelay100mS( ) { UINT8 i, j, c; for ( i = 200; i != 0; i -- ) for ( j = 200; j != 0; j -- ) c+=3; }
/* 將程序空間的字符串復(fù)制到內(nèi)部RAM中,返回字符串長(zhǎng)度 */ UINT8 mCopyCodeStringToIRAM( UINT8 idata *iDestination, UINT8 code *iSource ) { UINT8 i = 0; while ( *iDestination = *iSource ) { iDestination ++; iSource ++; i ++; } return( i ); }
/* 檢查操作狀態(tài),如果錯(cuò)誤則顯示錯(cuò)誤代碼并停機(jī) */ void mStopIfError( UINT8 iError ) { if ( iError == ERR_SUCCESS ) return; /* 操作成功 */ printf( "Error: %02X\n", (UINT16)iError ); /* 顯示錯(cuò)誤 */ while ( 1 ) { LED_OUT_ACT( ); /* LED閃爍 */ mDelay100mS( ); LED_OUT_INACT( ); mDelay100mS( ); } }
/* 為printf和getkey輸入輸出初始化串口 */ void mInitSTDIO( ) { SCON = 0x50; PCON&= 0x7f; TMOD = 0x20; TH1 = 0xf3; /* 24MHz晶振, 4800bps */ TR1 = 1; TI = 1; EA=1; ES=1; ReceiveCharcount=0; pBufferIn=&CommBuffer[0]; //寫(xiě)入指針 pBufferOut=&CommBuffer[0]; //讀出指針 StartFlag=0; }
void RI_Interrupt() interrupt 4 using 1 { if(RI) { c=SBUF; RI=0; StartFlag=1; ReceiveFlag=1; OverTimeCount=0; if(ReceiveCharcount { ReceiveCharcount++; //串口緩存區(qū)里的字符數(shù) *pBufferIn=c; pBufferIn++; if(pBufferIn==&CommBuffer[MAX_BUFF_SIZE]) { pBufferIn=&CommBuffer[0];} *err=COMM_NO_ERROR; } else{ *err=COMM_RX_FULL; } } }
void main( ) { UINT8 count=0; mInitSTDIO( ); /* 為了讓計(jì)算機(jī)通過(guò)串口監(jiān)控演示過(guò)程 */ printf( "Start\n" ); i = CH375LibInit( ); /* 初始化CH375程序庫(kù)和CH375芯片,操作成功返回0 */ mStopIfError( i ); while ( 1 ) { printf( "Wait Udisk\n" ); while ( CH375DiskStatus != DISK_CONNECT ) xQueryInterrupt( ); for ( i = 0; i < 5; i ++ ) { mDelay100mS( ); printf( "Ready ?\n" ); if ( CH375DiskReady( ) == ERR_SUCCESS ) break; } #ifdef EN_DISK_WRITE LED_WR_ACT( ); printf( "Create\n" ); mCopyCodeStringToIRAM( mCmdParam.Create.mPathName, "/new.TXT" ); i = CH375FileCreate( ); mStopIfError( i ); printf( "Write\n" ); while(1) { while(StartFlag) /*發(fā)生第一次接收中斷*/ { OverTimeCount++; /*主循環(huán)接收超時(shí)計(jì)數(shù)*/ if(ReceiveFlag==1) { ReceiveFlag=0; mCmdParam.ByteWrite.mByteBuffer[count]=*pBufferOut++; if(pBufferOut==&CommBuffer[MAX_BUFF_SIZE]) {pBufferOut=&CommBuffer[0];} ReceiveCharcount--; //串口緩存中的總數(shù)減一 if(count==MAX_BYTE_IO) { count=0; mCmdParam.ByteWrite.mByteCount = MAX_BYTE_IO; i = CH375ByteWrite( ); mStopIfError( i ); } else{count++;} /*寫(xiě)緩沖區(qū)計(jì)數(shù)*/ } if(OverTimeCount>50000)//超時(shí)處理,如果循環(huán)50000次還沒(méi)有中斷則認(rèn)為結(jié)束 { mCmdParam.ByteWrite.mByteCount = count; i = CH375ByteWrite( ); mStopIfError( i ); mCmdParam.Close.mUpdateLen = 1; printf( "Close\n" ); i = CH375FileClose( ); mStopIfError( i ); OverTimeCount=0; StartFlag=0; ReceiveCharcount=0; pBufferOut=&CommBuffer[0]; pBufferIn=&CommBuffer[0]; printf( "Take out\n" ); while ( CH375DiskStatus != DISK_DISCONNECT ) xQueryInterrupt( ); mDelay100mS( ); mDelay100mS( ); } } } } } #endif
我的程序用在寫(xiě)到U盤后,有亂碼和丟失數(shù)據(jù),各位大俠,給我出出主意,怎么能處理好串口接收和寫(xiě)U盤之間的時(shí)序問(wèn)題,很苦惱,怎么改更好一點(diǎn).(字節(jié)模式讀寫(xiě)).我用的單片機(jī)是STC89C51RD+ ,1280RAM,