历史上的今天
今天是:2024年11月06日(星期三)
2019年11月06日 | PIC单片机的USB接口的应用 一个简单的USB CDC 测试程序
2019-11-06 来源:51hei
单片机的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, ' 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的驱动文件内容,开发者可以根据需要进行相关信息的修改。
Signature="$Windows NT[ DISCUZ_CODE_2 ]quot;
史海拾趣
|
用DS18B20制作的网络型温度传感器资料这是用AT89C2051和DS18B20制作的网络型温度传感器,可以用RS232总线向电脑发送测量的温度,或用RS485总线实现网络通信。压缩包中包含有用Protel画的原理图,源程序,说明书和实物图片。特别是DS18B20的测量程序 ...… 查看全部问答> |
|
五一期间没有闲着,除了5月2日看望父母以外,其余时间都学习LPC1343。经过2天努力,总算对LPC1434有一个大概了解。 今天把学习笔记整理出来,和大家共享。其中免不了对原文的误解,诚心的请大家指正。有些 ...… 查看全部问答> |
|
求救 fatal error RC1015: cannot open include file 'winver.h'. 各位大人 我刚接触wince 今天装好平台 运行hello world 编译就报错 fatal error RC1015: cannot open include file \'winver.h\'. 请高手指点下… 查看全部问答> |
|
紧急求助:开发应用程序要访问USB设备,如何知道该USB2.0设备是否只支持全速呢 1。若主机支持USB2.0,当高速USB设备连接后是否肯定当前工作速度是高速呢?能否人为切换到全速呢? 2。主机是否会检测到USB2.0设备不能支持高速,并将此信息记录在描述符或regedit中呢? 3。若主机支持USB2.0, 且设备当前工作速度是全速,能否断 ...… 查看全部问答> |
|
CE上如何调用Smartphones下的Connection Manager函数,如ConnMgrEnumDestinations 我想调用ConnMgrEnumDestinations、ConnMgrMapURL这些函数。 查阅MSDN,要求是 Smartphone: Windows Mobile 2002 and later OS Versions: Windows CE 3.0 and later Header: connmgr.h Library: cellcore.lib 所以在工程中加入connmgr.h、cel ...… 查看全部问答> |
|
,LED应用产品尤其是半导体照明产品对大功率LED需求越来越旺,同时对LED的品质要求也越来越高,其主要表现在以下几个方面: 1.正向电压测试:正向电压的范围需在电路设计的许可范围内,很多客户设计驱动发光管点亮都以电压方式电量,正向电压 ...… 查看全部问答> |
|
做了个小东西,CPLD和单片机之间通信,刚开始弄好后,运行很好, 不管怎么断电,复位等都不会出什么问题,但是东西放一点时间后,再开机 就运行不正常了,是CPLD中的问题,感觉程序跑飞了一样,不知是什么 ...… 查看全部问答> |




