Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

H bridge motor logic

Status
Not open for further replies.

flemmard

New Member
Code:
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.

Please help me !

Thanks
 
Try this
Code:
int main()
{
  
   initialize_IO_ports();
   RC0 = 0;    // EDIT: paranoid
   RC1 = 0; 
   
   //put the infinite loop here
   while(1)
   {
       delay_ms(3000);
   
       RC0 = 1;        // forward
       RC1 = 0;
       delay_ms(6000); // for 6 secs
      
       RC0 = 0;        // -- HALT--No running
       RC1 = 0;
       delay_ms(3000); // for 3 seconds 
          
       RC0 = 0;        // backward
       RC1 = 1;
       delay_ms(6000); // for 6 secs
          
       RC0 = 0;
       RC1 = 0;       
       
    };   // end of while
    return(1);
}   // end of main
I am thinking the 1000 loop stuff may be leftover from some PWM that your code was not doing.
 
Last edited:
3v0 said:
Try this
Code:
int main()
{
  
   initialize_IO_ports();
   RC0 = 0;    // EDIT: paranoid
   RC1 = 0; 
   
   //put the infinite loop here
   while(1)
   {
       delay_ms(3000);
   
       RC0 = 1;        // forward
       RC1 = 0;
       delay_ms(6000); // for 6 secs
      
       RC0 = 0;        // -- HALT--No running
       RC1 = 0;
       delay_ms(3000); // for 3 seconds 
          
       RC0 = 0;        // backward
       RC1 = 1;
       delay_ms(6000); // for 6 secs
          
       RC0 = 0;
       RC1 = 0;       
       
    };   // end of while
    return(1);
}   // end of main
I am thinking the 1000 loop stuff may be leftover from some PWM that your code was not doing.


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?
 
flemmard said:
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
}
 
Last edited:
3v0 said:
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
}

You guys are really helpful ! Awesome ....Thanks a lot ! :)
 
Code:
void action(int cmd)
{
   if (cmd == forward)
   {
          RC0=1; RC1=0;
   }
   else if (cmd == backward)
   {
          RC0=0; RC1=1;
   }
   else // stop
    [B]      RC0=0; RC1=0;[/B][FONT=monospace]
}
[/FONT]

Hi 3v0, shoulnd't the highlited code be enclosed within brackets? or RC1 will be cleared regardless of the value of cmd.
 
The posted code is a simplified and modified version of working code that I am using, but I botched the modification.

That is what I get for breaking the rules for writting good code.

1. One should always use brackets even if the code is only one line.

2. Never put more then one statement on a line.

Had I followed either of these I would not have goofed.

I will fix it in the original post :eek:
 
Last edited:
3v0 said:
The posted code is a simplified and modified version of working code that I am using, but I botched the modification.

That is what I get for breaking the rules for writting good code.

1. One should always use brackets even if the code is only one line.

2. Never put more then one statement on a line.

Had I followed either of these I would not have goofed.

I will fix it in the original post :eek:

Hi, Not a problem ! It works well ! Thanks a lot
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top