User manual
Robotics experiment with PIC microcontroller l 47
char motor_duty_= 127; // Defalt PWM 50%
char motor_init_=0; // Status initial
// *** Motor A *****
// PD0 ====> 1A
// PD1 ====> 1B
// PC2 ====> 1E (PWM1)
// *** Motor B *****
// PB1 ====> 2A
// PB2 ====> 2B
// PC1 ====> 2E (PWM2)
//****************************************************
//********** Initial Motor Function ******************
//****************************************************
void Motor_Init()
{
if (motor_init_==0) // First time ?
{
motor_init_=1; // Status
ANSELH.F0=0; // RB1 ==> Digital IO
ANSELH.F2=0; // RB2 ==> Digital IO
TRISB.F1=0; // Motor B 2A
TRISB.F2=0; // Motor B 2B
TRISD.F0=0; // Motor A 1A
TRISD.F1=0; // MOtor A 1B
Pwm1_Init(5000); // Initail PWM 1E
Pwm2_Init(5000); // Initail PWM 2E
}
}
//****************************************************
//****************************************************
//********** Control Duty Cycle *********************
//****************************************************
void Change_Duty(char speed)
{
if (speed != motor_duty_) // Check Same old speed
{
motor_duty_=speed; // Save for old speed
Pwm1_Change_Duty(speed); // Motor A
Pwm2_Change_Duty(speed); // Motor B
}
}
//****************************************************
/********** Motor A Forward ********/
void Motor_A_FWD()
{
Pwm1_Start();
PORTD.F0 =0;
PORTD.F1 =1;
}
/************************************/
Listing 4-1 : The source code of motor.h library file for driving the DC motor
(continue)