USB bus communication circuit diagram

Figure 1: USB bus communication circuit diagram

Many computers especially portable ones do not have a serial port (COM Port) anymore.
When a connection to a Personal Computer (PC) is required, a USB is the choice. The Universal Serial Bus (USB) is the widely used interface in electronic consumer products today. Most of electronic devices have at least one USB port on them, this include PCs, cameras, GPS devices, printers and so on.
The USB bus communication protocol is very complex so in this article we are going to touch only  the basic principles, just enough to be able to use the USB bus.
Some PIC18 microcontrollers support USB interface directly. 16bits (PIC24) and 32-bits (PIC32) pic microcontrollers have also an embedded USB interface.
In this article we are going to use an 8-bit pic microcontroller like the PIC18F25K50, PIC18F4450 and PIC18F4550 microcontrollers just to name a few all have a full-speed compatible USB interface that allows communication between a host PC and the microcontroller.
Figure 1 above shows a typical connection of PIC18F4550 to a PC.
PORTC pins RC4 (pin 23) and RC5 (pin 24) are used for USB interface. RC4 is the USB data D- pin, and RC5 is the USB data D+ pin.
The maximum power available from the PC to an external USB device is limited to about 100mA at 5.0V, this is a nice feature that can be used to power a microcontroller as well rather than using an external power supply.

Advantages of USB communication

  • The USB is a high-speed serial interface that can also provide power to devices connected to it. this decreases the cost due to additional power supply for small applications.
  • Plug and play, simple connection.
  • A USB bus supports up to 127 devices (This is limited by the 7-bit address field. note that address 0 is not used as it has a special purpose: 27 -1 = 127) connected through a 4-wire serial cable (2 data lines and 2 power lines). The distance can go up to three or even five meters in length.
  • Many USB devices can be connected to the same bus with USB hubs.
  • Easy interface to PC and other devices.
  • High speed: USB 1.0 Low speed 1.5 Mbit/sec and Full speed 12 Mbit/sec. USB 2.0 High speed 480 Mbit/sec was introduced. and recently, USB 3.0 Super speed 5 Gbit/sec.

The PIC18F4550 microcontroller contains a full-speed and low-speed (USB 1.0) compatible USB interface that allows a fast communication between a host PC and the microcontroller.

Settings and clock configurations

USB uses four wires:

  • Data + : Green wire
  • Data – :  White wire
  • +5V : Red wire and
  • Ground: Black wire.

The PIC18F2455/2550/4455/4550 family incorporate a different oscillator and microcontroller clock system than previous PIC18F devices due to the USB module.
The USB module requires a stable clock source and has to be compliant with both USB low-speed and full-speed specifications.
To accommodate these requirements, PIC18F2455/ 2550/4455/4550 devices include a new clock branch to provide a 48 MHz clock for full-speed USB operation. Since it is driven from the primary clock source, an additional system of prescalers and postscalers has been added to accommodate a wide range of oscillator frequencies.
If the clock is 6MHz, then we are working with low speed, if it is 48MHz then we are working with high speed.
A built in  Phase Locked Loop (PLL) inside the oscillator module allows us to raise the frequency to 48MHz from a low frequency of 4MHz, 8MHz and so on.

Oscillator Block diagram

Figure 2: Oscillator Block diagram

As it can be seen from figure 2 above, actually this PLL converts a 4 MHz clock to 96 MHz clock, and then you can divide it by 2 to get your 48MHz.
More information can be found from the PIC18F4550 data sheet.
In order to set the 48MHz clock frequency in mikroC Pro, just start a new project and then open the Project menu then click on Edit Project to open the Edit Project window.
Select the appropriate values as highlighted in red on figure 3 below:

Edit Project

Figure 3: Edit Project

  • As shown from figure 3 above, the PLL circuit should take a 4 MHz clock, if we are using a 4 MHz crystal, then we don’t need to divide anything. If our crystal is 8 MHz as in this case, in the Prescaler Selection select divide by 2 (8/2 = 4 MHz) as shown on figure 3 above. If the clock is 20 MHz then Divide by 5 (20/5 = 4 MHz) and so on.
  • As the output of the PLL circuit generates a 96 MHz we have to divide it by 2 to get 48 MHz. In the System Clock Postscaler Selection select 96 MHz PLL Src:/2.
  • In the USB Clock Selection, select USB clock source comes from the 96 MHz PLL divide by 2.
  • Make sure your oscillator frequency is 8 MHz and edit your Oscillator Selection to be HS oscillator (HS).
  • Make sure that your USB Voltage Regulator is Enabled. This will enable the 3.3V built in USB voltage Regulator and it’s very important for your USB communication so you have to enable your PIC to generate the 3.3V or to supply it by yourself externally but take care, once you enabled your PIC to generate 3.3V you have to connect a capacitor to the Vusb pin and ground (this capacitor Value is 200nF to 470nF: C3 on figure 1)

USB Device Classes

The functionality of USB devices is defined by class codes. Device classes enable the same device driver to be used for several devices having similar functionalities, this allows the USB host to load the suitable software driver for each connected device. This provides for adaptability and device independence of the host to support new devices from different manufacturers. 
The most common device classes are given in Table 1 below:

Device class Description Example
0x00 Reserved
0x01 USB audio device Sound card. speaker, microphone
0x02 USB communications device Modem, fax, ethernet adapter
0x03 USB human interface device Keyboard, mouse, joystick
0x06 USB Image device Scanner
0x07 USB printer device Printer
0x08 USB mass storage device Memory card, flash drive, external hard drive, digital camera
0x09 USB hub device Hubs
0x0B USB smart card reader device Smart card reader
0x0D USB Content security Fingerprint reader
0x0E USB video device Webcam
0xE0 USB wireless device Bluetooth

Table 1: Most common USB device classes

For details on the USB specification, see www.usb.org
The device classes of great interest to us now are: 

In the next articles, we are going to learn how to use these USB Device Classes with MPLAB XC8, Flowcode and MikroC Pro for PIC compilers:

MikroC Pro for PIC : 

Flowcode: