This page provides an example of interfacing the Copal Electronics CJ25 Joystick Encoder to a Microchip MCU. The demonstration code and hardware schematic is provided for reference and could be leveraged for other suppliers MCU product.
Introduction
The Copal Electronics CJ25 Series Joystick Encoder combines a 4 or 8 direction joystick, push switch function, and 20 position encoder, into a single package. In this evaluation design, all of the functionality of the joystick is supported.
The Microchip dsPIC30F3011-30I/PT was selected because it includes a quadrature encoder peripheral for the encoder function, and sufficient I/O to support the joystick position and switch input, and various indicator outputs. The multiplexed outputs drive individual LED’s that illuminate and indicate the joystick position or pushbutton press, and also drive a 2-digit, 7-segment numeric display to indicate the encoder count.
System Design
The MCU provides sufficient I/O and peripherals to support all CJ25 functionality. Program flow is comprised of I/O and peripheral initialization, with all joystick position/encoding/switch actions processed in an interrupt driven timing loop that executes at a 100Hz rate.
When the interrupt is triggered, the interrupt service routine samples the joystick position, the joystick pushbutton switch, and the encoder output. This same interrupt routine then drives the numeric display, drives individual LED’s representing joystick position and button press state, resets the interrupt timer, clears the interrupt flag, and returns from the interrupt.
Joystick
The joystick direction is indicated by the output of a voltage divider circuit. When the joystick is centered, the X and Y joystick direction outputs are at +2.5V. With a joystick position change, the X or Y output voltage will be at +2.5V, ground, or +5V. Table 1 provides joystick position and corresponding X and Y voltage output (all voltages nominal), relative positions provided in Figure 1.
The position of the joystick could be decoded using discrete voltage comparators but the MCU comes equipped with an analog-to-digital converter (ADC) which is used to sample the X and Y joystick position output voltage. The ADC digital output is evaluated by the MCU software to determine the current joystick position.
Figure 1, Joystick position.
Push-on Switch
The joystick center position provides a single-throw single-pole switch action. This example implementation uses a hardware debounce filter.
Encoder
The encoder output code is a 2-bit, A/B channel. The MCU quadrature encoder peripheral greatly simplifies encoder monitoring since no software intervention is required to update the count. Every interrupt cycle, the encoder peripheral count register is read, compared to the previous count, then the display value is updated. Note: Don’t forget to add the pullups on the encoder A and B outputs (Figure 2). They are not included in the CJ25 joystick.
Figure 2, Joystick circuit.
LED Indicators
The joystick position and switch indicator LED’s are multiplexed to reduce the MCU pin count. Each pair of LED’s is connected anode-to-cathode with a single series current limit resistor connected to multiple LED pairs as illustrated in Figure 3.
Note: This application didn’t require both LED’s of an LED pair to illuminate. If the need arose to drive both LED’s ON, the output pin could toggle between Vdd and GND at a periodic rate, ideally with a duty cycle of ~50%. Both LED’s will illuminate but will have reduced light output.
Figure 3, LED Pair.
LED Numeric Display
The 2-digit numeric LED display is used to provide the encoder count value.
As can be seen in Figure 4, each digit LED segment is in parallel with the other, i.e. digit 1, LED segment C is wired in parallel with digit 2, LED segment C. All of the LED segments of each digit are connected in a common anode configuration (common cathode configurations are also available).
Update of the digits is controlled in the interrupt routine, with each digit updated at a 50% duty cycle. Every other pass through the interrupt routine will refresh the count value displayed by digit 1, with alternate passes updating digit 2.
Figure 4, 2-digit LED Display.
Conclusion
This completes the CJ25 interface description. The SW source files and HW schematic are included below to jump start any project that takes advantage of the control features the CJ25 joystick provides. The software project is compatible with MPLAB X IDE.
Supporting Files
Source Code
COPAL_CJ25_demo.X.zip (99.8 KB)
Hardware Schematic and BOM
JoystickSchem.pdf (36.7 KB)
COPALJOYBOM_final.xlsx (12.4 KB)