// Will Patrick // // Adapted from: // // hello.arduino.328P.blink.c // // test blinking LED // // Neil Gershenfeld // 10/21/13 // #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define portb PORTB // H-bridge port #define portd PORTD // H-bridge port // Stepper 1 #define bridge_direction_b DDRB // H-bridge direction #define bridge_direction_d DDRD // H-bridge direction #define S1_A_IN2 (1 << PB2) // H-bridge output pins #define S1_A_IN1 (1 << PB1) // " #define S1_B_IN2 (1 << PB0) // " #define S1_B_IN1 (1 << PD7) // " // Stepper 2 // Solenoid #define sol_gate (1 << PD4) // #define sol_port PORTD // Port used for solenoid #define on_delay() _delay_us(25) // PWM on time #define off_delay() _delay_us(5) // PWM off time #define PWM_count 100 // number of PWM cycles #define step_count 20 // number of steps static uint8_t count; // // A+ B+ PWM pulse // void pulse_ApBp() { clear(portb, S1_A_IN2); clear(portb, S1_B_IN2); set(portb, S1_A_IN1); set(portd, S1_B_IN1); for (count = 0; count < PWM_count; ++count) { set(portb, S1_A_IN1); set(portd, S1_B_IN1); on_delay(); clear(portb, S1_A_IN1); clear(portd, S1_B_IN1); off_delay(); } } // // A+ B- PWM pulse // void pulse_ApBm() { clear(portb, S1_A_IN2); clear(portd, S1_B_IN1); set(portb, S1_A_IN1); set(portb, S1_B_IN2); for (count = 0; count < PWM_count; ++count) { set(portb, S1_A_IN1); set(portb, S1_B_IN2); on_delay(); clear(portb, S1_A_IN1); clear(portb, S1_B_IN2); off_delay(); } } // // A- B+ PWM pulse // void pulse_AmBp() { clear(portb, S1_A_IN1); clear(portb, S1_B_IN2); set(portb, S1_A_IN2); set(portd, S1_B_IN1); for (count = 0; count < PWM_count; ++count) { set(portb, S1_A_IN2); set(portd, S1_B_IN1); on_delay(); clear(portb, S1_A_IN2); clear(portd, S1_B_IN1); off_delay(); } } // // A- B- PWM pulse // void pulse_AmBm() { clear(portb, S1_A_IN1); clear(portd, S1_B_IN1); set(portb, S1_A_IN2); set(portb, S1_B_IN2); for (count = 0; count < PWM_count; ++count) { set(portb, S1_A_IN2); set(portb, S1_B_IN2); on_delay(); clear(portb, S1_A_IN2); clear(portb, S1_B_IN2); off_delay(); } } // // clockwise step // void step_cw() { pulse_ApBp(); pulse_AmBp(); pulse_AmBm(); pulse_ApBm(); } // // counter-clockwise step // void step_ccw() { pulse_ApBm(); pulse_AmBm(); pulse_AmBp(); pulse_ApBp(); } int main(void) { // // main // static uint8_t i,j; // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // initialize bridge pins // clear(portb, S1_A_IN1); output(bridge_direction_b, S1_A_IN1); clear(portb, S1_A_IN2); output(bridge_direction_b, S1_A_IN2); clear(portd, S1_B_IN1); output(bridge_direction_d, S1_B_IN1); clear(portb, S1_B_IN2); output(bridge_direction_b, S1_B_IN2); // // main loop // while (1) { for (i = 0; i < step_count; ++i) { for (j = 0; j < i; ++j) step_cw(); set(sol_port, sol_gate); for (j = 0; j < i; ++j) step_ccw(); clear(sol_port,sol_gate); } for (i = step_count; i > 0; --i) { for (j = 0; j < i; ++j) step_cw(); set(sol_port, sol_gate); for (j = 0; j < i; ++j) step_ccw(); clear(sol_port,sol_gate); } } }