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.
PROJECT 8.3 — USB-Based Ambient Pressure Display on the PC
In this project, an ambient atmospheric pressure sensor is connected to a PIC18F4550 microcontroller, and the measured pressure is sent and displayed on a PC every second using a USB link.
An MPX4115A-type pressure sensor is used in this project. This sensor generates an analog voltage proportional to the ambient pressure. The device is available in either a 6-pin or an 8-pin package.
The pin configuration of a 6-pin sensor is:
Pin Description
1 Output voltage
2 Ground
3 +5V supply
4–6 not used
and for an 8-pin sensor:
Pin Description
1 not used
2 +5V supply
3 Ground
4 Output voltage
5–8 not used
Figure 8.34 shows pictures of this sensor with both types of pin configurations.
Figure 8.34: MPX4115A pressure sensors
The output voltage of the sensor is determined by:
V = 5.0 * (0.009 * kPa – 0.095) (8.1)
or
(8.2)
where
kPa = atmospheric pressure (kilopascals)
V = output voltage of the sensor (V)
The atmospheric pressure measurements are usually shown in millibars. At sea level and at 15°C the atmospheric pressure is 1013.3 millibars. In Equation (8.2) the pressure is given in kPa. To convert kPa to millibars we have to multiply Equation (8.2) by 10 to give:
(8.3)
or
(8.4)
Figure 8.35 shows the variation of the output voltage of MPX4115A sensor as the pressure varies. We are interested in the range of pressure between 800 and 1100 millibars.
Figure 8.35: Variation of sensor output voltage with pressure