Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Stepper motor rotation using loop works only once

Status
Not open for further replies.

madhushan90

Newbie level 6
Joined
Jul 26, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,362
Hi,

I am writing a code for move drill bit in x, y coordinates, when looping through x, y coordinates list two stepper motors are rotating only once, next coordinates are being ignored. Why this is happen? I am using MikroC for write the code. Also I am getting input from serial port. What could be the reasons for this situation. If anyone has similar project please give me suggestions, any help would be great. Thanks.

here is my code
Code:
#define speed 50
#define charLenth 6
#define xyLenth charLenth/2
#define roundPerCentimeter 1

char xCordinate[xyLenth];
char yCordinate[xyLenth];
int actualXLenth;
int actualYLenth;
int xValue1;
int xValue2;
int yValue1;
int yValue2;
int xNumRound; // how many rotate motor
int yNumRound; // how many rotate motor
int i;
int j = 0;
int k = 0;
char cordinate[charLenth];
int drillHeightRound =3;
int points = 0;
int fwdsteps = 0;
int bwdsteps = 0;

 void xStepperForward(int xRoundForwrd){
  for(i=1;i<xRoundForwrd+1;i++){
  portd = 1;
  delay_ms(speed);
  portd = 2;
  delay_ms(speed);
  portd = 4;
  delay_ms(speed);
  portd = 8;
  delay_ms(speed);
  portd = 1;

  }
 }
 void xStepperBackward(int xRoundBackwrd){
  for(i=1;i<xRoundBackwrd+1;i++){
  portd = 8;
  delay_ms(speed);
  portd = 4;
  delay_ms(speed);
  portd = 2;
  delay_ms(speed);
  portd = 1;
  delay_ms(speed);
  portd = 8;

  }
 }
 void yStepperForward(int yRoundForwrd1){
    for(i=1;i<yRoundForwrd1+1;i++){
  portd = 16;
  delay_ms(speed);
  portd = 32;
  delay_ms(speed);
  portd = 64;
  delay_ms(speed);
  portd = 128;
  delay_ms(speed);
  portd = 16;

  }
 }
 void yStepperBackward(int yRoundBackwrd){
    for(i=1;i<yRoundBackwrd+1;i++){
  portd = 128;
  delay_ms(speed);
  portd = 64;
  delay_ms(speed);
  portd = 32;
  delay_ms(speed);
  portd = 16;
  delay_ms(speed);
  portd = 128;

  }
 }
 void drillStepperForward(){
    for(i=0;i<drillHeightRound;i++){
  portb = 1;
  delay_ms(speed);
  portb = 2;
  delay_ms(speed);
  portb = 4;
  delay_ms(speed);
  portb = 8;
  delay_ms(speed);
  portb = 1;

  }
 }
 void drillStepperBackward(){
         for(i=0;i<drillHeightRound;i++){
  portb = 8;
  delay_ms(speed);
  portb = 4;
  delay_ms(speed);
  portb = 2;
  delay_ms(speed);
  portb = 1;
  delay_ms(speed);
  portb = 8;

  }
 }
 void Read_Serial(){

  Delay_ms(100);                  // Wait for UART module to stabilize
  
  UART1_Write_Text("Enter X cordinate and Y cordinate : ");
  while (1) {                     // Endless loop

        for(i=0;i<charLenth;)
                {
                    if(UART1_Data_Ready()) // if data ready
                    {
                        cordinate[i] = UART1_Read();
                          // read data
                        i++;
                    }

                }

      break;
      
    }

  }

    
void main() {
  ADCON1 = 0x06; // To turn off analog to digital converters
  UART1_Init(9600);     // Initialize UART module at 9600 bps

  TRISC=0x81;        //input for RX Serial input
  PORTC=0x00;

  TRISD=0x00;        // Initialize motor X and y
  PORTD=0x00;
  
  TRISB=0x00;        //  Initialize motor drill
  PORTB=0x00;
  TRISA=0x00;
  PORTA = 0x00;
  Read_Serial();
  
  Delay_ms(100);

  points = sizeof(xCordinate)/sizeof(xCordinate[0]);

  for(i = 0; i < points; i++){
   if(i == 0)
    {
            xNumRound = roundPerCentimeter*xCordinate[i] - '0';
            yNumRound = roundPerCentimeter*yCordinate[i] - '0';
            xStepperForward(xNumRound);
            Delay_ms(100);
            // stepper forword
            yStepperForward(yNumRound);
            Delay_ms(100);
    }else{
    //x logic
    if(xCordinate[i - 1] < xCordinate[i]){
       //forward
       fwdsteps = xCordinate[i] - xCordinate[i-1];
       xStepperForward(roundPerCentimeter*fwdsteps - '0');
    }else{
    //backward
        bwdsteps = xCordinate[i-1] - xCordinate[i];
        xStepperBackward(roundPerCentimeter*bwdsteps - '0');
    }
    } 
  }



}
 

You should not use delays for motors. Instead use Timer interrupts to generate the delays. You can use mikroE Timer Calculator tool for this. If you use delays then it becomes a blocking code and PIC will not be able to do other things when it is executing motor delays. If you have buttons then button press will not be detected when motor code is executing.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top