View previous topic :: View next topic |
Author |
Message |
apai
Joined: 12 Oct 2008 Posts: 4
|
Interfacing PIC16F877A with PlayStation2 controller |
Posted: Sun Oct 12, 2008 6:12 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
apai
Joined: 12 Oct 2008 Posts: 4
|
|
Posted: Wed Oct 15, 2008 10:36 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 15, 2008 1:21 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
apai
Joined: 12 Oct 2008 Posts: 4
|
|
Posted: Thu Oct 16, 2008 10:07 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
apai
Joined: 12 Oct 2008 Posts: 4
|
|
Posted: Thu Oct 30, 2008 3:24 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 30, 2008 12:16 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|