I know this is a recursive topic. I have been reading a lot on forums and actually tried the code proposed. With a lot of source code to base my project on I made the following code.
#include <msp430g2553.h>
#define MCU_CLOCK 1100000
#define PWM_FREQUENCY 46 // In Hertz, ideally 50Hz.
#define SERVO BIT2
#define SERVO_MAX 3000
#define SERVO_MIN 700
unsigned int PWM_PERIOD = (MCU_CLOCK / PWM_FREQUENCY);
unsigned int PWM_DUTY = 0;
unsigned int servo1pos = 180; // Default servo position
unsigned int adcValue = 0; // ADC buffer (stores ADC value)
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
// Initialize Clocks
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz
// Configure Pins
P1DIR |= SERVO ; // P1.4 & P1.5 are outputs
P1SEL |= SERVO;
// Configure ADC
ADC10CTL1 = INCH_0 ; // A1/A0, repeat multi channel
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE;
// Configure PWM with TIMERA
CCTL0 = CCIE; // CCR0 interrupt enabled
TACCR0 = PWM_PERIOD; // Set limit
TACCR1 = PWM_DUTY;
TACTL = TASSEL_2 + MC_1 + ID_3; // Timer_A is using 125kHz
_bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/interrupt
}
// CCR0 interrupt service routine
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0 (void) {
TACCR1 = servo1pos; // Set time for servo
__delay_cycles(1000000); // 1 second delay
ADC10CTL0 |= ENC + ADC10SC; // Start sampling
}
// ADC interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void) {
adcValue = ADC10MEM; // Get sample and modify duty cycle
servo1pos = (adcValue) + SERVO_MIN; // Scale the value
}
I connected the servo to the TP1 pin on my Launchpad for VCC and the GND of the servo from the Launchpad. The signal goes directly from P1.2 to the servo. Fixed some things in the code, now got the servo to move, unpredictable, but it moves. Servo is fine so the problem now appears to be the scaling of the potentiometer read and the PWM is not varying its duty cycle. Will keep trying to fix it and update if I get positive results.