// Jeffery Durand // // v_06: The brightness also decreases, adding a custom defines for modularity // // // Head: taken from Neil Gershenfeld's programs, with added functions // // Button PA3 (pin 10) // LED PA7 (pin 6) // Cristal Xtal 1 & 2 (pin 2 & 3) #include // basic library #include /* Functions to waste time */ #include // contains the booleans #include // contains math functions, but is big! // 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 // the button or input #define button_port PORTA #define button_direction DDRA #define button_pins PINA #define button_pin (1 << PA3) // the LED or output #define LED_port PORTA #define LED_direction DDRA #define LED_pins PINA #define LED_pin (1 << PA7) // delaying set out explicitly #define power_levels 12 // different levels of power #define delay_on 1 // number of us the LED is left on int main (void) { /* Initialization */ output (LED_direction, LED_pin); set (button_port, button_pin); // full brightness at first int delay = 0; uint8_t count = 0; // unsigned 8-bit integer // need to declare i here, does not work in for loop int i; // tells if the button has just been pressed bool pressed = false; /* Event Loop */ while (1) { /* action */ if (pin_test (button_pins, button_pin) == 0 && pressed == false) // meaning the input on PA3 is low, so ground is connected, so button is pressed { // clear to avoid flash at beginning clear (LED_port, LED_pin); // increase count and use exponential growth for the delay count = (count + 1) % power_levels; delay = pow (2, count); // set to pressed true so that the delay is not changed again while button remains pressed pressed = true; } else { // only change back to false once the button is up again if (pin_test(button_pins, button_pin) != 0) { pressed = false; } clear (LED_port, LED_pin); // turn the led pin off // need the for loop, cannot use variables inside _delay for (i = 0; i < delay; i++) { _delay_us(delay_on); } set (LED_port, LED_pin); // turn the LED pin on _delay_us(delay_on); // wait } } return (0); // never reached }