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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5525|回復: 2
打印 上一主題 下一主題
收起左側

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. };

復制代碼




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
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色视频在线 | 欧美日韩一 | 亚洲视频www | 91美女在线观看 | 久久精品91久久久久久再现 | av中文字幕在线播放 | 高清视频一区 | 国产三区视频在线观看 | 成人免费激情视频 | 日韩精品一区二区三区 | 另类亚洲视频 | 成人欧美一区二区三区白人 | 亚洲a视频 | 一级毛片免费视频观看 | 久久精品网 | 九色视频网站 | 亚洲精品中文字幕av | 欧美专区在线 | 久久精品亚洲欧美日韩精品中文字幕 | 国产男女猛烈无遮掩视频免费网站 | 久久久www成人免费无遮挡大片 | 久久草视频 | 一区二区三区四区在线视频 | 国产成人精品a视频一区www | 日韩精品在线看 |