路 ESW 聯 USB Chapter 9 Applications For Windows
Outline USB Application Requirements Variable Definition Communications Code for VB Code for Keil C Practice
USB I/O
USB / USB 3 料 2 1 3 路
USB / 列 料 料 料 LED 料 料 ADC 類 / 數 料 DAC 數 / 類 USB-RS232 料
列 路
狀 數 TD_Init ( ) 狀 數 SETUP TD_Poll ( ) SETUP USB 流 1s 列 TD_Suspend 理 TD_Resume ( )
FIFO 0 1 FIFO 1
WDM -INF USB Plug and Play IDs Selecting Device Drivers to load http://www.microsoft.com/hwdev/busbios/usbpnp.htm
WDM -INF [USBBook] ;Uses the USBBook Vendor ID (1234) ;Uses the Product ID 5678 %USB\VID_1234 1234&PID_5678 5678.DeviceDesc%=IOHID, USB\VID_1234 1234&PID_5678.. [IOHID.CopyList] hidusb.sys hidclass.sys hidparse.sys ;---------------------------------------------------------------; [Strings] Provider="USB Book" MfgName="USB Book" USB\VID_1234 1234&PID_5678 5678.DeviceDesc="USB / "
USB I/O Windows AP INF(Driver) Windows AP VID PID INF (Driver)
USB 列 Control Panel 0547 0080 Ezusbw2k INF VB AP 1234 5678 HIDDEV INF
WDM -INF
USB USBCHECK.exe
USB
數 流
VB DLL ( ) newdll ' api 'opendevice : HID 'Writedevice : 料 HID 'Readdevice : HID 料 'closedevice : HID Public hid As New newdll Const MyVendorID = &H1234 Const MyProductID = &H5678 hiddevice = hid.opendevice(myvendorid, MyProductID) ; Text1.Text = hiddevice If hiddevice = True Then send(0) = Val( &H + Text2.Text) hid.writedevice send() ; 料 recevice() = hid.readdevice ; 料 Text3.Text = recevice(0) End If hid.closedevice ; End Sub
VB DLL ( )
http://www.microsoft.com/hwdev/usb hwdev/usb/
Application Requirements
Variable Definition VB Dim Execution As Integer Dim Speed As Integer Keil C int execution = 0; int speed = 0;
Integration VB OutputReportData(0) = Execution OutputReportData(1) = Speed Call WriteReport Keil C void ISR_Ep2out(void) interrupt 0 { : execution = OUT2BUF[0]; speed = OUT2BUF[1]; : }
Turn On/Turn off (VB) Private Sub Command1_Click() Execution = 1 - Execution If MyDeviceDetected = False Then MyDeviceDetected = FindTheHid If MyDeviceDetected = True Then OutputReportData(0) = Execution OutputReportData(1) = Speed Call WriteReport End If If Execution = 0 Then Command1.Caption = "Turn Off" Command1.Picture = Turnoff.Picture Else Command1.Caption = "Turn On" Command1.Picture = Turnon.Picture End If End Sub
LED Control (Keil C) int cnt = 0; int led = 1; int execution = 0; int speed = 0; void TD_Poll(void) { if (!execution) return; cnt++; if (cnt == 10000+speed*100){ cnt=0; P0=led; led<<=1; if(led == 256) led = 1;} }
Practice To design direction control for LED
Speed Control (VB) Private Sub HScroll1_Change() Speed = HScroll1.Value If MyDeviceDetected = False Then MyDeviceDetected = FindTheHid If MyDeviceDetected = True Then OutputReportData(0) = Execution OutputReportData(1) = Speed Call WriteReport End If End Sub
Using HID with Visual BASIC
VB-HID Development Steps host side Read Axelson Chapter 16 Human Interface Devices: Host Application Example Shows code for both VB and VC++ First edition of the book had a CD, Second edition does not Go to lvr.com, download usbhidio Unpack it VisualBasic folder has VB Project (usbhidio.vbp) In project Modules folder: ApiDeclarations.bas Contains VB declarations to allow VB to make C-style API calls VB application code in frmmain.frm Change VID and PID values to 0547/7450: 'Set these to match the values in the device's firmware and INF file. Const MyVendorID = &H547 Const MyProductID = &H7450
VB-HID Development Steps device side Keil project: gphid.uv2 fw2hid.c (from last session) gphid_descriptors.a51 INT-IN and INT-OUT endpoints Report descriptor specifies 2-byte in and out reports Compile and load the code Start the debugger First time: you ll see the HID Device Found box Readout should start incrementing
VB-HID Development Steps both sides Run the VB code It should find the device Then Allows you to send two-byte out reports First byte is readout increment rate Second byte is decimal point Shows the results of two-byte input reports First byte is current digit Second byte is pushbutton states
My Customization based on Jan s code
A VB HID Test Control Panel Click
Sending an OUTPUT Report The PC Side SendBuffer(0) = 0 The first byte is the Report ID SendBuffer(1) = 31 - hsrate.value ' next 2 bytes are data--rate SendBuffer(2) = dp ' decimal point NumberOfBytesWritten = 0 Result = WriteFile _ (HidDevice, _ SendBuffer(0), _ CLng(Capabilities.OutputReportByteLength), _ NumberOfBytesWritten, _ 0)
Sending an OUTPUT Report The Peripheral Side if(!(out1cs & bmepbusy)) // Is there something available? { timeconst = OUT1BUF[0]*20; // Output Report Byte 0 is time // const (tenths of sec) dp = OUT1BUF[1]; // decimal point OUT1BC = 0; // Re-arm EP1-OUT IN1BUF[0] = count; IN1BUF[1] = buttons; IN1BC = 2; // arm the EP2-IN transfer }
Reading an INPUT Report The PC Side Result = ReadFile _ (HidDevice, _ ReadBuffer(0), _ CLng(Capabilities.InputReportByteLength), _ NumberOfBytesRead, _ 0) tb7seg = Hex$(ReadBuffer(1)) ' put first byte value into text box (7-seg value) readval = ReadBuffer(2) ' get button states
Reading an INPUT Report The Peripheral Side if(!(out1cs & bmepbusy)) // Is there something available? { timeconst = OUT1BUF[0]*20; // Output Report Byte 0 is time // const (tenths of sec) dp = OUT1BUF[1]; // decimal point OUT1BC = 0; // Re-arm EP1-OUT IN1BUF[0] = count; IN1BUF[1] = buttons; IN1BC = 2; // arm the EP2-IN transfer }
Click below to find more Mipaper at www.lcis.com.tw Mipaper at www.lcis.com.tw