/* * This program controls the rotational speed and direction of * a DC motor. * * The Wytec EduBase board has a quad half-H-bridge motor driver * (TB6612FNG), which is capable of driving two DC motors * or a stepper motor with up to 15V and 1.2A continous current. * * The enable pins of the half-bridge are connected to PORTB 3-0. * The PWM inputs are connected to PORTF 1 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 Timer1B. */ #include "TM4C123GH6PM.h" void delayMs(int n); void delayUs(int n); int main(void) { int pw = 0; SYSCTL->RCGCTIMER |= 0x02; // enable clock to TIMER1 SYSCTL->RCGCGPIO |= 0x20; // enable clock to GPIOF SYSCTL->RCGCGPIO |= 0x02; // enable clock to GPIOB TIMER1->CTL &= ~0x0100; // disable TIMER1B during setup TIMER1->CFG = 4; // configure as 32-bit timer mode TIMER1->TBMR = 0x0A; // down-count, PWM-periodic mode TIMER1->TBILR = 3999; // 16 MHz / 4000 = 4 kHz TIMER1->CTL |= 0x4100; // enable TIMER1B 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 |= 0x00007000; // set PORTF 3 alternate funtion to timer GPIOB->DEN |= 0x0C; // PORTB 2, 3 as digital pins GPIOB->DIR |= 0x0C; // set PORTB 2, 3 as output GPIOB->DATA |= 0x08; while(1) { // speed up for (pw = 100; pw < 3999; pw += 20) { TIMER1->TBMATCHR = pw; delayMs(50); } // slow down for (pw = 3900; pw >100; pw -= 20) { TIMER1->TBMATCHR = pw; delayMs(50); } // change dirction GPIOB->DATA &= ~0x08; GPIOB->DATA |= 0x04; // speed up for (pw = 100; pw < 3999; pw += 20) { TIMER1->TBMATCHR = pw; delayMs(50); } // slow down for (pw = 3900; pw >100; pw -= 20) { TIMER1->TBMATCHR = pw; delayMs(50); } // change dirction GPIOB->DATA &= ~0x04; GPIOB->DATA |= 0x08; } } // 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; }