An 8MHz crystal is used to provide clock pulses to the microcontroller. The microcontroller CPU clock and the USB module are operated at 48MHz, and the clock and configuration register settings are as in the other projects in this chapter.
The PC program, based on Visual Basic, is called PRESSURE. Subroutine OnRead receives the data arriving at the USB port of the PC and then displays it on the screen form. The program does not send any data to the USB bus. The program listing (except the global variable declarations) is given in Figure 8.40.
' 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
'*******************************************************************
' 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)
Dim pressure As String
If hidRead(pHandle, BufferIn(0)) Then
' The first byte is the report ID. i.e. BufferIn(0)=reportID
pressure = Chr(BufferIn(1)) & Chr(BufferIn(2)) & Chr(BufferIn(3)) & _
Chr(BufferIn(4))
txtno = pressure
End If
End Sub
Figure 8.40: Visual Basic program of the project
Figure 8.41 shows a typical output from the Visual Basic program, displaying the atmospheric pressure.
Figure 8.41: Typical output from the Visual Basic program
An installable version of the Visual Basic program is provided on the CDROM that comes with this book, in folder PRESSURE.
CHAPTER 9
Advanced PIC18 Projects — CAN Bus Projects
The Controller Area Network (CAN) is a serial bus communications protocol developed by Bosch (an electrical equipment manufacturer in Germany) in the early 1980s. Thereafter, CAN was standardized as ISO-11898 and ISO-11519, establishing itself as the standard protocol for in-vehicle networking in the auto industry. In the early days of the automotive industry, localized stand-alone controllers had been used to manage various actuators and electromechanical subsystems. By networking the electronics in vehicles with CAN, however, they could be controlled from a central point, the engine control unit (ECU), thus increasing functionality, adding modularity, and making diagnostic processes more efficient.
Early CAN development was mainly supported by the vehicle industry, as it was used in passenger cars, boats, trucks, and other types of vehicles. Today the CAN protocol is used in many other fields in applications that call for networked embedded control, including industrial automation, medical applications, building automation, weaving machines, and production machinery. CAN offers an efficient communication protocol between sensors, actuators, controllers, and other nodes in real-time applications, and is known for its simplicity, reliability, and high performance.
The CAN protocol is based on a bus topology, and only two wires are needed for communication over a CAN bus. The bus has a multimaster structure where each device on the bus can send or receive data. Only one device can send data at any time while all the others listen. If two or more devices attempt to send data at the same time, the one with the highest priority is allowed to send its data while the others return to receive mode.
As shown in Figure 9.1, in a typical vehicle application there is usually more than one CAN bus, and they operate at different speeds. Slower devices, such as door control, climate control, and driver information modules, can be connected to a slow speed bus. Devices that require faster response, such as the ABS antilock braking system, the transmission control module, and the electronic throttle module, are connected to a faster CAN bus.