[求助]hcn大哥请看看这个程序!谢谢!

/**************************************** ** Copyright (C) W.ch 1999-2004 ** ** Web: http://www.winchiphead.com ** **************************************** ** USB 1.1 Host Examples for CH375 ** ** KC7.0@MCS-51 ** **************************************** */ /* 用CH372模拟成HID设备,比如键盘,鼠标*/ #include #include #include //#include #include #include #define SUCCESS 1 #define ABORT 0 #define UINT8 unsigned char #define UINT16 unsigned int #define CH375_CMD_PORT XBYTE[0X0BDF1] // CH375命令端口的I/O地址 / #define CH375_DAT_PORT XBYTE[0X0BCF0] // CH375数据端口的I/O地址 / #define DEF_USB_HID_GET_REPORT 0x01 #define DEF_USB_HID_SET_REPORT 0x09 #define DEF_USB_HID_GET_IDLE 0x02 #define DEF_USB_HID_SET_IDLE 0x0A #define DEF_USB_HID_GET_PROTOCOL 0x03 #define DEF_USB_HID_SET_PROTOCOL 0x0B sbit P2_4 = P2^4; sbit P1_0 = P1^0; sbit P1_2 = P1^2;

UINT8 int_status; //中断状态

union _ep0_data //请求包 { struct { UINT8 bmRequestType; UINT8 bRequest; UINT16 wValue; UINT16 wIndx; UINT16 wLength; }device_request; UINT8 data_buf[8]; }ep0_data; unsigned char ep1_in_data[8]; //端点1上传缓冲区 struct _desc_info { UINT8 desc_type; UINT8 desc_index; UINT8 desc_length; }desc_info;

UINT8 configure_value; //设备配置值 UINT8 report_x=0,report_y=0; //鼠标上传数据 UINT8 const *p_desc_type; // UINT8 ep0_data_len,usb_address; // bit flag_return_desc; bit flag_configured; bit flag_set_add; bit flag_report_input; bit flag_return_ack; void delay( ) /* 至少延时1uS,根据单片机主频调整 */ { UINT8 i; for ( i = 5; i != 0; i -- ); } void delay20us() { UINT8 us=40; while(us--); } void delayms(UINT8 delay) { UINT8 ms,sm,c; for(ms=delay;ms!=0;ms--) { for(sm=200;sm!=0;sm--)c+=3; /* 在24MHz时钟下延时500uS */ for(sm=200;sm!=0;sm--)c+=3; /* 在24MHz时钟下延时500uS */ } } unsigned char CH37x_set_usb_mode(UINT8 mode) { CH375_CMD_PORT=CMD_SET_USB_MODE; delay(); CH375_DAT_PORT=mode; delay20us(); if(CH375_DAT_PORT==CMD_RET_SUCCESS) return(SUCCESS); else return(ABORT); } void init_CH37x() { flag_return_desc=0; flag_configured=0; flag_set_add=0; flag_report_input=0; if(!CH37x_set_usb_mode(0x01))while(1); }

UINT8 CH37x_get_status() { CH375_CMD_PORT=CMD_GET_STATUS; delay(); return(CH375_DAT_PORT); } void CH37x_unlock_usb() { CH375_CMD_PORT=CMD_UNLOCK_USB; delay();

}

UINT8 CH37x_rd_usb_data() { UINT8 data_len,data_len_temp; UINT8 *data_buf_r=ep0_data.data_buf; CH375_CMD_PORT=CMD_RD_USB_DATA; delay(); data_len_temp=data_len=CH375_DAT_PORT; while(data_len) { *data_buf_r=CH375_DAT_PORT; data_buf_r++; data_len--; } return(data_len_temp); } void CH37x_wr_usb_data5(UINT8 data_len) { UINT8 *data_buf_w=ep0_data.data_buf; CH375_CMD_PORT=CMD_WR_USB_DATA5; delay(); CH375_DAT_PORT=data_len;

while(data_len) { CH375_DAT_PORT=*data_buf_w; data_buf_w++; data_len--; } } void CH37x_wr_usb_data7(UINT8 data_len) { UINT8 *data_buf_w=ep0_data.data_buf; CH375_CMD_PORT=CMD_WR_USB_DATA7; delay(); CH375_DAT_PORT=data_len;

while(data_len) { CH375_DAT_PORT=*data_buf_w; data_buf_w++; data_len--; } } void CH37x_set_usb_addr(UINT8 address) { CH375_CMD_PORT=CMD_SET_USB_ADDR; delay(); CH375_DAT_PORT=address; } void CH37x_set_endp3(UINT8 mode_3) { UINT8 i=8; CH375_CMD_PORT=CMD_SET_ENDP3; delay(); CH375_DAT_PORT=mode_3;

while(i--); } void CH37x_set_endp4(UINT8 mode_4) { UINT8 i=8; CH375_CMD_PORT=CMD_SET_ENDP4; delay(); CH375_DAT_PORT=mode_4;

while(i--); } void CH37x_set_endp5(UINT8 mode_5) { UINT8 i=8; CH375_CMD_PORT=CMD_SET_ENDP5; delay(); CH375_DAT_PORT=mode_5;

while(i--); } void CH37x_set_endp6(UINT8 mode_6) { UINT8 i=8; CH375_CMD_PORT=CMD_SET_ENDP6; delay(); CH375_DAT_PORT=mode_6;

while(i--); }

