Using the HT66FB5x0 for 2D Joystick Application (C Language) 1 Using the HT66FB5x0 for 2D Joystick Applications C Language Example D/N : AN0323E Introduction The HT66FB5x0 series of devices are 8-bit A/D type Flash MCUs with a USB interface. This application note provides a reference for users about how to use the HT66FB550 to implement a USB 2D Joystick application using C language. Operating Principle Joystick Introduction A joystick is a game controller which works together with game machines and software to provide players with better playing experience. In this application, the 2D Joystick includes four operating keys and a Coolie Hat setup by X/Y axis parameters. It is a simple type joystick using USB communication. A joystick usually uses two potentiometers, whose resistance change will be converted to a positional change using the MCU A/D converter. The MCU is configured as a Joystick USB device by the descriptor settings. When the MCU is connected to a PC, the PC will automatically install the system-provided Joystick HID driver. Then the PC can identify the position and key information sent from the MCU through the USB. The HT66FB550 device includes an integrated A/D, USB SIE, etc., and is suitable for USB Joystick and other device developments. The following text will introduce how to use the HT66FB550 to develop a 2D Joystick application with one Coolie Hat and four keys. HT66FB550 main features: Flash Program Memory: 8K16 RAM Data Memory: 7688 USB 2.0 Full Speed compatible Supports 6 endpoints All endpoints except endpoint 0 can support interrupt and bulk transfer All endpoints except endpoint 0 can be configured as 8, 16, 32, 64 bytes FIFO size Endpoint 0 supports control transfer, 8 bytes FIFO
13
Embed
Using the HT66FB5x0 for 2D Joystick Applications C ...Using the HT66FB5x0 for 2D Joystick Application (C Language) 2 Supports 3.3V LDO and internal UDP 1.5k pull-up resistor Internal
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Using the HT66FB5x0 for 2D Joystick Application (C Language)
1
Using the HT66FB5x0 for 2D Joystick Applications C Language Example
D/N : AN0323E
Introduction The HT66FB5x0 series of devices are 8-bit A/D type Flash MCUs with a USB interface.
This application note provides a reference for users about how to use the HT66FB550 to
implement a USB 2D Joystick application using C language.
Operating Principle
Joystick Introduction
A joystick is a game controller which works together with game machines and software to
provide players with better playing experience. In this application, the 2D Joystick
includes four operating keys and a Coolie Hat setup by X/Y axis parameters. It is a simple
type joystick using USB communication.
A joystick usually uses two potentiometers, whose resistance change will be converted to
a positional change using the MCU A/D converter. The MCU is configured as a Joystick
USB device by the descriptor settings. When the MCU is connected to a PC, the PC will
automatically install the system-provided Joystick HID driver. Then the PC can identify the
position and key information sent from the MCU through the USB.
The HT66FB550 device includes an integrated A/D, USB SIE, etc., and is suitable for
USB Joystick and other device developments. The following text will introduce how to use
the HT66FB550 to develop a 2D Joystick application with one Coolie Hat and four keys.
HT66FB550 main features:
Flash Program Memory: 8K16
RAM Data Memory: 7688
USB 2.0 Full Speed compatible
Supports 6 endpoints
All endpoints except endpoint 0 can support interrupt and bulk transfer
All endpoints except endpoint 0 can be configured as 8, 16, 32, 64 bytes FIFO size
Endpoint 0 supports control transfer, 8 bytes FIFO
Using the HT66FB5x0 for 2D Joystick Application (C Language)
2
Supports 3.3V LDO and internal UDP 1.5k pull-up resistor
Internal 12MHz HIRC with 0.25% accuracy for all USB modes
Two 10-bit CTMs, one 10-bit STM and one 16-bit STM
Serial Interface Modules with Dual SPI and I2C interfaces
Supports In System Programming function - ISP
16 channels 12-bit resolution A/D converter
Dual Comparator functions
HT66FB550 A/D Converter Introduction
A/D Converter Registers
ADRL, ADRH: store the converted data, the data format is controlled by ADRFS bit;
ADCR0: controls the A/D power on/off, start function and A/D channel selection;
ADCR1: selects A/D reference voltage and A/D clock source;
ACER0: determines which pins on PB0~PB6 and PA7 are used as A/D converter inputs;
ACER1: determines which pins on PC0~PC7 are used as A/D converter inputs;
A/D Conversion Steps
1) Select the required A/D conversion clock by correctly programming the
ADCK2~ADCK1 bits in the ADCR1 register.
2) Enable the A/D by clearing the ADOFF bit in the ADCR0 register to zero.
3) Select which channel is to be connected to the internal A/D converter by correctly
programming the ACS4~ACS0 bits which are also contained in the ADCR1 and
ADCR0 registers.
4) Select which pins are to be used as A/D inputs and configure them by correctly
programming the ACE15~ACE0 bits in the ACER1~ACER0 registers.
※ If the A/D interrupt is to be used, the A/D converter interrupt bit, ADE, and the
master interrupt control bit, EMI, must both be set high to do this.
5) Setup the START bit in the ADCR0 register from low to high and then low again to
initialise an A/D conversion process.
6) Check the EOCB bit in the ADCR0 register. The conversion process is completed
when the EOCB bit goes low, then the user can read the ADRH and ADRL registers
to obtain the converted value.
※ If both the A/D interrupt and global interrupt are enabled and the stack is not full,
when an A/D conversion process is completed, an A/D interrupt occurs.
Using the HT66FB5x0 for 2D Joystick Application (C Language)
3
HT66FB550 USB Interface Introduction
The HT66FB550 has a series of registers associated with USB operation: SYSC,
Field Position Size/Byte Name Example bLength 0 1 Descriptor Length 0x09 bDescriptorType 1 1 Descriptor Type 0x02 bTotalLength 2 2 Configuration Total Length 0x0022 bNumInterfaces 4 1 Interface Numbers 0x01 bConfigurationValue 5 1 Configuration Value 0x01 iConfiguration 6 1 Configuration Name Index 0x00 bmAttributes 7 1 Configuration Attributes 0x80 bMaxPower 8 1 Max Power Consumption 0x32
Using the HT66FB5x0 for 2D Joystick Application (C Language)
6
bmAttributes:
D7 D6 D5 D4 D3 D2 D1 D0
1 Self-provided
Power RemoteWake-up
0 0 0 0 0
In this application, the power is supplied by the bus, a remote wake-up function is not
supported.
bMaxPower: 2mA as a unit, the maximum power consumption of this application device is
50×2=100mA.
Interface Descriptor
Field Position Size/Byte Name Example bLength 0 1 Descriptor Length 0x09 bDescriptorType 1 1 Descriptor Type 0x04 bInterfaceNumber 2 1 Interface Serial Number 0x00 bAlternateSetting 3 1 Alternate Setting 0x00 bNumEndpoints 4 1 Endpoint Numbers 0x01 bInterfaceClass 5 1 Interface Class 0x03 bInterfaceSubClass 6 1 Interface Sub-Class 0x00 bInterfaceProtocol 7 1 Interface Protocol 0x00 iInterface 8 1 Interface Name Index 0x00
In this application, the interface class is HID (0x03), the interface sub-class and interface
protocol code are both 0x00.
HID Descriptor
Field Position Size/Byte Name Example bLength 0 1 Descriptor Length 0x09 bDescriptorType 1 1 Descriptor Type 0x21 bcdHID 2 2 HID Protocol 0x0110 bCountryCode 4 1 Country Code 0x00
This application program is mainly to enumerate the device as a 2D Joystick, then use the
A/D converter to sample the X/Y displacement and the pressed status of five keys. When
the PC is accessing endpoint 1, the associated data will be sent out.
The program contains several parts, main program, USB interrupt subroutine, endpoint
FIFO accessing subroutine, endpoint 1 and 0 data processing subroutine, Coolie Hat X/Y
displacement sampling and key scanning subroutine.
Main Program Flowchart
Start
Initialise I/O ports, A/D, timer and USB
Enable USB interrupt and global
interrupt EMI
suspend?Enter halt,
wait for wake-up
enumerationfinished?
Joystick and key scanning,
update data buffer
Y
Y
N
N
Using the HT66FB5x0 for 2D Joystick Application (C Language)
9
USB Interrupt Subroutine Flowchart
Using the HT66FB5x0 for 2D Joystick Application (C Language)
10
Coolie Hat X/Y Displacement Sampling and Key Scanning
Subroutine
This subroutine is mainly used to implement four keys and Joystick switch scanning and
status updating. It uses AN4 and AN7 to sample Y and X displacements respectively.
Using the HT66FB5x0 for 2D Joystick Application (C Language)
11
Endpoint 0 Data Processing Subroutine Flowchart
Endpoint 0 data processing subroutine implements the following functions:
1. Analyse the setup token to determine the descriptor type and data length.
2. Analyse the in token and transmit data according to the data length.
Using the HT66FB5x0 for 2D Joystick Application (C Language)
12
Endpoint 1 Data Processing Subroutine Flowchart
Start
Clear endpoint 1 accessed flag
Clear key scanning finished flag; clear
transmit data buffer
Store X and Y axis data to buffer0 and buffer1
Key status changed?
Y
N
tx=1, transmit buffer data via endpoint 1
End
USB enumeration and key scanning finished?
Y
N
Buffer4=preBTNpreBTN=0
This subroutine uses endpoint 1 to transmit X/Y axis and key buffer data to the host.
Endpoint FIFO Accessing subroutine Flowchart
Using the HT66FB5x0 for 2D Joystick Application (C Language)
13
Test Result
Here we use the Windows XP system-provided game controller to test the 2D Joystick
usage effect. Open the “Game Controller” in the control panel, connect the device to the
PC, then the device will be shown in the controller window. Double-click the installed
device to start testing. When the joystick is moved, the “+” sign in the test window should
also move. Press four keys and the Joystick switch and the five button signs in the test
window should turn red.
Program Example Program Language: C Language HOLTEK IDE3000 Compiler: Holtek C Compiler V3.10 Configuration Option: High Freq.OSC: HIRC Low Freq.OSC: LIRC Fsub clock source: LIRC ;other option selected by user.
For other program codes and descriptions refer to the attachment.
Conclusion Using the HT66FB550 as a master MCU, this text has introduced how to use the A/D
converter and USB interface to achieve a simple USB 2D Joystick which contains a