LowTech.com


Details of the PS2 Joystick Game Controller









6. Interface Program for PS2 and Arduino UNO

Down load file: Gaming_controller.zip

(1) The program is divided two files. the first one is: "Gaming_controller.iso" extern void key_data_input(void); // key data input extern unsigned char key_data[]; unsigned char key_flg=0; unsigned char key_data_old0, key_data_old1; unsigned char key_data_old2, key_data_old3, key_data_old4, key_data_old5; void setup(){ // put your setup code here, to run once: pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(14, OUTPUT); pinMode(15, OUTPUT); pinMode(16, INPUT_PULLUP); //A2 : DAT pinMode(17, OUTPUT); //A3 : CLK pinMode(18, OUTPUT); //A4 : CMD pinMode(19, OUTPUT); //A5 : SEL // Timer1 sets CTC mode, PSR=256, and generates Interrupt when it matches the value of ICR1(1560). // Therefore, Timer1 interrupt cycle is CLK(16MHz)/PSR(256)/ICR1(1560)=40Hz(25ms). TCCR1A = 0; //clear mode reg. TCCR1B = 0; //clear mode reg. TCNT1 = 0; //clear timer1 count reg. ICR1 = 1560; //Freq.=(clk(16MHz)/psr(256))/1560=40Hz=25ms TCCR1B = bit(WGM12)|bit(WGM13)|bit(CS12); //CTC mode, TOP=ICR1, PSR=256 TIMSK1 = bit(ICIE1); //int. enable by ICR1 interrupts(); //General int. enable Serial.begin(9600); key_flg=0; } //In the main loop, "key_flg" is checked to detect that the Timer1 interrupt has occurred, and then communicate with the gaming controller. //The received data is stored in the key_data [6] array, and the first two bytes in the data array indicate the status of the key switch. //So, check each bit and display it if the key is pressed. void loop(){ if (key_flg){ //period 25ms key_flg = 0; //Clear key_flg key_data_input(); //Get key_data if((key_data_old0!=key_data[0])||(key_data_old1!=key_data[1])){ key_data_old0 = key_data[0]; key_data_old1 = key_data[1]; if(!(key_data[0]&1))Serial.print("SEL=ON,"); if(!(key_data[0]&8))Serial.print("STT=ON,"); if(!(key_data[0]&0x10))Serial.print("FW=ON,"); if(!(key_data[0]&0x20))Serial.print("R=ON,"); if(!(key_data[0]&0x40))Serial.print("BK=ON,"); if(!(key_data[0]&0x80))Serial.print("L=ON,"); if(!(key_data[1]&1))Serial.print("L2=ON,"); if(!(key_data[1]&2))Serial.print("R2=ON,"); if(!(key_data[1]&4))Serial.print("L1=ON,"); if(!(key_data[1]&8))Serial.print("R1=ON,"); if(!(key_data[1]&0x10))Serial.print("△=ON,"); if(!(key_data[1]&0x20))Serial.print("○=ON,"); if(!(key_data[1]&0x40))Serial.print("X=ON,"); if(!(key_data[1]&0x80))Serial.print("□=ON"); Serial.println(""); //The below is displayed for Left and Right Joy-stick. if((key_data_old2!=key_data[2])||(key_data_old3!=key_data[3]) ||(key_data_old4!=key_data[4])||(key_data_old5!=key_data[5])){ key_data_old2 = key_data[2]; key_data_old3 = key_data[3]; key_data_old4 = key_data[4]; key_data_old5 = key_data[5]; Serial.print("Lx="); Serial.print(key_data[4]); Serial.print(","); Serial.print("Ly="); Serial.print(key_data[5]); Serial.print(","); Serial.print("Rx="); Serial.print(key_data[2]); Serial.print(","); Serial.print("Ry="); Serial.print(key_data[3]); Serial.println(""); } } } } ISR (TIMER1_CAPT_vect){ // timer1 interrupt 25ms key_flg++; // key data flag }


Arduino-UNO Port Pin Details

(2) The second file is "serial_com.iso"

#define CLK 5 // pin19 = CLK = PORTC bit 5 #define clkport PORTC //clock port = PORTC #define SEL 4 // pin18 = SEL = PORTC bit 4 #define selport PORTC //select port = PORTC #define CMD 3 // pin17 = CMD = PORTC bit 3 #define cmdport PORTC //command port = PORTC #define DAT 2 // pin16 = DAT = PINC bit 2 #define datport PINC //data input port = PINC unsigned char key_data[6]; //key_data[0 -- 5] void key_data_input(void){ unsigned char i, j, num, read_data; key_data[0] = 0xff; //key_data[0] fill in 0xff key_data[1] = 0xff; //key_data[1] fill in 0xff for ( i=2; i<6; i++) key_data[i] = 0; //key_data[2-5]: Joy Stick data, clear 0 start_com(); //SELECT lo read_data = cmd_out_get_data(0x01); //command=0x01 delayMicroseconds(40); //delay >30us read_data = cmd_out_get_data(0x42); //command=0x42 num = (read_data&0x0f)*2; //Hi_4bit=type Lo_3bit=(Nmber of data)/2 delayMicroseconds(40); //delay >30us read_data = cmd_out_get_data(0); //command=0, get read_data "Z" = 0x5a delayMicroseconds(40); //delay >30us j = 0; if((read_data==0x5a)&&((7>num)&&(num>1))){ for (num; num!=0; num--){ key_data[j] = cmd_out_get_data(0); //command data=0, get key data (num) times j++; delayMicroseconds(40); //delay >30us } } stop_com(); //SELECT hi } //8 bit serial communication function. Send command(8bit) and get data(8bit) unsigned char cmd_out_get_data(unsigned char cmd_data){ unsigned char read_data=0; if(cmd_data&1){ //Send CMD bit0 cmdport |= bit(CMD); //Set CMD=HIGH }else{ cmdport &= ~bit(CMD); //Set CMD=LOW } clkport &= ~bit(CLK); //CLK=LOW delayMicroseconds(5); //5us delay clkport |= bit(CLK); //CLK=HIGH if(datport & bit(DAT))read_data |= 0x01; //Get data bit0 delayMicroseconds(5); //5us delay {bit1 through bit6 are omitted.} if(cmd_data&0x80){ //Send CMD bit7 cmdport |= bit(CMD); //Set CMD=HIGH }else{ cmdport &= ~bit(CMD); //Set CMD=LOW } clkport &= ~bit(CLK); //CLK=LOW delayMicroseconds(5); //5us delay clkport |= bit(CLK); //CLK=HIGH if(datport & bit(DAT))read_data |= 0x80; //Get data bit7 return read_data; } void start_com(void){ selport |= bit(SEL); clkport |= bit(CLK); cmdport |= bit(CMD); // SEL=CLK=CMD=HIGH; delayMicroseconds(30); //delay selport &= ~bit(SEL); //SEL=LOW delayMicroseconds(15); //delay clkport &= ~bit(CLK); //CLK=LOW } void stop_com(void){ delayMicroseconds(15); //delay clkport |= bit(CLK); cmdport |= bit(CMD); //CLK=CMD=HIGH; delayMicroseconds(30); //delay selport |= bit(SEL); //SEL=HIGH } You can check the key_data[6] on the serial monitor of Arduino IDE.

inserted by FC2 system