int main()
{
//Declare the variables for the main function here
int k = 3;
int j = 0;
int i = 0;
initialize_IO_ports();
//put the infinite loop here
while(1)
{
if( k == 3)
{
delay_ms(3000);
for(j=0; j<1000;j++)
{
RC0 = 1; // inputs for bi-directional DC motor circuit
RC1 = 0;
delay_ms(6000); // run forward for 6 secs
}
delay_ms(5000); //wait for 5 seconds -- HALT--No running
i = 0; // insert value
if(i == 0)
{
delay_ms(3000); //wait for sometime
for(j=0;j<1000;j++)
{
RC0 = 0;
RC1 = 1;
delay_ms(6000); // run backward for 6 secs
}
}
}
else
{
RC0 = 0;
RC1 = 0;
}
}; // end of while
return(1);
} // end ofmain
Hi all,
This is my PIC c code for H bridge motor driver.Manually, my H bridge motor driver works well.But when i use it with PIC, the motor runs continuously in one direction.
1) When the input is 1 , the motor should start after 3 secs and run forward for 6 secs
2) should stop now for 3 secs and run backward for 6 secs and stop.
Thanks for your reply.Before myself to test this code, need i to care about PWM stuff in this? Or simply this will do my job if i don't care about speed, etc?
Thanks for your reply.Before myself to test this code, need i to care about PWM stuff in this? Or simply this will do my job if i don't care about speed, etc?
The code as I edited it does not do PWM for speed control. Get this working first and understand it.
If you do not understand the non PWM code the following will not help.
PWM is not difficult. You can find many examples of it on the net. The code I wrote for my use is listed at leehow.blogspot.com.
The following is based on my code but modified to run one motor with the settings given in your original code.
Code:
//move forward for about 20 seconds at 75% power.
move(20,FORWARD,75,0);
// motion defines
#define STOP 0
#define FORWARD 1
#define BACKWARD 2
#pragma inline // another copy each time it is called
void action(int cmd)
{
if (cmd == forward)
{
RC0=1; RC1=0;
}
else if (cmd == backward)
{
RC0=0; RC1=1;
}
else // stop
{
RC0=0;
RC1=0;
}
[FONT=monospace] }
[/FONT]// command to move robot
void move(
int duration, // duration of command
int _val, // one of the motion defines
int powerLeft, // % power for left motor
int powerRight) // % power for right motor
{
int val, i, j;
val = _val;
// send the PWM signals to the H-Bridge
for (i=0; i < duration; i++)
{
action(_val); // _val will be FORWARD or BACKWARD
for (j=0;j<100;j++) // 100 as in 100%
{
if (j == powerLeft) // start low/off part for left
{
action(STOP);
}
// you only have the one motor so we skip this
//if (j == powerRight) //start low/off part for right
//{
// val = val & 0xF0;
// PORTB = val;
//}
delay_ms(1);
}
}
action(STOP); // done with cmd so stop both motors
}
The code as I edited it does not do PWM for speed control. Get this working first and understand it.
If you do not understand the non PWM code the following will not help.
PWM is not difficult. You can find many examples of it on the net. The code I wrote for my use is listed at leehow.blogspot.com.
The following is based on my code but modified to run one motor with the settings given in your original code.
Code:
//move forward for about 20 seconds at 75% power.
move(20,FORWARD,75,0);
// motion defines
#define STOP 0
#define FORWARD 1
#define BACLWARD 2
#pragma inline // another copy each time it is called
void action(int cmd)
{
if (cmd == forward)
{
RC0=1; RC1=0;
}
else if (cmd == backward)
{
RC0=0; RC1=1;
}
else // stop
RC0=0; RC1=0;[FONT=monospace]
}
[/FONT]// command to move robot
void move(
int duration, // duration of command
int _val, // one of the motion defines
int powerLeft, // % power for left motor
int powerRight) // % power for right motor
{
int val, i, j;
val = _val;
// send the PWM signals to the H-Bridge
for (i=0; i < duration; i++)
{
action(_val); // _val will be FORWARD or BACKWARD
for (j=0;j -lt- 100;j++)
{
if (j == powerLeft) // start low/off part for left
{
action(STOP);
}
// you only have the one motor so we skip this
//if (j == powerRight) //start low/off part for right
//{
// val = val & 0xF0;
// PORTB = val;
//}
delay_ms(1);
}
}
action(STOP); // done with cmd so stop both motors
}