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
The Microcontroller Software
The microcontroller receives the command P=nT from the PC and sends data byte n to PORTB. The listing of the microcontroller program (USB.C) without the USB code is shown in Figure 8.20. The program configures PORTB as digital output.
/*************************************************************************
USB BASED MICROCONTROLLER OUTPUT PORT
=======================================
In this project a PIC18F4550 type microcontroller is connected to a PC through
the USB link.
A Visual Basic program runs on the PC where the user enters the bits to be set
or cleared on PORTB of the microcontroller. The PC sends a command to the
microcontroller requesting it to set or reset the required bits of the
microcontroller
PORTB.
The command sent by the PC to the microcontroller is in the following format:
P=nT
where n is the byte the microcontroller is requested to send to PORTB of the
microcontroller.
Author: Dogan Ibrahim
Date: September 2007
File: USB.C
*************************************************************************/
void main() {
ADCON1 = 0xFF; // Set PORTB to digital I/O
TRISB = 0; // Set PORTB to outputs
PORTB = 0; // Clear all outputs
}
Figure 8.20: Microcontroller program without the USB code
The USB descriptor file must be included at the beginning of the mikroC program. This descriptor file is created using the Tools menu option of the mikroC compiler as follows:
• Select Tools→HID Terminal
• A new form should be displayed. Click on the Descriptor tab and the form shown in Figure 8.21 is displayed.
Figure 8.21: Creating the USBdsc descriptor file
• The important parameters to enter here are vendor ID (VID), product ID (PID), input buffer size, output buffer size, vendor name (VN), and product name (PN). Note that the VID and PID are in hexadecimal format and that the values entered here must be the same as the ones used in the Visual Basic program when generating the code using the EasyHID wizard. Choose VID=1234 (equivalent to decimal 6460), PID=1, input buffer size=4, output buffer size=4, and any names you like for the VN and PN fields.
• Check the mikroC compiler.
• Clicking the CREATE button will ask for a folder name and then create descriptor file USBdsc in this folder. Rename this file to have extension “.C” (i.e., the full file name should be USBdsc.C) and then copy it to the following folder (other required mikroC files are already in this folder, so it makes sense to copy USBdsc.C here as well).
C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HID-library\USBdsc.c
Do not modify the contents of file USBdsc.C. A listing of this file is given on the CDROM.
The microcontroller program listing with the USB code included is shown in Figure 8.22 (program USB1.C). At the beginning of the program the USB descriptor file USBdsc.C is included. The operation of the USB link requires the microcontroller to keep the connection alive by sending keep-alive messages to the PC every several milliseconds. This is achieved by setting up a timer interrupt service routine using TIMER 0. Inside the timer interrupt service routine the mikroC USB function HID_InterruptProc is called. Timer TMR0L is reloaded and timer interrupts are re-enabled just before returning from the interrupt service routine.
/***************************************************************************
USB BASED MICROCONTROLLER OUTPUT PORT
=======================================
In this project a PIC18F4550 type microcontroller is connected
to a PC through the USB link.
A Visual Basic program runs on the PC where the user enters the bits to be
set or
cleared on PORTB of the microcontroller. The PC sends a command to
the
microcontroller requesting it to set or reset the required bits of the
microcontroller
PORTB.
A 8MHz crystal is used to operate the microcontroller. The actual CPU clock
is raised
to 48MHz by setting configuration bits. Also, the USB module is
operated with
48MHz.
The command sent by the PC to the microcontroller is in the following format:
P=nT
where n is the byte the microcontroller is requested to send to PORTB of the
microcontroller.
This program includes the USB code.
Author: Dogan Ibrahim
Date: September 2007
File: USB1.C
****************************************************************************/
#include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HIDlibrary\USBdsc.c"
unsigned char Read_buffer[64];
unsigned char Write_buffer[64];