- CODE: SELECT_ALL_CODE
#define M1_DIR RC3 //MD10C (1) Enable
#define M1_PWM CCPR1L //MD10C (1) PWM register
#define M2_DIR RC0 //MD10C (2) Enable
#define M2_PWM CCPR2L //MD1OC (2) PWM register
#define PW3_DIR RC4
#define PW3_PWM RA5 //Power Window (3)
#define PW4_DIR RA0
#define PW4_PWM RA1 //Power Window (4)
#define PW5_DIR RA2
#define PW5_PWM RA3 //Power Window (5)
//left and right max speed
#define left_max_speed 255
#define right_max_speed 255
while(1)
{
//get data from joystick
joystick_rightup=SKPS(p_joy_ru); //get joystick_rightup value from p_joy_ru value
joystick_rightdown=SKPS(p_joy_rd); //get joystick_rightdown value from p_joy_rd value
joystick_rightleft=SKPS(p_joy_rl); //get joystick_rightleft value from p_joy_rl value
joystick_rightright=SKPS(p_joy_rr); //get joystick_rightright value from p_joy_rr value
joystick_leftup=SKPS(p_joy_lu); //get joystick_leftup value from p_joy_lu value
joystick_leftdown=SKPS(p_joy_ld); //get joystick_leftdown value from p_joy_ld value
joystick_leftleft=SKPS(p_joy_ll); //get joystick_leftleft value from p_joy_ll value
joystick_leftright=SKPS(p_joy_lr); //get joystick_leftright value from p_joy_lr value
//calculate left_wheel and right_wheel register from all 4 joystick value
left_wheel= joystick_leftleft*left_max_speed/100
-joystick_leftright*left_max_speed/100;
right_wheel= joystick_rightleft*right_max_speed/100
-joystick_rightright*right_max_speed/100;
//limit left wheel and right wheel register to the maximum value
if(left_wheel>left_max_speed)left_wheel=left_max_speed;
else if(left_wheel<-left_max_speed)left_wheel=-left_max_speed;
if(right_wheel>right_max_speed)right_wheel=right_max_speed;
else if(right_wheel<-right_max_speed)right_wheel=-right_max_speed;
//calculate left_wheel and right_wheel register from all 4 joystick value
fleft_wheel= joystick_leftup*left_max_speed/100
-joystick_leftdown*left_max_speed/100
-joystick_rightdown*left_max_speed/100
+joystick_rightup*left_max_speed/100;
fright_wheel= joystick_leftup*left_max_speed/100
-joystick_leftdown*left_max_speed/100
-joystick_rightdown*left_max_speed/100
+joystick_rightup*left_max_speed/100;
//limit left wheel and right wheel register to the maximum value
if(fleft_wheel>left_max_speed)fleft_wheel=left_max_speed;
else if(fleft_wheel<-left_max_speed)fleft_wheel=-left_max_speed;
if(fright_wheel>right_max_speed)fright_wheel=right_max_speed;
else if(fright_wheel<-right_max_speed)fright_wheel=-right_max_speed;
//brake both motor if PS2 controller not connected
if(SKPS(p_con_status)==0)
{
left_wheel=0;
right_wheel=0;
fleft_wheel=0;
fright_wheel=0;
}
//control motor driver based on left_wheel and right_wheel value
if(left_wheel>0) //left forward
{
M1_DIR=0;
M1_PWM=(unsigned char)left_wheel;
}
else if(left_wheel<0) //left reverse
{
M1_DIR=1;
M1_PWM=(unsigned char)(-left_wheel);
}
else //left brake
{
M1_PWM=0;
}
if(right_wheel>0) //right forward
{
M2_DIR=1;
M2_PWM=(unsigned char)right_wheel;
}
else if(right_wheel<0) //right forward
{
M2_DIR=0;
M2_PWM=(unsigned char)(-right_wheel);
}
else //right brake
{
M2_PWM=0;
}
//control motor driver based on left_wheel and right_wheel value
if(fleft_wheel>0) //left forward
{
M1_DIR=0;
M1_PWM=(unsigned char)fleft_wheel;
}
else if(fleft_wheel<0) //left reverse
{
M1_DIR=1;
M1_PWM=(unsigned char)(-fleft_wheel);
}
else //left brake
{
M1_PWM=0;
}
if(fright_wheel>0) //right forward
{
M2_DIR=1;
M2_PWM=(unsigned char)fright_wheel;
}
else if(fright_wheel<0) //right forward
{
M2_DIR=0;
M2_PWM=(unsigned char)(-fright_wheel);
}
else //right brake
{
M2_PWM=0;
}
//control up,down,in,out,gripper motion
if(SKPS(p_up)==0)
{
PW3_PWM=1;
PW3_DIR=0;
}
else if(SKPS(p_down)==0)
{
PW3_PWM=1;
PW3_DIR=1;
}
else
{
PW3_PWM=0;
}
if(SKPS(p_left)==0)
{
PW4_PWM=1;
PW4_DIR=0;
}
else if(SKPS(p_right)==0)
{
PW4_PWM=1;
PW4_DIR=1;
}
else
{
PW4_PWM=0;
}
if(SKPS(p_triangle)==0)
{
PW5_PWM=1;
PW5_DIR=0;
}
else if(SKPS(p_cross)==0)
{
PW5_PWM=1;
PW5_DIR=1;
}
else
{
PW5_PWM=0;
}
did my code had any problem? or is it i cannot just send the signal 1 to PWM to run the motor?PW3 to PW5 i don't want speed control i just want to control the motor to move when skps's button is push.