還是不行!返回仍然是FFH,下面是我的源程序,參考的是CH375HST上的主機(jī)程序,加了顯示和發(fā)送程序,應(yīng)該沒(méi)什么錯(cuò)啊,幫我看看吧,你來(lái)蘇州我請(qǐng)你吃飯?。?!我要答辯了啊 #include unsigned char volatile data CH375_CMD_PORT =0xBDF1; unsigned char volatile data CH375_DAT_PORT =0xBCF0; sbit CH375_INT_WIRE =0xB0^2;
#include #include #include”ch375.INC”
#define CH375_MAX_DATA_LEN 0x40 /* 最大數(shù)據(jù)包的長(zhǎng)度, 內(nèi)部緩沖區(qū)的長(zhǎng)度 */
sbit wr=P3^6; sbit en=P2^2;
void delay2us( ) { unsigned char i; for ( i = 2; i != 0; i -- ); }
void delay1us( ) { unsigned char i; for ( i = 1; i != 0; i -- ); }
void ERROR() { while(1); }
void CH375_WR_CMD_PORT( unsigned char cmd ) { delay2us(); CH375_CMD_PORT=cmd; delay2us(); }
void CH375_WR_DAT_PORT( unsigned char dat ) { CH375_DAT_PORT=dat; delay1us(); }
unsigned char CH375_RD_DAT_PORT() { delay1us(); return( CH375_DAT_PORT ); }
unsigned char wait_interrupt() { while( CH375_INT_WIRE ); CH375_WR_CMD_PORT( CMD_GET_STATUS ); return( CH375_RD_DAT_PORT() );
}
/* 設(shè)置CH375為USB主機(jī)方式 */ unsigned char mCH375Init( ) { unsigned char c, i; CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); /* 設(shè)置USB工作模式 */ CH375_WR_DAT_PORT( 6 ); /* 模式代碼,自動(dòng)檢測(cè)USB設(shè)備連接 */ for ( i = 0xff; i != 0; i -- ) { /* 等待操作成功,通常需要等待10uS-20uS */ c = CH375_RD_DAT_PORT( ); if ( c == CMD_RET_SUCCESS ) break; /* 操作成功 */ } if ( i != 0 ) return( USB_INT_SUCCESS ); /* 操作成功 */ else return( 0xff ); /* CH375出錯(cuò),例如芯片型號(hào)錯(cuò)或者處于串口方式或者不支持 */ }
unsigned char endp6_mode, endp7_mode;
#define TRUE 1 #define FALSE 0 unsigned char set_usb_mode( unsigned char mode ) { unsigned char i; CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); CH375_WR_DAT_PORT( mode ); endp6_mode=endp7_mode=0x80; for( i=0; i!=100; i++ ) { if ( CH375_RD_DAT_PORT()==CMD_RET_SUCCESS ) return( TRUE ); } return( FALSE ); }
void toggle_recv() { CH375_WR_CMD_PORT( CMD_SET_ENDP6 ); CH375_WR_DAT_PORT( endp6_mode ); endp6_mode^=0x40; delay2us(); }
void toggle_send() { CH375_WR_CMD_PORT( CMD_SET_ENDP7 ); CH375_WR_DAT_PORT( endp7_mode ); endp7_mode^=0x40; delay2us(); }
unsigned char clr_stall6() { CH375_WR_CMD_PORT( CMD_CLR_STALL ); CH375_WR_DAT_PORT( 2 | 0x80 ); endp6_mode=0x80; return( wait_interrupt() ); }
unsigned char clr_stall7() { CH375_WR_CMD_PORT( CMD_CLR_STALL ); CH375_WR_DAT_PORT( 2 ); endp7_mode=0x80; return( wait_interrupt() ); }
unsigned char rd_usb_data( unsigned char *buf ) { unsigned char i, len; CH375_WR_CMD_PORT( CMD_RD_USB_DATA ); len=CH375_RD_DAT_PORT(); for ( i=0; i!=len; i++ ) *buf++=CH375_RD_DAT_PORT(); return( len ); }
void wr_usb_data( unsigned char len, unsigned char *buf ) { CH375_WR_CMD_PORT( CMD_WR_USB_DATA7 ); CH375_WR_DAT_PORT( len ); while( len-- ) CH375_WR_DAT_PORT( *buf++ ); }
unsigned char issue_token( unsigned char endp_and_pid ) { unsigned char status; CH375_WR_CMD_PORT( CMD_ISSUE_TOKEN ); CH375_WR_DAT_PORT( endp_and_pid ); status=wait_interrupt(); if ( status!=USB_INT_SUCCESS && (endp_and_pid&0xF0)==0x20 ) { /* 操作失敗,如果設(shè)備端不是CH37X芯片,那么需要修改端點(diǎn)號(hào) */ if ( (endp_and_pid&0x0F)==DEF_USB_PID_OUT ) clr_stall7(); /* 復(fù)位設(shè)備端接收 */ else if ( (endp_and_pid&0x0F)==DEF_USB_PID_IN ) clr_stall6(); /* 復(fù)位設(shè)備端發(fā)送 */ } return( status ); }
void host_send( unsigned char len, unsigned char *buf ) { /* 主機(jī)發(fā)送buf為8051緩沖區(qū) */ wr_usb_data( len, buf ); toggle_send(); if ( issue_token( ( 2 << 4 ) | DEF_USB_PID_OUT )!=USB_INT_SUCCESS ) ERROR(); }
unsigned char host_recv( unsigned char *buf ) { toggle_recv(); if ( issue_token( ( 2 << 4 ) | DEF_USB_PID_IN )!=USB_INT_SUCCESS ) ERROR(); return( rd_usb_data( buf ) ); }
unsigned char get_descr( unsigned char type ) { unsigned char status; CH375_WR_CMD_PORT( CMD_GET_DESCR ); CH375_WR_DAT_PORT( type ); status=wait_interrupt(); if ( status==USB_INT_SUCCESS ) { unsigned char buffer[64]; unsigned char len; len=rd_usb_data( buffer ); } return( status ); }
unsigned char set_addr( unsigned char addr ) { unsigned char status; CH375_WR_CMD_PORT( CMD_SET_ADDRESS ); CH375_WR_DAT_PORT( addr ); status=wait_interrupt(); if ( status==USB_INT_SUCCESS ) { CH375_WR_CMD_PORT( CMD_SET_USB_ADDR ); CH375_WR_DAT_PORT( addr ); } return( status ); }
unsigned char set_config( unsigned char cfg ) { endp6_mode=endp7_mode=0x80; CH375_WR_CMD_PORT( CMD_SET_CONFIG ); CH375_WR_DAT_PORT( cfg ); return( wait_interrupt() ); }
void Inputkey(unsigned char*buf) { unsigned char keytmp,keytmp1; unsigned char keydata1,keydata2,keydata3,keydata4; keydata1=0; keydata2=0; keydata3=0; keydata4=0; while(P3^7==0&&P2^3==0) { keytmp=~(P0); keytmp1=keytmp&0x01; if(keytmp1==1)keydata1=keytmp1; keytmp1=keytmp&0x02; if(keytmp1==2)keydata2=keytmp1; keytmp1=keytmp&0x04; if(keytmp1==4)keydata3=keytmp1; keytmp1=keytmp&0x08; if(keytmp1==8)keydata4=keytmp1; *buf=keydata1+keydata2+keydata3+keydata4; } }
void bcddisplay(void) { unsigned char i; unsigned char *buf;//8952輸出緩沖區(qū) P0=0; while(1) {
P0=*buf; P0=~P0&0XF0; if(P0!=F0) {wr=0; en=0;} for(i=5;i>0;i--)delay2us( ); } }
void main(void) { unsigned char data data_to_send[16], data_by_recv[16]; //收發(fā)緩沖區(qū) unsigned char i, len; for ( i=0; i<50000; i++ ) delay2us(); CH375_WR_CMD_PORT( CMD_CHECK_EXIST ); /* 測(cè)試CH375是否正常工作 */ CH375_WR_DAT_PORT( 0x55 ); /* 寫(xiě)入測(cè)試數(shù)據(jù) */ i = ~ 0x55; /* 返回?cái)?shù)據(jù)應(yīng)該是測(cè)試數(shù)據(jù)取反 */
while(1){ mCH375Init( ); set_usb_mode( 6 ); //設(shè)置USB工作模式,06H切換到已啟用的USB主機(jī)方式,自動(dòng)產(chǎn)生SOF包 while ( wait_interrupt()!=USB_INT_CONNECT ); /* 等待設(shè)備端連接上來(lái) */ #define USB_RESET_FIRST 1 #ifdef USB_RESET_FIRST set_usb_mode( 7 ); //向已經(jīng)連接的USB設(shè)備提供USB總線復(fù)位方式,切換到其他工作模式后復(fù)位才會(huì)結(jié)束; for ( i=0; i<250; i++ ) { delay2us(); delay2us(); delay2us(); delay2us(); } set_usb_mode( 6 ); //結(jié)束復(fù)位 while ( wait_interrupt()!=USB_INT_CONNECT ); for ( i=0; i<250; i++ ) delay2us(); #endif get_descr(1); set_addr(5); get_descr(2); set_config(1); Inputkey(&data_to_send[i]); host_send( 8, &data_to_send[16] ); host_send( 0, NULL ); len=host_recv( &data_by_recv[16] ); bcddisplay(); while(1); } }