我在使用CH375做設(shè)備方式的時(shí)候參照CH372的資料,根據(jù)光盤資料中"CH372\CHECK\PUB\MCU_IF\C"目錄下的CH372FRM.C給的框架寫了一個(gè)簡單的程序,想看看CH375與PC機(jī)通信是否建立,并且傳輸一些數(shù)據(jù)看看是否能正確傳輸。但是程序執(zhí)行到:CH375_WR_CMD_PORT( CMD_GET_STATUS );IntStatus = CH375_RD_DAT_PORT( );的時(shí)候,IntStatus中的數(shù)據(jù)始終是F0,并沒有正確獲得中斷狀態(tài)。并且根據(jù)PC端的打印結(jié)果來看,數(shù)據(jù)也沒有完成上傳與下傳,不知道究竟是什么原因。請高手指點(diǎn)一下,感激不盡!
這是寫的簡單的PC機(jī)收發(fā)一組數(shù)據(jù)的程序:
#include #include #include #include #include #include "CH375DLL.H" void main ( ) { unsigned char mBuffer[4100]; unsigned char mReadBuf[100]; unsigned long i, mLength, mErrCnt, mTotal; printf( "*** CH375OpenDevice: mode 0 \n" ); if ( CH375OpenDevice( 0 ) == INVALID_HANDLE_VALUE ) return; CH375SetTimeout( 0, 2000, 2000 ); mErrCnt=0; mTotal=64; for(i=1;i<64;i++)mBuffer[i]=rand(); if ( CH375WriteData( 0, &mBuffer, &mTotal ) ) { printf("CH375WriteData length = %ld\n",mTotal); for(i=1;i<6;i++) printf("download first 5 data = %02XH\n",mBuffer[i]); } else { mErrCnt++; printf( "CH375ReadData return error\n"); } if ( CH375ReadData( 0, &mReadBuf, &mLength ) ) { printf("read data length = %ld",mLength); for(i=0;i printf("read data = %02XH\n",mReadBuf[i]); if ( mLength != mTotal || mLength==0 ) { mErrCnt++; printf( "return length error: %ld (%ld)\n", mLength, mTotal ); } else { for(i=1;i<6;i++) printf("upload first 5 data = %ld\n",mReadBuf[i]); } } else { mErrCnt++; printf( "CH375WriteData return error, length=%ld\n",mTotal ); } if ( mErrCnt==0 ) printf( "pass\n" ); Sleep(100); printf( "Total error = %ld \n", mErrCnt ); printf( "*** CH375CloseDevice: 0 \n" ); CH375CloseDevice( 0 ); printf( "\nExit.\n" ); getch(); }
這個(gè)是單片機(jī)程序
#include "reg52.H" #include "CH375INC.H" #ifdef __CX51__ #ifndef __C51__ #define __C51__ 1 #pragma NOAREGS #endif #endif typedef unsigned char UINT8; typedef unsigned short UINT16; typedef unsigned long UINT32; #ifdef __C51__ typedef unsigned char idata *PUINT8; typedef unsigned char volatile xdata IOPORT; #else typedef unsigned char *PUINT8; typedef unsigned char volatile IOPORT; #endif #define DELAY_START_VALUE 1
void Delay1us( ) { #if DELAY_START_VALUE != 0 UINT8 i; for ( i=DELAY_START_VALUE; i!=0; i-- ); #endif }
void Delay2us( ) { UINT8 i; for ( i=DELAY_START_VALUE*2+1; i!=0; i-- ); }
void CH375_WR_CMD_PORT( UINT8 cmd ) { CH375_CMD_PORT=cmd; Delay2us(); }
void CH375_WR_DAT_PORT( UINT8 dat ) { CH375_DAT_PORT=dat; Delay1us(); }
UINT8 CH375_RD_DAT_PORT( void ) { Delay1us(); return( CH375_DAT_PORT ); }
void CH375_Init( void ) { UINT8 i; #ifdef MY_USB_VENDOR_ID #ifdef MY_USB_PRODUCT_ID CH375_WR_CMD_PORT( CMD_SET_USB_ID ); CH375_WR_DAT_PORT( (UINT8)MY_USB_VENDOR_ID ); CH375_WR_DAT_PORT( (UINT8)(MY_USB_VENDOR_ID>>8) ); CH375_WR_DAT_PORT( (UINT8)MY_USB_PRODUCT_ID ); CH375_WR_DAT_PORT( (UINT8)(MY_USB_PRODUCT_ID>>8) ); #endif #endif
CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); CH375_WR_DAT_PORT( 2 ); for ( i=100; i!=0; i-- ) if ( CH375_RD_DAT_PORT( ) == CMD_RET_SUCCESS ) break;
#ifdef __C51__ IT0 = 0; IE0 = 0; EX0 = 1; #endif }
UINT8 UsbLength; UINT8 UsbBuffer[ CH375_MAX_DATA_LEN ];
void mCH375Interrupt( void ) interrupt 0 using 1 { UINT32 IntStatus = 0; UINT8 cnt; PUINT8 buf; CH375_WR_CMD_PORT( CMD_GET_STATUS ); IntStatus = CH375_RD_DAT_PORT( ); if ( IntStatus == USB_INT_EP2_OUT ) { CH375_WR_CMD_PORT( CMD_RD_USB_DATA ); UsbLength = cnt = CH375_RD_DAT_PORT( ); if ( cnt ) { buf = UsbBuffer; do { *buf = CH375_RD_DAT_PORT( ); buf ++; } while ( -- cnt ); } else return; CH375_WR_CMD_PORT( CMD_WR_USB_DATA7 ); cnt = UsbLength; CH375_WR_DAT_PORT( cnt ); if ( cnt ) { buf = UsbBuffer; do { CH375_WR_DAT_PORT( *buf ); buf ++; } while ( -- cnt ); } }
else if ( IntStatus == USB_INT_EP2_IN ) { CH375_WR_CMD_PORT( CMD_UNLOCK_USB ); } } main( void ) { CH375_Init( ); EA = 1; while ( 1 ) ; }
兩個(gè)程序都被你改過了,不巧的是都改出問題了。 你還是先用TEST目錄下的試吧,針對MCS51的,更簡單