Êíèãà: Advanced PIC Microcontroller Projects in C

The PC Software

The PC Software

The PC software is based on Visual Basic. It is assumed that the user has elementary knowledge of Visual Basic programming language. Instruction in programming using the Visual Basic language is beyond the scope of this book, and interested readers should refer to various books available on this topic.

The source program listing and the executables of the programs are given on the CDROM distributed with this book. Readers who do not want to do any programming can use or modify the given programs.

The Visual Basic program in this example consists of a single form as shown in Figure 8.12. The required PORTB data should be entered in decimal in the text box, and then the command button CLICK TO SEND should be clicked with the mouse. For example, entering decimal number 15 will turn on the LEDs connected to port pins RB0,RB1,RB2, and RB3 of PORTB.


Figure 8.12: The PC Visual Basic form

The program sends the entered number to the microcontroller as a packet consisting of four characters in the following format:

 P = nT

where character P indicates the start of data, n is the byte to be sent to PORTB, and T is the terminator character.

For example, if bits 3 and 4 of PORTB are to be set, i.e., PORTB = “00011000,” then the Visual Basic program sends packet P = 24T (number 24 is sent as a single binary byte and not as two ASCII bytes) to the microcontroller over the USB link. The bottom part of the form displays the connection status.

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)
 DisconnectFromHID
End Sub
'****************************************************************************
' a HID device has been plugged in...
'****************************************************************************
Public Sub OnPlugged(ByVal pHandle As Long)
 If hidGetVendorID(pHandle) = VendorID And _
  hidGetProductID(pHandle) = ProductID Then
  lblstatus = "USB Plugged....."
 End If
End Sub
'****************************************************************************
' a HID device has been unplugged...
'****************************************************************************
Public Sub OnUnplugged(ByVal pHandle As Long)
 If hidGetVendorID(pHandle) = VendorID And _
  hidGetProductID(pHandle) = ProductID Then
  lblstatus = "USB Unplugged...."
 End If
End Sub
'****************************************************************************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'****************************************************************************
Public Sub OnChanged()
 Dim DeviceHandle As Long
 ' get the handle of the device we are interested in, then set
 ' its read notify flag to true - this ensures you get a read
 ' notification message when there is some data to read...
 DeviceHandle = hidGetHandle(VendorID, ProductID)
 hidSetReadNotify DeviceHandle, True
End Sub
'****************************************************************************
' on read event...
'****************************************************************************
Public Sub OnRead(ByVal pHandle As Long)
 ' read the data (don't forget, pass the whole array)...
 If hidRead(pHandle, BufferIn(0)) Then
  ' ** YOUR CODE HERE **
  ' first byte is the report ID, e.g. BufferIn(0)
  ' the other bytes are the data from the microcontrolller...
 End If
End Sub

USB1.BAS

' this is the interface to the HID controller DLL - you should not
' normally need to change anything in this file.
'
' WinProc() calls your main form 'event' procedures - these are currently
' set to..
'
' MainForm.OnPlugged(ByVal pHandle as long)
' MainForm.OnUnplugged(ByVal pHandle as long)
' MainForm.OnChanged()
' MainForm.OnRead(ByVal pHandle as long)
Option Explicit
' HID interface API declarations...
Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Long) As Boolean
Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean
Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Long) As Long
Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Long
Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Long, ByRef pData As Byte) As Boolean
Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Long, ByRef pData As Byte) As Boolean
Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Long, ByVal pProductID As Long, ByRef pData As Byte) As Boolean
Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Long, ByVal pProductID As Long, ByRef pData As Byte) As Boolean
Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendorID As Long, ByVal pProductID As Long) As Long
Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Long) As Long
Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Long) As Long
Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Long) As Long
Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long
Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long
Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long
Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Long) As Long
Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Long) As Long
Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Long, ByVal pValue As Boolean)
Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Long) As Boolean
Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Long, ByVal pProductID As Long) As Boolean
' windows API declarations - used to set up messaging...
Private Declare Function CallWindowProc Lib user32 Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib user32 Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' windows API Constants
Private Const WM_APP = 32768
Private Const GWL_WNDPROC = -4
' HID message constants
Private Const WM_HID_EVENT = WM_APP + 200
Private Const NOTIFY_PLUGGED = 1
Private Const NOTIFY_UNPLUGGED = 2
Private Const NOTIFY_CHANGED = 3
Private Const NOTIFY_READ = 4
' local variables
Private FPrevWinProc As Long ' Handle to previous window procedure
Private FWinHandle As Long ' Handle to message window
' Set up a windows hook to receive notification
' messages from the HID controller DLL - then connect
' to the controller
Public Function ConnectToHID(ByVal pHostWin As Long) As Boolean
 FWinHandle = pHostWin
 ConnectToHID = hidConnect(FWinHandle)
 FPrevWinProc = SetWindowLong(FWinHandle, GWL_WNDPROC, AddressOf WinProc)
End Function
' Unhook from the HID controller and disconnect...
Public Function DisconnectFromHID() As Boolean
 DisconnectFromHID = hidDisconnect
 SetWindowLong FWinHandle, GWL_WNDPROC, FPrevWinProc
End Function
' This is the procedure that intercepts the HID controller messages...
Private Function WinProc(ByVal pHWnd As Long, ByVal pMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 If pMsg = WM_HID_EVENT Then
  Select Case wParam
  ' HID device has been plugged message...
  Case Is = NOTIFY_PLUGGED
   MainForm.OnPlugged (lParam)
  ' HID device has been unplugged
  Case Is = NOTIFY_UNPLUGGED
   MainForm.OnUnplugged (lParam)
  ' controller has changed...
  Case Is = NOTIFY_CHANGED
   MainForm.OnChanged
  ' read event...
  Case Is = NOTIFY_READ
   MainForm.OnRead (lParam)
  End Select
 End If
 ' next...
 WinProc = CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam)
End Function


Figure 8.19: Visual Basic program for the PC end of USB link

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.035. Çàïðîñîâ Ê ÁÄ/Cache: 0 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç