久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: PIC單片機的USB接口的應用 一個簡單的USB HID 測試程序 [打印本頁]

作者: oldspring    時間: 2018-10-20 10:17
標題: PIC單片機的USB接口的應用 一個簡單的USB HID 測試程序
     單片機的USB接口,通常用法:

1)HID  是Human Interface Device的縮寫,由其名稱可以了解HID設備是直接與人交互的設備,例如鍵盤、鼠標與游戲桿等。不過HID設備并不一定要有人機接口,只要符合HID類別規范的設備都是HID設備。(參考百度 https://baike.baidu.com/item/USB-HID
2)CDC 虛擬串口,可與PC機直接聯機通訊,如同RS232。
3)USB MSC (Mass Storage class) MSC是一種計算機和移動設備之間的傳輸協議,它允許一個通用串行總線(USB)設備來訪問主機的計算設備,使兩者之間進行文件傳輸。設備包括:移動硬盤移動光驅,U盤,SD、TF等儲存卡讀卡器,數碼相機,手機等等
..........

注意:每一個USB設備,都需要一個獨立的身份編碼 (ID),它由 2 組數字組成,一個是開發商代碼(Vender ID),另一個是產品代碼(Product ID)。如果是PIC使用者,可以向Microchip公司申請獲得免費的身份編碼

以下介紹一個簡單的HID 測試程序范例,希望對大家有幫助。

HID Custom Demo
  1. [font=Tahoma][size=2]/*
  2. * Project name:
  3.      HID Custom Demo
  4. * Description
  5.      Example showing usage of USB custom HID class. Attach usb cable in order to connect
  6.      development board to PC. After connection, the board is recognized as USB HID
  7.      device. Open HID Terminal from Tools menu and select HID Custom Demo device. When
  8.      sending data to device, data is echoed and result is displayed in the terminal
  9.      window.
  10. * Test configuration:
  11.      MCU:             P18F87J50
  12.      dev.board:       MikroMMB_for_PIC18FJ_hw_rev_1.10
  13.                       [url]http://www.mikroe.com/mikromedia/pic18fj/[/url]
  14.      Oscillator:      HS-PLL, 48.000MHz
  15.      Ext. Modules:    None.
  16.      SW:              mikroC PRO for PIC
  17.                       [url]http://www.mikroe.com/mikroc/pic/[/url]
  18. */

  19. #include <stdint.h>

  20. // Buffer of 64 bytes
  21. char buffer[64] absolute 0x500;
  22. volatile char dataReceivedFlag = 0;

  23. void interrupt(){
  24.   // Call library interrupt handler routine
  25.   USBDev_IntHandler();
  26. }

  27. // USB Device callback function called for various events
  28. void USBDev_EventHandler(uint8_t event) {
  29. //--------------------- User code ---------------------//
  30. }

  31. // USB Device callback function called when packet received
  32. void USBDev_DataReceivedHandler(uint8_t ep, uint16_t size) {
  33.   dataReceivedFlag = 1;
  34. }

  35. // USB Device callback function called when packet is sent
  36. void USBDev_DataSentHandler(uint8_t ep) {
  37. //--------------------- User code ---------------------//
  38. }

  39. void main(void){
  40.   PLLEN_bit=1;                           // PLL turned on
  41.   Delay_ms(150);                         // wait for a while to oscillator stabilizes

  42.   ANCON0 = 0xFF;                         // Default all pins to digital
  43.   ANCON1 = 0xFF;

  44.   // Initialize HID Class
  45.   USBDev_HIDInit();
  46.   
  47.   // Initialize USB device module
  48.   USBDev_Init();

  49.   // Enable USB device interrupt
  50.   IPEN_bit = 1;
  51.   USBIP_bit = 1;
  52.   USBIE_bit = 1;
  53.   GIEH_bit = 1;

  54.   // Wait until device is configured (enumeration is successfully finished)
  55.   while(USBDev_GetDeviceState() != _USB_DEV_STATE_CONFIGURED)
  56.     ;

  57.   // Set receive buffer where received data is stored
  58.   USBDev_SetReceiveBuffer(1, buffer);

  59.   // Infinite loop
  60.   while(1){
  61.     if(dataReceivedFlag){
  62.       dataReceivedFlag = 0;
  63.       // Send 64 bytes of data from buffer buff
  64.       USBDev_SendPacket(1, buffer, 64);

  65.       // Prepere buffer for reception of next packet
  66.       USBDev_SetReceiveBuffer(1, buffer);
  67.     }

  68.   }

  69. }[/size][/font]
