USB不能通信

還是不行!返回仍然是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); } }


還是不行,返回還是FFH,下面是我的源程序,參考的是CH375HST里的主機(jī)程序,加上顯示和發(fā)送程序,幫我看看吧,我要急死了

#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); } }


還是不行,返回還是FFH,我參考的是CH375HST里的主機(jī)程序,加上顯示和發(fā)送程序,我要急死了


還是不行,返回的仍然是FFH


我的電路是8952的P1口接375的數(shù)據(jù)口,P0口的低四位接了鎖存器74LS373后接數(shù)碼管顯示部分,373的控制信號(hào)由89C52的WR和P2.2通過(guò)或非門(mén)74LS02給出,P0口的高四位接了74LS244后接數(shù)據(jù)發(fā)送部分,244的控制信號(hào)由RD和P2.3通過(guò)或門(mén)74LS32給出。 在與CH375相連時(shí),89C52的外部中斷0接CH375的中斷信號(hào)線,CH375 的地址A0 由89C52 的 P20 驅(qū)動(dòng),CH375 的片選線CS#由89C52 的P21 驅(qū)動(dòng)。WR和WR相連,RD和RD相連 單片機(jī)和375都接了晶振


這不是程序的問(wèn)題,發(fā)測(cè)試程序的時(shí)候,返回的0XFF,那就說(shuō)明你的硬件有問(wèn)題,請(qǐng)仔細(xì)檢查硬件


有沒(méi)有可能是我的電路設(shè)計(jì)的不對(duì)呢


你的硬件電路和375相連接的8位數(shù)據(jù)線是P1口,那你要用模擬口方式,不能用總線方式


那我把數(shù)據(jù)口改成P0口是不是就可以了啊


我測(cè)了一下,我的RD、WR、A0、CS、INT都是5 V,V3是3.48V,你說(shuō)的那些方面我查了都沒(méi)問(wèn)題,還會(huì)是哪些地方錯(cuò)了呢


要仔細(xì)檢查電路,你的375是幾伏供電?你的程序訪問(wèn)375還是總線方式傳輸嗎?


是5V供電,程序訪問(wèn)375和總線方式有什么區(qū)別?還有我的復(fù)位端一直是低電平,又是怎么回事,我都是借鑒375數(shù)據(jù)手冊(cè)上的并口方式來(lái)連接電路的啊


因?yàn)?1單片機(jī)只有P0口可以做為總線口,在讀和寫(xiě)的時(shí)候單片機(jī)直接將RD或者WR拉高或者置0,而P1口則要進(jìn)行I/O口的模擬總線才可以操作,要在程序中將WR/RD拉高或者置低


但我現(xiàn)在改成了P0口,為什么還是不可以呢,RSTI一直為低電平,而RD,WR,A0,CS一直為高電平,我都是按數(shù)據(jù)手冊(cè)上的并口方式連接的,不會(huì)有錯(cuò)啊


那你發(fā)送測(cè)試命令的時(shí)候還是返回的0XFF嗎?還是別的數(shù)據(jù)?


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频