USB不能通信

还是不行!返回仍然是FFH,下面是我的源程序,参考的是CH375HST上的主机程序,加了显示和发送程序,应该没什么错啊,帮我看看吧,你来苏州我请你吃饭!!!我要答辩了啊 #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 /* 最大数据包的长度, 内部缓冲区的长度 */

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() );

}

/* 设置CH375为USB主机方式 */ unsigned char mCH375Init( ) { unsigned char c, i; CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); /* 设置USB工作模式 */ CH375_WR_DAT_PORT( 6 ); /* 模式代码,自动检测USB设备连接 */ 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出错,例如芯片型号错或者处于串口方式或者不支持 */ }

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 ) { /* 操作失败,如果设备端不是CH37X芯片,那么需要修改端点号 */ if ( (endp_and_pid&0x0F)==DEF_USB_PID_OUT ) clr_stall7(); /* 复位设备端接收 */ else if ( (endp_and_pid&0x0F)==DEF_USB_PID_IN ) clr_stall6(); /* 复位设备端发送 */ } return( status ); }

void host_send( unsigned char len, unsigned char *buf ) { /* 主机发送buf为8051缓冲区 */ 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输出缓冲区 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]; //收发缓冲区 unsigned char i, len; for ( i=0; i<50000; i++ ) delay2us(); CH375_WR_CMD_PORT( CMD_CHECK_EXIST ); /* 测试CH375是否正常工作 */ CH375_WR_DAT_PORT( 0x55 ); /* 写入测试数据 */ i = ~ 0x55; /* 返回数据应该是测试数据取反 */

while(1){ mCH375Init( ); set_usb_mode( 6 ); //设置USB工作模式,06H切换到已启用的USB主机方式,自动产生SOF包 while ( wait_interrupt()!=USB_INT_CONNECT ); /* 等待设备端连接上来 */ #define USB_RESET_FIRST 1 #ifdef USB_RESET_FIRST set_usb_mode( 7 ); //向已经连接的USB设备提供USB总线复位方式,切换到其他工作模式后复位才会结束; for ( i=0; i<250; i++ ) { delay2us(); delay2us(); delay2us(); delay2us(); } set_usb_mode( 6 ); //结束复位 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里的主机程序,加上显示和发送程序,帮我看看吧,我要急死了

#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 /* 最大数据包的长度, 内部缓冲区的长度 */

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() );

}

/* 设置CH375为USB主机方式 */ unsigned char mCH375Init( ) { unsigned char c, i; CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); /* 设置USB工作模式 */ CH375_WR_DAT_PORT( 6 ); /* 模式代码,自动检测USB设备连接 */ 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出错,例如芯片型号错或者处于串口方式或者不支持 */ }

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 ) { /* 操作失败,如果设备端不是CH37X芯片,那么需要修改端点号 */ if ( (endp_and_pid&0x0F)==DEF_USB_PID_OUT ) clr_stall7(); /* 复位设备端接收 */ else if ( (endp_and_pid&0x0F)==DEF_USB_PID_IN ) clr_stall6(); /* 复位设备端发送 */ } return( status ); }

void host_send( unsigned char len, unsigned char *buf ) { /* 主机发送buf为8051缓冲区 */ 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输出缓冲区 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]; //收发缓冲区 unsigned char i, len; for ( i=0; i<50000; i++ ) delay2us(); CH375_WR_CMD_PORT( CMD_CHECK_EXIST ); /* 测试CH375是否正常工作 */ CH375_WR_DAT_PORT( 0x55 ); /* 写入测试数据 */ i = ~ 0x55; /* 返回数据应该是测试数据取反 */ while(1){ mCH375Init( ); set_usb_mode( 6 ); //设置USB工作模式,06H切换到已启用的USB主机方式,自动产生SOF包 while ( wait_interrupt()!=USB_INT_CONNECT ); /* 等待设备端连接上来 */ #define USB_RESET_FIRST 1 #ifdef USB_RESET_FIRST set_usb_mode( 7 ); //向已经连接的USB设备提供USB总线复位方式,切换到其他工作模式后复位才会结束; for ( i=0; i<250; i++ ) { delay2us(); delay2us(); delay2us(); delay2us(); } set_usb_mode( 6 ); //结束复位 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里的主机程序,加上显示和发送程序,我要急死了


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


我的电路是8952的P1口接375的数据口,P0口的低四位接了锁存器74LS373后接数码管显示部分,373的控制信号由89C52的WR和P2.2通过或非门74LS02给出,P0口的高四位接了74LS244后接数据发送部分,244的控制信号由RD和P2.3通过或门74LS32给出。 在与CH375相连时,89C52的外部中断0接CH375的中断信号线,CH375 的地址A0 由89C52 的 P20 驱动,CH375 的片选线CS#由89C52 的P21 驱动。WR和WR相连,RD和RD相连 单片机和375都接了晶振


这不是程序的问题,发测试程序的时候,返回的0XFF,那就说明你的硬件有问题,请仔细检查硬件


有没有可能是我的电路设计的不对呢


你的硬件电路和375相连接的8位数据线是P1口,那你要用模拟口方式,不能用总线方式


那我把数据口改成P0口是不是就可以了啊


我测了一下,我的RD、WR、A0、CS、INT都是5 V,V3是3.48V,你说的那些方面我查了都没问题,还会是哪些地方错了呢


要仔细检查电路,你的375是几伏供电?你的程序访问375还是总线方式传输吗?


是5V供电,程序访问375和总线方式有什么区别?还有我的复位端一直是低电平,又是怎么回事,我都是借鉴375数据手册上的并口方式来连接电路的啊


因为51单片机只有P0口可以做为总线口,在读和写的时候单片机直接将RD或者WR拉高或者置0,而P1口则要进行I/O口的模拟总线才可以操作,要在程序中将WR/RD拉高或者置低


但我现在改成了P0口,为什么还是不可以呢,RSTI一直为低电平,而RD,WR,A0,CS一直为高电平,我都是按数据手册上的并口方式连接的,不会有错啊


那你发送测试命令的时候还是返回的0XFF吗?还是别的数据?


只有登录才能回复,可以选择微信账号登录