PIC16F887 Stepper Motor Control

Status
Not open for further replies.

KBM

New Member
Hello everyone, new to this site and coding (moved from stack overflow due to too many sarcastic people).

I am writing a code to control a stepper motor to act as a barrier for a car park;

the motor has to rotate 90* to open and close the gate - via a button (manual operation) and sensors (auto operation)

Although what i have at the moment does not appear to work. What i need at the moment is for someone to tell me how to get it turning 90 degrees both ways by clicking a button for each direction.


Code so far;

void main() {
ANSEL = 0;
ANSELH = 0;

TRISC = 0; // these two essentially enable PORTC to be used
PORTC = 0; // ^
PORTD = 0; // this is to allow for PORTD to be used as control buttons

TRISD.B0 = 1; // setting up PORTD bit 0 as a control button
TRISD.B1 = 1; // setting up PORTD bit 1 as a control button

if (PORTD.B0 == 1); // this should only do this line of code if PORTD bit 0 is pressed
do {
PORTC = 0x09;
Delay_ms(100);
PORTC = 0x0c;
Delay_ms(100);
PORTC = 0x06;
Delay_ms(100);
PORTC = 0x03;
Delay_ms(100);
} while (0);

if (PORTD.B1 == 1); // for some reason it only rotates one way and stops when bit 1 is pressed
do {
PORTC = 0x03;
Delay_ms (100);
PORTC = 0x06;
Delay_ms (100);
PORTC = 0x0c;
Delay_ms (100);
PORTC = 0x09;
Delay_ms (100);
} while (0);

Please be patient with me as i am new to coding but i could really do with the help.

Thank you
 
Two things stick out.....

If(blah = blah); <-- the semi colon finishes the if, so loose the semi colon and encapsulate the conditions arguments with curly braces

Two do while(condition) not zero..
C:
if (PORTD.B0 == 1)  // this should only do this line of code if PORTD bit 0 is pressed
   {
   do {
      PORTC = 0x09;
      Delay_ms(100);
      PORTC = 0x0c;
      Delay_ms(100);
      PORTC = 0x06;
      Delay_ms(100);
      PORTC = 0x03;
      Delay_ms(100);
      } while (correct_position_achieved); // A zero here will only let the DO/WHILE execute once only..
   }
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…