Bug in PicsimLab stepper motor with push buttons

Thread Starter

Samantha Groves

Joined Nov 25, 2023
63
Hello I have this code
C:
void turn(int direction,int speed)
{



   int i;

   if(direction==0)
   {
  
   for(i==0;i<50;i++)
  
   {

   PORTB = 0b10010000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b11000000;
  
   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }



   PORTB = 0b01100000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b00110000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }
  
   }
  
   }
   else if(direction==1)
   {
   for(i==0;i<50;i++)

   {

   PORTB = 0b00110000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b01100000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b11000000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b10010000;

   for(i==0;i<speed;i++)
   {
   delay_ms(1);
   }

   }
  
   }


}


void main() {

  TRISB = 0b00001111;
 
  TRISD = 0b00000011;
 
  while(1)
  {
 
    if(PORTB.F0)
    
    {
 
    turn(1,10);
    
    }
    
    if(PORTB.F1)
    {
    turn(0,20);
    }
    
    delay_ms(1000);



 
  }
 

 

 
 


}
This is MikroC for programing of a PIC microcontroller.The goal is to control a stepper motor by 2 push buttons.The push buttons are connected to pins PD0 and PD1 and the 4 pins of the stepper motor are connected to pins PB4,PB5,PB6 and PB7.Can anyone test if this is a bug because the stepper motor doesnt rotate at all , and the buttons are not working and I dont understand where I am wrong.I am using PicsimLab so if any of you has spare time and wants to answer reply!Thanks!
 

Thread Starter

Samantha Groves

Joined Nov 25, 2023
63
Well I found out why the buttons dont work if you examine down the code carefuly I say PORTB instead of PORTD where the buttons are connected to.But the step motor still doesnt turn.
 
Top