The Visual Basic program used in this section is based on the USB utility known as EasyHID USB Wizard, developed by Mecanique, and can be downloaded free of charge from their web site (www.mecanique.co.uk). EasyHID is designed to work with USB 2.0, and there is no need to develop a driver, as the XP operating system is shipped with a HID-based USB driver. This utility generates Visual Basic, Visual C++, or Borland Delphi template codes for the PC end of a USB application using an HID-type device interface. In addition, the utility can generate USB template code for the PIC18F4550 and similar microcontrollers, based on the Proton Development Suite (www.crownhill.co.uk), Swordish PIC Basic, or PicBasic Pro (www.melabs.com) programming languages. The generated codes can be expanded with the user code to implement the required application.
The steps in generating a Visual Basic code template follow:
• Load the EasyHID zip file from the Mecanique web site by clicking on “Download EasyHID as a Standalone Application”
• Extract the files and install the application by double-clicking on SETUP.
• When the program has started, you should see a form as shown in Figure 8.13. Enter your data in the fields Company Name, Product Name, and the optional Serial Number.
Figure 8.13: EasyHID first form
• Enter your Vendor ID (VID) and Product ID (PID) as shown in the form in Figure 8.14. Vendor IDs are unique throughout the world and are issued by the USB implementers (www.usb.org) at a cost. Mecanique owns a Vendor ID and can issue you a set of Product IDs at low cost so your products can be shipped all over the world with unique VID and PID combinations. In this example, VID=4660 and PID=1 are selected for test purposes.
Figure 8.14: EasyHID VID and PID entry form
• Clicking Next displays the form shown in Figure 8.15. The important parameters here are the output and input buffer sizes, which specify the number of bytes to be sent and received respectively between the PC and the microcontroller during USB data transactions. In this example, 4 bytes are chosen for both fields (our output is in the format P=nT, which is 4 bytes).
Figure 8.15: EasyHID input-output buffer selection
• In the next form (see Figure 8.16), select a location for the generated files, choose the microcontroller compiler to be used (this field is not important, as we are only generating code for Visual Basic (i.e., the PC end), choose the microcontroller type, and finally select Visual Basic as the language to be used.
Figure 8.16: EasyHID output folder, microcontroller type, and host compiler selection
• Clicking Next generates Visual Basic and microcontroller code templates in the selected directories (see the final form in Figure 8.17).
Figure 8.17: EasyHID last form
Figure 8.18 shows the Visual Basic files generated by the EasyHID wizard. The files basically consist of a blank form (FormMain.frm), a module file (mcHIDInterface. BAS), and a project file (USBProject.vbp).
Figure 8.18: Files generated by the EasyHID wizard
The files generated by the EasyHID wizard have been modified for our project as follows:
• The blank form has been modified to display the various controls shown in Figure 8.12.
• Messages are added to the program to display when a USB device is plugged into or unplugged from the PC.
• A subroutine has been added to read the data entered by the user and then send this data to the microcontroller over the USB bus when the button CLICK TO SEND is clicked. This code is as follows:
Private Sub Command2_Click()
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = Asc("P") ' first data item (“P”)
BufferOut(2) = Asc("=") ' second data item (“=”)
BufferOut(3) = Val(txtno) ' third data item (number to send)
BufferOut(4) = Asc("T") ' fourth data item (“T”)
' write the data (don't forget, pass the whole array)...
hidWriteEx VendorID, ProductID, BufferOut(0)
lblstatus = "Data sent..."
End Sub
BufferOut stores the data to be sent to the microcontroller over the USB bus. Notice that the first byte of this buffer is the report ID and must be set to 0. The actual data starts from address BufferOut(1) of the array and the data sent is in the format P=nT as described before. After the data is sent, the message “Data sent…” appears at the bottom part of the display.
Figure 8.19 shows the final listing of the Visual Basic program. The program is in two parts: the form USB1.FRM and the module USB1.BAS. The programs should be loaded and used in the Visual Basic development environment. An installable version of this program (in folder USB1) comes with the CDROM included with this book for those who do not have the Visual Basic development environment. This program should be installed as a normal Windows software installation.
USB1.FRM
' vendor and product IDs
Private Const VendorID = 4660
Private Const ProductID = 1
' read and write buffers
Private Const BufferInSize = 8
Private Const BufferOutSize = 8
Dim BufferIn(0 To BufferInSize) As Byte
Dim BufferOut(0 To BufferOutSize) As Byte
Private Sub Command1_Click()
Form_Unload (0)
End
End Sub
Private Sub Command2_Click()
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = Asc("P") ' first data item (“P”)
BufferOut(2) = Asc("=") ' second data item (“-“)
BufferOut(3) = Val(txtno) ' third data item (to send over USB)
BufferOut(4) = Asc("T") ' fourth data item (“T”)
' write the data (don't forget, pass the whole array)...
hidWriteEx VendorID, ProductID, BufferOut(0)
lblstatus = "Data sent..."
End Sub
'***************************************************************************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'***************************************************************************
Private Sub Form_Load()
' do not remove!
ConnectToHID (Me.hwnd)
lblstatus = "Connected to HID..."
End Sub
'****************************************************************************
' disconnect from the HID controller...
'****************************************************************************
Private Sub Form_Unload(Cancel As Integer)