Playstation controller interfacing

General discussion on mikroC.
Post Reply
Author
Message
murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

Playstation controller interfacing

#1 Post by murta » 30 May 2005 15:40

Anyone had any luck in communicating with an dualshock psx controller. I`ve read somewhere that they use SPI mode 3 for communication but I can´t get it to work. I´m using a PIC18F452 at 10Mhz.

murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

#2 Post by murta » 01 Jun 2005 00:19

Never mind this post.. I´ve got it to work! Had forgotten some pullup resistors. 8)

anton
Posts: 807
Joined: 23 Sep 2004 09:16
Location: South-Africa
Contact:

#3 Post by anton » 01 Jun 2005 00:27

Can you maybe post the code ? :)

I think here are people that will find it usefull some or other time
Another proud user of LV 24-33A Development System and mikroPascal PRO for dsPIC :)
PortA not working? Add CMCON := 7; PortD not working? Add ADCON1 := 6;
To paste code on the forum, please use the [b] Code [/b] button !! ;)

murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

#4 Post by murta » 01 Jun 2005 00:46

Yes I will do that after I finnished my project at school.

anton
Posts: 807
Joined: 23 Sep 2004 09:16
Location: South-Africa
Contact:

#5 Post by anton » 01 Jun 2005 00:51

Thank you 8)
Another proud user of LV 24-33A Development System and mikroPascal PRO for dsPIC :)
PortA not working? Add CMCON := 7; PortD not working? Add ADCON1 := 6;
To paste code on the forum, please use the [b] Code [/b] button !! ;)

murta
Posts: 41
Joined: 16 May 2005 23:39
Location: Sweden
Contact:

#6 Post by murta » 02 Jun 2005 19:26

Code: Select all

unsigned short Dp;     //Variable for SLCT, JOYR, JOYL, STRT, UP, RGHT, DOWN, LEFT
unsigned short Bt;     //Variable for L2 , R2 , L1 , R1 , /\ , O , X , |_|
unsigned short LRAR;    //Variable for Right Joy 0x00 = Left  0xFF = Right
unsigned short UDAR;    //Variable for Right Joy 0x00 = Up    0xFF = Down
unsigned short LRAL;    //Variable for Left Joy  0x00 = Left  0xFF = Right
unsigned short UDAL;    //Variable for Left Joy  0x00 = Up    0xFF = Down
char temp;
char tp;

void ACK()
{
  while(PORTD.F1 == 0); // Wait for ack from PAD
}

void main()
{
  ADCON1 = 0x02;     // Setup (VREF, VREF AN0-AN4)
  TMR2  =   0;
  T2CON = 0xFC;
  PIE1.TMR2IE = 1;   // enable interupt
  INTCON = 0xC0;     // for tmr2
  PR2 = 0x04;
  TRISD = 0x02;     // PORTD is input

 Spi_Init_Advanced(MASTER_TMR2, DATA_SAMPLE_END, CLK_IDLE_HIGH, HIGH_2_LOW);

 tp = 0;

 PORTD.F0 = 1;
 Delay_ms(1);

  while(1)
  {

  PORTD.F0 = 0; //Enable PSX PAD
  Delay_us(10);
  tp = Spi_Read(0x80);     //Send device check commmand
  ACK();
  tp = Spi_Read(0x42);     //Read ID from device
  ACK();

        if(tp == 0x82)     //If digital mode
        {
         temp = 0;
         tp = Spi_Read(0);     //Read "here comes data command from   device"
         ACK();
         Dp = Spi_Read(0);     //Read data from device
         ACK();
         Bt = Spi_Read(0);     //Read data from device
         ACK();

         PORTD.F0 = 1;
        }

        if(tp == 0xCE)     //If analog mode
        {
         temp = 1;
         tp = Spi_Read(0);          //Read "here comes data command from device"
         ACK();
         Dp = Spi_Read(0);          //Read data from device
         ACK();
         Bt = Spi_Read(0);          //Read data from device
         ACK();
         LRAR = Spi_Read(0);        //Read data from device
         ACK();
         UDAR = Spi_Read(0);        //Read data from device
         ACK();
         LRAL = Spi_Read(0);        //Read data from device
         ACK();
         UDAL = Spi_Read(0);        //Read data from device
         ACK();
         PORTD.F0 = 1; //Disable
        }
    Delay_ms(5);
    }
}//~!

Here is the code that I used to interface a PSX controller via SPI interface. It works almost fine, except I get some glitches on the oscilloscope, and I cant figure out why. I hope somebody can try and use this code and maybe refine it a little. :wink:

Post Reply

Return to “mikroC General”