找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 5421|回復(fù): 2
收起左側(cè)

PIC單片機(jī)的USB接口的應(yīng)用 一個(gè)簡(jiǎn)單的USB HID 測(cè)試程序

[復(fù)制鏈接]
ID:406093 發(fā)表于 2018-10-20 10:17 | 顯示全部樓層 |閱讀模式
     單片機(jī)的USB接口,通常用法:
USB.jpg
1)HID  是Human Interface Device的縮寫,由其名稱可以了解HID設(shè)備是直接與人交互的設(shè)備,例如鍵盤、鼠標(biāo)與游戲桿等。不過HID設(shè)備并不一定要有人機(jī)接口,只要符合HID類別規(guī)范的設(shè)備都是HID設(shè)備。(參考百度 https://baike.baidu.com/item/USB-HID
2)CDC 虛擬串口,可與PC機(jī)直接聯(lián)機(jī)通訊,如同RS232。
3)USB MSC (Mass Storage class) MSC是一種計(jì)算機(jī)和移動(dòng)設(shè)備之間的傳輸協(xié)議,它允許一個(gè)通用串行總線(USB)設(shè)備來訪問主機(jī)的計(jì)算設(shè)備,使兩者之間進(jìn)行文件傳輸。設(shè)備包括:移動(dòng)硬盤,移動(dòng)光驅(qū),U盤,SD、TF等儲(chǔ)存卡讀卡器,數(shù)碼相機(jī),手機(jī)等等
..........

注意:每一個(gè)USB設(shè)備,都需要一個(gè)獨(dú)立的身份編碼 (ID),它由 2 組數(shù)字組成,一個(gè)是開發(fā)商代碼(Vender ID),另一個(gè)是產(chǎn)品代碼(Product ID)。如果是PIC使用者,可以向Microchip公司申請(qǐng)獲得免費(fèi)的身份編碼。

以下介紹一個(gè)簡(jiǎn)單的HID 測(cè)試程序范例,希望對(duì)大家有幫助。

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]
復(fù)制代碼
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. };

復(fù)制代碼

詳細(xì)內(nèi)容,請(qǐng)參考:http://www.torrancerestoration.com/bbs/dpj-138111-1.html



回復(fù)

使用道具 舉報(bào)

ID:1 發(fā)表于 2018-10-20 14:23 | 顯示全部樓層
好資料,51黑有你更精彩!!!
回復(fù)

使用道具 舉報(bào)

ID:72238 發(fā)表于 2018-10-20 16:03 | 顯示全部樓層
好資料,謝謝分享.
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表