<<

PR26

USB Controller

Version 1.0

October 2009

Cytron Technologies Sdn. Bhd.

Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. No representation or warranty is given and no liability is assumed by Cytron Technologies Incorporated with respect to the accuracy or use of such information or infringement of patents or other intellectual property rights arising from such use or otherwise. Use of Cytron Technologies’s products as critical components in life support systems is not authorized except with express written approval by Cytron Technologies. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights.

OVERVIEW Just connect, play and you learn - it's that simple. This document describes the development of Cytron Schematic and source code is provided. Technologies DIY (Do It Yourself) Project PR26. This firmware implements a standard USB FEATURES with 12 inputs (4 directional buttons and 8 general purpose buttons) and 4 analog inputs. There are no PIC18F2550 drivers required, since USB standard defined device - 8-bit microcontroller with 22 I/O classes, this project using the human (HID) - Operate with 5V supply which allows user to tell the that the - Operating speed 20MHz connected USB device is a game controller. This - Full Speed USB 2.0 (12Mbit/s) interface project will be using PIC18F2550, push button and joystick to give a guideline to hobbyist on how to build - With 2-axis (x-axis and y-axis) and 1 push a standard USB Game Controller. The performance of button the USB Game Controller can be tested by using PC.

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

SYSTEM OVERVIEW

12 Push Buttons PIC18F2550 HID USB interface Computer

2

GENERAL DESCRIPTION

More information on PIC18F2550 can visit the website PIC18F2550 at: Ideal for low power (nanoWatt) and connectivity http://ww1.microchip.com/downloads/en/DeviceDoc/3 applications that benefit from the availability of three 9632D.pdf serial ports: FS-USB (12 Mbit/s), I²C™ and SPI™ (up to 10Mbit/s) and an asynchronous (LIN capable) (EUSART). Large amounts of RAM memory for HARDWARE buffering and Enhanced FLASH program memory This project will require following hardware: make it ideal for embedded control and monitoring applications that require periodic connection with a a. 1 x PIC18F2550 (legacy free) via USB for data b. 1 x PR26 Printed Circuit Board (PCB) upload/download and/or firmware updates. c. 12 x push button d. 2 x joystick Feature of the device: e. 1 x USB B type connector • High-Current Sink/Source: 25 mA/25 mA f. 1 x USB cable • Three External Interrupts g. 1 x 9V-12V power supply • Four Timer modules (Timer0 to Timer3) h. Related electronic components • Up to 2 Capture/Compare/PWM (CCP) modules: • Enhanced Capture/Compare/PWM (ECCP) Please refer to Appendix A for the board layout of module PR26. The board layout is provided free therefore • Enhanced USART module Cytron Technologies will not be responsible for any • 10-bit, up to 13-channel Analog-to-Digital further modification or improvement. Converter module (A/D) with Programmable Acquisition Time • Dual Analog Comparators with Input Multiplexing Interface joystick with PIC18F2550 • 100,000 Erase/Write Cycle Enhanced Flash Program Memory typical • 1,000,000 Erase/Write Cycle Data EEPROM Memory typical • Self-Programmable under Software Control • Priority Levels for Interrupts • 8 x 8 Single-Cycle Hardware Multiplier • Extended Watchdog Timer (WDT) • Programming™ (ICSP™) via two pins - In-Circuit Debug (ICD) via two pins - Optional dedicated ICD/ICSP port (44-pin Figure 1 devices only) - Wide Operating Voltage Range (2.0V to 5.5V) The analog-to-digital (A/D) converter module has inputs for the 28pins PIC18F2550. This module allows conversion of an analog input signal to a corresponding

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

10-bit digital number but we only use 8-bit for this Since USB standard defined device classes, this project project. using the human input device (HID) which allows user The module has five registers: to tell the computer that the connected USB device is a • A/D Result High Register (ADRESH) game controller and has 2 joysticks and 12 buttons. • A/D Result Low Register (ADRESL) • A/D Control Register 0 (ADCON0) First of all, we have to set down the data in the HID • A/D Control Register 1 (ADCON1) Report Descriptor to tell the computer of what function • A/D Control Register 2 (ADCON2) the device has, and how many bytes of data needed to be sent back to the computer. For more detail about USB, please visit www..org. The information on report descriptor will be explained more at software part later.

Device Class Definition for Human Interface Devices (HID)

The HID class device class uses the following standard USB descriptors: • Device

Figure 2 • Configuration • Interface In this PR, we have 4 analog outputs from two • Endpoint joysticks (x-axis, y-axis, z-axis, z-rotation), so analog • String input pin of AN0-AN3 are connected to the 4 presets from joysticks. But make sure the ADC configuration USB Descriptor in the programming is correct. Each device class includes one or more class-specific descriptors. These descriptors differ from standard USB descriptors. A HID class device uses the following class-specific descriptors: HID, Report and Physical.

• HID Descriptor The HID descriptor identifies the length and type of subordinate descriptors for a device. Figure 3

A/D converter module is enabled, by setting bit0 of ADCON0 as 1. Default ADCON1 as only 4 analog pins (AN0-AN3) are used. Use left justified for A/D Result Format Select bit by setting bit7 of ADCON2 as 1.

For ADCON2, set ADCS2:ADCS0 (A/D Conversion Clock Select bits) as 101, while ACQT2:ACQT0 (A/D Acquisition Time Select bits) as 110, since the maximum device frequency that used is 20MHz.

For reduce the noise (low pass filter), a capacitor (104) is added between the analog signal and GND for every analog input from joystick. More information on ADC configuration, please refer to PIC18F2550 datasheet at http://www.microchip.com. Figure 4

Interface HID Report Descriptor with • Report Descriptor joystick The Report descriptor is unlike other descriptors in that it is not simply a table of values. The length and

content of a Report descriptor vary depending on the For this PR, we are going to implements a standard number of data fields required for the device’s report USB game controller with twelve inputs (four or reports. A Report descriptor is the complete set of directional buttons and eight general purpose buttons). all items for a device. By looking at a Report descriptor

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller alone, an application knows how to handle incoming • Physical Descriptors data, as well as what the data could be used for. A Physical Descriptor is a data structure that provides The Report descriptor is made up of items that provide information about the specific part or parts of the information about the device. The first part of an item human body that are activating a control or controls. contains three fields: item type, item tag, and item size. For example, a physical descriptor might indicate that Together these fields identify the kind of information the right hand thumb is used to activate button 5. An the item provides. There are three item types: Main, application can use this information to assign Global, and Local. functionality to the controls of a device.

There are five Main item tags currently defined: Note: Physical Descriptors are entirely optional. They • Input item tag: Refers to the data from one or add complexity and offer very little in return for most more similar controls on a device. For devices. However, some devices, particularly those example, variable data such as reading the with a large number of identical controls (for example, position of a single axis or a group of levers or buttons) will find that Physical Descriptors help array data such as one or more push buttons or different applications assign functionality to these switches. controls in a more consistent manner. • Output item tag: Refers to the data to one or more similar controls on a device such as setting the position of a single axis or a group To make a clearer picture of HID device class of levers (variable data). Or, it can represent descriptor structure, all of these things can be data to one or more LEDs (array data). combined as shown below. Understand the HID • Feature item tag: Describes device input and descriptor well will help you know more about this output not intended for consumption by the PR26. end user —for example, a software feature or Control Panel toggle. • Collection item tag: A meaningful grouping of Input, Output, and Feature items—for example, mouse, keyboard, joystick, and pointer. • End Collection item tag: A terminating item used to specify the end of a collection of items.

A Report descriptor is the complete set of all items for a device. By looking at a Report descriptor alone, an application knows how to handle incoming data, as well as what the data could be used for. One or more fields of data from controls are defined by a Main item and further described by the preceding Global and Local items. Local items only describe the data fields defined by the next Main item. Global items become the default attributes for all subsequent data fields in that descriptor.

The Report Descriptor must include each of the following items to describe a control’s data (all other items are optional): Figure 5 - Input (Output or Feature) - Usage For this PR26, we will update the particular data every - Usage Page time reach the line “hid_report_in[ ]= ” in the main - Logical Minimum program. - Logical Maximum - Report Size - Report Count

For details about these descriptors as defined for a HID class device, see Appendix A: Example USB Figure 6 Descriptors for HID Class Devices (Mouse). For general information about standard USB, please refer Each data is indicated by different index. The file to Device Class Definition for Human Interface.pdf descriptor can be summarized as the table below. and Devices (HID) HID Usage Tables.pdf attached together with this project.

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

Index (byte) Name Power supply for the circuit 0 x-axis 1 y-axis 2 z-axis 3 z-rotation 4 hat switch 5 button Table 1

Total of byte needed to be sent by report descriptor to computer is decided earlier and set in the report Figure 9 descriptor (in usb_descriptor for this PR). This project differs with previous PR, where it gets power supply from computer through USB cable. From the figure above, Pwr is green LED to indicate the power status of the circuit. The power led that connected between Vcc and GND will light on, if the connection of the circuit is correct. R1 is resistor to protect Pwr from over current which might burn Pwr. D- pin from USB cable is connected to pin 15 of PIC18F2550. While D+ pin from USB cable is connected to pin 16 of PIC18F2550.

Figure 7

Then, all the collected six bytes of data (byte[0]- byte[5]) will be sent back every time when reach the line “lastTransmission = HIDTxP….. ” in the main Figure 10 program. Push Button as input of PIC Note that there is 0x06 at behind, it means the total microcontroller byte of data sent is 6.

Figure 8

Figure above shows the data in usb_descriptor. The description about the data is explained by the comment beside each line in the source code given.

For more information on USB data used, please refer to Device Class Definition for Human Interface.pdf and Figure 11 HID Usage Tables.pdf attached together with this project. One I/O pin is designated for a push button as input to PIC microcontroller. The connection of the push button to the I/O pin is shown in figure4. The I/O pin should be pull up to 5V using a resistor (with value range 1K- 10K) and this configuration will result an active-low input. When the button is being pressed, reading of I/O pin will be in logic 0, while when the button is not pressed, reading of that I/O pin will be logic 1.

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

ICSP for programming PIC Please refer to Appendix A for the PCB layout of PR26. microcontroller The PCB layout is provided free and therefore Cytron Technologies will not be responsible for any further modification or improvement.

Figure 12

MCLR, RB6 and RB7 need to be connected to the USB In Circuit Programmer (UIC00A) to program the PIC microcontroller. The programmer (UIC00A) is not included in DIY project set since it can be used several time for different project set. User can also choose other type of PIC programmer to load the program.

For the instruction of using PIC programmer, please refer to the particular PIC programmer user’s manual at: http://www.cytron.com.my/listProductCategory.asp?ci d=81

PCB circuit board

2 3 1

10 4 5 6 7

8 9

Figure 13

Component:

1. LED (indicate power status of circuit) 2. USB type-B connector 3. UIC00A box header (connect to UIC00A programmer to load program). 4. Push button (1, 2, 3 and 4) 5. Start button 6. Select button 7. Analog LED (indicate the status of analog) 8. Analog switch (activated/deactivated the analog) 9. Joystick 10. Directional push button

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

SOFTWARE The program is provided by Microchip Technology Inc. In this project, we modify the program to use as game Flow Chart: controller. For more information about the software for this system, please refer to the source code provided. The explanation of each instruction is provided in the Start source code as the comment of each line.

The source code is provided free and Cytron Initialize PIC Technologies will not be responsible for any further modification or improvement.

First time YES connected to PC? GETTING START

NO Installation User can obtain the hardware set for this project (PR26) either by online purchasing (www.cytron.com.my) or purchase it in Cytron Technologies Shop. Analog switch pressed? 1. Once user has the hardware set, soldering process can be started. Please solder the NO YES electronic components one by one according the symbols or overlays on the Printed Circuit Board (PCB). Ensure the component value Analog Analog and polarity is correctly soldered. Please refer deactivated activated to PCB Layout in Appendix B. Remember there is 4 jumpers that need to be soldered. Please use the single core wire given. NO Move joystick? Caution: Make sure all the connectors (2510) are NO Press soldered in proper side. Those electronic directional YES components have polarity such as capacitor, button? Save updated diode, PIC, PIR sensor, buzzer and LED value should be soldered in right polarity or it may cause the circuit board fail to work. YES

Warning:Before the battery (Power) is plugged in, make sure the polarity is correct to prevent the Press directional explosion. Wrong polarity of capacitor also button? may cause explosion. Save updated value 2. Connect the USB cable to the USB type-B YES connector.

Save updated 3. Please download the necessary files and value document from Cytron Technologies website. These included documentation, sample source code, schematic, component list and software.

Push button NO 4. The next step is to install MPLAB IDE and pressed? Microchip C18 Compiler into a computer. The MPLAB IDE and C18 Compiler can be downloaded from www.cytron.com.my Please YES refer document to install MPLAB software Save updated and C18 compiler software. value 5. After the installation complete, open the project file provided using MPLAB IDE. Send all updated Please refer Installation of C18 compiler value to computer and open project_18F document to open project.

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

6. When you create project for the game 12. Diagram below shown the example of file controller, create the subfolder and include the after all necessary file added. c.file, h.file, and linker into the project.

7. To create subfolder file in Source Files, right click on the Source Files, then click on Create Subfolders…, diagram below shown the example for create subfolder to Source Files.

Figure 14

8. Named the subfolder file and add file for source file and subfolder file.

Figure 16

13. Build the project and load the hex file into the PIC microcontroller using the USB In Circuit Programmer (UIC00A). When users build the project, MPLAB IDE will generate hex file. The hex file generated from MPLAB IDE will be named according to project name, not C file name. Cytron Technologies also provide hex file for user. Do not forget to switch ON Figure 15 the power. The programmer is not included in the hardware set but it can be found at Cytron 9. To add file in subfolder Files and Source website. (User manual is provided at website). Files, right click on the folder Files, then click on Add Files…, 14. Make sure when you load the hex file into PIC microcontroller, USB cable is connected 10. User may repeat the similar steps to create to USB type-B connector and computer. It is subfolder in the header file. First, right click because power supply is gets from computer on the Header Files, then click on Create through USB cable. Subfolder…, 15. Test the functionality of the PCB board. 11. Another file need to add is rm18f2550-HID Bootload.lkr, which is under Linker Script. 16. Have fun!

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

• Remember to unplug the programmer UIC00A from the PCB and also the computer after the program was loaded. If not, this might cause error when you want to open the control panel to test its functionality.

TEST METHOD

1. Connect the USB cable to the PC • Power LED (Green) will turn ON. • LED (Red) is off • If it is the first time you plug the game controller to your Personal Computer, the device will install to the PC. Figure 19 2. Click Control Panel from Start in your PC and open Game Controller 4. When analog is deactivated • LED is off • X-axis and Y-axis will move if directional button is pressed. • Buttons on screen from 1 to 12 will light on when button on game controller is pressed • There is nothing happen on screen when joystick is moved because analog is deactivated now. 5. When analog is activated

• LED (red) will light on

• Point of View Hat on screen will move Figure 17 when directional button is pressed.

• Buttons on screen from 1 to 12 will light • If the device is successfully installed, on when button on game controller is Cytron Game Controller will appear in pressed. the installed game controller list • X-axis and Y-axis will change according to the direction given by joystick1 when joystick1 is moving. • Z-axis and Z-Rotation will change when joystick2 is moving.

If all steps mention above can be executed, your project is done successfully. Congratulations!!

WARRANTY

No warranty will be provided as this is DIY project. Thus, user is advice to check the polarity of each Figure 18 electronic component before soldering it to board.

3. Double click the Cytron Game Controller, Cytron Game Controller Properties will appear. Click Test tab of Cytron Game Controller properties.

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

Appendix A

A.1 Device Descriptor:

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved

ROBOT . HEAD to TOE PR26 – USB Game Controller

A.2 Configuration Descriptor:

A.3 Interface Descriptor (Mouse):

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved 10

ROBOT . HEAD to TOE PR26 – USB Game Controller

A.4 HID Descriptor (Mouse):

A.5 Endpoint Descriptor (Mouse):

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved 11

ROBOT . HEAD to TOE PR26 – USB Game Controller

A.6 Report Descriptor (Mouse)

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved 12

ROBOT . HEAD to TOE PR26 – USB Game Controller

Appendix B

PCB Layout:

+

- -

+

- +

wire

wire wire

wire

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved 13

ROBOT . HEAD to TOE PR26 – USB Game Controller

USB Wire jumper Type-B Box 10K Header 10K 1N4148 330 C-cap 10K Wire jumper Wire jumper C-cap C-cap 10K 104 104 104 C-cap E-cap 10K 104 PIC18F2550 50V 1uF C-cap Wire jumper 104 20Mhz C-cap C-cap Crystal 104 104 C-cap C-cap C-cap C-cap 10K C-cap 30pF 10K C-cap 104 104 104 C-cap 104 104 10K 330 104 10K 10K 10K C-cap 104 Joystick 1 C-cap Joystick 2 10K 104 10K

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved 14

ROBOT . HEAD to TOE PR26 – USB Game Controller

* Cytron Technologies reserved the right to replace the component in the list with component of the same functionality without prior notice.

Prepared by

Cytron Technologies Sdn. Bhd.

19, Jalan Kebudayaan 1A,

Taman Universiti,

81300 Skudai,

Johor, Malaysia.

Tel: +607-521 3178

Fax: +607-521 1861

URL: www.cytron.com.my Email: [email protected] [email protected]

Created by Cytron Technologies Sdn. Bhd. – All Rights Reserved 15