[bascom] Bascom-AVR Want a great hand controller for your projects?


From "Matt Smith" <matt.smith@hksystems.com>
Date Mon, 10 Nov 2003 06:15:30 -0700
Organization HK Systems
User-agent Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01

Hello All,
First off I will say I love "Bascom", It allows me to easily implement micro's in my projects with hardly any extra effort. Well here is a snippet of code you can use to interface a Sony PS2 PAD Controller to your Micro's using the Hardware SPI and Bascom-AVR. I tested this on a PAD I have that runs in Red mode or Digital mode. So I have tested it with a ATMega128 16Mhz Xtal.I had to use a pull-up resistor on MISO pin to +5v when I used the 16Mhz Xtal , 12Mhz and lower didn't seem to need the pull-up. The controller I used has 14 buttons and two analog joysticks. It makes a great interface. See Ya!

Matt

'###############################################################################
'# File Name: PSX_PADInterface.bas # '# Written By: Matt Smith # '# Function: Allows you to hook a Sony PSX handcontroller to a AVR using # '# the Hardware Spi functions in Bascom. It is an awesome tool # '# for lots of different projects. The Analog Red Mode Controller# '# has 14 Buttons and two Joysticks with 8 bit resolution. # '# # '# Note: This software is provided as-is No warranties implied. # '# # '# PSX Pinout looking at the plug. # '# _______________________ # '# Pin 1->| o o o | o o o | o o o | # '# \_____________________/ # '# # '# Pin1 = Data (PSX PAD Data Line Output) Connects to MISO(PB3) # '# Pin2 = Command (PSX PAD Data Line Input) Connects to MOSI(PB2) # '# Pin3 = N/C No Connection # '# Pin4 = GND Obvious # '# Pin5 = Vcc +5Vdc Obvious # '# Pin6 = ATT (Select) Connects to PB4 # '# Pin7 = Clock Line Connects to SCK(PB1) # '# Pin8 = N/C No Connection #
'# Pin9 = ACK                 No Connection                     #
'# # '# I used a 16Mhz Crystal and I used pull-up Res 4.7Kohm on PB3 to Vcc(5V) # '# It didn't seem to matter with slower crystals. # '# Have Fun! #
'###############################################################################

$REGFILE = "m128DEF.DAT"
$baud = 19200
$crystal = 16000000


dim cmdStart as byte
    cmdStart = &H01
dim cmdStatus as byte
    cmdStatus = &H42

'PSX Type ID
Const cnstID_Digital = &H41
Const cnstID_NegCon = &H23
Const cnstID_AnalogRed = &H73
Const cnstID_AnalogGreen = &H53

'These are for local storage of last read packet from PSX PAD
 dim PSPAD_Present as byte , PSPAD_MODE as byte
 dim PSPAD_Byte1 as byte , PSPAD_Byte2 as byte
dim PSPAD_RJoyHPos as byte , PSPAD_LJoyHPos as byte ' 0 = left, 255 = Right dim PSPAD_RJoyVPos as byte , PSPAD_LJoyVPos as byte '0 = Up, 255 = Down

'All Buttons on the controller are active low
'Easy Reading
 PSPAD_Key_Select alias PSPAD_Byte1.0
 PSPAD_Key_JoyR alias PSPAD_Byte1.1
 PSPAD_Key_JoyL alias PSPAD_Byte1.2
 PSPAD_Key_Start alias PSPAD_Byte1.3
 PSPAD_Key_UP alias PSPAD_Byte1.4
 PSPAD_Key_Right alias PSPAD_Byte1.5
 PSPAD_Key_Down alias PSPAD_Byte1.6
 PSPAD_Key_Left alias PSPAD_Byte1.7

 PSPAD_Key_L2 alias PSPAD_Byte2.0
 PSPAD_Key_R2 alias PSPAD_Byte2.1
 PSPAD_Key_L1 alias PSPAD_Byte2.2
 PSPAD_Key_R1 alias PSPAD_Byte2.3
 PSPAD_Key_Triangle alias PSPAD_Byte2.4
 PSPAD_Key_Circle alias PSPAD_Byte2.5
 PSPAD_Key_X alias PSPAD_Byte2.6
 PSPAD_Key_Square alias PSPAD_Byte2.7

 'Subroutine Declarations
Declare sub QueryPSPad

'Configure the SPI in Hardware mode these are the only settings that will work Config Spi = hard , Interrupt = Off , Data Order = lsb , Master = Yes , Polarity = high , Phase = 1 , Clockrate = 128
'init the spi pins
Spiinit
'Now configure our pin we will use for the ATT line
config pinb.4 = output
Pad_ATT_Line Alias portb.4
'********************************* Main ****************************************
do
'Remember that the buttons are active low
  'We will update slow so that we can test easily in the Terminal Emulator
  waitms 1000
  'Get Current Button states from PSX Controller
  gosub QueryPSPAD
     'Now do something base on the results
if PSPAD_Present = 1 then '0 = not present, 1 = present
          print "Select:" ; PSPAD_Key_Select
          print "JoyR:" ; PSPAD_Key_JoyR
          print "JoyL:" ; PSPAD_Key_JoyL
          print "Start:" ; PSPAD_Key_Start
          print "Up:" ; PSPAD_Key_UP
          print "Right:" ; PSPAD_Key_Right
          print "Down:" ; PSPAD_Key_Down
          print "Left:" ; PSPAD_Key_Left
          print "L2:" ; PSPAD_Key_L2
          print "R2:" ; PSPAD_Key_R2
          print "L1:" ; PSPAD_Key_L1
          print "R1:" ; PSPAD_Key_R1
          print "Triangle:" ; PSPAD_Key_Triangle
          print "Circle:" ; PSPAD_Key_Circle
          print "X Key:" ; PSPAD_Key_X
          print "Square:" ; PSPAD_Key_Square
          print "Right Joystick H Position:" ; PSPAD_RJoyHPos
          print "Right Joystick V Position:" ; PSPAD_RJoyVPos
          print "Left Joystick H Position:" ; PSPAD_LJoyHPos
          print "Left Joystick V Position:" ; PSPAD_LJoyVPos

     else
          Print "No Response From PSX PAD"
     end if


loop
'******************************* End Main **************************************
Sub QueryPSPad
reset Pad_ATT_Line 'Get PS2PAD Attention
  spiout cmdstart , 1                                      'Send Start
PSPAD_Mode = spimove(cmdStatus) 'Send Request, Recieve ID Byte '

spiin PSPAD_Present , 1 'Is PAD Responding
   'Now see if controller is going to send something

if PSPAD_Present = 90 then '90 = Data on its way! PSPAD_Present = 1 'Set the global bit spiin PSPAD_Byte1 , 1 'Byte 1 and 2 are always used
        spiin PSPAD_Byte2 , 1
spiin PSPAD_RJoyHPos , 1 'Byte 3-6 are for Analog modes
        spiin PSPAD_RJoyVPos , 1
        spiin PSPAD_LJoyHPos , 1
        spiin PSPAD_LJoyVPos , 1
        set Pad_Att_Line
   else

PSPAD_Present = 0 'Set the global state set Pad_Att_Line 'Release Attention Line to the PS2 PAD
  end if


end sub
'*******************************************************************************