henryarnold
New Member
This code is a complete example of communicating with a Sony PlayStation PS2 wireless controller. It uses the hardware SPI port. Bit banging won't work on the PS2 wireless controllers because the clock is too slow and the controller errors out. The hardware SPI is very fast. The hardware SPI port sends and receives data MSB first while the PS2 requires LSB first. I flipped the commands going in and the joystick readings coming out. This means that the normal PS2 $01 and $42 header becomes $80 and $42. $42 flipped is $42.
This program will connect to the PS2, set it to analog mode, read 7 bytes of data and display in a PICAXE terminal any buttons pressed or joystick readings.
I would like to hear of a better way to flip bytes MSB for LSB. The method I used is crude but I couldn't think of anything else. NOTE: BeanieBots posted the solution which is to use REV (not the same as reverse the command). REV flips up to 16 bits.
Best Regards,
Henry Arnold
This program will connect to the PS2, set it to analog mode, read 7 bytes of data and display in a PICAXE terminal any buttons pressed or joystick readings.
I would like to hear of a better way to flip bytes MSB for LSB. The method I used is crude but I couldn't think of anything else. NOTE: BeanieBots posted the solution which is to use REV (not the same as reverse the command). REV flips up to 16 bits.
Best Regards,
Henry Arnold
Code:
'=========================================================================================
' Software to Interface PICAXE 28X2 to Sony Playstation PS2 Controller
' written by: Henry Arnold
' March 30, 2010
' This software uses the hardware SPI port of the PICAXE
' There are digital and anlog mode examples.
' The software prints the results to a PICAXE Terminal
' IMPORTANT the PIC hardware SPI does MSB first not LSB first so commands going to PS2
' controller and data coming from PS2 must be flipped. $01 becomes $80,
' and $43 becomes $C2. For data coming back, I did not flip the button bytes. I used
' them as is but they do not match normal PS2 documentation. For Joystick bytes, I
' flipped the data so it would become the normal 128 idle and 0/255 min/max.
' I ignore ACK but checking it could be an enhancement to the program.
'
' SPI Connections for PICAXE 28X2:
' DAT - pin C.4 pin 15 needs pullup (4-10K is fine)
' CMD - pin C.5 pin 16
' CLK - pin C.3 pin 14
' ATT - pin B.0 pin 21 or any other output
'Connections to the PS2 Controller
'
' Looking into the controller plug
'
' 1 2 3 4 5 6 7 8 9
' .----------.---------.----------.
' | o o o | o o o | o o o |
' \_________|_________|_________/
'
' D C N G V A C N A
' A M C N C T L C C
' T D D C T K K
'
' . | | | | | |
' | | | `--|---|------------ +V
' | | | | |
' pinC.4 w/ pullup--' | `-----|---|------------ 0V
' pinC.5-------' | |
' pinB.0--------------------' |
' pinC.3------------------------'
'=========================================================================================
#picaxe 28x2
#no_table
#com 4
#Terminal 9600
Symbol DAT = pinC.4 'Input with pull-up
Symbol CMD = pinC.5 'Output
Symbol CLK = pinC.3 'Output
Symbol ATT = pinB.0 'Output
'===============MAIN part of the program==================================================
We_Start_Here:
dirsB = %00000001 ' B.0 is output
ATT = 1
spimode10e, spimedium 'SPI HW setup, uses cmd msb first so we must flip commands
goto Analog_Mode 'or to Digital mode by commenting this out
Digital_Read: 'basic digital query
ATT =0 'ATT =attention must go low during transaction
hspiout($80, $42) 'output the header bytes $01 and $42 flipped is $80 & $42
hspiin (b7,b0,b1) ' input three bytes of data. b7 should be $5A
ATT =1 'flipped is also $5A
pause 50
if b7 <> $5A then Digital_Read
if w0 = $FFFF then Digital_Read
gosub display_button_results
goto Digital_Read
Analog_Mode:
gosub Analog_Enable
Analog_Read:
ATT =0
hspiout($80, $42) 'analog query
hspiin (b7,b0,b1,b2,b3,b4,b5) 'analog mode has 7 bytes, last four are joysticks
ATT =1
pause 50
if b7 <> $5A then Analog_Read
if w0 = $FFFF then joystick
gosub display_button_results
joystick:
b2 = b2 REV 8 'reverse the order of bits to make MSB first LSB first
b3 = b3 REV 8 'because SPI hardware does MSB first, this corrects bit order
b4 = b4 REV 8 'I did not do this for buttons
b5 = b5 REV 8
if b2 <> 128 Then : SerTxd("Right JSTK L/R ", #B2, CR,LF) : End If
if b3 <> 128 Then : SerTxd("Right JSTK U/D ", #B3, CR,LF) : End If
if b4 <> 128 Then : SerTxd("Leftt JSTK L/R ", #B4, CR,LF) : End If
if b5 <> 128 Then : SerTxd("Leftt JSTK U/D ", #B5, CR,LF) : End If
goto Analog_Read
'=======================subroutines=====================================
display_button_results:
w0 = w0 ^ $FFFF
If bit15 = 1 Then : SerTxd( "Left2 " ) : End If
If bit14 = 1 Then : SerTxd( "Right2 " ) : End If
If bit13 = 1 Then : SerTxd( "Leftt1 " ) : End If
If bit12 = 1 Then : SerTxd( "Right1 " ) : End If
If bit11 = 1 Then : SerTxd( "Triangle " ) : End If
If bit10 = 1 Then : SerTxd( "Circle " ) : End If
If bit9 = 1 Then : SerTxd( "Cross " ) : End If
If bit8 = 1 Then : SerTxd( "Square " ) : End If
If bit7 = 1 Then : SerTxd( "Select " ) : End If
If bit6 = 1 Then : SerTxd( "Left JSTK " ) : End If
If bit5 = 1 Then : SerTxd( "Right JSTK ") : End If
If bit4 = 1 Then : SerTxd( "Start " ) : End If
If bit3 = 1 Then : SerTxd( "Up " ) : End If
If bit2 = 1 Then : SerTxd( "Right " ) : End If
If bit1 = 1 Then : SerTxd( "Down " ) : End If
If bit0 = 1 Then : SerTxd( "Left " ) : End If
SerTxd( CR, LF )
return
Analog_Enable:
ATT =0
hspiout($80,$C2,$0,$80,$0) 'enter config mode
ATT =1
pause 1
ATT =0
hspiout($80,$22,$0,$80,$C0,$0,$0,$0,$0) 'Set Mode to Analog
ATT =1
ATT =0
hspiout($80,$C2,$0,$0,$0,$0,$0,$0,$0) 'exit config mode
ATT =1
pause 50
return
Attachments
-
5.2 KB Views: 70
Last edited: