本帖最后由 oldspring 于 2018-10-22 10:45 編輯
單片機的USB接口,通常用法, 1)HID 是Human Interface Device的縮寫,由其名稱可以了解HID設備是直接與人交互的設備,例如鍵盤、鼠標與游戲桿等。不過HID設備并不一定要有人機接口,只要符合HID類別規(guī)范的設備都是HID設備。(參考百度 https://baike.baidu.com/item/USB-HID)
2)CDC 虛擬串口,可與PC機直接聯(lián)機通訊,如同RS232。
3)USB MSC (Mass Storageclass) MSC是一種計算機和移動設備之間的傳輸協(xié)議,它允許一個通用串行總線(USB)設備來訪問主機的計算設備,使兩者之間進行文件傳輸。設備包括:移動硬盤,移動光驅,U盤,SD、TF等儲存卡讀卡器,數碼相機,手機等等 ..........
注意: 1)每一個USB設備,都需要一個獨立的身份編碼 (ID),它由 2 組數字組成,一個是開發(fā)商代碼(Vender ID),另一個是產品代碼(Product ID)。如果是PIC使用者,可以向Microchip公司申請獲得免費的身份編碼。 2)USB CDC 虛擬串口接口的用法與其他的 USB 接口有所不同,它在聯(lián)接PC 時,Windows 操作系統(tǒng)會提示安裝驅動文件(xxxx.inf)。這個文件的基本內容包含USB CDC 的身份編碼 (ID)和開發(fā)者的有關信息。
以下介紹一個簡單的CDC 測試程序范例,希望對大家有幫助。
- program CDCDevice_Custom
- ' Buffer of 64 bytes
- dim buffer as byte[64] absolute 0x500
- dim dataReceived as byte
- dim dataReceivedSize as word
- sub procedure USBDev_CDCParamsChanged()
- end sub
- ' USB interrupt service routine
- sub procedure interrupt()
- ' Call library interrupt handler routine
- USBDev_IntHandler()
- end sub
- ' Callback function which will be called on received packet
- sub procedure USBDev_CDCDataReceived(dim size as word)
- dataReceived = 1
- dataReceivedSize = size
- end sub
- main:
- dataReceived = 0
- ADCON1 = ADCON1 or 0x0F ' Configure all ports with analog function as digital
- CMCON = CMCON or 7 ' Disable comparators
- ' Initialize HID Class
- USBDev_CDCInit()
- ' Initialize USB device module
- USBDev_Init()
- ' Enable USB device interrupt
- IPEN_bit = 1
- USBIP_bit = 1
- USBIE_bit = 1
- GIEH_bit = 1
- ' Infinite loop
- while(1)
- ' If device is configured
- if USB_CDC_DeviceConfigured then
- ' Prepare receive buffer
- USBDev_CDCSetReceiveBuffer(@buffer)
- ' Reset configured flag
- USB_CDC_DeviceConfigured = false
- end if
-
- if(dataReceived = 1) then
- dataReceived = 0
- ' Send back received packet
- USBDev_CDCSendData(@buffer, dataReceivedSize)
- ' Prepare receive buffer
- USBDev_CDCSetReceiveBuffer(@buffer)
- end if
- wend
- end.
復制代碼- module CDC_Descriptor
- const _USB_CDC_INT_EP_IN as byte = 1 ' Communication interface IN endpoint
- const _USB_CDC_BULK_EP_IN as byte = 2 ' Data interface IN endpoint
- const _USB_CDC_BULK_EP_OUT as byte = 3 ' Data interface OUT endpoint
- const _USB_CDC_MANUFACTURER_STRING as string[16] = "Mikroelektronika"
- const _USB_CDC_PRODUCT_STRING as string[8] = "VCP Demo"
- const _USB_CDC_SERIALNUMBER_STRING as string[10] = "0x00000004"
- const _USB_CDC_CONFIGURATION_STRING as string[22] = "CDC Config desc string"
- const _USB_CDC_INTERFACE_STRING as string[25] = "CDC Interface desc string"
- const _USB_CDC_CONFIG_DESC_SIZ as byte = 3*9 + 3*5 + 4 + 3*7
- 'String Descriptor Zero, Specifying Languages Supported by the Device
- const USB_CDC_LangIDDesc as byte[4] = (
- 0x04,
- _USB_DEV_DESCRIPTOR_TYPE_STRING,
- 0x409 and 0xFF,
- 0x409 >> 8)
- ' device descriptor
- const USB_CDC_device_descriptor as byte[18] = (
- 0x12, ' bLength
- 0x01, ' bDescriptorType
- 0x00, ' bcdUSB
- 0x02,
- 0x02, ' bDeviceClass : CDC code
- 0x00, ' bDeviceSubClass
- 0x00, ' bDeviceProtocol
- 0x40, ' bMaxPacketSize0
- 0x00, 0x00, ' idVendor
- 0x00, 0x04, ' 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 USB_CDC_cfg_descriptor as byte[_USB_CDC_CONFIG_DESC_SIZ] = (
- ' Configuration Descriptor
- 0x09, ' bLength: Configuration Descriptor size
- 0x02, ' bDescriptorType: Configuration
- _USB_CDC_CONFIG_DESC_SIZ, ' wTotalLength: number of returned bytes
- _USB_CDC_CONFIG_DESC_SIZ >> 8,
- 0x02, ' bNumInterfaces: 2 interfaces
- 0x01, ' bConfigurationValue: Configuration value
- 0x00, ' iConfiguration: Index of string descriptor describing the configuration
- 0xC0, ' bmAttributes: self powered
- 0x32, ' bMaxPower: 100 mA
- ' Interface Descriptor
- 0x09, ' bLength: Interface Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_INTERFACE, ' bDescriptorType: Interface
- 0x00, ' bInterfaceNumber: Number of Interface
- 0x00, ' bAlternateSetting: Alternate setting
- 0x01, ' bNumEndpoints: One endpoint used
- 0x02, ' bInterfaceClass: Communication Interface Class
- 0x02, ' bInterfaceSubClass: Abstract Control Model
- 0x01, ' bInterfaceProtocol: AT commands
- 0x00, ' iInterface: string descriptor index
- ' Header Functional Descriptor
- 0x05, ' bLength: Descriptor size
- 0x24, ' bDescriptorType: CS_INTERFACE
- 0x00, ' bDescriptorSubtype: Header Functional Descriptor
- 0x10, ' bcdCDC: specification release number
- 0x01,
- ' Call Management Functional Descriptor
- 0x05, ' bFunctionLength: Descriptor size
- 0x24, ' bDescriptorType: CS_INTERFACE
- 0x01, ' bDescriptorSubtype: Call Management Functional descriptor
- 0x00, ' bmCapabilities: Device does not handle call management itself
- 0x01, ' bDataInterface: 1
- ' Abstract Control Management Functional Descriptor
- 0x04, ' bFunctionLength: Descriptor size
- 0x24, ' bDescriptorType: CS_INTERFACE
- 0x02, ' bDescriptorSubtype: Abstract Control Management descriptor
- 0x02, ' bmCapabilities: Device supports the request combination of
- ' Set_Line_Coding, Set_Control_Line_State,
- ' Get_Line_Coding, and the notification Serial_State
- ' Union Functional Descriptor
- 0x05, ' bFunctionLength: Descriptor size
- 0x24, ' bDescriptorType: CS_INTERFACE
- 0x06, ' bDescriptorSubtype: Union functional descriptor
- 0x00, ' bMasterInterface: Communication class interface
- 0x01, ' bSlaveInterface0: Data Class Interface
- ' Interrupt IN Endpoint Descriptor
- 0x07, ' bLength: Endpoint Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT, ' bDescriptorType: Endpoint
- 0x80 or _USB_CDC_INT_EP_IN, ' bEndpointAddress
- 0x03, ' bmAttributes: Interrupt
- 0x08, ' wMaxPacketSize
- 0x00,
- 0xFF, ' bInterval
- ' Data class interface descriptor
- 0x09, ' bLength: Endpoint Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_INTERFACE, ' bDescriptorType:
- 0x01, ' bInterfaceNumber: Number of Interface
- 0x00, ' bAlternateSetting: Alternate setting
- 0x02, ' bNumEndpoints: Two endpoints used
- 0x0A, ' bInterfaceClass: CDC
- 0x00, ' bInterfaceSubClass
- 0x00, ' bInterfaceProtocol
- 0x00, ' iInterface
- ' Bulk OUT Endpoint Descriptor
- 0x07, ' bLength: Endpoint Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT, ' bDescriptorType: Endpoint
- _USB_CDC_BULK_EP_OUT, ' bEndpointAddress
- 0x02, ' bmAttributes: Bulk
- 64, ' wMaxPacketSize
- 0x00,
- 0x00, ' bInterval: ignore for Bulk transfer
- ' Bulk IN Endpoint Descriptor
- 0x07, ' bLength: Endpoint Descriptor size
- _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT, ' bDescriptorType: Endpoint
- 0x80 or _USB_CDC_BULK_EP_IN, ' bEndpointAddress
- 0x02, ' bmAttributes: Bulk
- 64, ' wMaxPacketSize
- 0x00,
- 0x00 ' bInterval
- )
-
- end.
復制代碼
以下是USB CDC的驅動文件內容,開發(fā)者可以根據需要進行相關信息的修改。
- Signature="$Windows NT[ DISCUZ_CODE_2 ]quot;
- Class=Ports
- ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
- Provider=%ProviderName%
- DriverVer=04/30/2013,1.0.0.0
- CatalogFile.ntx86=VCPDriverX86.cat
- CatalogFile.ntamd64=VCPDriverX64.cat
- [MANUFACTURER]
- %ProviderName%=DeviceList, NTx86, NTamd64
- [DeviceList.NTx86]
- %MikroeCDC%=DriverInstall,USB\VID_0000&PID_0400
- [DeviceList.NTamd64]
- %MikroeCDC%=DriverInstall,USB\VID_0000&PID_0400
- [DriverInstall]
- include=mdmcpq.inf
- CopyFiles=FakeModemCopyFileSection
- AddReg=LowerFilterAddReg,SerialPropPageAddReg
- [DriverInstall.Services]
- include = mdmcpq.inf
- AddService = usbser, 0x00000002, LowerFilter_Service_Inst
- [SerialPropPageAddReg]
- HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
- [Strings]
- ProviderName = "MikroElektronika"
- MikroeCDC = "Mikroe Virtual Com Port"
復制代碼
USB.jpg (34.16 KB, 下載次數: 62)
下載附件
2018-10-22 10:40 上傳
|