單片機的USB接口,通常用法:
USB.jpg (34.16 KB, 下載次數: 94)
下載附件
2018-10-20 10:18 上傳
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
- };
復制代碼
|