找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4421|回复: 0
收起左侧

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

[复制链接]
ID:406093 发表于 2018-10-22 10:43 | 显示全部楼层 |阅读模式
本帖最后由 oldspring 于 2018-10-22 10:45 编辑

单片机的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 测试程序范例,希望对大家有帮助。

  1. program CDCDevice_Custom

  2. ' Buffer of 64 bytes
  3. dim buffer as byte[64] absolute 0x500
  4. dim dataReceived as byte
  5. dim dataReceivedSize as word

  6. sub procedure USBDev_CDCParamsChanged()
  7. end sub

  8. ' USB interrupt service routine
  9. sub procedure interrupt()
  10.   ' Call library interrupt handler routine
  11.   USBDev_IntHandler()
  12. end sub

  13. ' Callback function which will be called on received packet
  14. sub procedure USBDev_CDCDataReceived(dim size as word)
  15.   dataReceived = 1
  16.   dataReceivedSize = size
  17. end sub


  18. main:
  19.   dataReceived = 0

  20.   ADCON1 = ADCON1 or 0x0F  ' Configure all ports with analog function as digital
  21.   CMCON  = CMCON  or 7     ' Disable comparators

  22.   ' Initialize HID Class
  23.   USBDev_CDCInit()

  24.   ' Initialize USB device module
  25.   USBDev_Init()

  26.   ' Enable USB device interrupt
  27.   IPEN_bit = 1
  28.   USBIP_bit = 1
  29.   USBIE_bit = 1
  30.   GIEH_bit = 1

  31.   ' Infinite loop
  32.   while(1)
  33.       ' If device is configured
  34.       if USB_CDC_DeviceConfigured then
  35.         ' Prepare receive buffer
  36.         USBDev_CDCSetReceiveBuffer(@buffer)
  37.         ' Reset configured flag
  38.         USB_CDC_DeviceConfigured = false
  39.       end if
  40.       
  41.       if(dataReceived = 1) then
  42.         dataReceived = 0
  43.         ' Send back received packet
  44.         USBDev_CDCSendData(@buffer, dataReceivedSize)
  45.         ' Prepare receive buffer
  46.         USBDev_CDCSetReceiveBuffer(@buffer)
  47.       end if

  48.   wend

  49. end.
