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