Digital joystick - analog interface
pik33
Posts: 2,416
So, I have an Atari type digital joystick
Its pins are: up, down, left, fight, trigger, +5, GND.
Moving the joystick shorts one or two of these pins to the ground.
To not waste P2 pins I reused my robot battery voltage measuring object:
' ADC driver for a robot
' v.0.02 - 20220110
' pik33@o2.pl
' MIT License
var
long aresult
long adcpina
long agnd
long avcc
long bresult
long adcpinb
long bgnd
long bvcc
pub start(apin,bpin) :cog |iii, m, n
adcpina:=apin
adcpinb:=bpin
cog:=coginit(16,@mixer,@aresult)
waitms(100)
return cog
pub measure() :vx,vy
return 33000*(aresult-agnd)/(avcc-agnd), 33000*(bresult-bgnd)/(bvcc-bgnd)
DAT
org
mixer setq #7
rdlong aaresult,ptra
fltl aapin
wrpin adc_config1, aapin
wxpin #%00_1101, aapin
dirh aapin
fltl abpin
wrpin adc_config1, abpin
wxpin #%00_1101, abpin
dirh abpin
mov x, #%001<<6
add x,aapin
setse1 x
loop waitse1
rdpin x,aapin
rdpin x,abpin
waitse1
rdpin x,aapin
rdpin x,abpin
waitse1
rdpin gnda,aapin
rdpin gndb,abpin
wrpin adc_config2, aapin
wrpin adc_config2, abpin
waitse1
rdpin x,aapin
rdpin x,abpin
waitse1
rdpin x,aapin
rdpin x,abpin
waitse1
rdpin vcca,aapin
rdpin vccb,abpin
wrpin adc_config3, aapin
wrpin adc_config3, abpin
waitse1
rdpin x,aapin
rdpin x,abpin
waitse1
rdpin x,aapin
rdpin x,abpin
waitse1
rdpin aaresult,aapin
rdpin abresult,abpin
wrpin adc_config1, aapin
wrpin adc_config1, abpin
setq #7
wrlong aaresult,ptra
jmp #loop 'loop
adc_config1 long %0000_0000_000_100000_0000000_00_11000_0 ' 100000 gnd
adc_config2 long %0000_0000_000_100001_0000000_00_11000_0 ' 100001 3v3
adc_config3 long %0000_0000_000_100011_0000000_00_11000_0 ' 100011 pin
x long 0
Then I connected pins via resistors.
- up - 1k
- down - 2k
- left - 4k
- right - 8k
The other ends of these resistors were connected to the P2 pin and also, via 8k resistor, to +3v3 (instead of +5)
I also connected +3v3 via 10k resistor to the trigger - not necessary, the P2 can do a pullup itself, but I want a "clear" thing not dependent on pin settings.
Then I had to write a test program:
#include "retromachine.bi" dim adc as class using "adccog.spin2" dim x,y,dir as ulong dim dirt$ as string startmachine startvideo adc.start(16,17) v.setmode(0) v.cls(154,147) do x,y=adc.measure() if x<3090 then dir=5 :dirt$="Up-left" if x>=3090 andalso x<3350 then dir=9 :dirt$= "Up-right " if x>=3350 andalso x<4000 then dir=1:dirt$= "Up " if x>=4000 andalso x<4900 then dir=6:dirt$= "Down-left " if x>=4900 andalso x<6000 then dir=10:dirt$= "Down-right" if x>=6000 andalso x<8000 then dir=2:dirt$= "Down " if x>=8000 andalso x<12000 then dir=4:dirt$= "Left " if x>=12000 andalso x<20000 then dir=8:dirt$="Right " if x>=20000 then dir=0 : dirt$= "None " if y<16000 then pinhi(56) if y>=16000 then pinlo(56) position 1,10: v.write(v.inttostr2(x,8)): position 20,10: v.write(dirt$): position 40,10: v.write(v.inttostr2(y,8)) loop
to discover that the joystick needs servicing (up contact was bad) but after that, the contraption works. 2 pins used instead of 5
The next retro task: connect an analog PC joystick which I also have.
