This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Trying to drive a step signal low (0)

Hello everyone.  I am currently trying to drive a stepper motor via generating a step signal (from P3.6 on the MSP430), which is working fine, the part I am having trouble with is illuminating a LED from that same step signal. The LED will illuminate when the step signal (movemotor subroutine) is being called, however the issue is that the LED will sometimes stay on when this signal is not being called.  The movemotor subroutine is called when a button connected to P2.2 is high (1).

Using an oscilloscope, I checked the step signal after being called, and it is sometimes high and sometimes low; the signal isn't 'stepping or oscillating high -> low -> high -> low -> high -> low... etc,' however it is terminating on either a logic high or logic low and holding that value.  So I believe my issue is being able to ensure this signal always ends with a logic value of 'low'.  I greatly appreciate any suggestions or comments to aid me in hopefully figuring this out. 

Below is part of my program which I am trying to figure out a way to always end the signal with a logic level low:

#include  "msp430xG46x.h"
#include <intrinsics.h> // Intrinsic functions
#include <stdio.h>
#include <string.h>

int ib1=0;
unsigned int x;
volatile unsigned int i;

void delay(int x){
	for(int i=0;i<1000;i++)
		for(int j=0; j<x; j++)
                  i++;
}

void movemotor(int x){ 
  P3DIR |= BIT6;
  if(ib1%2==0){
  P3OUT |= BIT6;
  delay(10);
  }
   if(ib1%2==1){
  P3OUT &= ~BIT6;
  delay(10);
  }
	ib1++;
  if(ib1==10000)
    ib1=0;
}

void button(){

volatile int btn=0;
btn=P2IN&BIT2;
     
     if(btn==BIT2){
        movemotor(1);
  }
}
  • You can add the line in red as shown below:

    void button(){
    
    volatile int btn=0;
    btn=P2IN&BIT2;
         
         if(btn==BIT2){
            movemotor(1);
          movemotor(1);
      }
  • Thank you.  Could you please briefly explain why adding that movemotor(1); statement again would ensure that the signal terminates to a low logic value?

    Thank you,

    David Yazdani

  • void button(){
     
    volatile int btn=0;
    btn=P2IN&BIT2;
          
         if(btn==BIT2){
            movemotor(1);
            movemotor(1);
      }

  • Thanks again, I am still just extremely curious as to why that redundant line would be a possible solution ensuring a low logic value, could you please explain by chance?

    Thank you,

    David Yazdani

  • If you call movemotor(1) odd number (1,3,5,7,etc) of times, you end up high. If you call it even number (2,4,6,8,etc) of times, you end up low.

  • So I just tried adding that redundant movemotor(1), and tried running it with a single movemotor(2) statement, and nothing has changed; the LED stays illuminated quite often, and will sometimes turn off after several attempts of pushing the button. 

    Also when I am moving the motor, I hold the button down until the motor is in a desireable position - I don't simply press it momentarily.  Could this factor into why the output states are almost randomly choosing to be high or low after holding the button down? 

    The system i'm building has 4 motors, I simplified the code as much as I could without it being a whole heap to try to understand; it's very peculiar to me that 1 of the 4 motor step signals always go low (as desired), yet the 6 others are having the issue i've been trying to fix with the LEDs sometimes staying illuminated when the step signal is unaserted.  Any more insight into what's going on would be tremendously appreciated!

    Thank you,

    David Yazdani

  • Your code has lots of unusual features. The parameter x in movemotor(x); is never referenced. Hence calling movemotor(2) has exactly the same effect as calling movemotor(1). If you want to use movemotor(2), you have to use it twice too. And do not try to call movemotor(4) as a substitute for calling movemoter(2) twice. You will need to call movemotor(4) twice as well.

  • I'm still a bit confused.. you said "If you call movemotor(1) odd number (1,3,5,7,etc) of times, you end up high. If you call it even number (2,4,6,8,etc) of times, you end up low."  How can I call movemotor(2) twice while referencing x?

    Would turning the pin low at the end of the movemotor function possibly do it? My idea is below:

    void movemotor(int x){
      P3DIR |= BIT6;
      if(ib1%2==0){
      P3OUT |= BIT6;
      delay(10);
      }
       if(ib1%2==1){
      P3OUT &= ~BIT6;
      delay(10);
      }
        ib1++;
      if(ib1==10000)
        ib1=0;
    
      P3OUT &= ~BIT6;
      delay(5);
    }

  • (1) My original suggestion was, use your original code except the following addition in red:

    void button(){
     
    volatile int btn=0;
    btn=P2IN&BIT2;
          
         if(btn==BIT2){
            movemotor(1);
            movemotor(1);
      }


    (2) You did not follow my suggestion and used the following instead:

    void button(){
     
    volatile int btn=0;
    btn=P2IN&BIT2;
          
         if(btn==BIT2){
            movemotor(2);
      }

    (3) The above will produce exactly the same result as the original code. You need to call movemotor twice.

    void button(){
     
    volatile int btn=0;
    btn=P2IN&BIT2;
          
         if(btn==BIT2){
            movemotor(2);
            movemotor(2);
      }

    (4) Your latest change might work. But the motor might act strangely. I do not know how you drive the motor, nor what the motor need. You never showed that.

  • David Yazdani said:
    I hold the button down

    As soon as I see this, I think "switch bounce". This is a perennial topic in the Forum; search for "debounce".

    You haven't given much context, so that's why we're all guessing. When/by whom is "button()" called? If it's driven by a bouncing switch, it's unpredictable whether movemotor() is called an odd or even number of times.

**Attention** This is a public forum