/* * This program drives a stepper motor using half-step. * The 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 PB7-4. * These pins are shared with the LEDs. * The PWM inputs are connected to PB0 and PB1. PWM function * is not used in this program. It is turned on 100%. * * 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. * * PWM1 - PB0 - TIM8_CH2 - AF3 * LED0 - PB4 * LED1 - PB5 * * PWM2 - PB1 - TIM8_CH3 - AF3 * LED2 - PB6 * LED3 - PB7 */ #include "stm32f4xx.h" void delayMs(int n); int main(void) { const char halfsteps[ ] = {0x9, 0x1, 0x5, 0x4, 0x6, 0x2, 0xa, 0x8}; int i = 0; RCC->AHB1ENR |= 2; /* enable GPIOB clock */ GPIOB->MODER &= ~0x0000FF0F; /* clear pin mode */ GPIOB->MODER |= 0x00005505; /* set pins to output mode */ GPIOB->BSRR = 0x00000003; /* turn on PWM pins to enable the H-bridge */ while(1) { GPIOB->BSRR = 0x00F00000; /* clear motor enable pins */ GPIOB->BSRR = halfsteps[i++ & 7] << 4; /* set the motor enable pins */ delayMs(100); } } /* 16 MHz SYSCLK */ void delayMs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 3195; i++) ; }