![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| 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 | |
| |
| | (permalink) |
| See next post.
__________________ search engine for electronic partsJunebug USB PIC programmer kit., USB Bit Wacker, 3v0's Homepage The 15 Minute Printed Circuit Board! (+drill time) Last edited by 3v0; 14th September 2007 at 08:19 PM. | |
| |
| | (permalink) |
| 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
__________________ search engine for electronic partsJunebug USB PIC programmer kit., USB Bit Wacker, 3v0's Homepage The 15 Minute Printed Circuit Board! (+drill time) Last edited by 3v0; 14th September 2007 at 08:20 PM. | |
| |
| | (permalink) | |
| Quote:
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? | ||
| |
| | (permalink) | |
| Quote:
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. | ||
| |
| | (permalink) | |
| Quote:
| ||
| |
| | (permalink) |
| The line for (j=0;j -lt- 100;j++) should be for (j=0;j<100;j++) // 100 as in 100%
__________________ search engine for electronic partsJunebug USB PIC programmer kit., USB Bit Wacker, 3v0's Homepage The 15 Minute Printed Circuit Board! (+drill time) | |
| |
| | (permalink) | |
| And I think there is a typo in the motor defines... Quote:
| ||
| |
| | (permalink) | |
| Quote:
| ||
| |
| | (permalink) |
| 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;
}
| |
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) | |
| Quote:
| ||
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| 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 |