Playstation Controller on ATMEGAX

General discussion on mikroBasic for AVR.
Post Reply
Author
Message
TOKN
Posts: 13
Joined: 27 Nov 2007 21:24

Playstation Controller on ATMEGAX

#1 Post by TOKN » 04 Dec 2007 18:34

Hi, I am trying to connect a PSX controller to an ATMEGA16 MCU using the EasyAVR5 development board. Hardware looks OK but I cannot get the software to work properly. I am looking for someone to help me out because this is a blocking factor in my whole project !!!! I am willing to pay or reward otherwise anyone who can help me get to a working solution ! :D I get false answers from the controller back - always $FF and nothing meaningfull !

I will attach the source code here but can also pm it on request !

Any help is highly appreciated !!!!!!!!!!

Thanks !

Tony

Code: Select all

'///////////////////////////////////////////////////
'//     Playstation 2 Controller Interface        //
'//           Created by: Tony Knors              //
'//              December 1, 2007                 //
'//                                               //
'// ATMEGA16 on EASYAVR5                          //
'// Programming Language: MikroBasic AVR          //
'//                                               //
'// Summary:                                      //
'// This program reads the ID, Status a Values    //
'// of a Playstation2 controller                  //
'// and outputs the status vgia RS232 To terminal //
'//                                               //
'//                                               //
'// Pin Out for ATMEGA16:                         //
'// PortA.6 -> psxATT                             //
'// PortA.4 -> psxDAT                             //
'// PortA.5 -> psxCMD                             //
'// PortA.7 -> psxCLK                             //
'//                                               //
'//  Uses Pullup Resistor on Port A.4             //
'//                                               //
'///////////////////////////////////////////////////

program TESTPSX

'---------------------------------------------------
' Define ports
'---------------------------------------------------
symbol psxCLK = PortA.7
symbol psxCMD = PortA.5
symbol psxDAT = PortA.4
symbol psxATT = PortA.6
'---------------------------------------------------
' Variables
'---------------------------------------------------
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
dim text as string[7]
'---------------------------------------------------
'      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   25
next i
end sub
'---------------------------------------------------
'       Program Start
'---------------------------------------------------
main:

Soft_Uart_Init(PORTD, 0,1,9600,0)

DDRA=%11101111                 'All ports Output except P4 (DAT)

PortA.7=1                      'Set Clock High
PortA.6=1                      'Set ATT High
PortA.5=0                      'All Others Low
PortA.4=0
PortA.3=0
PortA.2=0
PortA.1=0
PortA.0=0                      ' For Test Only

Delay_ms(200)                  ' Wait a bit

'---------------------------------------------------
'           main Loop
'---------------------------------------------------
loop:

psxATT=0                        'psxAtt low, Activates Controller
Delay_us(50)

'-------------------------------------------------------------------
psxOut=0x01                     'Start Signal
psxTxRx(psxOut,psxIn)           ' Send and get answer from controller
Soft_Uart_Write_Text("Start Byte Sent:")  ' Write to RS2323 wat was sent
WordToStr(PsxOut,text)
Soft_Uart_Write_Text(text)
Soft_Uart_Write($0D)
Soft_Uart_Write($0A)
'-------------------------------------------------------------------
WordToStr(PsxIn,text)
Soft_Uart_Write_Text("Result Received:")  ' Wrire to RS232 Wat was received
Soft_Uart_Write_Text(text)
Soft_Uart_Write($0D)
Soft_Uart_Write($0A)
'-------------------------------------------------------------------
psxOut=0x42                     'Request Status
psxTxRx(psxOut,psxIn)
Soft_Uart_Write_Text("Status Requested:")
WordToStr(PsxOut,text)
Soft_Uart_Write_Text(text)
Soft_Uart_Write($0D)
Soft_Uart_Write($0A)
'-------------------------------------------------------------------
psxID=psxIn
Soft_Uart_Write_Text("ID Received:")     'Simultaneously receive controller ID
WordToStr(psxid,text)
Soft_Uart_Write_Text(text)
Soft_Uart_Write($0D)
Soft_Uart_Write($0A)
'-------------------------------------------------------------------
psxOut=0                        'Clear psxOut
psxTxRx(psxOut,psxIn)           'Get psxStatus
psxStatus=psxIn
Soft_Uart_Write_Text("Status:")     'Simultaneously receive controller ID
WordToStr(psxin,text)
Soft_Uart_Write_Text(text)
'---------------------------------------------------
PsxATT = 1
Goto loop
'---------------------------------------------------
' Future development
'---------------------------------------------------

'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
'WordToStr(psxrjoyx,text)
'Soft_Uart_write_Text("JoyRX:")
'Soft_Uart_write_Text(text)

'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)
'---------------------------------------------------
end.

Post Reply

Return to “mikroBasic for AVR General”