Advertisement
Guest User

Untitled

a guest
Feb 12th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1.  
  2. volatile int count = 0;   //variable counts 2sec delays to make 10
  3. volatile int state = 0;  //keeps track of what state the program is in
  4.  
  5. void setup()
  6. {
  7.     cli();  //disable interrupts
  8.     DDRB |= 0x01;  //pin D8 is output for LED
  9.     TCCR1A = 0x00; //normal timer1 operation
  10.     TCCR1B = 0x00; // "
  11.     TCCR1B |= (1 << CS10); //prescalar set to 1024
  12.     TCCR1B |= (1 << CS12); // "
  13.     sei();   //enable global interrupts
  14. }
  15.  
  16. void loop()
  17. {
  18.   if(!(PINB & 0x02)) //if pin D9 goes low
  19.   {    
  20.     if(state == 0) //light on 10 secs
  21.     {
  22.         PORTB |= 0x01; //LED ON
  23.         TCNT1 = 0;  //initialize timer to 0
  24.         count = 0;  //initialize counter to 0
  25.         TIMSK1 |= (1 << OCIE1A);  //enable 10 sec interrupt
  26.     }
  27.    
  28.     if(state == 1) //light flashes for 10 secs
  29.     {
  30.         PORTB &= ~0x01; //led off, then toggles in ISR
  31.         TCNT1 = 0;  //initialize timer to 0
  32.         count = 0;  //initialize counter to 0
  33.         TIMSK1 |= (1 << OCIE1A);  // start 10 sec
  34.         TIMSK1 |= (1 << OCIE1B);  // start toggle 200ms
  35.     }
  36.    
  37.     state += 1;  //increase button count
  38.     delay(100);  //debounce
  39.     while(!(PINB & 0x02));  //stay here while button is pressed
  40.     delay(100);  //debounce
  41.   }
  42. }
  43.  
  44. ISR(TIMER1_COMPA_vect) //10 second
  45. {
  46.   count += 1;
  47.   OCR1A = TCNT1 + 31250; //up the Output compare another 2 secs from now
  48.   if(count >= 5) //do 2 second delays 5 times
  49.   {
  50.     TIMSK1 = (0 << OCIE1A); //disable compares
  51.     TIMSK1 = (0 << OCIE1B); // "
  52.    
  53.     count = 0;
  54.     state = 0;
  55.     PORTB &= ~0x01; // LED off
  56.   }
  57. }
  58.  
  59. ISR(TIMER1_COMPB_vect) //200 ms
  60. {
  61.     OCR1B = TCNT1 + 3125; //up the OC 200 ms from now
  62.     if(PINB & 0x01)  //toggle LED
  63.     {
  64.       PORTB &= ~0x01;  
  65.     }else
  66.      {
  67.       PORTB |= 0x01;
  68.      }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement