Hi all, I faced some problem when interfacing PS2 controller with PIC24FJ256GB110 through SPI. I use PIC C compile to write the program, and the code is attached below. Unfortunately there was not data reply from the ps2 controller, can anyone give me some guidance what to check? Besides that, I would like to ask is PS2 controller compatible with 3.3V or not? Because the output of the controller is 3.3V, will is cause any problem?
Here is the code:
//Peripheral Pin Select Pin Definition
#pin_select SS1OUT=pin_b14 //Pin 44
#pin_select SDI1=pin_d14 //Pin 47
#pin_select SDO1=pin_d15 //Pin 48
#pin_select SCK1OUT=pin_f4 //Pin 49
//Funtion Declaration
void ps2(void);
int8 reverseByte(int8);
//I/O Pin Defination
#define ps2CMD PIN_D15
#define ps2ATTN PIN_B14
#define ps2DATA PIN_D14
#define ps2CLK PIN_F4
//Variable Declaration
int8 ps2Byte[9];
int8 ps2Status;
void main()
{ setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4);
SPIEN=0;
CKP=1;
CKE=0;
setup_spi2(SPI_SS_DISABLED);
While(true)
{
Restart:
ps2();
ps2Status = reverseByte(ps2Byte[2]);
if(ps2Status == 0x5A)
{
//restart_wdt();
Output_high(Pin_F0);
delay_ms(1000);
}
else
{
goto restart;
}
}
}
void ps2 (void)
{
SPIEN=1;
output_low(Pin_B14);
delay_us(20);
ps2Byte[0]=spi_read(0x80);
ps2Byte[1]=spi_read(0x42); //get controller ID
ps2Byte[2]=spi_read(0x00); //response from controller
ps2Byte[3]=spi_read(0x00); //ps2 left button
ps2Byte[4]=spi_read(0x00); //ps2 right button
ps2Byte[5]=spi_read(0x00); //right joystick - x axis
ps2Byte[6]=spi_read(0x00); //right joystick - y axis
ps2Byte[7]=spi_read(0x00); //left joystick - x axis
ps2Byte[8]=spi_read(0x00); //left joystick - y axis
output_high(Pin_B14);
}
int8 reverseByte(int8 x)
{
x = (x & 0x0F) << 4 | (x & 0xF0) >> 4;
x = (x & 0x33) << 2 | (x & 0xCC) >> 2;
x = (x & 0x55) << 1 | (x & 0xAA) >> 1;
return(x);
}