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

📄 u_keyboard.lst

📁 一个用 51 + d12 实现的usb键盘的完整源代码
💻 LST
📖 第 1 页 / 共 2 页
字号:
 260   1              ucLedFlashCount = 0;
 261   1              while( TRUE )
 262   1              {
 263   2      
 264   2                      memset(ucKeyboardDataBuff,0,8);
 265   2                      ucLedFlashCount++;
 266   2                      FlashLed(ucLedFlashCount/64);
 267   2      
 268   2                      if (bEPPflags.bits.timer)
 269   2                      {
 270   3                              DISABLE;//定时器溢出,检测按键状态
 271   3                              bEPPflags.bits.timer = 0;
 272   3                              ENABLE;
 273   3      
 274   3                              if(bEPPflags.bits.configuration)//设备未配置返回
 275   3                                      check_key_LED();
 276   3                      }
 277   2                      if (bEPPflags.bits.bUSB_Busy)
 278   2                      {
 279   3                              fn_usb_isr();
 280   3      
 281   3                      }
 282   2      
 283   2                      if (bEPPflags.bits.bus_reset) {//设备复位处理
 284   3                              DISABLE;
 285   3                              bEPPflags.bits.bus_reset = 0;
 286   3                              ENABLE;
 287   3                              // Release D12's SUSPEND pin after bus reset
 288   3                              // Enable 74HCT123 pulse generation before disconnect
 289   3                              D12SUSPD = 1;
 290   3                      } // if bus reset
 291   2      
 292   2                      if (bEPPflags.bits.suspend) {//挂起改变处理
 293   3                              DISABLE;
 294   3                              bEPPflags.bits.suspend= 0;
 295   3                              ENABLE;
 296   3      
 297   3                              if(D12SUSPD == 1) 
 298   3                              {//挂起处理
 299   4                                      D12SUSPD = 0;
 300   4                                      D12SUSPD = 1;
 301   4                              }
 302   3                      } // if suspend change
C51 COMPILER V7.50   U_KEYBOARD                                                            04/16/2007 15:22:52 PAGE 6   

 303   2      
 304   2                      if (bEPPflags.bits.setup_packet)
 305   2                      {//Setup包处理
 306   3                              DISABLE;
 307   3                              bEPPflags.bits.setup_packet = 0;
 308   3                              ENABLE;
 309   3                              control_handler();//调用请求处理子程序
 310   3                              D12SUSPD = 1;
 311   3                      } // if setup_packet
 312   2      
 313   2              
 314   2                      if(bEPPflags.bits.configuration)//设备未配置返回
 315   2                      {
 316   3                              if(ucStatusMac == 1)
 317   3                              {
 318   4                                      uchar ucEPStatus;
 319   4                                      
 320   4                                      if(KbHit())
 321   4                                      {
 322   5                                              continue;
 323   5                                      }
 324   4                                      memset(ucKeyboardDataBuff,0,8);
 325   4                                      ucEPStatus=D12_ReadEndpointStatus(3);
 326   4                                      if(     (ucEPStatus & 0x20) == 0 )
 327   4                                      {
 328   5                                              D12_WriteEndpoint(3, 8, ucKeyboardDataBuff);//发送按键状态
 329   5                                              ucStatusMac = 0;
 330   5                      //                      BeepShort();
 331   5                                      }
 332   4                              }
 333   3      
 334   3                              if(ucStatusMac == 0)
 335   3                              {
 336   4                                      #ifdef ConnectPS2Device 
 337   4      
 338   4                                      if(PS2KeyboardScan())
 339   4                                      {
 340   5                                              uiPS2ReturnData = PS2GetChar();
 341   5                              //              ComShowString(COM_1,"Kbd return Value is :");
 342   5                              //              ComSendText(COM_1,&uiPS2ReturnData,2);
 343   5      
 344   5                                              memset(ucKeyboardDataBuff,0,8);
 345   5                                              ucKeyboardDataBuff[2] = (uchar)(uiPS2ReturnData>>8);
 346   5                      
 347   5                                              D12_WriteEndpoint(3, 8, ucKeyboardDataBuff);//发送按键状态
 348   5                                              ucStatusMac = 1;
 349   5                                      }
 350   4                                      #endif 
 351   4      
 352   4                              }
 353   3              
 354   3                              if(KbHit())             //0x39 is Tab 
 355   3                              {
 356   4                                      uchar ucKeyValue;
 357   4      
 358   4                                      if(ucStatusMac == 0)
 359   4                                      {
 360   5                                              ucKeyValue = KbGet();
 361   5                                              if(ucKeyValue  == c_KeyScan_ErrCode)
 362   5                                              {
 363   6                                                      continue;
 364   6                                              }
C51 COMPILER V7.50   U_KEYBOARD                                                            04/16/2007 15:22:52 PAGE 7   

 365   5              
 366   5                                              if(ucKeyValue & K1_SCANCODE)
 367   5                                              {
 368   6              
 369   6                                                      ucKeyboardDataBuff[2] = 0x39;           //x Target Code
 370   6                                              }
 371   5                                              else if(ucKeyValue & K2_SCANCODE)
 372   5                                              {
 373   6                                                      ucKeyboardDataBuff[2] = 0x1b;
 374   6                                      //              D12_WriteEndpoint(3, 8, ucKeyboardDataBuff);//发送按键状态
 375   6                                              }
 376   5                                              ComShowString(COM_1,"发送到Host的数据为:");
 377   5                                              ComSendText(COM_1,ucKeyboardDataBuff,8);
 378   5                                              D12_WriteEndpoint(3, 8, ucKeyboardDataBuff);//发送按键状态
 379   5                                              ucStatusMac = 1;
 380   5                                      }
 381   4                              }
 382   3                      }
 383   2              } // Main Loop
 384   1      }
 385          //重新连接到USB总线
 386          void reconnect_USB(void)
 387          {
 388   1              MCU_LED0 = 0;
 389   1              MCU_LED1 = 0;
 390   1      
 391   1              D12_DisConnect();
 392   1      
 393   1      //      printf("Wait for 1 second ...\n");
 394   1              D12_Connect();
 395   1      
 396   1              MCU_LED0 = 1;
 397   1              MCU_LED1 = 1;
 398   1      }
 399          
 400          
 401          //LED和按键处理子程序
 402          void check_key_LED(void)
 403          {
 404   1              static unsigned char c, last_key = 0xf;
 405   1      
 406   1              c = MCU_SWM0 & MCU_SWM1;         //无键按下时均为1
 407   1              c &= 0x0f;
 408   1              if (c != last_key) {
 409   2                      D12_WriteEndpoint(3, 1, &c);//发送按键状态
 410   2              }
 411   1      
 412   1              last_key = c;
 413   1      
 414   1              if(bEPPflags.bits.ep1_rxdone) 
 415   1              {
 416   2                      uchar  ucGetData;
 417   2      
 418   2                      DISABLE;
 419   2                      bEPPflags.bits.ep1_rxdone = 0;
 420   2                      ENABLE;
 421   2      
 422   2                      BeepShort();
 423   2                      ucGetData = GenEpBuf[3];
 424   2                      ComShowString(1, "从USB接收到的数据为:");
 425   2                      ComSendText (1,&ucGetData, 1);
 426   2      
C51 COMPILER V7.50   U_KEYBOARD                                                            04/16/2007 15:22:52 PAGE 8   

 427   2                      MCU_LED0 = !(GenEpBuf[3] & 0x1);//改变LED状态
 428   2                      MCU_LED1 = !(GenEpBuf[3] & 0x2);
 429   2              }
 430   1      }
 431          
 432          
 433          
 434          //请求处理子程序
 435          void control_handler()
 436          {
 437   1              unsigned char type, req;
 438   1      
 439   1              type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
 440   1              req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
 441   1      
 442   1      //      Disp_DevReq(type, req); //显示设备请求
 443   1      
 444   1              if (type == USB_STANDARD_REQUEST)
 445   1              {
 446   2                      (*StandardDeviceRequest[req])();//调用标准请求
 447   2              }
 448   1              else if (type == USB_VENDOR_REQUEST)
 449   1              {
 450   2                      (*VendorDeviceRequest[req])();//调用厂商请求
 451   2              }
 452   1              else if (type == USB_CLASS_REQUEST)
 453   1              {
 454   2                      (*ClassFunctionTable[req])();//调永嗵请求
 455   2              }
 456   1              else
 457   1              {
 458   2                      stall_ep0();
 459   2              }
 460   1      }
 461          
 462          void CloseAlarm(void)
 463          {
 464   1              Delay_1S(1);
 465   1              SetComBreakPoint(COM_1);
 466   1              Ready_Serial(COM_1);
 467   1              D12_ReadEndpointStatus(4);
 468   1      }
 469          
 470          void main(void)
 471          {
 472   1              uchar uci;
 473   1      
 474   1              CKCON |= 0X01;
 475   1      
 476   1      
 477   1      
 478   1              uci = 0;
 479   1              if(uci)
 480   1              {
 481   2                      CloseAlarm();   
 482   2              }
 483   1      
 484   1                      Open_Serial(COM_1,SER_BAUD_9600,SER_PARITY_NONE | SER_BITS_8 | SER_STOP_1);
 485   1      
 486   1              BeepLong();
 487   1              if(KbHit())
 488   1              {
C51 COMPILER V7.50   U_KEYBOARD                                                            04/16/2007 15:22:52 PAGE 9   

 489   2                      uchar ucKeyValue;
 490   2                      
 491   2                      ucKeyValue = KbGet();
 492   2                      if( (ucKeyValue & K1_SCANCODE) && (ucKeyValue & K2_SCANCODE) )
 493   2                      {
 494   3      
 495   3                              while(1)
 496   3                              {
 497   4                                      LedOn(LED1);
 498   4                                      LedOn(LED2);
 499   4                                      BeepShort();
 500   4                                      LedOff(LED1);
 501   4                                      LedOff(LED2);
 502   4                                      Pause (10);
 503   4      
 504   4                              }
 505   3      
 506   3                      }
 507   2              }
 508   1              ComShowString(COM_1,"系统复位");
 509   1              InitKbd();
 510   1      
 511   1              TestUsbMain();
 512   1              Close_Serial(COM_1);
 513   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1279    ----
   CONSTANT SIZE    =    208    ----
   XDATA SIZE       =     26      25
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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