单片机 > 单片机程序设计 > 详情

PIC单片机的USB接口的应用 一个简单的USB CDC 测试程序

发布时间:2020-06-09 发布时间:
|

单片机的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 Storageclass) MSC是一种计算机和移动设备之间的传输协议,它允许一个通用串行总线(USB)设备来访问主机的计算设备,使两者之间进行文件传输。设备包括:移动硬盘,移动光驱,U盘,SD、TF等储存卡读卡器,数码相机,手机等等

..........

注意:

1)每一个USB设备,都需要一个独立的身份编码 (ID),它由 2 组数字组成,一个是开发商代码(Vender ID),另一个是产品代码(Product ID)。如果是PIC使用者,可以向Microchip公司申请获得免费的身份编码。

2)USB CDC 虚拟串口接口的用法与其他的 USB 接口有所不同,它在联接PC 时,Windows 操作系统会提示安装驱动文件(xxxx.inf)。这个文件的基本内容包含USB CDC 的身份编码 (ID)和开发者的有关信息。


以下介绍一个简单的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,


关键字:PIC单片机  USB接口  CDC 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
C8051F020的UART