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.

Rotating 2 stepper motor using pic16f877a

Status
Not open for further replies.
I just put it there to see if it would solve my problem but it didn't..
Yeah i want it to go back to case 0 after case 3
 
And forgive me I didn't get your point about how to display 1.8 on the lcd screen.
And the itoa() function is not for mikroC..
 
I thought i had got the answer for the loop thing by replacing if by while but it worked at first but now its giving the same problem
 
Last edited:
itoa(); is in every C compilier.... It's a standard feature

Here's how I do it
C:
int sum = 12345;

Lcd_Out(1,1, print_float(sum,2) );  // This will print " 123.45"

char * print_float(int FLT, int decimal)
   {
    char* result[7];
    char point = 0;
  
    result[0] = (FLT / 10000) + 48;
    if(result[0] == 48) result[0] = 32;
    result[1] = ((FLT % 10000) / 1000) + 48;
    if(decimal==4)
        {
         result[1] = '.';
         point = 1;
         }
    else
        if((result[0] ==32) && ( result[1] == 48)) result[1] = 32;
    result[2] = ((FLT % 1000) / 100) + 48;
    if(decimal==3)
        {
         result[2] = '.';
         point = 1;
         }
    else
          if((result[1+point] ==32) && ( result[2+point] == 48)) result[2+point] = 32;
    result[3+point] = ((FLT % 100) / 10) + 48;  
    if(decimal==2)
        {
         result[3] = '.';
         point = 1;
         }
    else
         if((result[2+point] ==32) && ( result[3+point] == 48)) result[3+point] = 32;
    if(decimal==1)
        {
         result[4] = '.';
         point = 1;
         }
    result[4+point] = (FLT % 10) + 48;
    result[5+ point] = 0;
   }

I haven't had time to check this but if you need up to 4 decimal places, this will put it in a string.

There are other methods but I find writing your own is nearly always the best idea!!!
 
M still finding that decimal stuff confusing..
BTW the loop problem i was having i thought i fixed it but on simulation it leads to another problem.
I have put 2 while loops if certain conditions are not satisfied..
Now after the while loops it should go to the infinite loop but after the first 2 for loops in the infinite loop it seems to start from the begining i.e. the while loop
Just see what is the problem..
Thanks again..
 

Attachments

  • LCD.rar
    80.1 KB · Views: 136
Last edited:
Your code has become illegible... I am struggling to follow the switch statements... I'm sure the brackets are wrong. The else statement at the end??? Is it from the " if(count4==1) "or from the next one.." if (count2==0)" .?
 
I noticed something..
If i remove the 2nd while loop i.e. while(count2<13) the program seems to work properly
and vice versa.. if 1st while loop is removed and 2nd kept it works properly..
Now i dunno whats causing the previous problem to occur..
I uploaded 2 videos one showing how it is supposed to work that occurs when i put only one loop and the other when the problem occurs when i put both the conditions
 

Attachments

  • video.rar
    382.6 KB · Views: 140
I am uploading the same program with comments
Code:
int i;
int j;
int k;
int m;
int l;
int ch;
  int count=0;          //Keep track of days
  int count1=0;         //Reference position of motor
  int count2=13+2;      //Equator count value+(days passed since equator/7)
  int count3=0;
  int count4=0;         //Direction of N-S motor  0=clockwise   1=anticlockwise
  int count5=0;
  int count6=0;
  int count7=0;
  int count8=13;       //Count at equator
int am1=0.0;
int am2=0.0;
char txt1[6];
//LCD Module Connection Initialization
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;

sbit LCD_D7 at RC7_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D4 at RC4_bit;
//End of LCD Module Connection

//Intialization of LCD Pin Direction
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;

sbit LCD_D7_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC4_bit;
//End of LCD Pin Direction

void main()
{ //Intialization of Ports
  TRISB=0X00;     //Initialize Port B as output
  PORTB=0X00;     //Assign Value 0 to port B
  TRISD=0X00;     //Initialize Port D as output
  PORTD=0X00;     //Assign Value 0 to port D
  //End of Initialization
Lcd_Init();                           //Initailize LCD Module
Lcd_Cmd(_LCD_CLEAR);                  //Clear Display
Lcd_Cmd(_LCD_CURSOR_OFF);             //Cursor Off
while(count2>13){                     //If Earth greater than 0* of the Equator
        PORTD=0X04;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8++;                     //Increment value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=1;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        PORTD=0X02;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8++;                     //Increment value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=2;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        PORTD=0X08;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8++;                     //Increment value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=3;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        PORTD=0X01;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8++;                     //Increment value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=0;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        }

while(count2<13){                     //If Earth lesser than 0* of the Equator
        PORTD=0X08;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8--;                     //Decrement value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=1;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        PORTD=0X02;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8--;                     //Decrement value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=2;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        PORTD=0X04;                   //Move stepper motor by a step
        delay_ms(500);                //Short delay
        count8--;                     //Decrement value to increment motor value by 1
        if(count8==count2){           //Check if motor value = Equinox value
        PORTD=0X00;                   //Pass no current to motor
        count1=3;                     //Reference position value set
        delay_ms(500);                //Short delay
        break;
        }
        PORTD=0X01;                  //Move stepper motor by a step
        delay_ms(500);               //Short delay
        count8--;                    //Decrement value to increment motor value by 1
        if(count8==count2){          //Check if motor value = Equinox value
        PORTD=0X00;                  //Pass no current to motor
        count1=0;                    //Reference position value set
        delay_ms(500);               //Short delay
        break;
        }
        }
for(;;)                               //Infinite Loop
{
  for(i=0;i<1;i++)                    //Rotating from east to west by 1.8 every 7 minutes
  {
        PORTB=0X04;                   //Move stepper motor by a step
        delay_ms(500);                //Delay
        PORTB=0X00;                   //Pass no current to motor
        Lcd_Out(1,1,"Angle M1:");
        am1=am1+1.8;
        Lcd_Chr(1,10,48+am1);               //Output value of am1 on LCD
        Lcd_Chr_CP('.');                    //To display value "."
        ch=am1*10;
        ch=ch%10;
        Lcd_Chr(1,10,48+ch);

           
        PORTB=0X02;                   //Move stepper motor by a step
        delay_ms(500);                //Delay
        PORTB=0X00;                   //Pass no current to motor
        am1=am1+1.8;

        PORTB=0X08;                   //Move stepper motor by a step
        delay_ms(500);                //Delay
        PORTB=0X00;                   //Pass no current to motor
        am1=am1+1.8;

        PORTB=0X01;                   //Move stepper motor by a step
        delay_ms(500);                //Delay
        PORTB=0X00;                   //Pass no current to motor
        am1=am1+1.8;

  }
  for(j=0;j<1;j++)  //Rotating from west to east
  {
    PORTB=0X08;                       //Move stepper motor by a step
    delay_ms(500);                    //Delay
    am1=am1-1.8;
        PORTB=0X02;                   //Move stepper motor by a step
    delay_ms(500);                    //Delay
    am1=am1-1.8;
        PORTB=0X04;                   //Move stepper motor by a step
    delay_ms(500);                    //Delay
    am1=am1-1.8;
    PORTB=0X01;                       //Move stepper motor by a step
    delay_ms(500);                    //Delay
    am1=am1-1.8;
    count++;
  }
        if(count==2){                 //For 7 days ((365/94)=3.80) --> (1.8*3.80 = 6.84 = 7 days)
        if(count4==0){                //Reference that motor moving in clockwise direction
        switch(count1){               //Reference position the motor stopped
case 0:
{        if (count2==26){             //One end of Equinox = 23.5* --> For 47 degrees  ((47/1.8) = 26.11) =26 steps
        count4=1;                     //Reference that motor should move in anti-clockwise direction
        break;
        }
        PORTD=0X04;                   //Move stepper motor by a step
        count=0;                      //Reset 7 day counter to 0
        count1=1;                     //Reference position value set
        delay_ms(500);                //delay
        count2=count2++;              //Increment Equinox Step by 1
}
        break;
case 1:
{        if (count2==26){             //One end of Equinox = 23.5*
        count4=1;                     //Reference that motor should move in anti-clockwise direction
        break;
        }
        PORTD=0X02;                   //Move stepper motor by a step
        count1=2;                     //Reference position value set
        count=0;                      //Reset 7 day counter to 0
        delay_ms(500);                //delay
        count2=count2++;              //Increment Equinox Step by 1
}
        break;
case 2:
{        if (count2==26){             //One end of Equinox = 23.5*
        count4=1;                     //Reference that motor should move in anti-clockwise direction
        break;
        }
        PORTD=0X08;                   //Move stepper motor by a step
        count1=3;                     //Reference position value set
        count=0;                      //Reset 7 day counter to 0
        delay_ms(500);                //delay
        count2=count2++;              //Increment Equinox Step by 1
        break;
}
case 3:
{        if (count2==26){             //One end of Equinox = 23.5*
        count4=1;                     //Reference that motor should move in anti-clockwise direction
        break;
        }   PORTD=0X01;               //Move stepper motor by a step
        count1=0;                     //Reference position value set
        count=0;                      //Reset 7 day counter to 0
        delay_ms(500);                //delay
        count2=count2++;              //Increment Equinox Step by 1
        break;
}
}
}
if(count4==1){                        //Reference that motor should move in anti-clockwise direction
switch(count5){
case 0:
{
        if (count2==0){               //One end of Equinox = 23.5*
        count4=0;                     //Reference that motor should move in clockwise direction
        break;
        }
        switch(count1){              //Reference position of the motor
        case 0:
        {count6=1;
break;
        }
        case 1:{count6=4;
break;
}
        case 2:{count6=3;
break;
}
        case 3:{count6=2;
break;
}
}
}
}
switch(count6){
case 1:
{
        if (count2==0){               //One end of Equinox = 0.0*
        count4=0;                     //Reference that motor should move in clockwise direction
        break;
        }
        PORTD=0X08;                   //Move stepper motor by a step
        count=0;                      //Reset 7 day counter to 0
        count1=3;                     //Reference position value set
        delay_ms(500);                //delay
        count2=count2-1;              //Decrement Equinox Step by 1
        break;
}
case 2:
{        if (count2==0){              //One end of Equinox = 0.0*
        count4=0;                     //Reference that motor should move in clockwise direction
        break;
        }
        PORTD=0X02;                   //Move stepper motor by a step
        count1=2;                     //Reference position value set
        count=0;                      //Reset 7 day counter to 0
        delay_ms(500);                //delay
        count2=count2-1;              //Decrement Equinox Step by 1
        break;
}
case 3:
{        if (count2==0){              //One end of Equinox = 0.0*
        count4=0;                     //Reference that motor should move in clockwise direction
        break;
        }
        PORTD=0X04;                   //Move stepper motor by a step
        count1=1;                     //Reference position value set
        count=0;                      //Reset 7 day counter to 0
        delay_ms(500);                //delay
        count2=count2-1;              //Decrement Equinox Step by 1
        break;
}
case 4:
{        if (count2==0){              //One end of Equinox = 0.0*
        count4=0;                     //Reference that motor should move in clockwise direction
        break;
        }   PORTD=0X01;               //Move stepper motor by a step
        count1=0;                     //Reference position value set
        count=0;                      //Reset 7 day counter to 0
        delay_ms(500);                //delay
        count2=count2-1;              //Decrement Equinox Step by 1
        break;
}
}
}
if (count2==0){                       //One end of Equinox = 0.0*
        count4=0;                     //Reference that motor should move in clockwise direction
        }
}
else
{for (l=0;l<2;l++){                   //Delay for the rest of the 12 hours
        delay_ms(431);
}
}
}
}
 
Status
Not open for further replies.

Latest threads

Back
Top