Tutorial - Playstation 2 Controller and Mikrobasic
This is brief tutorial on how to interface a PIC microcontroller to a Playstation 2 game controller. The code is very simple.
The reasons I wanted to interface with a PS2 controller is because it offers a wide range of User Control in a nice package that is perfect for controlling a robotics project or anything requiring a lot of user input. Also they are cheap and readily available. The reason for a tutorial is because I love Mikrobasic and since I couldn't find any info specifically for it I thought I'd provide some.
Before I begin I need to give credit where credit is due. The majority of the information here comes from a nice article written by Jon Williams in the parallax Nuts and Volts #101 column. He showed how to interface with a controller using a basic stamp 2 and my tutorial is basically his article adapted for use with Mikrobasic.
So don't sue me for plagarism because I quoted my source :D
Wiring your controller to your PIC
There are nine lines in a PS2 cable. Four are for communication, one is 5V power, one is ground, one is 9V power (for rumble motors only, we don't need it), and two I'm not sure about but we don't need them.
Below is a wiring diagram created by Jon Williams:
NOTE: He used an inverted clock line to work with one of the Basic Stamps commands but this tutorial assumes that the clock line is NOT inverted and that there is not transistor present.
How a PS2 controller communicates
Basically it uses a form of SPI communication. A clock pulse from the PIC signals that it is time to send the next bit of data. Communication both ways is simulataneous and conincides with the same clock line.
I think it would be very possible to use Mikrobasic's SPI_Init_Advance command to do this but I have had some issues with it so far and did the code all manually for now (not hard at all).
The psxCLK line and psxATT lines are held normally high. The psxATT operates like the Slave Select line in SPI. You pull it low to tell the controller you are talking to it and then send it back high once a communications cycle is complete.
psxCMD is the data line to the controller and psxDAT is the data coming from the controller.
For my circuit I used a 16F88 and tied all the lines to teh corresponding SPI line (even though I didn't use SPI) with the exception of psxATT which I tied to RB0.
Mikrobasic Code
The basic communication cycle goes like this:
1) Prepare data bit to be sent on psxCMD
2) Pull psxCLK low
3) Read data bit off of psxDAT
4) Send psxCLK high
5) Do eight times to send/receive entire byte (LSB to MSB)
Basic program structure:
-First the psxATT line is pulled low to activate the controller.
-Next 0x01 is sent to the controller to signal the start of a communications cycle
-Third we send byte 0x42 to the controller to request the status. While we send it we will simultaneously receive the controllers ID which will be sent because of the earlier start signal
-Next we receive the status byte telling us whether the controller is in analog or digital mode (digital mode just means it won't send the joystick bytes)
-The next six byte receives will be button data in this order: Left hand buttons, Right hand buttons, Right joystick X-axis, Right joystick Y-axis, Left joystick X-axis, Left joystick Y-axis.
-Finally we pull psxATT high to signal that this communications cycle is complete.
I'll post a chart from Jon Williams' article in an edit soon, for now just refer to his article.
Joystick data is a byte between 0 and 255 for each axis with 128 being center. For example with the Left joystick X-axis byte, 0 would be fully left and 255 would be fully right.
Below is my code that I wrote in Mikrobasic for a 16F88 to turn on LEDs on ports RA0 and RA1 in correspondance with the "X" and "O" buttons.
You can download the project with all source code and schematics here.
'///////////////////////////////////////////////////
'// Playstation 2 Controller Interface //
'// Created by: Isaac Hayes //
'// February 1, 2007 //
'// //
'// PIC Used: 16F88 in INTRC Mode //
'// Programming Language: MikroBasic //
'// //
'// Summary: //
'// This program uses Mikrobasic SPI functions //
'// to communicate with a Playstation2 controller //
'// and output the status of the "X" and "O" //
'// buttons on pins RA0 and RA1. //
'// //
'///////////////////////////////////////////////////
program PICNonSPI
symbol psxCLK = PORTB.4
symbol psxCMD = PORTB.2
symbol psxDAT = PORTB.1
symbol psxATT = PORTB.0
dim psxOut as byte
dim psxIn as byte
dim psxID as byte
dim i as byte
dim psxStatus as byte
dim psxLeftB as byte
dim psxRightB as byte
dim psxRJoyX as byte
dim psxRJoyY as byte
dim psxLJoyX as byte
dim psxLJoyY as byte
'////////////////////////////////////
'// Sub Procedures //
'////////////////////////////////////
'// Send and Receive data simultaneously
sub procedure psxTxRx(dim byref byteOut, byteIn as byte)
byteIn=0 'Reset Receive byte
for i = 0 to 7 'Read data bits LSB to MSB
psxCMD=testbit(byteOut,i) 'Prepare first bit to send
psxCLK=0 'Generate clock pulse
delay_us(25) 'Used to regulate communication speed
if psxDAT = 0 then
Setbit(byteIn,i) 'Low Data line indicates set bit
end if
psxCLK=1 'End clock pulse
Delay_us(25) 'Regulate speed
next i
end sub
'/////////////////////////////////////
'// Program Start //
'/////////////////////////////////////
main:
OSCCON=%01111110 'Set INTRC to 8Mhz
ANSEL=0 'Set I/O to digital
TRISA=0 'PortA all outputs
PORTA=0 'PortA all off
TRISB=%00000010 'PortB all output except SDI Pin (R1)
PORTB=%00010001 'PortB SCK and psxAtt are high, all else low
PortA.0=1 'Flashes LED on RA0 for debugging purposes
Delay_ms(500)
PortA.0=0
Delay_ms(500)
PortA.0=1
Delay_ms(200)
PortA.0=0
Delay_ms(200)
'/////////////////////////////////////
'// main Loop //
'/////////////////////////////////////
do
psxATT=0 'psxAtt low, Activates Controller
Delay_us(50)
psxOut=0x01 'Start Signal
psxTxRx(psxOut,psxIn)
psxOut=0x42 'Request Status
psxTxRx(psxOut,psxIn)
psxID=psxIn 'Simultaneously receive controller ID
psxOut=0 'Clear psxOut
psxTxRx(psxOut,psxIn) 'Get psxStatus
psxStatus=psxIn
psxTxRx(psxOut,psxIn) 'Get Left side buttons
psxLeftB=psxIn
psxTxRx(psxOut,psxIn) 'Get Right side buttons
psxRightB=psxIn
psxTxRx(psxOut,psxIn) 'Get Right joystick X-axis byte
psxRJoyX=psxIn
psxTxRx(psxOut,psxIn) 'Get Right joystick Y-axis byte
psxRJoyY=psxIn
psxTxRx(psxOut,psxIn) 'Get Left joystick X-axis byte
psxLJoyX=psxIn
psxTxRx(psxOut,psxIn) 'Get Left joystick Y-axis byte
psxLJoyY=psxIn
Delay_us(50)
psxATT=1 'Release controller by setting psxATT high
PortA.0=psxRightB.6 'RA0 equals status of "X" Button
PORTA.1=psxRightB.5 'RA1 equals status of "O" Button
Delay_ms(10)
loop until false 'Loop Forever
end.
The end
EDIT:
My old file server has since been stopped but MikroE was kind enough to add this to their projects section and host a zip file that contains all the code and schematics for this. You can find it HERE
Tutorial: Playstation 2 Controller Interface with Mikrobasic
Tutorial: Playstation 2 Controller Interface with Mikrobasic
Last edited by Ikefu on 06 Apr 2009 17:59, edited 2 times in total.
Re: Tutorial: Playstation 2 Controller Interface with Mikrob
Just an update, my old e-mail I used was a university e-mail account that has since been closed and unfortunately without it I can't recover my old mikroe account (Ikefu). So if you have any questions regarding this post you can send me a PM on my new account (Ikefu42) and I will give you my e-mail.
Thanks!
Ike
Thanks!
Ike
-
- Posts: 1
- Joined: 17 Dec 2010 21:27
Re: Tutorial: Playstation 2 Controller Interface with Mikrob
HI, the download link is dead, could you please reupload it? Thanks!
Re: Tutorial: Playstation 2 Controller Interface with Mikrob
Here is the updated link to my project: http://www.mikroe.com/download/projects ... obasic.zip
- dejan.odabasic
- mikroElektronika team
- Posts: 2649
- Joined: 30 Apr 2012 14:20
Re: Tutorial: Playstation 2 Controller Interface with Mikrob
Hello,
we have created website dedicated to projects/libraries written by our users .
Please upload your project and share it with our embedded community.
We would be pleased that this project become a part of LibStock.
Best regards.
we have created website dedicated to projects/libraries written by our users .
Please upload your project and share it with our embedded community.
We would be pleased that this project become a part of LibStock.
Best regards.
-
- Posts: 1
- Joined: 17 Sep 2012 12:54
Re: Tutorial: Playstation 2 Controller Interface with Mikrob
Hi Ikefu, Best regards .. I hope you can read this post and answer as I need to clarify certain things about this tutorial ... I expect prompt response .. regards