我的系統(tǒng)想做一個(gè)AD轉(zhuǎn)換,把數(shù)據(jù)傳到上位機(jī)去,不知道怎么回事,上面不能得到數(shù)據(jù),驅(qū)動(dòng)已經(jīng)裝上了,設(shè)備也打開了,可是就是得不到數(shù)據(jù),希望能給我指點(diǎn)一二。 下面是固件程序的核心代碼。 void CH375_Init( ) { unsigned char i; CH375_WR_CMD_PORT( CMD_CHECK_EXIST ); /* 測試CH375是否正常工作 */ CH375_WR_DAT_PORT( 0x55 ); /* 寫入測試數(shù)據(jù) */ i = ~ 0x55; /* 返回?cái)?shù)據(jù)應(yīng)該是測試數(shù)據(jù)取反 */ if ( CH375_RD_DAT_PORT( ) != i ) { /* CH375不正常 */ for ( i=1000; i!=0; i-- ) { CH375_WR_CMD_PORT( CMD_RESET_ALL ); /* 多次重復(fù)發(fā)命令,執(zhí)行硬件復(fù)位 */ CH375_RD_DAT_PORT( ); } CH375_WR_CMD_PORT( 0 ); Delay50ms( ); } /* 設(shè)置USB工作模式, 必要操作 */ CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); CH375_WR_DAT_PORT( 2 ); /* 設(shè)置為使用內(nèi)置固件的USB設(shè)備方式 */ for ( i=100; i!=0; i-- ) { /* 等待操作成功,通常需要等待10uS-20uS */ if ( CH375_RD_DAT_PORT( ) == CMD_RET_SUCCESS ) break; } }
//--------------------------------------- //AD轉(zhuǎn)換程序的相應(yīng)函數(shù) void GET_AD_RESULT(void) { UINT8 *p; UINT8 n; UINT8 temp; n=0; if(F0==0)p=(UINT8 *)&PACK.up.mBuffer0; else p=(UINT8 *)&PACK.up.mBuffer1; while(1) { while(ADC_CONTR&0x10)break; ADC_CONTR|=0X08; if(n<=MAX_DATA_SIZE) { temp= ADC_DATA>>6;
*(p+n)=ADC_DATA<<2|ADC_LOW2; n++; *(p+n)=temp; n++; } else { F0=~F0; break; } } } //--------------------------------------- void set_p1_adc_channel(void) { P1=0xff; ADC_CONTR=ADC_CONTR|0X80; P1M0=0X01; P1M1=0X01; delay2us(); } //--------------------------------------- void Senddata(void) { UINT8 cnt; UINT8* buf; CH375_WR_CMD_PORT( CMD_WR_USB_DATA7 ); /* 向USB端點(diǎn)2的發(fā)送緩沖區(qū)寫入數(shù)據(jù)塊 */ cnt=MAX_DATA_SIZE; CH375_WR_DAT_PORT( cnt ); /* 首先寫入后續(xù)數(shù)據(jù)長度 */ if(F0==1) buf=(UINT8*)&PACK.up.mBuffer0; /* 指向狀態(tài)包緩沖區(qū) */ else buf=(UINT8*)&PACK.up.mBuffer1; do { CH375_WR_DAT_PORT( *buf ); /* 寫入數(shù)據(jù)到CH372 */ buf ++; } while ( -- cnt ); } //--------------------------------------- main(void) { UINT8 int_state; CH375_Init( ); set_p1_adc_channel(); ADC_CONTR|=0XE0; F0=0; ADC_DATA=0; ADC_LOW2=0; ADC_CONTR|=0X08; GET_AD_RESULT(); while( 1) { delay2us(); Senddata(); ADC_CONTR|=0x0E8; GET_AD_RESULT(); while(INT1); INT1=1; CH375_WR_CMD_PORT(CMD_GET_STATUS); int_state=CH375_RD_DAT_PORT(); switch(int_state) {case USB_INT_EP2_IN: { CH375_WR_CMD_PORT( CMD_UNLOCK_USB ); break; } default: { CH375_WR_CMD_PORT( CMD_UNLOCK_USB ); break; } } } } 上位機(jī)的簡單接收程序,根據(jù)TEST修改的。 void main ( ) { int key; unsigned char mBuffer[100]={0},m; unsigned char mReadBuf[100]; unsigned long i, mLength=61, mTestCount, mErrCnt, mStep, mTotal;
printf( "\nCH372/CH375 Bulk Data Test Program V1.1 , Copyright (C) W.ch 2004.12\n" ); printf( "test data correctness \n" );
// 需要使用DLL則需要先加載,沒有此句則會(huì)自動(dòng)加載 printf( "*** Load DLL: CH375DLL.DLL \n" ); if ( LoadLibrary( "CH375DLL.DLL" ) == NULL ) return; // 加載DLL失敗,可能未安裝到系統(tǒng)中
printf( "*** CH375OpenDevice: 0# \n" ); if ( CH375OpenDevice( 0 ) == INVALID_HANDLE_VALUE ) return; /* 使用之前必須打開設(shè)備 */
CH375SetTimeout( 0, 5000, 5000 ); // 設(shè)置USB數(shù)據(jù)讀寫的超時(shí),超過5000mS未完成讀寫將強(qiáng)制返回,避免一直等待下去
printf( "*** Step-1: test data correctness: 10000000 times, random length and data\n" ); for ( mTestCount=0; mTestCount<10000000; ++mTestCount ) // 循環(huán)測試 { if ( kbhit() ) { key = getch( ); if ( key == 0x1b || key == 0x20 ) { // ESC鍵或者空格鍵終止循環(huán) printf( "*** CH375CloseDevice by break: 0 \n" ); CH375CloseDevice( 0 ); exit(2); break; } }
if ( CH375ReadData( 0, mReadBuf, &mLength ) ) // 接收成功 { if(mLength==0) printf("數(shù)據(jù)讀失敗\n"); else { for ( i=0; i printf( " %02XH", mReadBuf[i] ); } } } else printf("不能讀數(shù)據(jù)\n"); } if ( mErrCnt==0 ) printf( "*** passed \n" ); // 關(guān)閉CH375設(shè)備,如果不繼續(xù)使用則必須關(guān)閉設(shè)備,就象讀寫完硬盤中的數(shù)據(jù)文件后要關(guān)閉一樣 printf( "*** Total error = %ld \n", mErrCnt ); printf( "*** CH375CloseDevice: 0 \n" ); CH375CloseDevice( 0 ); printf( "\nExit.\n" ); getch(); }