#include //this library has the AVR's functions #include //This library has some utilities, only the delay utility is used here #define output(directions,pin) (directions |= pin) // set port direction for output #define input(directions,pin) (directions &= (~pin)) // set port direction for input #define set(port,pin) (port |= pin) // set port pin (setting its value to 1) #define clear(port,pin) (port &= (~pin)) // clear port pin (setting its value to 0) #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define PWM_delay() _delay_us(25) // PWM delay=25 MicroSeconds #define led_port PORTA //the led is connected to a pin from the pins of port A #define led_direction DDRA #define led (1 << PA7) // PA7 is connected to the led #define input_port PORTA #define input_direction DDRA #define input_pin (1 << PA3) #define input_pins PINA int main(void) { // // main // unsigned char count, pwm; //Making two variables will be used in the main loop // set clock divider to /1 // CLKPR = (1 << CLKPCE); // CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // initialize LED pins // clear(led_port, led); //initial value is zero for the led set(input_port, input_pin); // turn on pull-up output(led_direction, led); input(input_direction, input_pin); // // main loop // while (1) { // // //waiting the button to be pushed down in an infinite loop while (0 != pin_test(input_pins,input_pin)); for (count = 0; count < 255; ++count) { clear(led_port,led); for (pwm = count; pwm < 255; ++pwm) PWM_delay(); set(led_port,led); for (pwm = 0; pwm < count; ++pwm) PWM_delay(); } for (count = 255; count > 0; --count) { clear(led_port,led); for (pwm = count; pwm < 255; ++pwm) PWM_delay(); set(led_port,led); for (pwm = 0; pwm < count; ++pwm) PWM_delay(); } clear(led_port,led); //switch off the led } }