/* 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-bridge are connected to P2.5, P2.4, * P3.3, and P3.2. These pins are shared with the LEDs on the EduPad board. * The PWM inputs are connected to P2.7 and P2.6. * * 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 P3.3 and P3.2 for direction control and P2.7 for * PWM control. The outputs are terminals M1 and M2. * The PWM is generated by Timer A0.4. * * 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; P3->DIR |= 0x0C; /* P3.3, P3.2 set as output for half-bridge control */ /* configure TimerA0.4 as PWM */ TIMER_A0->CCR[0] = 299; /* PWM Period */ TIMER_A0->CCR[4] = 299; /* 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) { // set direction P3->OUT &= ~0x04; P3->OUT |= 0x08; // ramp up the pulse width for (pw = 10; pw < 300; pw += 2) { TIMER_A0->CCR[4] = pw; delayMs(50); } // ramp down the pulse width for (pw = 299; pw > 10; pw -= 2) { TIMER_A0->CCR[4] = pw; delayMs(50); } // reverse direction P3->OUT &= ~0x08; P3->OUT |= 0x04; // ramp up the pulse width for (pw = 10; pw < 300; pw += 2) { TIMER_A0->CCR[4] = pw; delayMs(50); } // ramp down the pulse width for (pw = 299; pw > 10; pw -= 2) { TIMER_A0->CCR[4] = pw; delayMs(50); } } } /* 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 */ }