历史上的今天
今天是:2024年11月06日(星期三)
2019年11月06日 | PIC单片机的USB接口的应用 一个简单的USB HID 测试程序
2019-11-06 来源:51hei
1)HID 是Human Interface Device的缩写,由其名称可以了解HID设备是直接与人交互的设备,例如键盘、鼠标与游戏杆等。不过HID设备并不一定要有人机接口,只要符合HID类别规范的设备都是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 // 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); } } } HID_Descriptor.c /* * Project name HID Custom Demo * Project file HID_Descriptor.c */ #include 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
史海拾趣
|
本帖最后由 jameswangsynnex 于 2015-3-3 19:57 编辑 (文中S—发射端,R—接收端) 今日有一让大家的使用手机而无需缴纳话费的设想,愿与大家共同分享,也希望其中的一些尚未解决之处大家集思广益。 首先,为什么我们要向移动、联通等运营商 ...… 查看全部问答> |
|
本开发板命名为Landbcm 2008 下面开始介绍介绍Landbcm 2008 1.Landbcm 2008系统框图: 1.Landbcm 2008学习板提供了 硬件: 1个WAN口, 1个100M以太网口, 4M Flash,16M SDRAM , 主控芯片为BCM47XX , 交换芯片为BCM5XXX 软件: 全套开 ...… 查看全部问答> |
|
网上有文章说,使用从CWnd派生类中的GetDC()和ReleaseDC()会出现内存泄漏,不知道是否属实? 我使用以下代码进行测试: 使用一Timer,进行窗口Invalidate()。 void CTestDCDlg::OnPaint() { //CPaintDC dc(this); // device con ...… 查看全部问答> |
|
数据库同步 SqlCeRemoteDataAccess.Pull 里面的trackoption 参数设置问题 需要同步PDA和服务器数据库。用SQLCE3.0 SqlCeRemoteDataAccess.Pull方法可以下载服务器数据库的数据到PDA, 其中有个参数叫trackoption 。 当我把这个参数设置为TRACKINGOFF,也就是不跟踪数据变化时,没有任何问题,可以下载数据。 但当把它 ...… 查看全部问答> |
|
一块段显小屏,LCM046 一个单片机STC89C58RD+,一个串口下载器。 要完成一个大任务的一个小部分-----------点亮段式LCD。 呵呵,这个对我来说小菜一盘,没太在意。 因为我以前就点亮过只不过那个东东存在(对人 ...… 查看全部问答> |




