Part Number:MSP430FR2311
Tool/software: Code Composer Studio
I am controlling a servo motor and my goal is to make it turn counterclockwise for 1 second, stop for 1 minute, and move clockwise for 1 second. In the commented code, I made it work in 3-second intervals using __delay_cycles(). But I know this is very inefficient and I want to use a timer to make these delays work. I used TB1 (using the SMCLK) to setup up the PWM period and duty cycles.
1. How can I use TB0 with the ACLK to setup a timer and make my delays work to achieve my goal?
2. Also, I am not sure if I setup my interrupt correctly.
When I run this code, the servo does not move for about 2 seconds, and then moves clockwise without stopping.
Thank you in advance.
#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 /* 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(600000000); //Servo is stopped for 600 seconds (10min) 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; } }