Controlling Two RC Servos Using a MSP430G2231

The following is code to control two RC servos using the timers on a MSP430G2231.

The only interrupt occurs when the timer counter overflows (approximately every 16ms).

Simply set Servo1Abs or Servo2Abs to the desired servo position // 2000 - 10000

#include "MSP430G2231.h"

#define SERVO_CENTER 5625 // Measure by adjusting until scope shows 1500us
#define SERVO_MIN 1875 
#define SERVO_MAX 9375 

//-----------------------------------------------------------------------------
// global variables
//-----------------------------------------------------------------------------
volatile unsigned short Servo1Abs, Servo2Abs;

//-----------------------------------------------------------------------------
// Access Functions
//-----------------------------------------------------------------------------
void InitServos( void )
{
Servo1Abs = SERVO_CENTER;
Servo2Abs = SERVO_CENTER;

TACCR0 = Servo1Abs; // PWM pulse width
TACCR1 = Servo2Abs; // PWM pulse width

TACCTL0 = 0x0004; // Mode = 0, OUT = 1
TACCTL1 = 0x0004; // Mode = 0, OUT = 1

TAR = 0x8FFF; // Offset Timer1 Overflow interrupt

// 4 counts / us
// 15.25MHz - 0x02A2
// TASSELx = 10 - SMCLK
// IDx = 10 - /4
// MCx = 10 - Continuous mode: the timer counts up to 0FFFFh.
// TACLR = 0
// TAIE = 1 - Overflow Interrupt Enable
TACTL = 0x02A2;
}



//-----------------------------------------------------------------------------
// Interrupt Service Routine (ISR) - Timer Rollover
//
// Every timer rollover, set PWM LOW for servo[] counts.
//-----------------------------------------------------------------------------
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer1_A1 (void)
{
volatile unsigned short i;

if( TAIV == TAIV_TAIFG )
{
// Set to mode 0
TACCTL0 = 0; //0x0004;
TACCTL1 = 0; //0x0004;

i = TAR;
while( TAR == i );


if( Servo1Abs < SERVO_MIN ) Servo1Abs = SERVO_MIN;
if( Servo1Abs > SERVO_MAX ) Servo1Abs = SERVO_MAX;
if( Servo2Abs < SERVO_MIN ) Servo2Abs = SERVO_MIN;
if( Servo2Abs > SERVO_MAX ) Servo2Abs = SERVO_MAX;

TACCR0 = Servo1Abs; // 2000 - 10000 @ 0.25us/count
TACCR1 = Servo2Abs; // 2000 - 10000 @ 0.25us/count

TACCTL0 = 0x0020; // 0x00A4;
TACCTL1 = 0x0020; // 0x00A4;

}
}

Views: 378

Tags: msp430, msp430G2231, servo

Comment by roberto runfola on August 9, 2013 at 4:52pm

how can you use this code?

do you have a launchpad for programming the msp 430g2231

Comment

You need to be a member of buildsmartrobots to add comments!

EMGRobotics

© 2014   Created by eric gregori.

Badges  |  Report an Issue  |  Terms of Service