This code runs the Low Voltage Motor Booster Pack for the MSP-430 with the MSP430G2553 chip but could easily be adapted to any microcontroller. This uses Timer1_A0 to make a timing interrupt. Using the timing interrupt it changes the duty cycle of the PWM. Supports reverse by sending the “move_motors” command negative values.
Default is P2.0, P2.1, P2.2, and P2.3 for controlling the motor driver. P1.0 is toggled (on board LED) in the ISR.
Code: CCS V5
isr_motor_pwm.c
isr_motor_pwm.h
Project File Ver1.0
/*
*isr_motor_pwm.c
*Version: 1.0
*Parker Dillmann
*The Longhorn Engineer (c) 2013
*www.longhornengineer.com
*
*Check bottom of file for License.
*
*/
#include "msp430g2553.h"
#include "isr_motor_pwm.h"
#define Motor_DIR P2DIR
#define Motor_OUT P2OUT
#define Motor_A1 BIT0
#define Motor_A2 BIT1
#define Motor_B1 BIT2
#define Motor_B2 BIT3
volatile int Motor_Speed_A;
volatile int Motor_Speed_B;
volatile int Motor_isr_cnt;
void init_motors(void)
{
TA1CCTL0 = CCIE; //Enable the CCR0 interrupt
TA1CCR0 = 50000; // Set the interrupt register to "trip" each 50000 cycles"
TA1CTL = TASSEL_2 + MC_2 + TACLR; //Timer_A Source Select; Set source to SMCLK, continuous mode, clear TAR
P1DIR |= 0x41; // P1.0 output
Motor_DIR |= (Motor_A1 | Motor_A2 | Motor_B1 | Motor_B2); //Set Motors to output
Motor_OUT &= ~(Motor_A1 | Motor_A2 | Motor_B1 | Motor_B2); //Set Motor outputs to 0
Motor_isr_cnt = 1;
return;
}
void move_motors(int Speed_A, int Speed_B)
{
Motor_Speed_A = Speed_A;
Motor_Speed_B = Speed_B;
return;
}
void stop_motors(void)
{
Motor_Speed_A = 0;
Motor_Speed_B = 0;
return;
}
#pragma vector = TIMER1_A0_VECTOR
__interrupt void Timer1_A0_ISR(void)
{
//Upkeep Time
Motor_isr_cnt++;
if (Motor_isr_cnt == 512)
{
Motor_isr_cnt = 1;
}
//Motor_A PWM
if (abs(Motor_Speed_A) >= Motor_isr_cnt)
{
if (Motor_Speed_A > 0)
{
Motor_OUT |= Motor_A1;
Motor_OUT &= ~(Motor_A2);
}
else
{
Motor_OUT |= Motor_A2;
Motor_OUT &= ~(Motor_A1);
}
}
else
{
Motor_OUT &= ~(Motor_A1 + Motor_A2);
}
//Motor_B PWM
if (abs(Motor_Speed_B) >= Motor_isr_cnt)
{
if (Motor_Speed_B > 0)
{
Motor_OUT |= Motor_B1;
Motor_OUT &= ~(Motor_B2);
}
else
{
Motor_OUT |= Motor_B2;
Motor_OUT &= ~(Motor_B1);
}
}
else
{
Motor_OUT &= ~(Motor_B1 + Motor_B2);
}
P1OUT ^= 0x40; // Toggle P1.0
TA1CCR0 += 500; //Add offset to CCR0
}