void CH37x_set_endp7(UINT8 mode_7) { UINT8 i=8; CH375_CMD_PORT=CMD_SET_ENDP7; delay(); CH375_DAT_PORT=mode_7;

while(i--); } void CH37x_wr_usb_data3(UINT8 data_len) { UINT8 *data_buf_w=ep0_data.data_buf; CH375_CMD_PORT=CMD_WR_USB_DATA3; delay(); CH375_DAT_PORT=data_len;

while(data_len) { CH375_DAT_PORT=*data_buf_w; data_buf_w++; data_len--; } } void CH37x_return_desc() { UINT8 i=0,j; if(flag_return_desc==1) { if(desc_info.desc_length>8) { for(j=8;j!=0;j--)ep0_data.data_buf[i++]=*p_desc_type++; desc_info.desc_length-=8; CH37x_wr_usb_data3(8); } else { for(j=desc_info.desc_length;j!=0;j--)ep0_data.data_buf[i++]=*p_desc_type++; CH37x_wr_usb_data3(desc_info.desc_length); desc_info.desc_length=0; flag_return_desc=0; } } } void init_stdio() { SCON = 0x50; PCON = 0x80; TMOD = 0x20; TH1 = 0xff; /* 22.1184MHz晶振, 115200bps */ TR1 = 1; TI = 1; }

void intt0() interrupt 0 //using 1 { unsigned char requesttype_temp,status_temp; status_temp=CH37x_get_status(); switch(status_temp) { case(USB_INT_EP0_SETUP): { ep0_data_len=CH37x_rd_usb_data(); if(ep0_data_len!=8) { CH37x_set_endp3(0x0f); break; } requesttype_temp=ep0_data.device_request.bmRequestType>>5&0x03; switch(requesttype_temp) { case(0): /*setup standard request */ { switch(ep0_data.device_request.bRequest) { case(DEF_USB_GET_STATUS): { ep0_data.data_buf[0]=0; ep0_data.data_buf[1]=0; CH37x_wr_usb_data3(2); break; } case(DEF_USB_CLR_FEATURE): { if((ep0_data.device_request.bmRequestType&0x1f)==0x02) { switch(ep0_data.data_buf[4]) { case(0x82): { CH37x_set_endp7(0x8E); break; } case(0x02): { CH37x_set_endp6(0x80); break; } case(0x81): { CH37x_set_endp5(0x8E); break; } case(0x01): { CH37x_set_endp4(0x80); break; } default: break; } } else{CH37x_set_endp3(0x0f);} break; } case(DEF_USB_SET_FEATURE): { CH37x_set_endp3(0x0f); break; } case(DEF_USB_SET_ADDRESS): { usb_address=ep0_data.data_buf[2]; CH37x_wr_usb_data3(0); flag_return_ack=1; flag_set_add=1; break; } case(DEF_USB_GET_DESCR): { desc_info.desc_type=ep0_data.data_buf[3]; desc_info.desc_index=ep0_data.data_buf[2]; desc_info.desc_length=ep0_data.data_buf[6]; flag_return_desc=1; switch(desc_info.desc_type) { case(1):{p_desc_type=device_descriptor; if(desc_info.desc_length!=0x12)desc_info.desc_length=0x12;break;} case(2):{p_desc_type=config_descriptor;break;} case(3): { break; } case(4):{p_desc_type=interface_descriptor;break;} case(5):{p_desc_type=endpoint_descriptor;break;} case(0x21):{p_desc_type=hid_descriptor;break;}/*hid descriptor*/ case(0x22): { p_desc_type=report_descriptor; desc_info.desc_length=0x32; // desc_info.desc_length=0x3f;//keyboard flag_report_input=1; break;}/*report descriptor*/ } CH37x_return_desc(); break; } case(DEF_USB_SET_DESCR): { CH37x_set_endp3(0x0f); break; } case(DEF_USB_GET_CONFIG): { if(!flag_configured){ep0_data.data_buf[0]=0;CH37x_wr_usb_data3(1);} else {ep0_data.data_buf[0]=configure_value;CH37x_wr_usb_data3(1);} break; } case(DEF_USB_SET_CONFIG): { configure_value=ep0_data.data_buf[2]; flag_configured=1; flag_return_ack=1; break; } case(DEF_USB_GET_INTE

hcn大哥  ,我调鼠标还是调不通啊?用的372的评估板 ,我把372换成375,还是无法识别设备啊?谢谢!


你换个375或者372试下看可以不可以


换了375 还换了51单片机,还是提示无法识别 ,我就郁闷了,提示无法识别,应该是USB模式没有设好吗? 我用的就是372的评估板,急切得到回复!谢谢!


你先用XFIRM下的文件夹下面的程序试下看可以不可以


hcn大哥,我想问你一下,在程序中 P1_0和P1_2可以理解为鼠标的2 个按键吗? 要是可以理解按键的话,那么这个程序模拟的鼠标可以象我们用的正常的鼠标一样显示在电脑的桌面上吗?谢谢!


你的372评估板上面带的451是具有键盘的功能,所以你直接用451就可以做成左键和右键了,不过,左键和右键的数据要你自己写,你检测到这个键按下,那么你就通过372的中断端点上传左键或者右键的数据就可以了,你只要在设备管理器里面看有没有人体学设备下面有没有HID类的设备就可以了


多谢hcn 大哥!我去看看!


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