Using the HT66FB5x0 for 2D Joystick Applications C ...Using the HT66FB5x0 for 2D Joystick...
Transcript of Using the HT66FB5x0 for 2D Joystick Applications C ...Using the HT66FB5x0 for 2D Joystick...
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,
USB_STAT, UINT, USC, USR, UCC, AWR, STLO, STLI, SIES, MISC, UFOEN, UFIEN,
UFC0, UFC1 and FIFOn (n=0~5).
SYSC: used for USB SIE enable/disable control, etc.
USB_STAT: used to indicate USB BUS noise detection and select UDN and UDP
pull-high register, etc.
UINT: used for endpoint interrupts control.
USC: used to select the USB or PS2 mode, indicate USB status, etc.
USR: used to indicate endpoints accessed detection.
UCC: used to select UDP pull-high resistor, MCU clock source, USB clock control and
endpoint FIFO to be accessed.
AWR: used for USB device address setup and remote wake-up control.
STLO, STLI: used for FIFO OUT and IN stall endpoints indication.
SIES: used for USB communication status indication, etc.
MISC: used for FIFO read and write operations, etc.
UFIEN, UFOEN: used for endpoints input and output FIFO control.
UFC0, UFC1: used for endpoints FIFO size selection.
FIFOn: used to store the endpoint accessing data.
For more register information, refer to the datasheet.
Using the HT66FB5x0 for 2D Joystick Application (C Language)
4
Application Circuit
PA
0/TC
K1/D
BIO
1
NC
2
UD
N/G
PIO
03
UD
P/G
PIO
14
V33O
5
UB
US
/PE
1/AV
DD
/VD
D6
HV
DD
7
VS
S8
PE
3/XT
19
PE
4/XT
210
/RE
S/D
BC
LK
11
PE
212
PE
513
PD0/T
P3_1/O
SC
114
PD1/T
P2_1/O
SC
215
PD
2/TC
K2
16
PD
317
PD
4/TC
K3
18
PD5/T
P1_1
19
PD6/T
P3_0
20
PD7/T
P2_0
21
NC
22
NC
23
NC
24P
B0/S
DO
/SD
A/A
N0
25P
B1/S
DI/S
CL
/AN
126
PB
2/SC
K/A
N2
27P
B3/S
CS
/AN
328
PB4/T
P0_1/A
N4
29P
B5/P
CK
/AN
530
PB
6/INT
1/AN
631
PB
732
PC
0/AN
833
PC
1/AN
934
PC2/A
N10
35
PC3/A
N11
36PC
4/AN
1237
PC5/A
N13
38PC
6/AN
1439
PC7/A
N15
40P
E0/V
DD
IO/V
RE
F41
PA
7/INT
0/AN
742
PA6/T
CK
0/C1-
43PA
5/TP
1_0/C1+
44PA
4/SCSA
/TP0_0/C
1X45
PA
3/SC
KA
/C0-
46P
A2/S
DIA
/C0+
47P
A1/S
DO
A/C
0X48
U2
HT
66FB550
100K
R29
300
R30
0.1uFC
12
VD
D
VC
C0.1uF
C13
KE
Y0
12
34
56
78
910
1112
ICP
/OC
DS
12
J15
DP
I
F1
Fuse
D9
Yellow
Vin
Vout
GN
D
U1
HT
7833
10uF/25V
C6
1 2 3
J16P
A0
UD
N
VD
D
OC
DS
CK
SH
EL
D6
GN
D5
NC
4D
+3
D-
2V
US
B1
7
US
BB
ead FB
1
33R
1633
R17
47P
C10
47P C11
UD
NU
DP
VU
SB
1 2 3
J10
1 2 3
J14
VU
SB
VD
DIN
VE
XT
1 2
P3
0.1uF
C8
10uF/25V
C7
0.1uF
C9
470R
2
D10Y
ellow
470R
1
VD
D
V33O
KE
Y1
KE
Y2
KE
Y3
PD
4P
B0
PB
1
PB
2
PB
3
1
2
3
4
5
6789 10
2D Joystick
12
34
56
J9Header 3X
2
PA
6
PB
4P
A7
VD
D
ST
ICK
_U/D
ST
ICK
_L/R
ST
ICK
_SW
OC
DS
CK
PA
0
UD
NU
DP
OC
DS
CK
PA
6P
A7
PB
0P
B1
PB
2P
B3
PB
4
PD
4
V33O
VD
D
GN
DV
DD
PO
WE
R
KE
Y
2D Joystick
ICP/O
CD
S
US
B
Using the HT66FB5x0 for 2D Joystick Application (C Language)
5
Program Description
The resistors and capacitors in the USB cable of the USB interface circuit are mainly
used for impedance matching to reduce interference, improve communication stability
and reduce the possibility of data errors during USB data transmission. The USB host
device power uses a bead to provide a noise free power source for the system.
This Joystick is essentially composed of two potentiometers and a switch. The 2D
Joystick circuit mainly uses the A/D converter to convert the joystick position information
to electrical signals. PB4 and PA7 correspond to A/D channel AN4 and AN7, they are
used to sample the Y and X axis displacements respectively. PA6 is used to sample the
switch information. When detecting another four keys, KEY0~KEY3, PD4 first outputs a
low level, PB0~PB3 are setup as inputs and their internal pull-high resistors are enabled.
If a low level is detected on PB0~PB3, which means that one of the four keys has been
pressed, the corresponding key value will be sent to the PC.
USB 2D Joystick Descriptors Introduction
The 2D Joystick is a USB HID device. This section introduces the USB 2D Joystick
descriptors used in this application program.
Device Descriptor
Field Position Size/Byte Name Example
bLength 0 1 Descriptor Length 0x12 bDescriptorType 1 1 Descriptor Type 0x01 bcdUSB 2 2 Protocol Version 0x0110 bDeviceClass 4 1 Device Class 0x00 bDeviceSubClass 5 1 Device Sub-Class 0x00 bDeviceProtocol 6 1 Device Protocol 0x00 bMaxPacketSize0 7 1 Max Packet Size 0x08 idVender 8 2 Vender ID code 0x04d9 idProduct 10 2 Product ID code 0xffff bcdDevice 12 2 Device Version 0x0001 iManufacturer 14 1 Manufacturer Name Index 0x01 iProduct 15 1 Product Name Index 0x02
iSerialNumber 16 1 Product Serial Number
Index 0x00
bNumConfigurations 17 1 Configuration Numbers 0x01
Configuration Descriptor
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
bNumDescriptors 5 1 Subsequent Descriptor
Numbers 0x01
bDescriptorType 6 1 Subsequent Descriptor Type 0x22 wEntityLength 7 2 Subsequent Descriptor Length 0x0052
The subsequent descriptor is a report descriptor, its type code is 0x22.
Endpoint Descriptor
Field Position Size/Byte Name Example bLength 0 1 Descriptor Length 0x07 bDescriptorType 1 1 Descriptor Type 0x05 bEndpointAddress 2 1 Endpoint Address 0x81 bmAttributes 3 1 Endpoint Attributes 0x03 wMaxPacketSize 4 2 Max Packet Size 0x0008 bInterval 6 1 Polling Interval 0x08
bEndpointAddress: 0x81 indicates that endpoint 1 is setup as input.
Using the HT66FB5x0 for 2D Joystick Application (C Language)
7
D7 D6 D5 D4 D3 D2 D1 D0
In/Out Direction 0 0 0 Endpoint Address
bmAttributes: 0x03 indicates that endpoint transfer type is interrupt transfer.
0x00 0x01 0x02 0x03
Control Transfer Real-time Transfer Bulk Transfer Interrupt Transfer
bInterval: the USB host polling interval, it means how often the host will ask the device for
data. In this application, the polling interval is 8ms.
Report Descriptor 0x05,0x01, //Usage Page (Generic Desktop Control) 0x09,0x04, //Usage (Joystick) 0xa1,0x01, //Collection (Application) 0xa1,0x02, //Collection (Logical) 0x75,0x08, //Report Size (8) 0x95,0x04, //Report Count (4) 0x15,0x00, //Logical Minimum (0) 0x26,0xff,0x00, //Logical_Maximum (255) 0x35,0x00, //Physical Minimum (0) 0x46,0xff,0x00, //Physical Maximum (255) 0x09,0x30, //Usage (X) 0x09,0x31, //Usage (Y) 0x09,0x02, //Usage (Reserved) 0x09,0x02, //Usage (Reserved) 0x81,0x02, //Input(Data,Var,Abs) 0x75,0x01, //Report Size (1) 0x95,0x05, //Report Count (5) 0x25,0x01, //Logical Maximum (1) 0x45,0x01, //Physical Maximum (1) 0x05,0x09, //Usage Page (Button) 0x19,0x01, //Usage Minimum (1) 0x29,0x05, //Usage Maximum (5) 0x81,0x02, //Input (Data, Variable, Absolute) 0x06,0x00,0xff, //Usage Page (Reserved) 0x75,0x01, //Report Size (1) 0x95,0x1b, //Report Count (27) 0x25,0x01, //Logical Maximum (1) 0x45,0x01, //Physical Maximum (1) 0x09,0x01, //Usage (Reserved) 0x81,0x02, //Input (Data, Variable, Absolute) 0xc0, //End Collection 0xa1,0x02, //Collection (Logical) 0x75,0x08, //Report Size (8) 0x95,0x07, //Report Count (7) 0x46,0xff,0x00, //Physical Maximum (255) 0x26,0xff,0x00, //Logical Maximum (255) 0x09,0x02, //Usage (Reserved) 0x91,0x02, //Feature (Data, Variable) 0xc0, //End Collection 0xc0 //End Collection
Using the HT66FB5x0 for 2D Joystick Application (C Language)
8
Byte D7 D6 D5 D4 D3 D2 D1 D0
0 X displacement
1 Y displacement 2 Reserved 3 Reserved 4 Reserved Switch Key3 Key2 Key1 Key05 Reserved 6 Reserved 7 Reserved
S/W Flowchart
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
Coolie Hat and four keys.
Attachment
HT66FB550_2DJY.rar