Hello TI community,
I am working with msp430fr2311 to run a servo motor in clockwise and counter clock wise direction each for 3 seconds with a pause of 6 seconds in between.
I am trying the following code, but it does not work for me at all. It would be great if you could let me know where I am going wrong.
Thanks for your time.
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Configure GPIO
P2DIR |= BIT0 + BIT1;
P2SEL0 |= BIT0 + BIT1;
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
TB1CCR0 = 20000-1; // PWM Period is 20ms or (20,000/1,000,000 Hz)
TB1CTL = TBSSEL__SMCLK | MC__UP | TBCLR; // SMCLK, up mode, clear TBR
TB0CCTL1 = CAP__COMPARE + CCIE; // compare mode + enable interrupts
TB0CTL = TBSSEL__ACLK | CNTL__16 | MC__CONTINUOUS | TBCLR ; //use ACLK, max counter length (TBxR) 16bits, continuous mode, start timer
// TB0CTL = TBSSEL__ACLK | MC__CONTINUOUS;
if (TB1CCR1==1800){
__delay_cycles(3000000); // servo moves ccw for 3 seconds
TB1CCTL1 = OUTMOD_7; // CCR1 reset/set
TB1CCR1 = 1550; // Stop the servo
if (TB1CCR1==1550){
__delay_cycles(6000000); //Servo is stopped for 6 seconds
TB1CCTL1 = OUTMOD_7; // CCR1 reset/set
TB1CCR1 = 1300; //Move servo cw
if(TB1CCR1==1300){
__delay_cycles(3000000); //servo moves cw for 3 seconds
TB1CCTL1 = OUTMOD_7; // CCR1 reset/set
TB1CCR1 = 1550; // Stop the servo
}
__bis_SR_register(LPM0_bits); // Enter LPM0
__no_operation();
}
}
while(1) {
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0
__no_operation(); // For debugger
/*
TB1CCTL1 = OUTMOD_7; // CCR1 reset/set
TB1CCR1 = 1550; // Stop the servo
TB0CCR0 = 32767; //I'm not sure this is doing anything
TB1CCTL1 = OUTMOD_7; // CCR1 reset/set
TB1CCR1 = 1300; // move servo Clockwise
*/
}
}
//Timer_B1 TBCCR1 Interrupt Vector Handler Routine
#pragma vector = TIMER0_B1_VECTOR
__interrupt void TIMER0_B1_ISR (void)
{
switch(__even_in_range(TB0IV,TB0IV_TBIFG))
{
case TB0IV_NONE: break; //Vector 0: No interrupt
case TB0IV_TBCCR1: //Vector 2: TBCCR1 CCIFG. Interrupt source:capture/compare R1. Interrupt Flag: TBxCCR1 CCIFG.
TB1CCTL1 = OUTMOD_7; // CCR1 reset/set
TB1CCR1 = 1800; // CCR1 PWM duty cycle, start servo Counterclockwise
__bic_SR_register_on_exit(LPM0_bits + GIE); //exit LPM0
break;
case TB0IV_TBCCR2: break;//Vector 4: TBCCR2 CCIFG
case TB0IV_TBIFG: break; //Vector 6: TBIFG.
default: break;
}
}