复制代码
  1. module CDC_Descriptor

  2. const _USB_CDC_INT_EP_IN as byte = 1    ' Communication interface IN endpoint
  3. const _USB_CDC_BULK_EP_IN as byte = 2   ' Data interface IN endpoint
  4. const _USB_CDC_BULK_EP_OUT as byte = 3  ' Data interface OUT endpoint

  5. const _USB_CDC_MANUFACTURER_STRING  as string[16] = "Mikroelektronika"
  6. const _USB_CDC_PRODUCT_STRING       as string[8] = "VCP Demo"
  7. const _USB_CDC_SERIALNUMBER_STRING  as string[10] = "0x00000004"
  8. const _USB_CDC_CONFIGURATION_STRING as string[22] = "CDC Config desc string"
  9. const _USB_CDC_INTERFACE_STRING     as string[25] = "CDC Interface desc string"

  10. const _USB_CDC_CONFIG_DESC_SIZ   as byte = 3*9 + 3*5 + 4 + 3*7

  11. 'String Descriptor Zero, Specifying Languages Supported by the Device
  12. const USB_CDC_LangIDDesc as byte[4] = (
  13.   0x04,
  14.   _USB_DEV_DESCRIPTOR_TYPE_STRING,
  15.   0x409 and 0xFF,
  16.   0x409 >> 8)

  17. ' device descriptor
  18. const USB_CDC_device_descriptor as byte[18] = (
  19.   0x12,       ' bLength
  20.   0x01,       ' bDescriptorType
  21.   0x00,       ' bcdUSB
  22.   0x02,
  23.   0x02,       ' bDeviceClass : CDC code
  24.   0x00,       ' bDeviceSubClass
  25.   0x00,       ' bDeviceProtocol
  26.   0x40,       ' bMaxPacketSize0
  27.   0x00, 0x00, ' idVendor
  28.   0x00, 0x04, ' idProduct
  29.   0x00,       ' bcdDevice
  30.   0x01,
  31.   0x01,       ' iManufacturer
  32.   0x02,       ' iProduct
  33.   0x03,       ' iSerialNumber
  34.   0x01        ' bNumConfigurations
  35.   )

  36. 'contain configuration descriptor, all interface descriptors, and endpoint
  37. 'descriptors for all of the interfaces
  38. const USB_CDC_cfg_descriptor as byte[_USB_CDC_CONFIG_DESC_SIZ] = (
  39.     ' Configuration Descriptor
  40.     0x09,   '  bLength: Configuration Descriptor size
  41.     0x02,   '  bDescriptorType: Configuration
  42.     _USB_CDC_CONFIG_DESC_SIZ,       '  wTotalLength: number of returned bytes
  43.     _USB_CDC_CONFIG_DESC_SIZ >> 8,
  44.     0x02,   '  bNumInterfaces: 2 interfaces
  45.     0x01,   '  bConfigurationValue: Configuration value
  46.     0x00,   '  iConfiguration: Index of string descriptor describing the configuration
  47.     0xC0,   '  bmAttributes: self powered
  48.     0x32,   '  bMaxPower: 100 mA

  49.     ' Interface Descriptor
  50.     0x09,   '  bLength: Interface Descriptor size
  51.     _USB_DEV_DESCRIPTOR_TYPE_INTERFACE,  '  bDescriptorType: Interface
  52.     0x00,   '  bInterfaceNumber: Number of Interface
  53.     0x00,   '  bAlternateSetting: Alternate setting
  54.     0x01,   '  bNumEndpoints: One endpoint used
  55.     0x02,   '  bInterfaceClass: Communication Interface Class
  56.     0x02,   '  bInterfaceSubClass: Abstract Control Model
  57.     0x01,   '  bInterfaceProtocol: AT commands
  58.     0x00,   '  iInterface: string descriptor index

  59.     ' Header Functional Descriptor
  60.     0x05,   '  bLength: Descriptor size
  61.     0x24,   '  bDescriptorType: CS_INTERFACE
  62.     0x00,   '  bDescriptorSubtype: Header Functional Descriptor
  63.     0x10,   '  bcdCDC: specification release number
  64.     0x01,

  65.     ' Call Management Functional Descriptor
  66.     0x05,   '  bFunctionLength: Descriptor size
  67.     0x24,   '  bDescriptorType: CS_INTERFACE
  68.     0x01,   '  bDescriptorSubtype: Call Management Functional descriptor
  69.     0x00,   '  bmCapabilities: Device does not handle call management itself
  70.     0x01,   '  bDataInterface: 1

  71.     ' Abstract Control Management Functional Descriptor
  72.     0x04,   '  bFunctionLength: Descriptor size
  73.     0x24,   '  bDescriptorType: CS_INTERFACE
  74.     0x02,   '  bDescriptorSubtype: Abstract Control Management descriptor
  75.     0x02,   '  bmCapabilities:  Device supports the request combination of
  76.             '  Set_Line_Coding, Set_Control_Line_State,
  77.             '  Get_Line_Coding, and the notification Serial_State

  78.     ' Union Functional Descriptor
  79.     0x05,   '  bFunctionLength: Descriptor size
  80.     0x24,   '  bDescriptorType: CS_INTERFACE
  81.     0x06,   '  bDescriptorSubtype: Union functional descriptor
  82.     0x00,   '  bMasterInterface: Communication class interface
  83.     0x01,   '  bSlaveInterface0: Data Class Interface

  84.     ' Interrupt IN Endpoint Descriptor
  85.     0x07,   '  bLength: Endpoint Descriptor size
  86.     _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT,   '  bDescriptorType: Endpoint
  87.     0x80 or _USB_CDC_INT_EP_IN,          '  bEndpointAddress
  88.     0x03,   '  bmAttributes: Interrupt
  89.     0x08,   '  wMaxPacketSize
  90.     0x00,
  91.     0xFF,   '  bInterval

  92.     ' Data class interface descriptor
  93.     0x09,   '  bLength: Endpoint Descriptor size
  94.     _USB_DEV_DESCRIPTOR_TYPE_INTERFACE,  '  bDescriptorType:
  95.     0x01,   '  bInterfaceNumber: Number of Interface
  96.     0x00,   '  bAlternateSetting: Alternate setting
  97.     0x02,   '  bNumEndpoints: Two endpoints used
  98.     0x0A,   '  bInterfaceClass: CDC
  99.     0x00,   '  bInterfaceSubClass
  100.     0x00,   '  bInterfaceProtocol
  101.     0x00,   '  iInterface

  102.     ' Bulk OUT Endpoint Descriptor
  103.     0x07,   '  bLength: Endpoint Descriptor size
  104.     _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT,   '  bDescriptorType: Endpoint
  105.     _USB_CDC_BULK_EP_OUT,                '  bEndpointAddress
  106.     0x02,   '  bmAttributes: Bulk
  107.     64,     '  wMaxPacketSize
  108.     0x00,
  109.     0x00,   '  bInterval: ignore for Bulk transfer

  110.     ' Bulk IN Endpoint Descriptor
  111.     0x07,   '  bLength: Endpoint Descriptor size
  112.     _USB_DEV_DESCRIPTOR_TYPE_ENDPOINT,   '  bDescriptorType: Endpoint
  113.     0x80 or _USB_CDC_BULK_EP_IN,         '  bEndpointAddress
  114.     0x02,   '  bmAttributes: Bulk
  115.     64,     '  wMaxPacketSize
  116.     0x00,
  117.     0x00    '  bInterval
  118. )
  119.   

  120. end.
复制代码

以下是USB CDC的驱动文件内容,开发者可以根据需要进行相关信息的修改。
  1. Signature="$Windows NT[        DISCUZ_CODE_2        ]quot;
  2. Class=Ports
  3. ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
  4. Provider=%ProviderName%
  5. DriverVer=04/30/2013,1.0.0.0
  6. CatalogFile.ntx86=VCPDriverX86.cat
  7. CatalogFile.ntamd64=VCPDriverX64.cat

  8. [MANUFACTURER]
  9. %ProviderName%=DeviceList, NTx86, NTamd64

  10. [DeviceList.NTx86]
  11. %MikroeCDC%=DriverInstall,USB\VID_0000&PID_0400

  12. [DeviceList.NTamd64]
  13. %MikroeCDC%=DriverInstall,USB\VID_0000&PID_0400

  14. [DriverInstall]
  15. include=mdmcpq.inf
  16. CopyFiles=FakeModemCopyFileSection
  17. AddReg=LowerFilterAddReg,SerialPropPageAddReg

  18. [DriverInstall.Services]
  19. include = mdmcpq.inf
  20. AddService = usbser, 0x00000002, LowerFilter_Service_Inst

  21. [SerialPropPageAddReg]
  22. HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

  23. [Strings]
  24. ProviderName = "MikroElektronika"
  25. MikroeCDC = "Mikroe Virtual Com Port"
复制代码


USB.jpg


VCPDriver.zip

6.55 KB, 下载次数: 12, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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