Here is my program:
- CODE: SELECT_ALL_CODE
'******************************************************************
'Title:
'======
'Get PS2 controller code
'
'Description:
'============
'This is the program that use to interface PS2 controller.
'It is interface the contorller to the PIC microcontorller.
'This program is using "shiftin/shiftout" to interface the
'serial data from the PS2 controller into the "byte".
'The "byte" will used later to contorller the robot.
'In this program, it can detect digital and analog input
'from the PS2 controller (analog only work on Sony origin
'contorller).
'For debug purpose, the portB are set to be output to indicate
'the byte of the "psxThumbR". The LED will OFF when the
'corresponding button is pressed. (Use only psxThumbR button)
'
'
'Author:
'Date: 21 Sept 2009
'Compiler: PicBasic Pro
'File: ####
'
'Modifications:
'==============
'
'
'******************************************************************
'Definations
INCLUDE "MODEDEFS.BAS"
ADCON1=7 'Analog to Digital Converter Off
TRISA.0=1
TRISA.1=0
TRISA.2=0
TRISA.3=0
TRISB=0
PsxClk var porta.3
PsxAtt var porta.2
PsxCmd var porta.1
PsxDat var porta.0
psxThumbL VAR Byte 'left thumb buttons
psxThumbR VAR Byte 'right thumb buttons
psxStatus VAR Byte 'status ($5A)
psxJoyRX VAR Byte 'R joystick - X axis
psxJoyRY VAR Byte 'R joystick - Y axis
psxJoyLX VAR Byte 'L joystick - X axis
psxJoyLY VAR Byte 'L joystick - Y axis
'Setup
PsxAtt=1
PsxClk=1
'Start of Main Program
Main:
gosub PSX
portb = psxThumbR
pause 15
Goto Main
PSX:
PsxAtt = 0
SHIFTOUT PsxCmd, PsxClk, LSBFIRST, [$01\8, $42\8]
SHIFTIN PsxDat, PsxClk, 5, [psxStatus\8, psxThumbL\8, psxThumbR\8, psxJoyRX\8, psxJoyRY\8, psxJoyLX\8, psxJoyLY\8]
PsxAtt = 1
return
End