找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4361|回复: 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 单片机教程网

快速回复 返回顶部 返回列表