⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbkeyboard.lst

📁 EDN USB学习板之实战第一个USB程序: USB键盘
💻 LST
📖 第 1 页 / 共 5 页
字号:
 249   1       uint16 j;
 250   1       for(i=0;i<x;i++)
 251   1       for(j=0;j<230;j++);
 252   1      }
 253          /********************************************/
 254          
 255          /*******************************写USB命令******************************************/
 256          void write_usb_command(uint8 usb_command)
 257          {
 258   1       USB_A0=USB_COMMAND_ADD;
 259   1       USB_DATA=usb_command;
 260   1       USB_WR=0;
 261   1       USB_WR=1;
 262   1       USB_DATA=0xFF;
 263   1      }
 264          /******************************************************************************/
 265          
 266          /*********************写一字节USB数据*******************************************/
 267          void write_a_usb_data(uint8 usb_data)       
 268          {
 269   1       USB_A0=USB_DATA_ADD;
 270   1       USB_DATA=usb_data;
 271   1       USB_WR=0;
 272   1       USB_WR=1;
 273   1       USB_DATA=0XFF;
 274   1      }
 275          /******************************************************************************/
 276          
 277          /****************************读一字节USB数据************************************/
 278          uint8 read_a_usb_data(void)
 279          {
 280   1       uint8 temp;
 281   1       USB_A0=USB_DATA_ADD;
 282   1       USB_RD=0;
 283   1       temp=USB_DATA;
 284   1       USB_RD=1;
 285   1       return temp;
 286   1      }
 287          /******************************************************************************/
 288          
 289          /************************读USB中断寄存器**************************************/
 290          void read_interrupt_register(void)
 291          {
 292   1       write_usb_command(Read_Interrupt_Register);
 293   1       Interrupt_Register.Register[0]=read_a_usb_data();
 294   1       Interrupt_Register.Register[1]=read_a_usb_data();
 295   1      }
 296          /******************************************************************************/
 297          
 298          /************************设置USB地址*******************************************/
 299          void set_usb_addr(uint8 addr)
 300          {
 301   1       write_usb_command(Set_Address);
 302   1       write_a_usb_data(0x80|addr);
C51 COMPILER V8.02   USBKEYBOARD                                                           12/12/2007 21:37:17 PAGE 6   

 303   1       Prints("   设置地址.\r\n");
 304   1       Prints("       地址为:  ");
 305   1       PrintLongInt(addr);
 306   1       Prints("\r\n");
 307   1      }
 308          /******************************************************************************/
 309          
 310          /*************************端点使能******************************************/
 311          void set_endpoint_enable(void)
 312          {
 313   1       write_usb_command(Set_Endpoint_Enable);
 314   1       write_a_usb_data(0x01);
 315   1      }
 316          /******************************************************************************/
 317          
 318          /****************************选择终端点*************************************/
 319          uint8 select_endpoint(uint8 endp)
 320          {
 321   1       write_usb_command(Select_EndPoint+endp);
 322   1       return read_a_usb_data();
 323   1      }
 324          /******************************************************************************/
 325          
 326          /****************************读端点最后状态**********************************/
 327          uint8  read_last_status(uint8 endp)
 328          {
 329   1       write_usb_command(Read_Last_Status+endp);
 330   1       return read_a_usb_data();
 331   1      }
 332          /******************************************************************************/
 333          
 334          /****************************设置端点状态**************************************/
 335          void set_endpoint_status(uint8 endp,uint8 status)  
 336          {
 337   1       write_usb_command(0x40+endp);
 338   1       write_a_usb_data(!status);
 339   1      }
 340          /******************************************************************************/
 341          
 342          /*****************************读端点状态**************************************/
 343          uint8 read_endpoint_status(uint8 endp)
 344          {
 345   1       write_usb_command(0x80+endp);
 346   1       return read_a_usb_data();
 347   1      }
 348          /******************************************************************************/
 349          
 350          /************************清缓冲,在读取缓冲数据后调用**************************/
 351          void clear_buffer(void)
 352          {
 353   1       write_usb_command(Clear_Buffer);
 354   1      #ifdef debug
               Prints("Clear buffer.\r\n");
              #endif
 357   1      }
 358          /******************************************************************************/
 359          
 360          /***********************缓冲区数据有效,在写缓冲后调用**************************/
 361          void validate_buffer(void)
 362          {
 363   1       write_usb_command(Validate_Buffer);
 364   1      #ifdef debug
C51 COMPILER V8.02   USBKEYBOARD                                                           12/12/2007 21:37:17 PAGE 7   

               Prints("Validate buffer.\r\n");
              #endif
 367   1      }
 368          /******************************************************************************/
 369          
 370          /***************************应答建立包************************************/
 371          void ack_setup(uint8 endp)
 372          {
 373   1       select_endpoint(endp);
 374   1       write_usb_command(Ack_Setup);
 375   1      #ifdef debug
               Prints("Ack setup ");
               PrintLongInt(endp);
               Prints(".\r\n");
              #endif
 380   1      }
 381          /******************************************************************************/
 382          
 383          /***********************出错处理******************************************/
 384          void error(uint8 number)
 385          {
 386   1       Prints("有错误发生!!!\r\n");
 387   1       number=0;
 388   1      }
 389          /******************************************************************************/
 390          
 391          /*************************读终端点缓冲****************************************/
 392          uint8 read_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
 393          {
 394   1       uint8 i,j;
 395   1       read_last_status(endp);
 396   1       if(!(select_endpoint(endp)&0x01)){error(0); return 0;}
 397   1       if((read_endpoint_status(endp)&0x60)!=0x60)  //两个缓冲没有都满,才能清中断
 398   1       {
 399   2        read_last_status(endp);  //清中断
 400   2       }
 401   1       write_usb_command(Read_Buffer);
 402   1       read_a_usb_data();
 403   1       j=read_a_usb_data();
 404   1       if(j>len)
 405   1        j=len;  
 406   1      #ifdef debug
               Prints("Read endpoint");
               PrintLongInt(endp);
               Prints("\'s buffer ");
               PrintLongInt(j);
               Prints(" bytes.\r\n");
              #endif  
 413   1       for(i=0;i<j;i++)
 414   1        {
 415   2         USB_RD=0;
 416   2         *(buff+i)=USB_DATA;
 417   2         USB_RD=1;
 418   2      #ifdef debug
                 PrintHex(*(buff+i));
              #endif
 421   2        }
 422   1      #ifdef debug
               Prints("\r\n");
              #endif
 425   1       clear_buffer();
 426   1       return j;
C51 COMPILER V8.02   USBKEYBOARD                                                           12/12/2007 21:37:17 PAGE 8   

 427   1      }
 428          /******************************************************************************/
 429          
 430          /*************************写终端点缓冲*****************************************/
 431          uint8 write_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
 432          {
 433   1       uint8 i;
 434   1       read_last_status(endp);
 435   1       select_endpoint(endp);
 436   1       write_usb_command(Write_Buffer);
 437   1       write_a_usb_data(0);
 438   1       write_a_usb_data(len);
 439   1      #ifdef debug
               Prints("Write endpoint");
               PrintLongInt(endp);
               Prints("\'s buffer ");
               PrintLongInt(len);
               Prints(" bytes.\r\n");
              #endif
 446   1       for(i=0;i<len;i++)
 447   1        {
 448   2         USB_DATA=*(buff+i);
 449   2         USB_WR=0;
 450   2         USB_WR=1;
 451   2      #ifdef debug
                 PrintHex(*(buff+i));
              #endif
 454   2        }
 455   1      #ifdef debug
               Prints("\r\n");
              #endif  
 458   1       USB_DATA=0xFF;
 459   1       validate_buffer();
 460   1       return len;
 461   1      }
 462          /******************************************************************************/
 463          
 464          /***************************断开USB连接****************************************/
 465          void disconnect_usb(void)
 466          {
 467   1       Prints("断开USB连接.\r\n");
 468   1       write_usb_command(0xf3);
 469   1       write_a_usb_data(0x0e);
 470   1       write_a_usb_data(0x47);
 471   1       delay(100);
 472   1      }
 473          /******************************************************************************/
 474          
 475          /*******************************连接USB**************************************/
 476          void connect_usb(void)
 477          {
 478   1       Prints("连接USB.\r\n");
 479   1       write_usb_command(0xf3);                         //初始化USBD12
 480   1       write_a_usb_data(0x1e);                          //连接USB
 481   1       write_a_usb_data(0x47);                          //设置频率
 482   1      }
 483          /******************************************************************************/
 484          
 485          /***************************初始化USB***************************************************/
 486          void init_usb(void)               
 487          {
 488   1       Prints("USBD12芯片初始化\r\n");
C51 COMPILER V8.02   USBKEYBOARD                                                           12/12/2007 21:37:17 PAGE 9   

 489   1       set_usb_addr(0);
 490   1       set_endpoint_enable();
 491   1      }
 492          /******************************************************************************/
 493          
 494          /****************************USB总线复位处理***********************************/
 495          void usb_bus_reset(void)
 496          {
 497   1       Prints("USB总线复位.\r\n");
 498   1       usb_flags.Register=0;
 499   1       set_endpoint_enable();
 500   1      }
 501          /******************************************************************************/
 502          
 503          /*****************************USB总线挂起处理**********************************/
 504          void usb_bus_suspend(void)
 505          {
 506   1       Prints("USB总线挂起.\r\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -