/* This program controls the rotational speed and direction of * a DC motor. * * The Wytec EduPad board has a quad half-H-bridge motor driver, * which is capable of driving two DC motors or a stepper motor * with up to 15V and 1.2A continuous current. * * The enable pins of the half-bridges are connected to PORTB 3-0. * The PWM inputs are connected to PORTF 2 and PORTF 3. These * pins are shared with the LEDs. The PORTB pins are connected to * the LEDs on the EduBase board. The PORTF pins are connected to * the LEDs on the Tiva LaunchPad. * * The four output pins of the motor driver device are connected to * the blue terminals marked M1, M2, M3, and M4. Use of external * power supply for motor is recommended. External power should be * connected to the terminal T3 and the jumper J4 should be at EXT. * * This program uses PORTB 3, 2 for direction control and PORTF 3 for * PWM control. The outputs are terminals M3 and M4. * The PWM is generated by PWM1_3B. * * Built and tested with Keil MDK-ARM v5.24a and TM4C_DFP v1.1.0 */ #include "TM4C123.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->RCGCGPIO |= 0x02; // enable clock to GPIOB delayMs(1); // PWM1 seems to take a while to start SYSCTL->RCC &= ~0x00100000; // use system clock for PWM 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 = 3999; // 4 kHz 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 |= 0x0C; // PORTB 3 as digital pins GPIOB->DIR |= 0x0C; // set PORTB 3 as output GPIOB->DATA |= 0x08; // enable PORTB 3 while(1) { // set direction GPIOB->DATA &= ~0x04; GPIOB->DATA |= 0x08; // ramp up speed for (pw = 100; pw < 3999; pw += 20) { PWM1->_3_CMPB = pw; delayMs(50); } // ramp down speed for (pw = 3900; pw >100; pw -= 20) { PWM1->_3_CMPB = pw; delayMs(50); } // reverse direction GPIOB->DATA &= ~0x08; GPIOB->DATA |= 0x04; // ramp up speed for (pw = 100; pw < 3999; pw += 20) { PWM1->_3_CMPB = pw; delayMs(50); } // ramp down speed for (pw = 3900; pw >100; pw -= 20) { PWM1->_3_CMPB = pw; delayMs(50); } } } /* 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 */ }