#define F_CPU 16000000UL #include #include int main(void) { DDRD |= (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5); PORTD &= ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5)); DDRB &= ~(1 << PB1); PORTB |= (1 << PB1); const uint8_t half_step[8] = {0x04, 0x0C, 0x08, 0x18, 0x10, 0x30, 0x20, 0x24}; const uint8_t motor_mask = (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5); uint8_t step_index = 0; uint8_t direction = 1; uint8_t button_prev = 1; while (1) { uint8_t button_now = (PINB & (1 << PB1)) ? 1 : 0; if (button_prev == 1 && button_now == 0) { _delay_ms(20); if (!(PINB & (1 << PB1))) direction = !direction; } button_prev = button_now; PORTD = (PORTD & ~motor_mask) | half_step[step_index]; _delay_ms(10); if (direction) { step_index++; if (step_index >= 8) step_index = 0; } else { if (step_index == 0) step_index = 7; else step_index--; } } }