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

Testing the Project 

Testing the Project 

The project can be tested using one of the methods described in the previous project. If you are using the Visual Basic program, send data to the microcontroller and make sure the correct LEDs are turned on. Then connect some of the PORTB pins to logic 0 and click the CLICK TO RECEIVE button. The microcontroller will read its PORTB data and send it to the PC, where it will be displayed on the PC screen.

The project can also be tested using the HID terminal of mikroC IDE. The steps are:

• Start the HID terminal.

• Send a command to the microcontroller to turn on the LEDs (e.g., P=1T) and make sure the correct LEDs are turned on (in this case, LEDs 0, 4, and 5 should turn on, corresponding to the data pattern “0011 0001”).

• Connect bits 2 and 3 of PORTB to logic 1 and the other six bits to ground.

• Send command P=?? to the microcontroller.

• The PC will display the number 12, corresponding to bit pattern “0000 1100”.

The Visual Basic program listing of the project is given in Figure 8.33. Only the main program is given here, as the library declarations are the same as in Figure 8.19. The program jumps to subroutine OnRead when data arrives at the USB bus. The format of this data is checked to be in the format P=nT, and if the format is correct, the received data byte is displayed in the text box.

' 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 byte 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 (data)
 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
'****************************************************************************
' Send command P=?? to the microcontroller to request its PORTB data
'****************************************************************************
Private Sub Command3_Click()
 BufferOut(0) = 0        ' first byte is always the report ID
 BufferOut(1) = Asc("P") ' first data item ("P")
 BufferOut(2) = Asc("=") ' second data item ("=")
 BufferOut(3) = Asc("?") ' third data item ("?")
 BufferOut(4) = Asc("?") ' fourth data item ("?")
 ' write the data (don't forget, pass the whole array)...
 hidWriteEx VendorID, ProductID, BufferOut(0)
 lblstatus = "Data requested..."
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
  ' The data is received in the format: P=nT where the first byte
  ' is the report ID. i.e. BufferIn(0)=reportID, BufferIn(0)="P" and so on
  ' Check to make sure that received data is in correct format
  If (BufferIn(1) = Asc("P") And BufferIn(2) = Asc("=") And _
   BufferIn(4) = Asc("T")) Then
   txtreceived = Str$(BufferIn(3))
   lblstatus = "Data received..."
  End If
 End If
End Sub


Figure 8.33: Visual Basic program listing of the project

An installable version of the Visual Basic PC program is available in folder USB2 on the CDROM included with this book.

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

Îãëàâëåíèå ñòàòüè/êíèãè

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