// Button PA2 (pin 11). // LED PA7 (pin 6). #include #define output(directions,pin) (directions |= pin) // macro used to set port direction for output (set to '1') #define input(directions,pin) (directions &= (~pin)) // macro used to set port direction for input (set to '0') #define set(port,pin) (port |= pin) // macro used to set port pin #define clear(port,pin) (port &= (~pin)) // macro used to clear port pin #define pin_test(pins,pin) (pins & pin) // macro used to test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // macro used to test for bit set // // The following definitions are specific to the particular configuration of LEDs and buttons on pins. // These are the only things that need to change when you move to other pins or add devices. // #define input_port PORTA // The button is on port A #define input_direction DDRA // DDRA defines input/output direction for port A #define input_pin (1 << PA2) // The button is on pin 2 of port A #define input_pins PINA // PINA is the register that is read to detect input high or low. #define output_port PORTA // port A will be used for the LED #define output_direction DDRA // DDRA defines input/output direction for port A #define output_pin (1 << PA7) // The LED is on pin 7 of port A int main(void) { // This is the main function // // initialize pins // output(output_direction, output_pin); // sets the proper bit for output to the LED input(input_direction, input_pin); // sets the appropriate bit to enable input on the input pin. set(input_port, input_pin); // turn on pull-up resistor on the input (button) pin // // main loop // while (1) { // loops forever if (pin_test(input_pins,input_pin)) // test to detect button push. Result is true if button is up. clear(output_port,output_pin); // set output low if button is up. else // otherwise... set(output_port,output_pin); // set output high. } }