View previous topic :: View next topic |
Author |
Message |
glena
Joined: 25 Jul 2007 Posts: 284 Location: PA near Philly
|
Posted: Sun Mar 28, 2010 4:41 am Post subject: Playstation2 controller code |
|
|
I'm currently working on a RoboMagellan project and was using a VEX 6 channel radio for RC control but started needing more buttons so I decided to look into trying a wireless Playstation2 controller I got from Eddy Wright a while back. Eddy had an example of working with the PS2 controller but the code used the hardware SPI. I needed the hardware SPI for File IO with a MicroSD card so I decided to rewrite it to not require or interfere with the other SPI commands or hardware. The result turned out to work very well and makes using a PS2 controller much more appealing for shot distance control.
I hope others are able to use the code in there projects!
The actual code is very small, less then 20 lines total!
Here is full example:
Code: | '===============================================================================
' Project: PlayStationII Wireless (or wired) controller reader
' Version: v1.0
' Created: 2010-03-27
' By: Glen Aidukas (C)Bahbots.com
' Credits: Original help from Eddy Wright on the PS2 protocol
'
' Description:
'
' PS2 Shockwave controller wiring and PSI data reading
' _____________________________
' | |
' | |1 2 3||4 5 6||7 8 9| | <-- Male connector of controller
' \___________________________/
'
' Pin Color Function Notes
' 1 GREEN MISO Data In
' 2 WHITE MOSI Command out
' 3 VIBRATE +9V
' 4 BLACK GND
' 5 RED VCC 3.3V-5V
' 6 BLUE SS Select
' 7 YELLOW SCK Clock
' 8 N/C N/C
' 9 ACK N/C
'
' This code was tested and working with a wireless PlayStationII controller
'===============================================================================
$regfile = "m644pdef.dat"
$crystal = 18432000
$hwstack = 32
$swstack = 16
$framesize = 48
$baud = 38400
'=====[ Dimension and initialize variables ]====================================
Dim PS2_CMD(2) as Byte ' init cmd sent to PS2 controller
Dim PS2_Data(7) as Byte ' hold returned data from PS2 controller
Dim i as Byte ' General purpose variables
PS2_MISO alias pinc.7 : DDRC.7= 0 ' data in
PS2_MOSI alias portc.6 : DDRC.6= 1 ' data out
PS2_SS alias portc.5 : DDRC.5= 1 ' Select
PS2_SCK alias Portc.4 : DDRC.4= 1 ' Clock
PS2_MISO= 1 ' turn on internal pull up resister
PS2_CMD(1) = &H01 ' Start Command
PS2_CMD(2) = &H42 ' Request Data
'-------------------------------------------------------------------------------
print "{027}[2J"; ' ansi clear screen...
print "Starting..."
do
gosub PS2_Read ' read the PS controller data
print "{027}[3;1H"; ' move to page top (ansi)
Print "Byte1 : "; PS2_Data(1); " "
print
Print "Select: "; PS2_Data(2).0 ; " Start : "; PS2_Data(2).3
Print "ThumbL: "; PS2_Data(2).1 ; " ThumbR: "; PS2_Data(2).2
print
Print "CNT-Up: "; PS2_Data(2).4 ; " CNT-R : "; PS2_Data(2).5
Print "CNT-Dn: "; PS2_Data(2).6 ; " CNT-L : "; PS2_Data(2).7
print
Print "L1 : "; PS2_Data(3).2 ; " R1 : "; PS2_Data(3).3
Print "L2 : "; PS2_Data(3).0 ; " R2 : "; PS2_Data(3).1
print
Print "Triang: "; PS2_Data(3).4 ; " Cross : "; PS2_Data(3).6
Print "Circle: "; PS2_Data(3).5 ; " Square: "; PS2_Data(3).7
print
print "Joy RX: "; PS2_Data(4); " "
print "Joy RY: "; PS2_Data(5); " "
print "Joy LX: "; PS2_Data(6); " "
print "Joy LY: "; PS2_Data(7); " "
waitms 100
loop
'-------------------------------------------------------------------------------
'=====[ Read the PS2 controller data ]==========================================
PS2_Read:
PS2_SS = 0 ' assert select line
SHIFTOUT PS2_MOSI, PS2_SCK, PS2_cmd(1) , 2, 8, 20 ' send start command
SHIFTOUT PS2_MOSI, PS2_SCK, PS2_cmd(2) , 2, 8, 20 ' send request data cmd
For i = 1 To 7
SHIFTIN PS2_MISO, PS2_SCK, PS2_Data(i), 2, 8, 20 ' read in data
Next i
PS2_SS = 1 ' release select line
return
'------------------------------------------------------------------------------- |
_________________ http://bahbots.com
Last edited by glena on Tue Mar 30, 2010 3:06 pm; edited 1 time in total |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Sun Mar 28, 2010 12:22 pm Post subject: Nanchuck |
|
|
Have seen also Nanchuck code for the Arduino, but written in C. Is also wireless... Have tested it, working well.
have fun
Ben Zijlstra |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Thu Sep 22, 2011 9:38 pm Post subject: |
|
|
Hello All,
I tested Glen's code last night and it works great. I have also used SPI hardware mode with success but I tried SPI software last night and I can't get it to work. Has anyone tried SPI in software mode to connect a PS2 controller? I think the issue could be timing since the clock is derived in software instead of the system clock.
I don't really need SPI software mode but for sake of completeness, I would like to get it working too.
I'll post the code tonight though the only difference between SPI hardware and software is the config statement.
Thanks,
Eddy Wright _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|