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.

MSP430G2553: Bidirectional Motor control with MSP430G2553

Part Number: MSP430G2553
Other Parts Discussed in Thread: L293D,

Hello, I have started programming the MSP430G2553 recently. I am trying to make the dc motor rotate in either direction with a push of a button. The motor driver I am using is L293D. I got it to spin in 1 direction, but when I try to spin it in the other direction, it keeps on spinning in the original direction. I know that the motor is capable of spinning in the other direction as I switched the cable around and it worked.

Here is the code:

void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;           // Stop Watchdog
    if (CALBC1_1MHZ==0xFF)              // Check if calibration constant erased
    {
        while(1);                       // do not load program
    }
    timer_init();

    // configuring button interrupt
    P1DIR &= ~BIT3;                       // Set SW pin -> Input
    P1REN |= BIT3;                        // Enable Resistor for SW pin
    P1OUT |= BIT3;                        // Select Pull Up for SW pin

    P1IES &= ~BIT3;                       // Select Interrupt on Rising Edge
    P1IE |= BIT3;                         // Enable Interrupt on SW pin
    P1IFG &= ~BIT3;                     // clear the flag


    // pwm motor port config
    //Motor Control pins
    P2DIR |=  BIT0 + BIT1 + BIT2;   // P2.0,P2.1,P2.2 all output
    P2OUT &= ~BIT0;   // Clear P2.0,P2.1,P2.2
    P2OUT &= ~BIT1;
    P2OUT &= ~BIT2;
    while(1)
    {    }
}

Here is the timer config

    //pwm timer config
    TA1CCTL1 = OUTMOD_7;            // TACCR1 reset/set
    TA1CCR0  = 1000-1;        // PWM Period
    TA1CCR1  = 300;            // TA1CCR1 PWM Duty Cycle
    TA1CTL   = TASSEL_2 + MC_1 + ID_0 + TACLR;     // SMCLK, upmode

    // Global Interrupt Enable
    _BIS_SR(GIE);

And finally, the button interrupt

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
    if (P1IFG & BIT3) // make sure it's button interrupt
    {
        if (select == 0)  // turn motor on 1 way
        {
            select = 1;
            P2SEL |= BIT1;   // pwm P1A
            P2OUT &= ~BIT2; // P2A low
            P2OUT |= BIT0;  // select enable
        }
        else if (select == 1) // turn off motor
        {
            select = 2;
            P2SEL &= ~BIT1;
            P2SEL &= ~BIT2;
            P2OUT &= ~BIT1;
            P2OUT &= ~BIT0;
            P2OUT &= ~BIT2;
        }
        else if (select == 2)  // turn on motor the other way
        {
            select = 3;
            P2SEL |= BIT2;   // P2A pwm
            P2OUT &= ~BIT1;  // P1A low
            P2OUT |= BIT0;   // select enable
        }
        else                // turn off motor again
        {
            select = 0;
            P2SEL &= ~BIT1;
            P2SEL &= ~BIT2;
            P2OUT &= ~BIT1;
            P2OUT &= ~BIT0;
            P2OUT &= ~BIT2;
        }
        P1IFG &= ~BIT3;  // clear interrupt flag
    }
}

I have P2.0 connected to the EN of the L293D, P2.1 and P2.2 are connected to P1A and P2A, respectively.

  • Hi,

    From the description and mcu sw, i cannot find any mistake. please send out your circuit and get the oscilloscope waveform for p21 and p22 then we can discuss with more information.

  • Hello Hawken, thank you for replying

    I think I have figured out the problem and have partially mitigated it. I do not have an oscilloscope or a multimeter at home to measure the voltage of the out put, however, I do have some small LEDs lying around. I hooked them up to the MSP430G2553 and found that for some reasons, the P2.2 is always half way between on and off regardless of what I do.

    I believe that P2.2's is broken on the chip or that the trace on the PCB have been damaged. What I did to solve this is to use P2.3 instead of P2.2.

    Since P2.3 is linked to a different timer channel of timer1, I had to switch the pwm configuration around a little bit. Not wanting to configure another channel to do PWM, I moved the P1A and P2A to port P2.0 and P2.3, respectively just to do straight on/off. Then, I connected the P2.1 (still pwm) to the EN of the motor driver. PWMing the EN is achieves pretty much the same effect as PWMing P1A and P2A separately. For convenience's sake, I really should buy some cheap oscilloscope for easy troubleshooting in the future.

**Attention** This is a public forum