Controlling 4 servomotors movement using SKPS

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

Controlling 4 servomotors movement using SKPS

Postby gorgom » Sun Nov 06, 2011 10:18 pm

I am trying to use ps2 controller to control the movement of servomotor. Besides ps2 controller, I using 2 pushbuttons to test the circuit. Unfortunately, the servomotors did not response when push the ps2 controller. Only the LED is blinking. When I test with pushbuttons, the servo will rotate with the respective LED will blinking according to which pushbutton is press. The interrupt subroutine to control TIMER1 and TIMER2 seem did not response when is push ps2 controller. I just wonder if there are any restriction in using SKPS. The source code is provided below. Please anyone help me to solve this problem. Thank you.
CODE: SELECT_ALL_CODE
//   include
//=======================================================================
#include <pic.h>

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

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

#define hibyte(temp)       (unsigned char)(temp>>8)
#define lobyte(temp)       (unsigned char)(temp & 0xFF)

#define LED1   RB6
#define LED2   RB7

#define pushbutton1      RA0
#define pushbutton2      RA1

//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
//=======================================================================

unsigned int temp;
unsigned char tempA, tempB;
unsigned char Base, Shoulder,Elbow, Wrist, start=0;
unsigned char a,b,c,d;

//   function prototype
//=======================================================================
void init(void);
void delay10ms(unsigned int x);
void motion(void);
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);

//   main function
//=======================================================================
void main(void)
{
   init();         //initialize microcontroller

   //Setup Timer 1
   TMR1CS=0;      //Internal Clock (Fosc/4)
   TMR1L=0xC0;
   TMR1H=0x63;

   //Setup Timer 2
   T2CON=0b00000000;   //Timer 2 off;Prescaler=1
   TMR2=56;               


   //Setup Interrupt
   GIE=1;
   PEIE=1;      
   TMR1IE=1;
   TMR1IF=0;
   TMR2IE=1;
   TMR2IF=0;
   TMR1ON=1;         //Enable Timer 1
   TMR2ON=1;         //Timer 2 on
   
   while(1)
   {   
      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))
      {
         LED1=1;
         TMR1IE=1;
         TMR2IE=1;
         TMR1ON=1;         //Enable Timer 1
         a=6;b=6;c=6;d=6;
         temp = 0x63BF + (a*200);
         tempA = hibyte(temp);     
         tempB = lobyte(temp);            
         motion();
      }
      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))
      {
         LED2=1;
         TMR1IE=1;
         TMR2IE=1;
         TMR1ON=1;         //Enable Timer 1
         a=13;b=13;c=13;d=13;
         temp = 0x63BF + (a*200);
         tempA = hibyte(temp);     
         tempB = lobyte(temp);            
         motion();
      }
      else if (pushbutton1==0)
      {
           LED1=1;
         TMR1IE=1;
         TMR2IE=1;
         TMR1ON=1;         //Enable Timer 1
         a=6;b=6;c=6;d=6;
         temp = 0x63BF + (a*200);
         tempA = hibyte(temp);     
         tempB = lobyte(temp);            
         motion();
         }
      else if (pushbutton2==0)
      {
           LED2 = 1;
         TMR1IE=1;
         TMR2IE=1;
         TMR1ON=1;         //Enable Timer 1
         a=13;b=13;c=13;d=13;
         temp = 0x63BF + (a*200);
         tempA = hibyte(temp);     
         tempB = lobyte(temp);            
         motion();
         }
      else
      {
         LED1=0;
         LED2=0;
         TMR1IE=0;
         TMR1IF=0;
         TMR2IE=0;
         TMR2IF=0;
         TMR1ON=0;         //Enable Timer 1
         TMR2ON=0;         //Timer 2 on
      }
   }
}

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

   // Tris configuration (input or output)
   TRISA = 0b00000011;            //set RA0 pin as input,other as output
   TRISB = 0b00000010;            //set PORTB as output
   
   //setup USART
   SPBRG = 0x33;                  //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


   LED1 = 0;
   LED2 = 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);   
}

//Function to create delay in 10millisec
//=======================================================================
void delay10ms(unsigned int x)
{
   for(;x>0;x--)
   {
      __delay_ms(10);
   }
}

//==========================================================================
//functions
//==========================================================================
void motion (void)
{
while(1)
   {
      if(Base==0){RB0=0;Base=0xFF;}
      if(Shoulder==0){RB3=0;Shoulder=0xFF;}
      if(Elbow==0){RB4=0;Elbow=0xFF;}
      if(Wrist==0){RB5=0;Wrist=0xFF;}
      if((RA0==1) && (RA1==1))break;
   }
}

static void interrupt
isr(void)         // Here be interrupt function
{
   if(TMR1IF==1)
   {
      //RC2=1;
      RB0 = 1;
      RB3 = 1;
      RB4 = 1;
      RB5 = 1;
      TMR1IF=0;
      TMR1L=tempB;
      TMR1H=tempA;            //0xB1

            
      Base=a; Shoulder=b; Elbow=c; Wrist=d;
      TMR2ON=1;
                        //   start=0;
   }   
   else if (TMR2IF==1)
   {
      TMR2IF=0;
      TMR2=56;            //256-56=200; 200*0.5u=100us
      Base-=1;Shoulder-=1;Elbow-=1;Wrist-=1;
   }
}
gorgom
Newbie
 
Posts: 13
Joined: Sat Oct 15, 2011 11:11 pm

Re: Controlling 4 servomotors movement using SKPS

Postby hyng » Tue Nov 08, 2011 4:18 pm

First please make sure the UART communication between PIC and SKPS is working.
Did your LED1 or LED2 lit ON when you press the button on PS2?
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Controlling 4 servomotors movement using SKPS

Postby gorgom » Wed Nov 09, 2011 2:37 pm

there is no problem the communication between pic and skps because the LED is blinking when I push the ps2 controller. Is there any setting at the usart register to enable interupt while pic reading skps because I am using interupt subroutine function to move the servomotors?
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 5 guests