Table 8.14: Example endpoint descriptor
Offset | Field | Size | Description |
---|---|---|---|
0 | bLength | 7 | Descriptor size in bytes |
1 | bDescriptorType | 0x05 | Endpoint (0x05) |
2 | bcdEndpointAddress | 0x50 | Endpoint address |
4 | bmAttributes | 0x03 | Interrupt type endpoint |
5 | wMaxPacketSize | 0x0002 | Max packet size is 2 |
6 | bInterval | 0x14 | Polling interval is 20ms |
8.5 PIC18 Microcontroller USB Bus Interface
Some of the PIC18 microcontrollers support USB interface directly. For example, the PIC18F4550 microcontroller contains a full-speed and low-speed compatible USB interface that allows communication between a host PC and the microcontroller. In the USB projects in this chapter we will use the PIC18F4550 microcontroller.
Figure 8.8 is an overview of the USB section of the PIC18F4550 microcontroller. 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. Internal pull-up resistors are provided which can be disabled (setting UPUEN=0) if desired and external pull-up resistors can be used instead. For full-speed operation an internal or external resistor should be connected to data pin D+, and for low-speed operation an internal or external resistor should be connected to data pin D–.
Figure 8.8: PIC18F4550 microcontroller USB overview
Operation of the USB module is configured using three control registers, and a total of twenty-two registers are used to manage the actual USB transactions. Configuration of these registers is a highly complex task and is not covered in this book. Interested readers should refer to the PIC18F4550 data sheet and to books on USB internals. In this chapter we are using the mikroC language USB library functions to implement USB transactions. The details of these functions are given in the next section.
8.6 mikroC Language USB Bus Library Functions
The mikroC language supports a number of functions for USB HID-type communications. Each project based on the USB library should include a descriptor source file which contains vendor ID and name, product ID and name, report length, and other relevant information. To create a descriptor source file we can use mikroC’s integrated USB HID terminal tool (see Tools→HID Terminal). The default name for descriptor file is USBdsc.c, but it can be renamed if required. The USBdsc.c file must be included in USB-based projects either via the mikroC IDE tool, or as an #include option in the program source file.
The mikroC language supports the following USB bus library functions when a PIC microcontroller with built-in USB is used (e.g., PIC18F4550), and port pins RC4 and RC5 are connected to the D+ and D– pins of the USB connector respectively:
Hid_Enable: This function enables USB communication and requires two arguments: the read-buffer address and the write-buffer address. It must be called before any other functions of the USB library, and it returns no data.
Hid_Read: This function receives data from the USB bus and stores it in the receive-buffer. It has no arguments but returns the number of characters received.
Hid_Write: This function sends data from the write-buffer to the USB bus. The name of the buffer (the same buffer used in the initialization) and the length of the data to be sent must be specified as arguments to the function. The function does not return any data.
Hid_Disable: This function disables the USB data transfer. It has no arguments and returns no data.
The USB interface of a PIC18F4550 microcontroller is shown in Figure 8.9. As the figure shows, the interface is very simple. In addition to the power supply and ground pins, it requires just two pins to be connected to the USB connector. The microcontroller receives power from the USB port.
Figure 8.9: PIC18F4550 USB interface
PROJECT 8.1 — USB-Based Microcontroller Output Port
This project describes the design of a USB-based microcontroller output port. A PIC18F4550 microcontroller is interfaced to a PC through a USB cable. A Visual Basic program runs on the PC and sends commands to the microcontroller through the USB bus, asking the microcontroller to set/reset the I/O bits of its PORTB.
The block diagram of the project is shown in Figure 8.10. The circuit diagram is given in Figure 8.11. The USB lines of the PIC18F4550 microcontroller are connected to a USB connector. The microcontroller is powered from the USB line (i.e., no external power supply is required). This makes the design of USB-based products relatively cheap and very attractive in applications where the total power consumption is below 100mA. The microcontroller is operated from an 8MHz crystal.
Figure 8.10: Block diagram of the project
Figure 8.11: Circuit diagram of the project
The PORTB pins of the microcontroller are connected to LEDs so we can see the state changes as commands are sent from the PC. This makes testing the project very easy. Note that a capacitor (about 200nF) should be connected between the VUSB pin (pin 18) of the microcontroller and the ground for stability.
The project software consists of two parts: the PC software, and the microcontroller software. Both are described in this section.
The PC Software
The PC software is based on Visual Basic. It is assumed that the user has elementary knowledge of Visual Basic programming language. Instruction in programming using the Visual Basic language is beyond the scope of this book, and interested readers should refer to various books available on this topic.
The source program listing and the executables of the programs are given on the CDROM distributed with this book. Readers who do not want to do any programming can use or modify the given programs.
The Visual Basic program in this example consists of a single form as shown in Figure 8.12. The required PORTB data should be entered in decimal in the text box, and then the command button CLICK TO SEND should be clicked with the mouse. For example, entering decimal number 15 will turn on the LEDs connected to port pins RB0,RB1,RB2, and RB3 of PORTB.
Figure 8.12: The PC Visual Basic form
The program sends the entered number to the microcontroller as a packet consisting of four characters in the following format:
P = nT
where character P indicates the start of data, n is the byte to be sent to PORTB, and T is the terminator character.
For example, if bits 3 and 4 of PORTB are to be set, i.e., PORTB = “00011000,” then the Visual Basic program sends packet P = 24T (number 24 is sent as a single binary byte and not as two ASCII bytes) to the microcontroller over the USB link. The bottom part of the form displays the connection status.