Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 14th September 2007, 07:17 PM   (permalink)
Default H bridge motor logic

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
flemmard is offline  
Old 14th September 2007, 07:57 PM   (permalink)
Default

See next post.

Last edited by 3v0; 14th September 2007 at 08:19 PM.
3v0 is online now  
Old 14th September 2007, 08:18 PM   (permalink)
Default

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 by 3v0; 14th September 2007 at 08:20 PM.
3v0 is online now  
Old 15th September 2007, 11:15 AM   (permalink)
Default

Quote:
Originally Posted by 3v0
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 is offline  
Old 15th September 2007, 07:01 PM   (permalink)
Arrow

Quote:
Originally Posted by flemmard
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;
   }
 }

// 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 by 3v0; 16th September 2007 at 03:40 PM.
3v0 is online now  
Old 15th September 2007, 07:22 PM   (permalink)
Default

Quote:
Originally Posted by 3v0
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;
}

// 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 !
flemmard is offline  
Old 15th September 2007, 08:01 PM   (permalink)
Default

The line

for (j=0;j -lt- 100;j++)

should be

for (j=0;j<100;j++) // 100 as in 100%
3v0 is online now  
Old 16th September 2007, 12:36 PM   (permalink)
Default

And I think there is a typo in the motor defines...

Quote:
#define BACLWARD 2
Kyle-s4h is offline  
Old 16th September 2007, 02:56 PM   (permalink)
Default

Quote:
Originally Posted by Kyle-s4h
And I think there is a typo in the motor defines...
Yes and thanks. I fixed it in the original post.
3v0 is online now  
Old 16th September 2007, 03:05 PM   (permalink)
Default

Code:
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;
}
Hi 3v0, shoulnd't the highlited code be enclosed within brackets? or RC1 will be cleared regardless of the value of cmd.
eng1 is offline  
Old 16th September 2007, 03:37 PM   (permalink)
Default

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

Last edited by 3v0; 16th September 2007 at 03:50 PM.
3v0 is online now  
Old 17th September 2007, 04:48 AM   (permalink)
Default

Quote:
Originally Posted by 3v0
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
Hi, Not a problem ! It works well ! Thanks a lot
flemmard is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Need Help on High Current H Bridge pidot Robotics Chat 19 12th November 2008 09:10 AM
MOSFET For H Bridge Ayne General Electronics Chat 1 16th February 2007 01:08 AM
bridge rectifier batman Electronic Projects Design/Ideas/Reviews 2 15th December 2003 05:27 PM
Bridge Rectifier Current Rating -- How much is enough? hamfiles General Electronics Chat 6 6th August 2003 07:15 AM
Wireless network bridge monitoring circuit...need pointers. Goose Electronic Projects Design/Ideas/Reviews 0 17th February 2003 03:03 PM



All times are GMT. The time now is 09:07 PM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker