CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Interfacing PIC16F877A with PlayStation2 controller

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
apai



Joined: 12 Oct 2008
Posts: 4

View user's profile Send private message

Interfacing PIC16F877A with PlayStation2 controller
PostPosted: Sun Oct 12, 2008 6:12 am     Reply with quote

I'm new at this forum. I need help for solving the problem to interface PlayStation2 controller with PIC16F877A. I'm using SPI. The connection between PlayStation2 and PIC is :
PS2 ---> PIC
Data ---> RC4/SDI/SDA
Command ---> RC5/SDO
Clock ---> RC3/SCK/SCL
Attention ---> RA5

I want to read a value from PlayStation2 controller button to manipulate LED at Port D(PIC16F877A) such as X=PIN D0, O=PIN D1, triangle=PIN D2, square=PIN D3. I want to know how to setup SPI, how to setup the register(SSPCON, SSPSTAT, SSPBUFF) and how to read the value from PS2 controller. My compiler version is 3.170b. Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 12, 2008 12:16 pm     Reply with quote

Use Google to search for this information.

Here is one of the many examples. They use a logic analyzer to
research the PS2 SPI protocol:
http://www.nearfuturelaboratory.com/2008/06/19/playstation2-logic-analysis/

Then read posts on this forum about how to select the correct SPI mode:
http://www.ccsinfo.com/forum/viewtopic.php?t=36080
http://www.ccsinfo.com/forum/viewtopic.php?t=35566
http://www.ccsinfo.com/forum/viewtopic.php?t=33255
apai



Joined: 12 Oct 2008
Posts: 4

View user's profile Send private message

PostPosted: Wed Oct 15, 2008 10:36 am     Reply with quote

Code:

#include <16F877A.h>
#device adc=8
#use delay(clock=20000000)
#fuses HS, NOWDT
#use standard_IO(A,B,C,D,E)

// this is a macro shortcut (this code will replace bitrev(c) at compile time) for reversing the bit order//
#define bitrev(c)   c = (c & 0x0F) << 4 | (c & 0xF0) >> 4; \
         c = (c & 0x33) << 2 | (c & 0xCC) >> 2; \
         c = (c & 0x55) << 1 | (c & 0xAA) >> 1;

#include "E:\SPI\Robocon.h"

void output_D();           // LED at Port D
char spi_write(char);      // RC5/(command for PS2) - from PIC to PS2
char spi_data_is_in(char);
char spi_read(char);       // RC4/(data for PS2) - from PS2 to PIC
int8 d;


/************************** MAIN ******************************/

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_counters(RTCC_INTERNAL,WDT_18MS);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
   output_high(PIN_C3);       // Set clock
   output_high(PIN_C0);       // pull high (ACKNOWLEDGE of PS2)
   output_high(PIN_A1);       
   output_high(PIN_C5);       // data need to give high
   
      while(1)
      {
         output_low(PIN_A1);     // pull low (ATTENTION of PS2)
         spi_write(0x01);        // start communicate with PS2
         spi_write(0x42);        // request for PS2 controller ID and send all PS2 data
         spi_write(0x00);
         d=spi_read(0);           // data read from PS2
         output_D(d);            // turn ON LED at PORT D
         output_high(PIN_A1); 
      }
     
}



This is my simple program to generate a clock and all the signal that I see in the oscilloscope is correct but there is no data coming into the my PIC device. I don't know why. I was thinking that there is a data when clock is generated by PIC, but when I press the button on PlayStation 2 controller, there is no result. What I have to do? Please help me. I appreciate your cooperation. Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 15, 2008 1:21 pm     Reply with quote

I don't want to do this project for you, but it looks like you didn't study the
links that I gave, such as the link about interfacing to the PS2.

I read the PS2 link, and it shows the relationship of clock to data in
the SPI signals. The clock idles at a high level, and it samples on the
rising edge. If you looked the SPI Mode links that I give in my posts,
you would see that this is SPI Mode 3. You would also see these defines:
Code:

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H) 

From this table, it's apparent that your code is using the wrong SPI
mode. You're using Mode 1, and therefore you are sampling data on
the wrong edge. It must be changed to Mode 3. Put those #define
statements at the start of your program and edit your setup_spi() line
to put in SPI_MODE_3 instead of "spi_l_to_h".

There might be other problems in your code. You need to read all links
completely.
apai



Joined: 12 Oct 2008
Posts: 4

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 10:07 am     Reply with quote

Thank you for your explanation. I had set the clock and the clock is generated as I wanted but the data cannot be read. I don't know why spi_read has not read the data that is sent by the PlayStation 2 controller.
Please do help me. Thank you vey much.
apai



Joined: 12 Oct 2008
Posts: 4

View user's profile Send private message

PostPosted: Thu Oct 30, 2008 3:24 am     Reply with quote

I have problem to read the data from PlayStation 2 controller. I have done the first plug in initialize and its work but when i press the button of PlayStation 2 controller, no data received by PIC 16F877A output. What are the command for programming to read data from PlayStation 2 controller? Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 30, 2008 12:16 pm     Reply with quote

I can't do this project for you. Don't ask me any more questions.
Use Google to find websites that have already done the project.
Example: Total analysis of the PS2 controller, including C18 source code.
http://www.curiousinventor.com/guides/ps2
I'm done with this.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group