Skip to content

Instantly share code, notes, and snippets.

@s-estay
Last active October 27, 2016 09:41
Show Gist options
  • Save s-estay/47c2bf9d031c2b9595a7a36e0896f149 to your computer and use it in GitHub Desktop.
Save s-estay/47c2bf9d031c2b9595a7a36e0896f149 to your computer and use it in GitHub Desktop.
msp430: servo pwm test
// a PWM signal is created with the TimerA0 where CCR0 is the periode 20ms and CCR1 the duty cycle
// the PWM signal is output through pin P1.6 that correspond to TA0.1 (see MSP430G2553 uC datasheet)
// this code does not generate interruptions but uses the reset/set mode to generate the PWM signal when the timer counts up to CCR1
// in this case a duty cycle of 2ms (CCR1 = 2000 - 1) will rotate the axis clockwise (see sg90 datasheet)
// a duty cycle of 1.0ms (CCR1 = 1000 - 1) will rotate the axis anticlockwise
// a duty cycle of 1.5ms (CCR1 = 1500 - 1) will rotate the axis to the center position
// the servo used was a sg90 but I guess the code will work with any servo (refer to the servo's datasheet to confirm this)
#include <msp430g2553.h>
#define SERV BIT6 // P1.6 SERV
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop WDT
BCSCTL1 = CALBC1_1MHZ; // submainclock 1MHz
DCOCTL = CALDCO_1MHZ;
P1DIR |= SERV; // SERV pin set as output
P1SEL |= SERV; // SERV TA0.1 CCR1 output
while(1){
TACCR0 = 20000 - 1; // CCR0 PWM period 20ms
TACCR1 = 2000 - 1; // CCR1 PWM duty cycle 2ms BIT6
TACCTL1 |= OUTMOD_7; // CCR1 reset/set
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode (count up to CCR1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment