/* p10_2.c: Stepper motor control * * This program controls a unipolar stepper motor * using PB00, PB01, PB02, PB03. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" void delayMs(int n); int delay = 10; int direction = 0; int main(void) { const char steps[ ] = {0x9, 0x3, 0x6, 0xC}; int i = 0; REG_PORT_DIRSET1 = 0x0000000F; /* make PB03-PB00 output */ while (1) { if (direction) { REG_PORT_OUTCLR1 = 0xF; /* clear four control bits */ REG_PORT_OUTSET1 = (steps[i++ & 3]); /* set control bits */ } else { REG_PORT_OUTCLR1 = 0xF; /* clear four control bits */ REG_PORT_OUTSET1 = (steps[i-- & 3]); /* set control bits */ } delayMs(delay); } } /* millisecond delay based on 1 MHz system clock */ void delayMs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 199; i++) __asm("nop"); }