Advertisement
Guest User

updated C code for AVR

a guest
Jun 15th, 2014
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. /*
  2.  * Current setup (1000000UL) will result in delays longer than a minute. During testing I used TCNT1 > 5000 and if(extra_time == 1)
  3.  * which resulted in 10 second delays before PORTD update.
  4.  */
  5.  
  6. #ifndef F_CPU
  7.    #define F_CPU 1000000UL
  8. #endif
  9.  
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include <util/delay.h>
  13.  
  14. volatile int extra_time=0;
  15. volatile int num_presses=0;
  16.  
  17. int main(void)
  18. {
  19.    DDRD = 0b11111111;
  20.    PORTD = 0b10000000;
  21.    DDRB = 0b00000000;
  22.    PORTB = 0b00000000;
  23.    
  24.    char button_state = 0;
  25.    
  26.    TCCR1B |= (1 << CS12) | (1 << CS10);
  27.  
  28.    while(1)
  29.    {
  30.       if (bit_is_clear(PINB, 0)) //button is pressed
  31.       {
  32.          if (button_state==0) //was previously not pressed)
  33.          {
  34.             num_presses++;
  35.          }
  36.          button_state=1;
  37.       }
  38.       else
  39.       {
  40.          if (button_state==1) //was previously pressed
  41.          { }
  42.          button_state=0;
  43.       }
  44.      
  45.       if (TCNT1 >= 15625)
  46.       {
  47.          if (extra_time == 5 )
  48.             {
  49.                switch(num_presses)
  50.                {
  51.                   case 0:
  52.                      PORTD = 0b10010000;
  53.                      break;
  54.                   case 1:
  55.                      PORTD = 0b10000000;
  56.                      break;
  57.                   case 2:
  58.                      PORTD = 0b01000000;
  59.                      break;
  60.                   case 3:
  61.                      PORTD = 0b11000000;
  62.                      break;
  63.                   case 4:
  64.                      PORTD = 0b00100000;
  65.                      break;
  66.                   case 5:
  67.                      PORTD = 0b10100000;
  68.                      break;
  69.                   default:
  70.                      PORTD =0b11110000;
  71.                      break;  
  72.                }
  73.                extra_time = 0;
  74.                num_presses = 0;
  75.             }
  76.             else
  77.             {
  78.             ++extra_time;
  79.             }
  80.          TCNT1 = 0;
  81.       }
  82.    }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement