📄 usbmouse.lst
字号:
253 1 for(i=0;i<x;i++)
254 1 for(j=0;j<230;j++);
255 1 }
256 /********************************************/
257
258 /*******************************写USB命令******************************************/
259 void write_usb_command(uint8 usb_command)
260 {
261 1 USB_A0=USB_COMMAND_ADD;
262 1 USB_DATA=usb_command;
263 1 USB_WR=0;
264 1 USB_WR=1;
265 1 USB_DATA=0xFF;
266 1 }
267 /******************************************************************************/
268
269 /*********************写一字节USB数据*******************************************/
270 void write_a_usb_data(uint8 usb_data)
271 {
272 1 USB_A0=USB_DATA_ADD;
273 1 USB_DATA=usb_data;
274 1 USB_WR=0;
275 1 USB_WR=1;
276 1 USB_DATA=0XFF;
277 1 }
278 /******************************************************************************/
279
280 /****************************读一字节USB数据************************************/
281 uint8 read_a_usb_data(void)
282 {
283 1 uint8 temp;
284 1 USB_A0=USB_DATA_ADD;
285 1 USB_RD=0;
286 1 temp=USB_DATA;
287 1 USB_RD=1;
288 1 return temp;
289 1 }
290 /******************************************************************************/
291
292 /************************读USB中断寄存器**************************************/
293 void read_interrupt_register(void)
294 {
295 1 write_usb_command(Read_Interrupt_Register);
296 1 Interrupt_Register.Register[0]=read_a_usb_data();
297 1 Interrupt_Register.Register[1]=read_a_usb_data();
298 1 }
299 /******************************************************************************/
300
301 /************************设置USB地址*******************************************/
302 void set_usb_addr(uint8 addr)
C51 COMPILER V7.06 USBMOUSE 12/10/2007 14:20:43 PAGE 6
303 {
304 1
305 1 write_usb_command(Set_Address);
306 1 write_a_usb_data(0x80|addr);
307 1 Prints(" 设置地址.\r\n");
308 1 Prints(" 地址为: ");
309 1 PrintLongInt(addr);
310 1 Prints("\r\n");
311 1 }
312 /******************************************************************************/
313
314 /*************************端点使能******************************************/
315 void set_endpoint_enable(void)
316 {
317 1 write_usb_command(Set_Endpoint_Enable);
318 1 write_a_usb_data(0x01);
319 1 }
320 /******************************************************************************/
321
322 /****************************选择终端点*************************************/
323 uint8 select_endpoint(uint8 endp)
324 {
325 1 write_usb_command(Select_EndPoint+endp);
326 1 return read_a_usb_data();
327 1 }
328 /******************************************************************************/
329
330 /****************************读端点最后状态**********************************/
331 uint8 read_last_status(uint8 endp)
332 {
333 1 write_usb_command(Read_Last_Status+endp);
334 1 return read_a_usb_data();
335 1 }
336 /******************************************************************************/
337
338 /****************************设置端点状态**************************************/
339 void set_endpoint_status(uint8 endp,uint8 status)
340 {
341 1 write_usb_command(0x40+endp);
342 1 write_a_usb_data(!status);
343 1 }
344 /******************************************************************************/
345
346 /*****************************读端点状态**************************************/
347 uint8 read_endpoint_status(uint8 endp)
348 {
349 1 write_usb_command(0x80+endp);
350 1 return read_a_usb_data();
351 1 }
352 /******************************************************************************/
353
354 /************************清缓冲,在读取缓冲数据后调用**************************/
355 void clear_buffer(void)
356 {
357 1 write_usb_command(Clear_Buffer);
358 1 #ifdef debug
Prints("Clear buffer.\r\n");
#endif
361 1 }
362 /******************************************************************************/
363
364 /***********************缓冲区数据有效,在写缓冲后调用**************************/
C51 COMPILER V7.06 USBMOUSE 12/10/2007 14:20:43 PAGE 7
365 void validate_buffer(void)
366 {
367 1 write_usb_command(Validate_Buffer);
368 1 #ifdef debug
Prints("Validate buffer.\r\n");
#endif
371 1 }
372 /******************************************************************************/
373
374 /***************************应答建立包************************************/
375 void ack_setup(uint8 endp)
376 {
377 1 select_endpoint(endp);
378 1 write_usb_command(Ack_Setup);
379 1 #ifdef debug
Prints("Ack setup ");
PrintLongInt(endp);
Prints(".\r\n");
#endif
384 1 }
385 /******************************************************************************/
386
387 /***********************出错处理******************************************/
388 void error(uint8 number)
389 {
390 1 Prints("有错误发生!!!\r\n");
391 1 number=0;
392 1 }
393 /******************************************************************************/
394
395 /*************************读终端点缓冲****************************************/
396 uint8 read_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
397 {
398 1 uint8 i,j;
399 1 read_last_status(endp);
400 1 if(!(select_endpoint(endp)&0x01)){error(0); return 0;}
401 1 if((read_endpoint_status(endp)&0x60)!=0x60) //两个缓冲没有都满,才能清中断
402 1 {
403 2 read_last_status(endp); //清中断
404 2 }
405 1 write_usb_command(Read_Buffer);
406 1 read_a_usb_data();
407 1 j=read_a_usb_data();
408 1 if(j>len)
409 1 j=len;
410 1 #ifdef debug
Prints("Read endpoint");
PrintLongInt(endp);
Prints("\'s buffer ");
PrintLongInt(j);
Prints(" bytes.\r\n");
#endif
417 1 for(i=0;i<j;i++)
418 1 {
419 2 USB_RD=0;
420 2 *(buff+i)=USB_DATA;
421 2 USB_RD=1;
422 2 #ifdef debug
PrintHex(*(buff+i));
#endif
425 2 }
426 1 #ifdef debug
C51 COMPILER V7.06 USBMOUSE 12/10/2007 14:20:43 PAGE 8
Prints("\r\n");
#endif
429 1 clear_buffer();
430 1 return j;
431 1 }
432 /******************************************************************************/
433
434 /*************************写终端点缓冲*****************************************/
435 uint8 write_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
436 {
437 1 uint8 i;
438 1 read_last_status(endp);
439 1 select_endpoint(endp);
440 1 write_usb_command(Write_Buffer);
441 1 write_a_usb_data(0);
442 1 write_a_usb_data(len);
443 1 #ifdef debug
Prints("Write endpoint");
PrintLongInt(endp);
Prints("\'s buffer ");
PrintLongInt(len);
Prints(" bytes.\r\n");
#endif
450 1 for(i=0;i<len;i++)
451 1 {
452 2 USB_DATA=*(buff+i);
453 2 USB_WR=0;
454 2 USB_WR=1;
455 2 #ifdef debug
PrintHex(*(buff+i));
#endif
458 2 }
459 1 #ifdef debug
Prints("\r\n");
#endif
462 1 USB_DATA=0xFF;
463 1 validate_buffer();
464 1 return len;
465 1 }
466 /******************************************************************************/
467
468 /***************************断开USB连接****************************************/
469 void disconnect_usb(void)
470 {
471 1 Prints("断开USB连接.\r\n");
472 1 write_usb_command(0xf3);
473 1 write_a_usb_data(0x0e);
474 1 write_a_usb_data(0x47);
475 1 delay(100);
476 1 }
477 /******************************************************************************/
478
479 /*******************************连接USB**************************************/
480 void connect_usb(void)
481 {
482 1 Prints("连接USB.\r\n");
483 1 write_usb_command(0xf3); //初始化USBD12
484 1 write_a_usb_data(0x1e); //连接USB
485 1 write_a_usb_data(0x47); //设置频率
486 1 }
487 /******************************************************************************/
488
C51 COMPILER V7.06 USBMOUSE 12/10/2007 14:20:43 PAGE 9
489 /***************************初始化USB***************************************************/
490 void init_usb(void)
491 {
492 1 Prints("USBD12芯片初始化\r\n");
493 1 set_usb_addr(0);
494 1 set_endpoint_enable();
495 1 }
496 /******************************************************************************/
497
498 /****************************USB总线复位处理***********************************/
499 void usb_bus_reset(void)
500 {
501 1 Prints("USB总线复位.\r\n");
502 1 usb_flags.Register=0;
503 1 set_endpoint_enable();
504 1 }
505 /******************************************************************************/
506
507 /*****************************USB总线挂起处理**********************************/
508 void usb_bus_suspend(void)
509 {
510 1 Prints("USB总线挂起.\r\n");
511 1 }
512 /******************************************************************************/
513
514 /***************************设置地址***************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -