Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. // Included files and definitions
  2. #define F_CPU 4000000UL // Clock
  3. #include <avr/io.h> // I/O
  4. #include <util/delay.h> // Delay
  5.  
  6. int main(void)
  7. {
  8. DDRB=0xff; // Initializes LEDs (PORTB) as OUTPUT
  9. DDRD=0x00; // Initializes Buttons (PORTD) as INPUT
  10. PORTB=0xff; // Sets all LEDs to OFF as default (low-enable)
  11. PORTD=0xff; // Sets all Buttons to OFF as default (low-enable)
  12.  
  13. while(1) // Loop forever
  14. {
  15. switch(PIND) // Use Button status as a switch
  16. {
  17. case 0xfc: // SW1 = ON, SW0 = ON (LOW-Enable: 0b1111_1100)
  18. for (int i = 7; i >= 0; i--) // Starts at most significant bit (left) then goes to least significant (right)
  19. {
  20. PORTB = ~(1<<i); // Bit shift, then invert bits
  21. _delay_ms(100); // Delay for 100ms
  22. }
  23. case 0xfe: // SW1 = ON, SW0 = OFF (LOW-Enable: 0b1111_1110)
  24. {
  25. for (int i = 7; i >= 0; i--) // Starts at most significant bit (left) then goes to least significant (right)
  26. {
  27. PORTB = ~(1<<i); // Bit shift, then invert bits
  28. _delay_ms(100); // Delay for 100ms
  29. }
  30. for (int i = 0; i <= 7; i++) // Starts at least significant bit (right) then goes to most significant (left)
  31. {
  32. PORTB = ~(1<<i); // Bit shift, then invert bits
  33. _delay_ms(100); // Delay for 100ms
  34. }
  35. }
  36. case 0xfd: // SW1 = OFF, SW0 = ON (LOW-Enable: 0b1111_1101)
  37. for (int i = 0; i <= 7; i++) // Starts at least significant bit (right) then goes to most significant (left)
  38. {
  39. PORTB = ~(1<<i); // Bit shift, then invert bits
  40. _delay_ms(100); // Delay for 100ms
  41. }
  42. default: // ANY other button combination
  43. {
  44. PORTB = 0x55; // 0b0101_0101
  45. _delay_ms(100); // Delay for 100ms
  46. PORTB = 0xAA; // 0b1010_1010
  47. _delay_ms(100); // Delay for 100ms
  48. }
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement