📄 usbofcompuer00.lst
字号:
249 uint8 Register;
250 struct
251 {
252 uint8 successful :1;
253 uint8 error_code :4;
254 uint8 setup_packet :1;
255 uint8 data_1 :1;
256 uint8 prestatus_not_read :1;
257 }Status;
258 }Last_Status;
259
260
261 /***************** 延时x毫秒函数 ***********/
262 void delay(uint16 x)
263 {
264 1 uint16 i;
265 1 uint16 j;
266 1 for(i=0;i<x;i++)
267 1 for(j=0;j<230;j++);
268 1 }
269 /********************************************/
270
271 /*******************************写USB命令******************************************/
272 void write_usb_command(uint8 usb_command)
273 {
274 1 USB_A0=USB_COMMAND_ADD;
275 1 USB_DATA=usb_command;
276 1 USB_WR=0;
277 1 USB_WR=1;
278 1 USB_DATA=0xFF;
279 1 }
280 /******************************************************************************/
281
282 /*********************写一字节USB数据*******************************************/
283 void write_a_usb_data(uint8 usb_data)
284 {
285 1 USB_A0=USB_DATA_ADD;
286 1 USB_DATA=usb_data;
287 1 USB_WR=0;
288 1 USB_WR=1;
289 1 USB_DATA=0XFF;
290 1 }
291 /******************************************************************************/
292
293 /****************************读一字节USB数据************************************/
294 uint8 read_a_usb_data(void)
295 {
296 1 uint8 temp;
297 1 USB_A0=USB_DATA_ADD;
298 1 USB_RD=0;
299 1 temp=USB_DATA;
300 1 USB_RD=1;
301 1 return temp;
302 1 }
303 /******************************************************************************/
C51 COMPILER V7.06 USBOFCOMPUER00 03/13/2008 12:38:31 PAGE 6
304
305 /************************读USB中断寄存器**************************************/
306 void read_interrupt_register(void)
307 {
308 1 write_usb_command(Read_Interrupt_Register);
309 1 Interrupt_Register.Register[0]=read_a_usb_data();
310 1 Interrupt_Register.Register[1]=read_a_usb_data();
311 1 }
312 /******************************************************************************/
313
314 /************************设置USB地址*******************************************/
315 void set_usb_addr(uint8 addr)
316 {
317 1 write_usb_command(Set_Address);
318 1 write_a_usb_data(0x80|addr);
319 1 #ifdef debug
Prints(" 设置地址.\r\n");
Prints(" 地址为: ");
PrintLongInt(addr);
Prints("\r\n");
#endif
325 1 }
326 /******************************************************************************/
327
328 /*************************端点使能******************************************/
329 void set_endpoint_enable(void)
330 {
331 1 write_usb_command(Set_Endpoint_Enable);
332 1 write_a_usb_data(0x01);
333 1 }
334 /******************************************************************************/
335
336 /****************************选择终端点*************************************/
337 uint8 select_endpoint(uint8 endp)
338 {
339 1 write_usb_command(Select_EndPoint+endp);
340 1 return read_a_usb_data();
341 1 }
342 /******************************************************************************/
343
344 /****************************读端点最后状态**********************************/
345 uint8 read_last_status(uint8 endp)
346 {
347 1 write_usb_command(Read_Last_Status+endp);
348 1 return read_a_usb_data();
349 1 }
350 /******************************************************************************/
351
352 /****************************设置端点状态**************************************/
353 void set_endpoint_status(uint8 endp,uint8 status)
354 {
355 1 write_usb_command(0x40+endp);
356 1 write_a_usb_data(!status);
357 1 }
358 /******************************************************************************/
359
360 /*****************************读端点状态**************************************/
361 uint8 read_endpoint_status(uint8 endp)
362 {
363 1 write_usb_command(0x80+endp);
364 1 return read_a_usb_data();
365 1 }
C51 COMPILER V7.06 USBOFCOMPUER00 03/13/2008 12:38:31 PAGE 7
366 /******************************************************************************/
367
368 /************************清缓冲,在读取缓冲数据后调用**************************/
369 void clear_buffer(void)
370 {
371 1 write_usb_command(Clear_Buffer);
372 1 #ifdef debug
Prints("Clear buffer.\r\n");
#endif
375 1 }
376 /******************************************************************************/
377
378 /***********************缓冲区数据有效,在写缓冲后调用**************************/
379 void validate_buffer(void)
380 {
381 1 write_usb_command(Validate_Buffer);
382 1 #ifdef debug
Prints("Validate buffer.\r\n");
#endif
385 1 }
386 /******************************************************************************/
387
388 /***************************应答建立包************************************/
389 void ack_setup(uint8 endp)
390 {
391 1 select_endpoint(endp);
392 1 write_usb_command(Ack_Setup);
393 1 #ifdef debug
Prints("Ack setup ");
PrintLongInt(endp);
Prints(".\r\n");
#endif
398 1 }
399 /******************************************************************************/
400
401 //#define debug2
402 /***********************出错处理******************************************/
403 void error(uint8 number)
404 {
405 1 #ifdef debug2
Prints("有错误发生!!!\r\n错误号: ");
PrintHex(number);
#endif
409 1 number=0;
410 1 }
411 /******************************************************************************/
412
413 /*************************读终端点缓冲****************************************/
414 uint8 read_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
415 {
416 1 uint8 i,j;
417 1
418 1 if(!(select_endpoint(endp)&0x01)){error(0); return 0;}
419 1 if((read_endpoint_status(endp)&0x60)!=0x60) //两个缓冲没有都满,才能清中断
420 1 {
421 2 read_last_status(endp); //清中断
422 2 }
423 1 write_usb_command(Read_Buffer);
424 1 read_a_usb_data();
425 1 j=read_a_usb_data();
426 1 if(j>len)
427 1 j=len;
C51 COMPILER V7.06 USBOFCOMPUER00 03/13/2008 12:38:31 PAGE 8
428 1 #ifdef debug
Prints("Read endpoint");
PrintLongInt(endp);
Prints("\'s buffer ");
PrintLongInt(j);
Prints(" bytes.\r\n");
#endif
435 1 for(i=0;i<j;i++)
436 1 {
437 2 USB_RD=0;
438 2 *(buff+i)=USB_DATA;
439 2 USB_RD=1;
440 2 #ifdef debug
PrintHex(*(buff+i));
#endif
443 2 }
444 1 #ifdef debug
Prints("\r\n");
#endif
447 1 if(endp)
448 1 {
449 2 select_endpoint(endp);
450 2 clear_buffer();
451 2 }
452 1 return j;
453 1 }
454 /******************************************************************************/
455
456 /*************************写终端点缓冲*****************************************/
457 uint8 write_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
458 {
459 1 uint8 i;
460 1 read_last_status(endp);
461 1 select_endpoint(endp);
462 1 write_usb_command(Write_Buffer);
463 1 write_a_usb_data(0);
464 1 write_a_usb_data(len);
465 1 #ifdef debug
Prints("Write endpoint");
PrintLongInt(endp);
Prints("\'s buffer ");
PrintLongInt(len);
Prints(" bytes.\r\n");
#endif
472 1 for(i=0;i<len;i++)
473 1 {
474 2 USB_DATA=*(buff+i);
475 2 USB_WR=0;
476 2 USB_WR=1;
477 2 #ifdef debug
PrintHex(*(buff+i));
#endif
480 2 }
481 1 #ifdef debug
Prints("\r\n");
#endif
484 1 USB_DATA=0xFF;
485 1 validate_buffer();
486 1 return len;
487 1 }
488 /******************************************************************************/
489
C51 COMPILER V7.06 USBOFCOMPUER00 03/13/2008 12:38:31 PAGE 9
490 /***************************断开USB连接****************************************/
491 void disconnect_usb(void)
492 {
493 1 #ifdef debug
Prints("断开USB连接.\r\n");
#endif
496 1 write_usb_command(0xf3);
497 1 write_a_usb_data(0x0e);
498 1 write_a_usb_data(0x47);
499 1 delay(100);
500 1 }
501 /******************************************************************************/
502
503 /*******************************连接USB**************************************/
504 void connect_usb(void)
505 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -