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.

Problem with motor controlling

Other Parts Discussed in Thread: MSP430F5438A

Hai,

i am controlling DC motor with MSP430F5438A Exp board. i am generating PWM signal at Port5.0 and Port5.1. This ports i am controlling through Joystick. That means when i press left side joy stick, Port5.0 has to generate PWM signal and When i press right side of joy stick, Port5.1 has to generate PWN signal.


But When i press left side joy stick, Port5.0 is generating PWM signal and when i press right side joy stick Port5.1 is not generating any signal( Port 5.0 also not generating).

This is my code:

#include<msp430f5438a.h>

int i,j;
/************ Main program start from here**********/
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;

/************ Intilization of Input and output ports**********/

//P2DIR &=~ BIT6+BIT7; //P2.6 is used as Input Pin

P5DIR |= BIT0;

P2REN |= BIT1+BIT2; // Enable P2.6 internal resistance
P2OUT |= BIT1+BIT2; // Set P2.6 as pull-Up resistance
P2IE |= BIT1+BIT2; // P2.6 interrupt enabled
P2IES |= BIT1+BIT2; // P2.6 Hi/Lo edge
P2IFG &=~ BIT1+BIT2; // P2.6 IFG cleared

__bis_SR_register(GIE); // Enter LPM4 w/interrupt

while(1)
{
if(i)
{
P1DIR |= BIT0; // Port 1 and Bit 0 set as dircttion
P1OUT |= BIT0; // P1.0 High

P5OUT |= BIT0;
__delay_cycles(250);

P5OUT &=~ BIT0;
__delay_cycles(250);
}

else if(j)
{
P1DIR |= BIT1; // Port 1 and Bit 1 set as dircttion
P1OUT |= BIT1; // P1.0 High

P5OUT |= BIT1;
__delay_cycles(250);

P5OUT &=~ BIT1;
__delay_cycles(250);
}
else
{
P5OUT &= ~BIT0;
P5OUT &= ~BIT1;
}
}
}

// Port 2 interrupt service routine
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{

if(P2IFG && BIT1)
{
i = 1;
j = 0;
P2IFG &=~ BIT1;
}
else if(P2IFG && BIT2)
{
j = 1;
i = 0;
P2IFG &=~BIT2;
}
else
{
i = 0;
j = 0;
P2IFG &=~ BIT1+BIT2;
}

}

i am unable find out mistake..

Where i am doing mistake..??

Please advice me if i m doing any wrong..??

Thankyou

  • First thing, i and j must be declared volatile.

    The compiler does not know that you are changing them inside an ISR and an ISR may happen everytime. SO it will optimize the usage of them inside main. Like putting I into a register until something in the code flow indicates that I might me change (e.g. by a function call). Declaring the variables volatile tells the compiler that it may change ‘magically’ or reading it or writing to it may have side-effects, so all accesses are carried out exactly as in the source code, even if they seem superfluous.
    All hardware registers are declared volatile for the same reason. However, declaring all global variables volatile by default would be counterproductive, as any volatile variable limits the code optimization. So it’s up to you to give the compiler the proper hints.

    Then, the port logic is wrong. You only get a port interrupt on one edge. So you never get the ISR called when neither pin is activated. You always have either P2IFG.1 or P2IFG.2 set. You ‘else’ case in the ISR will never be executed, as in this case, your ISR isn’t called at all.

    Finally, did you thing about bouncing? When you close a switch, it is not a clean, single transition from open to closed. Instead, there will be many open and close evens for a duration of up to some ms. You’ll need some mechanism do remove duplicate events. Search the board for the term ‘debounce’ or ‘debouncing’

**Attention** This is a public forum