ps2 controller starter kit

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

ps2 controller starter kit

Postby gorgom » Sat Oct 15, 2011 11:38 pm

anyone can debug this code? PIC16f628A cannot communicate with SKPS.
CODE: SELECT_ALL_CODE
#include <pic.h>

//   configuration
//=======================================================================
__CONFIG ( 0x3F10  );      //configuration For PIC16F628A

//   define
//=======================================================================
#define _XTAL_FREQ   12000000      //Fosc=12MHz

#define motor_ra   RB6
#define motor_rb   RB7

//skps protocol
#define p_select      0
#define p_joyl         1
#define p_joyr         2
#define p_start         3
#define p_up         4
#define p_right         5
#define p_down         6
#define p_left         7
#define p_l2         8
#define p_r2         9
#define p_l1         10
#define p_r1         11
#define p_triangle      12
#define p_circle       13
#define p_cross         14
#define p_square      15
#define p_joy_lx      16
#define p_joy_ly      17
#define p_joy_rx      18
#define p_joy_ry      19
#define p_joy_lu      20
#define p_joy_ld      21
#define p_joy_ll              22
#define p_joy_lr              23
#define p_joy_ru      24
#define p_joy_rd      25
#define p_joy_rl              26
#define p_joy_rr      27

#define p_con_status           28
#define p_motor1      29
#define p_motor2      30

//   global variable
//=======================================================================



//   function prototype
//=======================================================================
void init(void);
void delay(unsigned long data);

void uart_send(unsigned char data);
unsigned char uart_rec(void);
unsigned char skps(unsigned char data);
void skps_vibrate(unsigned char motor, unsigned char value);

//   interrupt prototype
//=======================================================================
static void interrupt isr(void)
{

}

//   main function
//=======================================================================
void main(void)
{
   init();         //initialize microcontroller
   
   while(1)
   {   
      //test button for horn
      if((skps(p_l1)==0)||(skps(p_l2)==0)||(skps(p_r1)==0)||(skps(p_r2)==0)||(skps(p_triangle)==0)||(skps(p_circle)==0)) motor_ra=1;
      else if((skps(p_up)==0)||(skps(p_down)==0)||(skps(p_right)==0)||(skps(p_left)==0)||(skps(p_cross)==0)||(skps(p_square)==0)) motor_rb=1;
      else
      {
         motor_ra=0;
         motor_rb=0;
      }
   }
}

// initailization
//=======================================================================
void init()
{
   CMCON = 0b00000111;            // disable comparators

   // Tris configuration (input or output)
   TRISA = 0b00000001;            //set RA0 pin as input,other as output
   TRISB = 0b00000010;            //set PORTB as output

   //setup USART
   SPBRG = 0x4D;                  //set baud rate to 9600 for 12Mhz
   BRGH = 1;                     //baud rate high speed option
   TXEN = 1;                     //enable transmission
   TX9 = 0;
   CREN = 1;                     //enable reception
   SPEN = 1;                     //enable serial port
   RX9 = 0;
   RCIE = 0;                     //disable interrupt on eachdata received

   // enable all unmasked interrupt   
   GIE = 0;
   PEIE = 0;

   motor_ra = 0;
   motor_rb = 0;
}

// uart function
//=======================================================================
void uart_send(unsigned char data)   //function to send out a byte via uart
{   
   while(TXIF==0);               //wait for previous data to finish send out
   TXREG=data;                  //send new data
}

unsigned char uart_rec(void)      //function to wait for a byte receive from uart
{
   unsigned char temp;
   while(RCIF==0);               //wait for data to received
   temp=RCREG;               
   return temp;               //return the received data
}

// skps function
//=======================================================================
unsigned char skps(unsigned char data)         //function to read button and joystick
{                                    //information on ps controller
   uart_send(data);
   return uart_rec();
}

void skps_vibrate(unsigned char motor, unsigned char value)
{                                    //function to control the vibrator motor
   uart_send(motor);                     //on ps controller
   uart_send(value);   
}
gorgom
Newbie
 
Posts: 13
Joined: Sat Oct 15, 2011 11:11 pm

Re: ps2 controller starter kit

Postby shahrul » Sun Oct 16, 2011 12:10 am

You can try this code SKPS.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: ps2 controller starter kit

Postby gorgom » Sun Oct 16, 2011 12:10 pm

Thanks for the reply. I have no problem communicate skps with PIC16F877A. I just want to try communicate the skps with PIC16F628A but there are no response when I press the PS2 controller key. pic-lesson-uart-module show coding for PIC16F887. I just wonder whats wrong in my coding. I will do some adjustment according to the given example. Please show me if some one out there find any error in my coding. Thanks.
gorgom
Newbie
 
Posts: 13
Joined: Sat Oct 15, 2011 11:11 pm

Re: ps2 controller starter kit

Postby gorgom » Sun Oct 16, 2011 12:55 pm

Thanks shahrul. After examine your coding. The problem of my program is just the configuration bits. I should use HS oscillator. SKPS communicate with PIC16F628A succeed.
gorgom
Newbie
 
Posts: 13
Joined: Sat Oct 15, 2011 11:11 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 4 guests

cron