/* * This program drives a standard servo motor. * * The PWM1 Generator 3B is used to generate a 50 Hz signal * with pulse width between 1 ms and 2 ms. * The quad half-H-bridge motor driver (TB6612FNG) of Wytec EduBase board * is configured so that the output of the PWM is also available at M4 terminal. * */ #include "TM4C123GH6PM.h" void delayMs(int n); void delayUs(int n); int main(void) { int pw = 0; SYSCTL->RCGCPWM |= 0x02; // enable clock to PWM1 SYSCTL->RCGCGPIO |= 0x20; // enable clock to GPIOF SYSCTL->RCGCGPIO |= 0x02; // enable clock to GPIOB SYSCTL->RCC |= 0x00100000; // enable clock divider for PWM SYSCTL->RCC &= ~0x000E0000; // clear clock divide SYSCTL->RCC |= 0x00040000; // set clock divide to 8 (16 MHz / 8 = 2 MHz) 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 = 40000 - 1; // 50 Hz PWM1->_3_CMPB = 2000 - 1; // 1000 us PWM1->_3_CTL = 1; // enable PWM1_3 PWM1->ENABLE |= 0x80; // enable PWM1 GPIOF->DIR |= 0x08; // set PORTF 3 pins as output (LED) 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 GPIOB->DEN |= 0x08; // PORTB 3 as digital pins GPIOB->DIR |= 0x08; // set PORTB 3 as output GPIOB->DATA |= 0x08; // enable PORTB 3 while(1) { for (pw = 2000 - 1; pw < 4000 - 1; pw += 50) { PWM1->_3_CMPB = pw; delayMs(100); } } } // delay n milliseconds (16 MHz CPU clock) void delayMs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 3180; j++) {} // do nothing for 1 ms } // delay n microseconds (16 MHz CPU clock) void delayUs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 3; j++) {} // do nothing for 1 us } // This function is called by the startup assembly // code to perform system specific initialization tasks. void SystemInit(void) { // Grant coprocessor access // This is required since TM4C123G has // a floating point coprocessor SCB->CPACR |= 0x00f00000; }