復制代碼
HID_Descriptor.c
  1. /*
  2. * Project name
  3.      HID Custom Demo
  4. * Project file
  5.      HID_Descriptor.c
  6. */

  7. #include <stdint.h>

  8. const uint8_t _USB_HID_MANUFACTURER_STRING[]  = "Mikroelektronika";
  9. const uint8_t _USB_HID_PRODUCT_STRING[]       = "HID Custom Demo";
  10. const uint8_t _USB_HID_SERIALNUMBER_STRING[]  = "0x00000003";
  11. const uint8_t _USB_HID_CONFIGURATION_STRING[] = "HID Config desc string";
  12. const uint8_t _USB_HID_INTERFACE_STRING[]     = "HID Interface desc string";

  13. // Sizes of various descriptors
  14. const uint8_t _USB_HID_CONFIG_DESC_SIZ   = 34+7;
  15. const uint8_t _USB_HID_DESC_SIZ          = 9;
  16. const uint8_t _USB_HID_REPORT_DESC_SIZE  = 33;
  17. const uint8_t _USB_HID_DESCRIPTOR_TYPE   = 0x21;

  18. // Endpoint max packte size
  19. const uint8_t _USB_HID_IN_PACKET  = 64;
  20. const uint8_t _USB_HID_OUT_PACKET = 64;

  21. // Endpoint address
  22. const uint8_t _USB_HID_IN_EP      = 0x81;
  23. const uint8_t _USB_HID_OUT_EP     = 0x01;

  24. //String Descriptor Zero, Specifying Languages Supported by the Device
  25. const uint8_t USB_HID_LangIDDesc[0x04] = {
  26.   0x04,
  27.   _USB_DEV_DESCRIPTOR_TYPE_STRING,
  28.   0x409 & 0xFF,
  29.   0x409 >> 8,
  30. };


  31. // device descriptor
  32. const uint8_t USB_HID_device_descriptor[] = {
  33.   0x12,       // bLength
  34.   0x01,       // bDescriptorType
  35.   0x00,       // bcdUSB
  36.   0x02,
  37.   0x00,       // bDeviceClass
  38.   0x00,       // bDeviceSubClass
  39.   0x00,       // bDeviceProtocol
  40.   0x40,       // bMaxPacketSize0
  41.   0x00, 0x00, // idVendor
  42.   0x00, 0x03, // idProduct
  43.   0x00,       // bcdDevice
  44.   0x01,
  45.   0x01,       // iManufacturer
  46.   0x02,       // iProduct
  47.   0x03,       // iSerialNumber
  48.   0x01        // bNumConfigurations

  49. };

  50. //contain configuration descriptor, all interface descriptors, and endpoint
  51. //descriptors for all of the interfaces
  52. const uint8_t USB_HID_cfg_descriptor[_USB_HID_CONFIG_DESC_SIZ] = {
  53.   // Configuration descriptor
  54.   0x09,                                   // bLength: Configuration Descriptor size
  55.   _USB_DEV_DESCRIPTOR_TYPE_CONFIGURATION, // bDescriptorType: Configuration
  56.   _USB_HID_CONFIG_DESC_SIZ & 0xFF,        // wTotalLength: Bytes returned
  57.   _USB_HID_CONFIG_DESC_SIZ >> 8,          // wTotalLength: Bytes returned
  58.   0x01,                                   // bNumInterfaces: 1 interface
  59.   0x01,                                   // bConfigurationValue: Configuration value
  60.   0x04,                                   // iConfiguration: Index of string descriptor describing  the configuration
  61.   0xE0,                                   // bmAttributes: self powered and Support Remote Wake-up
  62.   0x32,                                   // MaxPower 100 mA: this current is used for detecting Vbus

  63.   // Interface Descriptor
  64.   0x09,                                   // bLength: Interface Descriptor size
  65.   0x04,                                   // bDescriptorType: Interface descriptor type
  66.   0x00,                                   // bInterfaceNumber: Number of Interface
  67.   0x00,                                   // bAlternateSetting: Alternate setting
  68.   0x02,                                   // bNumEndpoints
  69.   0x03,                                   // bInterfaceClass: HID
  70.   0x00,                                   // bInterfaceSubClass : 1=BOOT, 0=no boot
  71.   0x00,                                   // nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse
  72.   5,                                      // iInterface: Index of string descriptor

  73.   // HID Descriptor
  74.   0x09,                                   // bLength: HID Descriptor size
  75.   _USB_HID_DESCRIPTOR_TYPE,               // bDescriptorType: HID
  76.   0x01,                                   // bcdHID: HID Class Spec release number
  77.   0x01,
  78.   0x00,                                   // bCountryCode: Hardware target country
  79.   0x01,                                   // bNumDescriptors: Number of HID class descriptors to follow
  80.   0x22,                                   // bDescriptorType
  81.   _USB_HID_REPORT_DESC_SIZE,              // wItemLength: Total length of Report descriptor
  82.   0x00,

  83.   // Endpoint descriptor
  84.   0x07,                                   // bLength: Endpoint Descriptor size
  85.   _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT,      // bDescriptorType:
  86.   _USB_HID_IN_EP,                         // bEndpointAddress: Endpoint Address (IN)
  87.   0x03,                                   // bmAttributes: Interrupt endpoint
  88.   _USB_HID_IN_PACKET,                     // wMaxPacketSize
  89.   0x00,
  90.   0x0A,                                   // bInterval: Polling Interval (10 ms)

  91.   // Endpoint descriptor
  92.   0x07,                                   // bLength: Endpoint Descriptor size
  93.   _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT,      // bDescriptorType:
  94.   _USB_HID_OUT_EP,                        // bEndpointAddress: Endpoint Address (IN)
  95.   0x03,                                   // bmAttributes: Interrupt endpoint
  96.   _USB_HID_IN_PACKET,                     // wMaxPacketSize
  97.   0x00,
  98.   0x0A                                    // bInterval: Polling Interval (10 ms)

  99. };

  100. // HID report descriptor
  101. const uint8_t USB_HID_ReportDesc[_USB_HID_REPORT_DESC_SIZE] ={
  102.      0x06, 0x00, 0xFF,       // Usage Page = 0xFF00 (Vendor Defined Page 1)
  103.       0x09, 0x01,             // Usage (Vendor Usage 1)
  104.       0xA1, 0x01,             // Collection (Application)
  105.   // Input report
  106.       0x19, 0x01,             // Usage Minimum
  107.       0x29, 0x40,             // Usage Maximum
  108.       0x15, 0x00,             // Logical Minimum (data bytes in the report may have minimum value = 0x00)
  109.       0x26, 0xFF, 0x00,       // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
  110.       0x75, 0x08,             // Report Size: 8-bit field size
  111.       0x95, 64,               // Report Count
  112.       0x81, 0x02,             // Input (Data, Array, Abs)
  113.   // Output report
  114.       0x19, 0x01,             // Usage Minimum
  115.       0x29, 0x40,             // Usage Maximum
  116.       0x75, 0x08,             // Report Size: 8-bit field size
  117.       0x95, 64,               // Report Count
  118.       0x91, 0x02,             // Output (Data, Array, Abs)
  119.       0xC0                   // End Collection
  120. };

復制代碼

詳細內容,請參考:http://m.zg4o1577.cn/bbs/dpj-138111-1.html




作者: admin    時間: 2018-10-20 14:23
好資料,51黑有你更精彩!!!
作者: fuermalin    時間: 2018-10-20 16:03
好資料,謝謝分享.




歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 欧美精品一区二区三区在线播放 | 国产91在线视频 | 亚洲精品99 | 日韩欧美综合 | 日韩黄色av | 国产91在线 | 欧美 | 一区二区在线看 | 日本一区二区视频 | 亚洲欧美激情四射 | 国产午夜精品一区二区三区嫩草 | 一区网站 | 国产乱码精品一区二区三区忘忧草 | 亚洲日本欧美 | 黄视频国产 | 99riav国产一区二区三区 | 精品日韩 | 亚洲婷婷一区 | 日韩五月天 | 天堂视频一区 | 四虎影院免费在线播放 | 欧美视频福利 | 亚洲精品一 | 免费观看一级特黄欧美大片 | aa级毛片毛片免费观看久 | 亚洲天堂影院 | 免费观看一级特黄欧美大片 | 少妇精品亚洲一区二区成人 | 91av视频在线免费观看 | 伊伊综合网 | 日韩午夜一区二区三区 | 亚洲国产欧美国产综合一区 | 久久久精 | 91在线精品秘密一区二区 | 亚洲啊v在线 | 2020国产在线 | 国产欧美精品区一区二区三区 | 久久久久久成人网 | 日韩精品国产精品 | 精品福利视频一区二区三区 | 性高朝久久久久久久3小时 av一区二区三区四区 | 91视频观看|