/* Playing with getting the small stepper motors driven. */ /* Standard Includes */ #include #include #include #define F_CPU 1000000UL #include /* Pin defs for ATTiny2313 */ /* Clockwise order */ #define BLUE _BV(PB0) #define BLACK _BV(PB1) #define RED _BV(PB2) #define YELLOW _BV(PB3) /* Prototypes */ void halfStepping(uint16_t delay, uint8_t direction[]); int main(void){ const uint8_t clockwise[] = {BLUE, BLACK, RED, YELLOW, BLUE}; const uint8_t counterClockwise[] = {YELLOW, RED, BLACK, BLUE, YELLOW}; uint8_t i; DDRB = 0xff; /* Enable output on all of the B pins */ PORTB = 0x00; /* Set them all to 0v */ while(1){ /* main loop here */ for (i=0; i<=20; i++){ halfStepping(20, clockwise); } for (i=0; i<=20; i++){ halfStepping(20, counterClockwise); } } } void halfStepping(uint16_t delay, uint8_t direction[]){ uint8_t i; for ( i=0; i<=3; i++ ){ /* step through the phases */ PORTB = direction[i]; /* single-coil part */ _delay_ms(delay); PORTB |= direction[i+1]; /* add in half-step */ _delay_ms(delay); } }