/* Generate control signal for a standard servo motor. * * The PWM1 Generator 3B is used to generate a 50 Hz signal * with pulse width varies between 1 ms and 2 ms. This should * control the servo motor to turn clockwise and counterclockwise. * The EduPad board has four servo motor control * pins PF0, PF1, PF2, and PF3. This program controls PF3. * The signal is on J5 header PWM2. * * Built and tested with Keil MDK-ARM v5.24a and TM4C_DFP v1.1.0 */ #include "TM4C123GH6PM.h" void delayMs(int n); int main(void) { int pw = 0; SYSCTL->RCGCPWM |= 0x02; // enable clock to PWM1 SYSCTL->RCGCGPIO |= 0x20; // enable clock to GPIOF SYSCTL->RCC |= 0x00100000; // enable clock divider for PWM SYSCTL->RCC &= ~0x000E0000; // clear clock divide SYSCTL->RCC |= 0x00060000; // set clock divide to 16 (50 MHz / 16 = 3.125 MHz) delayMs(1); // wait for PWM clock to stablize PWM1->INVERT |= 0x80; // positive pulse PWM1->_3_CTL = 0; // disable PWM1_3 during configuration PWM1->_3_GENB = 0x0000080C; // output high when load and low when match PWM1->_3_LOAD = 62500 - 1; // 50 Hz PWM1->_3_CMPB = 3125 - 1; // 1000 us PWM1->_3_CTL = 1; // enable PWM1_3 PWM1->ENABLE |= 0x80; // enable PWM1 GPIOF->DIR |= 0x08; // set PORTF 3 pins as output pin GPIOF->DEN |= 0x08; // set PORTF 3 pins as digital pins GPIOF->AFSEL |= 0x08; // enable alternate function GPIOF->PCTL &= ~0x0000F000; // clear PORTF 3 alternate function GPIOF->PCTL |= 0x00005000; // set PORTF 3 alternate funtion to PWM while(1) { // ramp up the pulse width for (pw = 3124; pw < 6249; pw += 50) { PWM1->_3_CMPB = pw; delayMs(20); } // ramp down the pulse width for (pw = 6249; pw > 3124; pw -= 50) { PWM1->_3_CMPB = pw; delayMs(20); } } } /* delay n milliseconds (50 MHz CPU clock) */ void delayMs(int n) { int i, j; for(i = 0 ; i< n; i++) for(j = 0; j < 6265; j++) {} /* do nothing for 1 ms */ }