Advertisement
Guest User

Untitled

a guest
May 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <avr/interrupt.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4.  
  5. #define RED1 PORTA0
  6. #define YEL1 PORTA1
  7. #define GRN1 PORTA3
  8.  
  9. #define RED2 PORTA4
  10. #define YEL2 PORTA5
  11. #define GRN2 PORTA6
  12.  
  13. #define SW1 PINA7
  14. #define SW2 PINB0
  15.  
  16. int main(void) {
  17.  
  18.     DDRA = ((1<<DDA3) | (1<<DDA6) | (1<<DDA4) | (1<<DDA0) | (1<<DDA1) | (1<<DDA5)); // Set up Port A pin 3 and 6 mode to output (green LEDs)
  19.  
  20.     DDRA &= ~(1<<SW1);  // Set input direction for push button 1
  21.     DDRB &= ~(1<<SW2);  // Set input direction for push button 2
  22.     PUEA |= (1<<SW1);   // Set internal pullup for push button 1
  23.     PUEB |= (1<<SW2);   // Set internal pullup for push button 2
  24.    
  25.     PORTA |= 1<<SW1;    // Set input high for push button 1 (probably not needed)
  26.     PORTB |= 1<<SW2;    // Set input high for push button 2 (probably not needed)
  27.  
  28.     PORTA &= ~((1<<GRN1) | (1<<GRN2));
  29.    
  30.     volatile int counter_state = 0;
  31.     PORTA &= ~((1<<GRN1) | (1<<GRN2) | (1<<YEL1) | (1<<YEL2) | (1<<RED1) | (1<<RED2));
  32.    
  33.  
  34.     while(1) {
  35.         if(!(PINA & (1<<SW1))) {
  36.             counter_state = (counter_state + 1) % 64;
  37.         }
  38.         if(!(PINB & (1<<SW2))) {
  39.             counter_state = counter_state == 0 ? 63 : counter_state - 1;
  40.         }
  41.        
  42.         if(counter_state & 1) {
  43.             PORTA |= (1<<RED2);
  44.         } else {
  45.             PORTA &= ~(1<<RED2);
  46.         }
  47.        
  48.         if(counter_state & 2) {
  49.             PORTA |= (1<<YEL2);
  50.         } else {
  51.             PORTA &= ~(1<<YEL2);
  52.         }
  53.        
  54.         if(counter_state & 4) {
  55.             PORTA |= (1<<GRN2);
  56.         } else {
  57.             PORTA &= ~(1<<GRN2);
  58.         }
  59.    
  60.         if(counter_state & 8) {
  61.             PORTA |= (1<<RED1);
  62.         } else {
  63.             PORTA &= ~(1<<RED1);
  64.         }
  65.        
  66.         if(counter_state & 16) {
  67.             PORTA |= (1<<YEL1);
  68.         } else {
  69.             PORTA &= ~(1<<YEL1);
  70.         }
  71.        
  72.         if(counter_state & 32) {
  73.             PORTA |= (1<<GRN1);
  74.         } else {
  75.             PORTA &= ~(1<<GRN1);
  76.         }
  77.        
  78.        
  79.        
  80.  
  81.         _delay_ms(100);
  82.        
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement