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

USB Game Controller

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: USB Game Controller

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

OVERVIEW

This document describes the development of Cytron Technologies DIY (Do It Yourself) Project PR26. This firmware implements a standard USB game controller with 12 inputs (4 directional buttons and 8 general purpose buttons) and 4 analog inputs. There are no drivers required, since USB standard defined device classes, this project using the human input device (HID) which allows user to tell the computer that the connected USB device is a game controller. This project will be using PIC18F2550, push button and joystick to give a guideline to hobbyist on how to build a standard USB Game Controller. The performance of the USB Game Controller can be tested by using PC.

PR26 Just connect, play and you learn - it's that simple. Schematic and source code is provided. FEATURES

PIC18F2550 - 8-bit microcontroller with 22 I/O - Operate with 5V supply - Operating speed 20MHz - Full Speed USB 2.0 (12Mbit/s) interface

Joystick - With 2-axis (x-axis and y-axis) and 1 push

button

USB Game 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.

Page 2: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

SYSTEM OVERVIEW

GENERAL DESCRIPTION PIC18F2550

Ideal for low power (nanoWatt) and connectivity applications that benefit from the availability of three serial ports: FS-USB (12 Mbit/s), I²C™ and SPI™ (up to 10Mbit/s) and an asynchronous (LIN capable) serial port (EUSART). Large amounts of RAM memory for buffering and Enhanced FLASH program memory make it ideal for embedded control and monitoring applications that require periodic connection with a (legacy free) Personal Computer via USB for data upload/download and/or firmware updates. Feature of the device: • High-Current Sink/Source: 25 mA/25 mA • Three External Interrupts • Four Timer modules (Timer0 to Timer3) • Up to 2 Capture/Compare/PWM (CCP) modules: • Enhanced Capture/Compare/PWM (ECCP)

module • Enhanced USART module • 10-bit, up to 13-channel Analog-to-Digital

Converter module (A/D) with Programmable Acquisition Time

• Dual Analog Comparators with Input Multiplexing • 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

devices only) - Wide Operating Voltage Range (2.0V to 5.5V)

More information on PIC18F2550 can visit the website at: http://ww1.microchip.com/downloads/en/DeviceDoc/39632D.pdf HARDWARE

This project will require following hardware:

a. 1 x PIC18F2550 b. 1 x PR26 Printed Circuit Board (PCB) c. 12 x push button d. 2 x joystick e. 1 x USB B type connector f. 1 x USB cable g. 1 x 9V-12V power supply h. Related electronic components

Please refer to Appendix A for the board layout of PR26. The board layout is provided free therefore Cytron Technologies will not be responsible for any further modification or improvement. Interface joystick with PIC18F2550

Figure 1

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

2 Joysticks

PIC18F2550

12 Push Buttons

Computer HID USB interface

Page 3: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

10-bit digital number but we only use 8-bit for this project. The module has five registers:

• A/D Result High Register (ADRESH) • A/D Result Low Register (ADRESL) • A/D Control Register 0 (ADCON0) • A/D Control Register 1 (ADCON1) • A/D Control Register 2 (ADCON2)

Figure 2

In this PR, we have 4 analog outputs from two joysticks (x-axis, y-axis, z-axis, z-rotation), so analog input pin of AN0-AN3 are connected to the 4 presets from joysticks. But make sure the ADC configuration in the programming is correct.

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. Interface HID Report Descriptor with joystick For this PR, we are going to implements a standard USB game controller with twelve inputs (four directional buttons and eight general purpose buttons).

Since USB standard defined device classes, this project using the human input device (HID) which allows user to tell the computer that the connected USB device is a game controller and has 2 joysticks and 12 buttons. First of all, we have to set down the data in the HID Report Descriptor to tell the computer of what function 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.usb.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 • Configuration • Interface • Endpoint • String

USB Descriptor 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 4

• Report Descriptor 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 number of data fields required for the device’s report or reports. A Report descriptor is the complete set of all items for a device. By looking at a Report descriptor

Page 4: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

alone, an application knows how to handle incoming data, as well as what the data could be used for. The Report descriptor is made up of items that provide information about the device. The first part of an item contains three fields: item type, item tag, and item size. Together these fields identify the kind of information the item provides. There are three item types: Main, Global, and Local. There are five Main item tags currently defined:

• Input item tag: Refers to the data from one or more similar controls on a device. For example, variable data such as reading the position of a single axis or a group of levers or array data such as one or more push buttons or switches.

• 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 of levers (variable data). Or, it can represent data to one or more LEDs (array data).

• Feature item tag: Describes device input and output not intended for consumption by the 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):

- Input (Output or Feature) - Usage - Usage Page - Logical Minimum - Logical Maximum - Report Size - Report Count

For details about these descriptors as defined for a HID class device, see Appendix A: Example USB Descriptors for HID Class Devices (Mouse). For general information about standard USB, please refer to Device Class Definition for Human Interface.pdf and Devices (HID) HID Usage Tables.pdf attached together with this project.

• Physical Descriptors A Physical Descriptor is a data structure that provides information about the specific part or parts of the human body that are activating a control or controls. For example, a physical descriptor might indicate that the right hand thumb is used to activate button 5. An application can use this information to assign functionality to the controls of a device. Note: Physical Descriptors are entirely optional. They add complexity and offer very little in return for most devices. However, some devices, particularly those with a large number of identical controls (for example, buttons) will find that Physical Descriptors help different applications assign functionality to these controls in a more consistent manner. To make a clearer picture of HID device class descriptor structure, all of these things can be combined as shown below. Understand the HID descriptor well will help you know more about this PR26.

Figure 5 For this PR26, we will update the particular data every time reach the line “hid_report_in[ ]= ” in the main program.

Figure 6

Each data is indicated by different index. The file descriptor can be summarized as the table below.

Page 5: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

Index (byte) Name 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 descriptor (in usb_descriptor for this PR).

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 program. Note that there is 0x06 at behind, it means the total 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 HID Usage Tables.pdf attached together with this project.

Power supply for the circuit

Figure 9 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 10 Push Button as input of PIC microcontroller

Figure 11

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.

Page 6: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

ICSP for programming PIC microcontroller

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?cid=81 PCB circuit board

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

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

12 3

4

8

5 6

7

9

10

Page 7: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

SOFTWARE

Flow Chart:

The program is provided by Microchip Technology Inc. In this project, we modify the program to use as game 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 source code as the comment of each line. The source code is provided free and Cytron Technologies will not be responsible for any further modification or improvement. GETTING START

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.

1. Once user has the hardware set, soldering process can be started. Please solder the electronic components one by one according the symbols or overlays on the Printed Circuit Board (PCB). Ensure the component value and polarity is correctly soldered. Please refer to PCB Layout in Appendix B. Remember there is 4 jumpers that need to be soldered. Please use the single core wire given.

Caution: Make sure all the connectors (2510) are

soldered in proper side. Those electronic components have polarity such as capacitor, diode, PIC, PIR sensor, buzzer and LED should be soldered in right polarity or it may cause the circuit board fail to work.

Warning:Before the battery (Power) is plugged in,

make sure the polarity is correct to prevent the explosion. Wrong polarity of capacitor also may cause explosion.

2. Connect the USB cable to the USB type-B

connector. 3. Please download the necessary files and

document from Cytron Technologies website. These included documentation, sample source code, schematic, component list and software.

4. The next step is to install MPLAB IDE and

Microchip C18 Compiler into a computer. The MPLAB IDE and C18 Compiler can be downloaded from www.cytron.com.my Please refer document to install MPLAB software and C18 compiler software.

5. After the installation complete, open the

project file provided using MPLAB IDE. Please refer Installation of C18 compiler and open project_18F document to open project.

Start

Initialize PIC

Analog switch pressed?

NO

Analog deactivated

Analog activated

Save updated value

NO

YES

YES

NO

NO

YES

NO

YES

Save updated value

Save updated value

Send all updated value to computer

Save updated value

First time connected to PC?

Installation

YES

YES

Press directional

button?

Push button pressed?

Press directional

button?

Move joystick?

Page 8: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

6. When you create project for the game

controller, create the subfolder and include the 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 15

9. To add file in subfolder Files and Source Files, right click on the folder Files, then click on Add Files…,

10. User may repeat the similar steps to create

subfolder in the header file. First, right click on the Header Files, then click on Create Subfolder…,

11. Another file need to add is rm18f2550-HID

Bootload.lkr, which is under Linker Script.

12. Diagram below shown the example of file

after all necessary file added.

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 the power. The programmer is not included in the hardware set but it can be found at Cytron website. (User manual is provided at website).

14. Make sure when you load the hex file into

PIC microcontroller, USB cable is connected to USB type-B connector and computer. It is because power supply is gets from computer through USB cable.

15. Test the functionality of the PCB board.

16. Have fun!

Page 9: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

• 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.

2. Click Control Panel from Start in your PC and open Game Controller

Figure 17

• If the device is successfully installed, Cytron Game Controller will appear in the installed game controller list

Figure 18

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

Figure 19

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

when directional button is pressed. • Buttons on screen from 1 to 12 will light

on when button on game controller is pressed.

• 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 electronic component before soldering it to board.

Page 10: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

Appendix A A.1 Device Descriptor:

Page 11: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

A.2 Configuration Descriptor:

A.3 Interface Descriptor (Mouse):

Page 12: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

A.4 HID Descriptor (Mouse):

A.5 Endpoint Descriptor (Mouse):

Page 13: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

A.6 Report Descriptor (Mouse)

Page 14: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

Appendix B PCB Layout:

+ -

+

- -

+

wire

wire

wire

wire

Page 15: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

330 10K

10K

10K

10K

10K

10K

10K

10K

10K

C-cap 104

C-cap 104

C-cap

104

C-cap 104

C-cap 104

Box Header

USB Type-B

1N4148

E-cap 50V 1uF

20Mhz Crystal

C-cap 30pF

10K 10K

C-cap 104

C-cap 104

10K

C-cap 104

C-cap 104

C-cap 104

C-cap 104

C-cap 104

C-cap 104

C-cap 104

Joystick 1 Joystick 2

PIC18F2550

10K

C-cap 104

330

Wire jumper

Wire jumper

Wire jumper Wire jumper

Page 16: USB Game Controller

ROBOT . HEAD to TOE

PR26 – USB Game Controller

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

* 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]