单片机的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
- [font=Tahoma][size=2]/*
- * Project name:
- HID Custom Demo
- * Description
- Example showing usage of USB custom HID class. Attach usb cable in order to connect
- development board to PC. After connection, the board is recognized as USB HID
- device. Open HID Terminal from Tools menu and select HID Custom Demo device. When
- sending data to device, data is echoed and result is displayed in the terminal
- window.
- * Test configuration:
- MCU: P18F87J50
- dev.board: MikroMMB_for_PIC18FJ_hw_rev_1.10
- [url]http://www.mikroe.com/mikromedia/pic18fj/[/url]
- Oscillator: HS-PLL, 48.000MHz
- Ext. Modules: None.
- SW: mikroC PRO for PIC
- [url]http://www.mikroe.com/mikroc/pic/[/url]
- */
- #include <stdint.h>
- // Buffer of 64 bytes
- char buffer[64] absolute 0x500;
- volatile char dataReceivedFlag = 0;
- void interrupt(){
- // Call library interrupt handler routine
- USBDev_IntHandler();
- }
- // USB Device callback function called for various events
- void USBDev_EventHandler(uint8_t event) {
- //--------------------- User code ---------------------//
- }
- // USB Device callback function called when packet received
- void USBDev_DataReceivedHandler(uint8_t ep, uint16_t size) {
- dataReceivedFlag = 1;
- }
- // USB Device callback function called when packet is sent
- void USBDev_DataSentHandler(uint8_t ep) {
- //--------------------- User code ---------------------//
- }
- void main(void){
- PLLEN_bit=1; // PLL turned on
- Delay_ms(150); // wait for a while to oscillator stabilizes
- ANCON0 = 0xFF; // Default all pins to digital
- ANCON1 = 0xFF;
- // Initialize HID Class
- USBDev_HIDInit();
-
- // Initialize USB device module
- USBDev_Init();
-
- // Enable USB device interrupt
- IPEN_bit = 1;
- USBIP_bit = 1;
- USBIE_bit = 1;
- GIEH_bit = 1;
- // Wait until device is configured (enumeration is successfully finished)
- while(USBDev_GetDeviceState() != _USB_DEV_STATE_CONFIGURED)
- ;
-
- // Set receive buffer where received data is stored
- USBDev_SetReceiveBuffer(1, buffer);
- // Infinite loop
- while(1){
- if(dataReceivedFlag){
- dataReceivedFlag = 0;
- // Send 64 bytes of data from buffer buff
- USBDev_SendPacket(1, buffer, 64);
- // Prepere buffer for reception of next packet
- USBDev_SetReceiveBuffer(1, buffer);
- }
-
- }
- }[/size][/font]
复制代码 HID_Descriptor.c
- /*
- * Project name
- HID Custom Demo
- * Project file
- HID_Descriptor.c
- */
- #include <stdint.h>
- const uint8_t _USB_HID_MANUFACTURER_STRING[] = "Mikroelektronika";
- const uint8_t _USB_HID_PRODUCT_STRING[] = "HID Custom Demo";
- const uint8_t _USB_HID_SERIALNUMBER_STRING[] = "0x00000003";
- const uint8_t _USB_HID_CONFIGURATION_STRING[] = "HID Config desc string";
- const uint8_t _USB_HID_INTERFACE_STRING[] = "HID Interface desc string";
- // Sizes of various descriptors
- const uint8_t _USB_HID_CONFIG_DESC_SIZ = 34+7;
- const uint8_t _USB_HID_DESC_SIZ = 9;
- const uint8_t _USB_HID_REPORT_DESC_SIZE = 33;
- const uint8_t _USB_HID_DESCRIPTOR_TYPE = 0x21;
- // Endpoint max packte size
- const uint8_t _USB_HID_IN_PACKET = 64;
- const uint8_t _USB_HID_OUT_PACKET = 64;
- // Endpoint address
- const uint8_t _USB_HID_IN_EP = 0x81;
- const uint8_t _USB_HID_OUT_EP = 0x01;
- //String Descriptor Zero, Specifying Languages Supported by the Device
- const uint8_t USB_HID_LangIDDesc[0x04] = {
- 0x04,
- _USB_DEV_DESCRIPTOR_TYPE_STRING,
- 0x409 & 0xFF,
- 0x409 >> 8,
- };
- // device descriptor
- const uint8_t USB_HID_device_descriptor[] = {
- 0x12, // bLength
- 0x01, // bDescriptorType
- 0x00, // bcdUSB
- 0x02,
- 0x00, // bDeviceClass
- 0x00, // bDeviceSubClass
- 0x00, // bDeviceProtocol
- 0x40, // bMaxPacketSize0
- 0x00, 0x00, // idVendor
- 0x00, 0x03, // idProduct
- 0x00, // bcdDevice
- 0x01,
- 0x01, // iManufacturer
- 0x02, // iProduct
- 0x03, // iSerialNumber
- 0x01 // bNumConfigurations
- };
- //contain configuration descriptor, all interface descriptors, and endpoint
- //descriptors for all of the interfaces
- const uint8_t USB_HID_cfg_descriptor[_USB_HID_CONFIG_DESC_SIZ] = {
- // Configuration descriptor
- 0x09, // bLength: Configuration Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_CONFIGURATION, // bDescriptorType: Configuration
- _USB_HID_CONFIG_DESC_SIZ & 0xFF, // wTotalLength: Bytes returned
- _USB_HID_CONFIG_DESC_SIZ >> 8, // wTotalLength: Bytes returned
- 0x01, // bNumInterfaces: 1 interface
- 0x01, // bConfigurationValue: Configuration value
- 0x04, // iConfiguration: Index of string descriptor describing the configuration
- 0xE0, // bmAttributes: self powered and Support Remote Wake-up
- 0x32, // MaxPower 100 mA: this current is used for detecting Vbus
- // Interface Descriptor
- 0x09, // bLength: Interface Descriptor size
- 0x04, // bDescriptorType: Interface descriptor type
- 0x00, // bInterfaceNumber: Number of Interface
- 0x00, // bAlternateSetting: Alternate setting
- 0x02, // bNumEndpoints
- 0x03, // bInterfaceClass: HID
- 0x00, // bInterfaceSubClass : 1=BOOT, 0=no boot
- 0x00, // nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse
- 5, // iInterface: Index of string descriptor
- // HID Descriptor
- 0x09, // bLength: HID Descriptor size
- _USB_HID_DESCRIPTOR_TYPE, // bDescriptorType: HID
- 0x01, // bcdHID: HID Class Spec release number
- 0x01,
- 0x00, // bCountryCode: Hardware target country
- 0x01, // bNumDescriptors: Number of HID class descriptors to follow
- 0x22, // bDescriptorType
- _USB_HID_REPORT_DESC_SIZE, // wItemLength: Total length of Report descriptor
- 0x00,
- // Endpoint descriptor
- 0x07, // bLength: Endpoint Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT, // bDescriptorType:
- _USB_HID_IN_EP, // bEndpointAddress: Endpoint Address (IN)
- 0x03, // bmAttributes: Interrupt endpoint
- _USB_HID_IN_PACKET, // wMaxPacketSize
- 0x00,
- 0x0A, // bInterval: Polling Interval (10 ms)
- // Endpoint descriptor
- 0x07, // bLength: Endpoint Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT, // bDescriptorType:
- _USB_HID_OUT_EP, // bEndpointAddress: Endpoint Address (IN)
- 0x03, // bmAttributes: Interrupt endpoint
- _USB_HID_IN_PACKET, // wMaxPacketSize
- 0x00,
- 0x0A // bInterval: Polling Interval (10 ms)
- };
- // HID report descriptor
- const uint8_t USB_HID_ReportDesc[_USB_HID_REPORT_DESC_SIZE] ={
- 0x06, 0x00, 0xFF, // Usage Page = 0xFF00 (Vendor Defined Page 1)
- 0x09, 0x01, // Usage (Vendor Usage 1)
- 0xA1, 0x01, // Collection (Application)
- // Input report
- 0x19, 0x01, // Usage Minimum
- 0x29, 0x40, // Usage Maximum
- 0x15, 0x00, // Logical Minimum (data bytes in the report may have minimum value = 0x00)
- 0x26, 0xFF, 0x00, // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
- 0x75, 0x08, // Report Size: 8-bit field size
- 0x95, 64, // Report Count
- 0x81, 0x02, // Input (Data, Array, Abs)
- // Output report
- 0x19, 0x01, // Usage Minimum
- 0x29, 0x40, // Usage Maximum
- 0x75, 0x08, // Report Size: 8-bit field size
- 0x95, 64, // Report Count
- 0x91, 0x02, // Output (Data, Array, Abs)
- 0xC0 // End Collection
- };
复制代码
|