Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Included files and definitions
- #define F_CPU 4000000UL // Clock
- #include <avr/io.h> // I/O
- #include <util/delay.h> // Delay
- int main(void)
- {
- DDRB=0xff; // Initializes LEDs (PORTB) as OUTPUT
- DDRD=0x00; // Initializes Buttons (PORTD) as INPUT
- PORTB=0xff; // Sets all LEDs to OFF as default (low-enable)
- PORTD=0xff; // Sets all Buttons to OFF as default (low-enable)
- while(1) // Loop forever
- {
- switch(PIND) // Use Button status as a switch
- {
- case 0xfc: // SW1 = ON, SW0 = ON (LOW-Enable: 0b1111_1100)
- for (int i = 7; i >= 0; i--) // Starts at most significant bit (left) then goes to least significant (right)
- {
- PORTB = ~(1<<i); // Bit shift, then invert bits
- _delay_ms(100); // Delay for 100ms
- }
- case 0xfe: // SW1 = ON, SW0 = OFF (LOW-Enable: 0b1111_1110)
- {
- for (int i = 7; i >= 0; i--) // Starts at most significant bit (left) then goes to least significant (right)
- {
- PORTB = ~(1<<i); // Bit shift, then invert bits
- _delay_ms(100); // Delay for 100ms
- }
- for (int i = 0; i <= 7; i++) // Starts at least significant bit (right) then goes to most significant (left)
- {
- PORTB = ~(1<<i); // Bit shift, then invert bits
- _delay_ms(100); // Delay for 100ms
- }
- }
- case 0xfd: // SW1 = OFF, SW0 = ON (LOW-Enable: 0b1111_1101)
- for (int i = 0; i <= 7; i++) // Starts at least significant bit (right) then goes to most significant (left)
- {
- PORTB = ~(1<<i); // Bit shift, then invert bits
- _delay_ms(100); // Delay for 100ms
- }
- default: // ANY other button combination
- {
- PORTB = 0x55; // 0b0101_0101
- _delay_ms(100); // Delay for 100ms
- PORTB = 0xAA; // 0b1010_1010
- _delay_ms(100); // Delay for 100ms
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement