Jump to content

MSP430 Launchpad Servo Control

Recommended Posts

Just found this while browsing the web. Submitter's name is Vihapuu. So credit goes to him.




His code is posted on pastebin. So I'm archiving it here just in case.


#include "msp430G2231.h"

#define TEST2 1

#define SERVO_0 650
#define SERVO_180 2350

#define SERVO_COUNT 4

#define SETSERVO(servo, pos) periods[servo].cycle = servopos(pos);

typedef struct period_t_ {
uint16_t cycle;
uint8_t pins;
} period_t;

period_t periods[1 + SERVO_COUNT];
uint8_t period = 0;

void mysleep(uint16_t i);
uint16_t servopos(uint8_t pos);
void initperiod(uint8_t period, uint16_t cycle, uint8_t pins);

int main(void)
WDTCTL = WDTPW + WDTHOLD; /* Stop watchdog timer */

initperiod(0, 20000, 0x00);
initperiod(1, servopos(90), BIT5);
initperiod(2, servopos(90), BIT4);
initperiod(3, servopos(90), BIT3);
initperiod(4, servopos(90), BIT2);

P1OUT = 0x00;
P1DIR |= BIT5;
P1DIR |= BIT4;
P1DIR |= BIT3;
P1DIR |= BIT2;


CCR0 = 65535;


//_BIS_SR(LPM0_bits + GIE);

while (1) {
	uint16_t i;

	SETSERVO(1, 90);
	SETSERVO(2, 90);
	SETSERVO(3, 0);
	for (i = 90; i != 25; i -= 5) {
		SETSERVO(2, i);
	SETSERVO(2, 25);
	SETSERVO(3, 130);
	for (i = 0; i 			SETSERVO(2, 15);
		SETSERVO(3, 120);
		SETSERVO(2, 25);
		SETSERVO(3, 130);
	SETSERVO(2, 10);
	for (i = 0; i 			SETSERVO(1, (90 - 30) + i);
	for (i = 0; i 			SETSERVO(1, (90 + 30) - i);


#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A_0(void)
P1OUT = periods[period].pins;
CCR0 = periods[period].cycle;
if (period ++ >= SERVO_COUNT) period = 0;

void mysleep(uint16_t i)
uint16_t y, x;
y = i;
while (--y != 0) {
	for (x = 0; x 	}

uint16_t servopos(uint8_t pos)
       return (uint16_t) ((SERVO_180 - SERVO_0) * (float) pos / 180) + SERVO_0;

void initperiod(uint8_t period, uint16_t cycle, uint8_t pins)
periods[period].cycle = cycle;
periods[period].pins = pins;


/* vim: set sw=8 noet: */

Link to post
Share on other sites
Can't tell from the video; are the servos hooked directly to the 430 pins, or through an interface?


The signal wire comes in from the Launchpad. Looks like he is powering the servos externally through a voltage regulator(~5V). You can see the breadboard around 0.04 minutes.


[EDIT] gatesphere raced me to it.

Link to post
Share on other sites
  • 2 weeks later...

Hi all!


I'm the author of the video in question. I was pleasantly surprised to find that my little learning experiment was being linked to by msp430 related sites :)


In the video I used the TP1 and TP3 connections (if I remember the names correctly, near the USB anyway) on the launchpad to draw 5V directly from USB. It gives enough juice to do some basic servo testing.


For more servos however I had to switch to an external power supply.


In the future I hope to build a little robot arm/leg. I'm just not sure about what material to use yet.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...