Выбрать главу

Figure 8.26: Device manager display showing the USB devices

Enter data into the Visual Basic form and click the CLICK TO SEND button. The corresponding microcontroller LEDs should turn on. For example, entering 3 should turn on LEDs 0 and 1.

Using a USB Protocol Analyzer

If for any reason the project is not working, a USB protocol analyzer can be used to check the data transactions on the USB bus. There are many USB protocol analyzers on the market. Some expensive professional ones are hardware-based and require the purchase of special hardware. Most low-cost USB protocol analyzers are software-based. Two such tools are described here briefly.

UVCView

UVCView is a free Microsoft product that runs on a PC and displays the descriptors of a USB device after it is plugged in. Figure 8.27 shows the UVCView display after the microcontroller is plugged into the PC. The left side of the display shows the USB ports available in the system. Clicking on a device in this part of the display shows descriptor details of the device in the middle of the screen. In Figure 8.27 the descriptors of our device are shown. The UVCView display is useful when various fields of the device descriptors must be checked.

Figure 8.27: UVCView display of the project

USBTrace

USBTrace is a software USB protocol analyzer developed by SysNucleus (www.sysnucleus.com) and runs on a PC. The software monitors the USB ports of the PC it is running on and displays all the transactions on the bus. This software can be an invaluable tool when all the transactions on the line must be monitored and logged.

A limited-time demo version of USBTrace is available on the manufacturer’s web site. An example using the program is given in this section to show the data sent from the PC to the microcontroller:

• Start the USBTrace program.

• Connect the microcontroller to the USB port of the PC.

• Select the device from the left side of the display by checking the appropriate box.

• Start the Visual Basic program.

• Start capturing data by clicking the green arrow at the top left of the USBTrace menu. You should see the START OF LOG message in the middle part of the screen

• Enter number 3 on the Visual Basic form to turn on LEDs 0 and 1 of PORTB, and click the CLICK TO SEND button.

• You should see data packets in the middle of the screen as shown in Figure 8.28.

Figure 8.28: Transactions on the bus when CLICK TO SEND is clicked

• Move the cursor over the first packet. This is the packet sent from the PC to the microcontroller (OUT packet). A pop-up window will appear, and information about this packet will be displayed, with the data sent appearing in hexadecimal at the bottom of the display, as shown in Figure 8.29. Note that the data consists of the following 4 bytes:

50 3D 03 54

P  =  3  T

which correspond to the ASCII string P=3T. This is the actual packet sent from the PC to the microcontroller.

Figure 8.29: Displaying contents of the packet

USBTrace can also display the device descriptors in detail, as shown in the lower part of the screen in Figure 8.29.

Using the HID Terminal of mikroC

The mikroC IDE provides a USB terminal interface that can be used for sending and receiving data over the USB bus. This program can be used instead of the Visual Basic program to test the USB interface. The steps are as follows:

• In mikroC IDE, Select Tools→HID Terminal

• Plug the microcontroller into the PC’s USB port

• You should see the product ID under HID Devices:

 ○ To turn on LEDs 0,1,4, and 5, type P=3T under Communication and click the SEND button as shown in Figure 8.30 (remember that the ASCII value of number 3 has the bit pattern “0011 0011”)

 ○ LEDs 0,1,4, and 5 of the microcontroller should turn on

Figure 8.30: Using the HID terminal to send data to a USB device

PROJECT 8.2 — USB-Based Microcontroller Input/Output

This project is very similar to Project 8.1, except that it includes two-way communication, while in Project 8.1 data to be output on PORTB was sent to the microcontroller. In addition, PORTB data is received from the microcontroller and displayed on the PC.

The PC sends two commands to the microcontroller:

• Command P=nT requests the microcontroller to send data byte n to PORTB.

• Command P=?? requests the microcontroller to read its PORTB data and send it as a byte to the PC. The PC then displays this data on the screen. The microcontroller sends its data in the familiar format P=nT.

The hardware of this project is the same as the hardware for the previous project, shown in Figure 8.11, where eight LEDs are connected to PORTB of a PIC18F4550 microcontroller which is operated from a 8MHz crystal.

A single form is used in this project, and Figure 8.31 shows the format of this form. The upper part of the form is the same as in Project 8.1, i.e., sending data to PORTB of the microcontroller. A text box and a command button named CLICK TO RECEIVE are also placed on the form. When the button is pressed, the PC sends command P=?? to the microcontroller. The microcontroller reads its PORTB data and sends it in the format P=nT to the PC where it is displayed in the text box.

Figure 8.31: Visual Basic form of the project

Figure 8.32 shows the mikroC program of the project. The program is named USB2.C and is very similar to the one for the previous project. But here, in addition, when the command P=?? is received from the PC, the microcontroller reads PORTB data and sends it to the PC in the format using the mikroC function Hid_Write.

/***********************************************************************

            USB BASED MICROCONTROLLER INPUT/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. In addition,

the PORTB data can be requested from the microcontroller and displayed

on the PC.

The microcontroller is operated from a 8MHz crystal, but the CPU

clock frequency is increased to 48MHz. Also, the USB module operates

with 48MHz.

The commands are:

From PC to microcontroller: P=nT (Send data byte n to PORTB)

                            P=?? (Give me PORTB data)

From microcontroller to PC: P=nT (Here is my PORTB data)

Author: Dogan Ibrahim

Date:   September 2007

File:   USB2.C