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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5600|回復: 2
收起左側

PIC單片機的USB接口的應用 一個簡單的USB HID 測試程序

[復制鏈接]
ID:406093 發表于 2018-10-20 10:17 | 顯示全部樓層 |閱讀模式
     單片機的USB接口,通常用法:
USB.jpg
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. };

復制代碼




回復

使用道具 舉報

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

使用道具 舉報

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本一级黄色 | 久久久一区二区三区 | 曰韩一级片| 欧美日韩免费在线 | 成人免费视频视频 | 在线一区二区三区四区 | 久久久二区 | 日皮视频在线观看 | 久久免费视频网站 | 国产黄色在线播放 | 国产一级黄色大片 | 亚洲激情网 | 精品国产一区二区三区四区 | 国产三级在线免费观看 | 欧美三级韩国三级日本三斤在线观看 | 日韩精品视频免费在线观看 | 91成人在线观看喷潮蘑菇 | 涩涩久久 | 午夜精品一区二区三区在线播放 | 午夜爽爽影院 | 国产精品理论片 | 神马午夜视频 | 日韩在线资源 | 欧美激情亚洲 | 国产高清在线视频 | 丨国产丨调教丨91丨 | 国产永久免费视频 | 午夜看看| 黄网站免费大全入口 | 神马九九 | 成人在线免费视频 | 黄色片国产| 日本黄色a级片 | 久久精品视频一区二区 | 伊人国产精品 | 欧美在线视频一区二区 | 国产精品麻豆免费版 | 日本免费黄色网址 | 国产精品麻豆 | 国产黄色片在线观看 | 四虎影视库 |