Advertisement
grist

ATtiny2313 Interrupt handler

Feb 6th, 2012
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. /*
  2.  * AT2313_InterruptHandler.c
  3.  *
  4.  * Created: 2/7/2012 3:59:47 PM
  5.  *  Author: grist.carrigafoyl
  6.  */
  7.  
  8.  
  9. #define F_CPU 8000000UL  //  8Mhz
  10.  
  11. #include <avr/io.h>
  12. #include <util/delay.h>
  13. #include <avr/interrupt.h>
  14.  
  15. // Digit to pin mapping.
  16. const uint8_t LED_PIN = _BV(PD4);
  17.  
  18.  
  19. uint8_t mode = 0; // will be changed on interrupt to set the led on or off
  20.  
  21. int main(void)
  22. {
  23.  
  24.     // Set the pin mode for output
  25.     DDRD = LED_PIN;
  26.    
  27.     // Set Pin 6 (PIND2) as the interrupt pin
  28.     PCMSK |= (1<<PIND2);
  29.     // interrupt on falling edge
  30.     MCUCR = _BV(ISC01) | _BV(ISC00);
  31.     // Turn on interrupts
  32.     GIMSK |= _BV(INT0);
  33.     sei();
  34.    
  35.     // loop forever
  36.     for(;;)
  37.     {
  38.         // turn the led on or off depending on the current value of mode
  39.         if (mode == 1) {
  40.             PORTD |= LED_PIN; // on
  41.         } else {
  42.             PORTD &= ~LED_PIN; // off
  43.         }
  44.        
  45.     }  
  46.  
  47. }
  48. //----------------------------------------------------------------------------
  49. // Interrupt Handler
  50. //----------------------------------------------------------------------------
  51. SIGNAL (SIG_INT0) {
  52.  // Flip the mode  
  53.  mode = 1 - mode;  
  54.    
  55. }
  56. //----------------------------------------------------------------------------
  57. // Functions
  58. //----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement