Hi,
Anyone know of simple code that reads button presses from the ps2 controller which in turn toggles the digital IO pins on the Bot Board II? Trying to write it myself but having problems with the language (Very new to programming). I’m consulting the mbasic manual and scavenging ps2 sections from other programs. Any help or suggestions would be helpful.
Here is a quick and dirty. Some cut and paste from examples…
[code];--------------------------------------------------------------------
TRUE con 1
FALSE con 0
;[PS2 Controller]
PS2DAT con P12 ;PS2 Controller DAT (Brown)
PS2CMD con P13 ;PS2 controller CMD (Orange)
PS2SEL con P14 ;PS2 Controller SEL (Blue)
PS2CLK con P15 ;PS2 Controller CLK (White)
PadMode con $79
;--------------------------------------------------------------------
;[PIN NUMBERS]
RightChannelPin con P10 ;Sabertooth Motor driver Right channel
LeftChannelPin con P11 ;Sabertooth Motor driver Left channel
;[Ps2 Controller]
DualShock var Byte(7)
DSLast var Byte(7)
DSChanged var byte
LastButton var Byte(2)
DS2Mode var Byte
PS2Index var byte
i var sword
;--------------------------------------------------------------------
;[REMOTE]
DeadZone con 10 ;The deadzone for the analog input from the remote
;====================================================================
;Interrupt init
;ENABLEHSERVO ;Enable Interrupts for HServo
enable
low RightChannelPin
low LeftChannelPin
;====================================================================
;[INIT]
;PS2 controller
sound P9,[100\5000, 100\4500, 100\4000]
high PS2CLK
LastButton(0) = 255
LastButton(1) = 255
pause 100
;====================================================================
;[MAIN]
main:
;Read remote input
GOSUB Ps2Input
pause 100
goto main
;--------------------------------------------------------------------
;[PS2Input] reads the input data from the PS2 controller and processes the
;data to the parameters.
Ps2Input:
low PS2SEL
shiftout PS2CMD,PS2CLK,FASTLSBPRE,$1\8]
shiftin PS2DAT,PS2CLK,FASTLSBPOST,[DS2Mode\8]
high PS2SEL
pause 1
low PS2SEL
shiftout ps2CMD,PS2CLK,FASTLSBPRE,$1\8,$42\8]
shiftin ps2DAT,ps2CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
high PS2SEL
pause 1
if DS2Mode <> PadMode then
DEBUGOUTPUT con 1
#ifdef DEBUGOUTPUT
low p0
serout s_out, i9600, "DS mode: ", hex DS2MODE, 13]
high p0
#endif
low PS2SEL
shiftout PS2CMD,PS2CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
high PS2SEL
pause 1
low PS2SEL
shiftout PS2CMD,PS2CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
high PS2SEL
pause 1
low PS2SEL
shiftout PS2CMD,PS2CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
high PS2SEL
pause 1
low PS2SEL
shiftout PS2CMD,PS2CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
high PS2SEL
pause 1
low PS2SEL
shiftout PS2CMD,PS2CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
high PS2SEL
pause 288
goto ps2input
endif
; IF (DualShock(1).bit4 = 0) and LastButton(0).bit4 THEN ;Up Button test
; ENDIF
; IF (DualShock(1).bit6 = 0) and LastButton(0).bit6 THEN ;Down Button test
; ENDIF
; IF (DualShock(2).bit4 = 0) and LastButton(1).bit4 THEN ;Triangle Button test
; ENDIF
; IF (DualShock(2).bit5 = 0) and LastButton(1).bit5 THEN ;Circle Button test
; ENDIF
; IF (DualShock(2).bit6 = 0) and LastButton(1).bit6 THEN ;Cross Button test
; ENDIF
; IF (DualShock(2).bit7 = 0) and LastButton(1).bit7 THEN ;Square Button test
; ENDIF
IF (DualShock(2).bit3 = 0) AND (LastButton(1).bit3 = 1) THEN;R1 Button test
toggle RightChannelPin
ENDIF
IF (DualShock(2).bit2 = 0) AND (LastButton(1).bit2 = 1) THEN;L1 Button test
toggle LeftChannelPin
ENDIF
; IF (DualShock(2).bit0 = 0) THEN;AND (LastButton(1).bit0 = 1) THEN ;L2 Button test
; ENDIF
; IF (DualShock(2).bit3 = 0) THEN;AND (LastButton(1).bit3 = 1) THEN ;R2 Button test
; ENDIF
#ifdef DEBUGOUTPUT
DSChanged = FALSE
for i = 0 to 6
if DualShock(i) <> DSLast(i) then
DSLast(i) = DualShock(i)
DSChanged = TRUE
endif
next
if DSChanged then
serout s_out, i9600, "DS: ", hex DualShock(0), " ", hex Dualshock(1), " ", hex DualSHock(2), " ", hex DualShock(3), " ", hex DualSHock(4), |
" ", hex DualShock(5), " ", hex DualShock(6), 13]
endif
#endif
LastButton(0) = DualShock(1)
LastButton(1) = DualShock(2)
return
[/code]
There are several full program examples of this up on line. Some of this is from the Brat with at tude…
Good Luck
Kurt
Well I tried cutting out all of the stuff I thought I didn’t need from the ps2 code for controlling the J5 robot but of course it’s riddled with errors that multiply when I try to fix it. I guess I just need to completely start from scratch and do some serious programming homework. Just rushing too much to get results.
What I want is a basic program that reads button pushes from the d-pad (up,down,left,right), A,S,O,X,L1,L2,R1,R2 for a total of 12 buttons which in turn toggle pins 0-11 on the BBII.
DAT con P12
CMD con P13
SEL con P14
CLK con P15
temp var byte ’ Variable definitions.
buttons var word
Lastbuttons var word
bpress1 var bit
bpress2 var bit
Button_L1 var bit
Button_L2 var bit
Button_R1 var bit
Button_R2 var bit
Button_A var bit
Button_O var bit
Button_X var bit
Button_S var bit
Dpad_UP var bit
Dpad_RIGHT var bit
Dpad_LEFT var bit
Dpad_DOWN var bit
low P10 ’ Ensure device connected to pin 10 and 11 is off on startup
low P11
high CLK ’ Ensure CLK is negative going.
sound 9, [100\880, 100\988, 100\1046, 100\1175] 'four quick ascending notes.
pause 1000
setup ’ This section sets the PSX Controller to Analog Mode. (red LED should light)
’ This will work with Sony and Madcatz wired, but not with Medcatz wireless.
’ You must press the analog button on the controller to set to analog mode.
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8]
shiftin DAT,CLK,FASTLSBPOST,[temp\8]
high SEL
pause 1
if (temp <> $73) and (temp <> $79) then
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
high SEL
pause 1
low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
high SEL
pause 100
low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
high SEL
pause 1
;low SEL
;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
;high SEL
;pause 1
low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
high SEL
pause 1
low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
high SEL
pause 1
goto setup
endif
gosub get_PSX_data
main
gosub get_PSX_data
’ I/O on/off
if Button_A and (bpress1=0) then ' This makes a latching output pin that responds to button presses.
bpress1=1
toggle p10
endif
if (Button_A=0) and bpress1 then
bpress1=0
endif
if Button_X and (bpress2=0) then ' This makes a latching output pin that responds to button presses.
bpress2=1
toggle p11
endif
if (Button_X=0) and bpress2 then
bpress2=0
endif
get_PSX_data ’ This section gets the data from the PSX controller.
’ The first byte is the mode (Temp)
’ The 2 next bytes are the pushbutton data (button1, button2).
’ The 4 next bytes are the analog joystick data (rhori,rvert,lhori,lvert).
Lastbuttons = buttons
low SEL
’ Initiate request for data from PSX controller.
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]
’ Temp is the Mode value, it will be dumped. Then it puts the button data into two byte variables.
shiftin DAT,CLK,FASTLSBPOST,[temp\8,buttons.LOWBYTE\8,buttons.HIGHBYTE\8]
high SEL
pause 1
buttons = buttons ^ $FFFF ' SO WE CAN USE POSITIVE BOOLEAN LOGIC WHEN EVALUATING BUTTONS
Button_SELECT = Buttons.bit0
Button_START = Buttons.bit3
Dpad_UP = Buttons.bit4
Dpad_RIGHT = Buttons.bit5
Dpad_DOWN = buttons.bit6
Dpad_LEFT = Buttons.bit7
Button_L2 = Buttons.bit8
Button_R2 = Buttons.bit9
Button_L1 = Buttons.bit10
Button_R1 = Buttons.bit11
Button_A = Buttons.bit12
Button_O = Buttons.bit13
Button_X = Buttons.bit14
Button_S = Buttons.bit15
’ remove the rems (’) to see the RAW joystick and Button values in terminal 1.
’ serout S_OUT,i57600,[bin buttons\16," “]
’ serout S_OUT,i57600,[dec3 rhori\3,” “,dec3 rvert\3,” “,dec3 lhori\3,” “,dec3 lvert\3,” "]
’ serout S_OUT,i57600,[13]
return
The program I put in earlier should compile, and loop reading the PS2. IT also had examples of toggling two IO pins, one on R1 and the other on L1, so the rest should be simply adding the additional ifs for the different IO pins…
Kurt
I appologize Kurt,
I’m just such a newbie that I didn’t think all that code was necessary for such a task. I’ve been pouring over basic programs all day and I believe it’s making more sense to me now. I’ll continue to dissect and experiment and I truly thank you for your help. The program does indeed compile and toggle pins 10 and 11 with the L1 R1 buttons.
…and now I got the other I/O pins to toggle with more button presses for a total of 10 (pins 2-11)! Am I correct in the belief that P0 and P1 are analog and cannot be used as digital I/O or could they be if the AX0 and 1 jumpers are removed. Also, could I switch the ps2 receiver to the DT, CM, AT, CK and PS2 pins closer to the ABC buttons on the Bot Board to free up P12-15?
No Problem,
As for which IO pins you can use:
P0-P8 without problem
P9 if you disable the speaker
P10-P11
P16-P19 which are labeled AX0-AX3 (remove jumpers on AX0 and Ax1)
P12-P15 are used for the PS2 controller, you can move the controller To the other 4 pins where the jumpers are, but this is still P12-P15.
Kurt