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.