// // Jeffery Durand // DC_motor.c: listens through 2 by 2 header for signals // telling the motor to go forward or backward. // The following code puts these signals into action // // Used parts of Neil Gershenfeld's code // #include // basic library #include /* Functions to waste time */ #include // contains the booleans // Certain functions are the same in fact but conceptually different hance the different names // pin x is always one bit shifted x times // DDRx manipulation #define output(directions,pin) (directions |= pin) // set port direction for output (make 1) #define input(directions,pin) (directions &= (~pin)) // set port direction for input, default (make 0) // PORTx manipulation #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define all_set(port) (port = 0xff) // set all port pins, not recommended as different names may be given to same port #define all_clear(port) (port = 0x00) // clear all port pins // PINx reading #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set // PROGRAM SPECIFIC // variables #define on_delay() _delay_ms(1) // PWM on time #define power_levels 15 // different levels of power // COMPONENTS // H-bridge output #define bridge_port PORTB // H-bridge port #define bridge_direction DDRB // H-bridge direction #define OUT1 (1 << PB3) // OUT1 #define OUT2 (1 << PB4) // OUT2 // external input #define instruct_port PORTB #define instruct_direction DDRB #define instruct_pins PINB #define IN1 (1 << PB0) #define IN2 (1 << PB2) int main(void) { static uint8_t delay = 0; // delay where the motor is inactive static uint8_t count_delay = 0; // variable for loop iteration // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // initialize H-bridge pins // clear(bridge_port, OUT1); output(bridge_direction, OUT1); clear(bridge_port, OUT2); output(bridge_direction, OUT2); // // initialize pins listening for instuctions // clear(instruct_port, IN1); // do not need pull up resistors for the connection should be constant input(instruct_direction, IN1); clear(instruct_port, IN2); input(instruct_direction, IN2); // // main loop // while (1) { // active input pin 1 only: turn forward! if (pin_test (instruct_pins, IN1) != 0 && pin_test (instruct_pins, IN2) == 0){ clear(bridge_port, OUT2); // set direction set(bridge_port, OUT1); // make go forward } // active input pin 2 only: turn backward! if (pin_test (instruct_pins, IN1) == 0 && pin_test (instruct_pins, IN2) != 0){ clear(bridge_port, OUT1); // set direction set(bridge_port, OUT2); // make go forward } // nothing instructed: turn off if (pin_test (instruct_pins, IN1) == 0 && pin_test (instruct_pins, IN2) == 0){ clear(bridge_port, OUT1); clear(bridge_port, OUT2); } // both turned on (should not happen): ignore and turn off if (pin_test (instruct_pins, IN1) != 0 && pin_test (instruct_pins, IN2) != 0){ clear(bridge_port, OUT1); clear(bridge_port, OUT2); } } return 0; }