/* Generate control signal for a standard servo motor. * * Using Timer_A0.4 PWM to generate a 50 Hz signal at P2.7 * with pulse width varies between 1 ms and 2 ms. This should * control the servo motor to turn clockwise and counterclockwise. * This program controls J5 PWM1 output of the EduPad board. * * Built and tested with MSP432P401R Rev. C, Keil MDK-ARM v5.24a and MSP432P4xx_DFP v3.1.0 */ #include "msp.h" void delayMs(int n); int main(void) { int pw; /* Configure P2.7 as Timer A0.4 output */ P2->SEL0 |= 0x80; P2->SEL1 &= ~0x80; P2->DIR |= 0x80; /* configure TimerA0.4 as PWM */ TIMER_A0->CCR[0] = 60000 - 1; /* PWM Period */ TIMER_A0->CCR[4] = 3000 - 1; /* CCR4 PWM duty cycle */ TIMER_A0->CCTL[4] = 0xE0; /* CCR4 reset/set mode */ TIMER_A0->CTL = 0x0214; /* use SMCLK, count up, clear TA0R register */ while(1) { // ramp up the pulse width for (pw = 3000 - 1; pw < 6000 - 1; pw += 50) { TIMER_A0->CCR[4] = pw; delayMs(20); } // ramp down the pulse width for (pw = 6000 - 1; pw > 3000 - 1; pw -= 50) { TIMER_A0->CCR[4] = pw; delayMs(20); } } } /* system clock at 3 MHz, MSP432P401R Rev. C, Start-up v2.2.1 */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* Delay */ }