Interrupts

Revision one: Does this work for you?

// ***********************************************************
// Project: A silly counter using interrupts
// Author: Charles Hart
// Module description: Count the number of rising edges on (INT0)/PD2
//                              Output this number on PORTB
// ***********************************************************
 
#include <avr\io.h>              
#include <avr\interrupt.h>    
 
uint8_t globalCount = 0;
 
ISR(INT0_vect)
{
   globalCount++;
}
 
void display(void)
{
    DDRB = 0xFF;
    PORTB = globalCount;
   return;
}
 
void blank(void)
{
    DDRB = 0x00;
   return;
}
 
// ***********************************************************
// Main program
//
int main(void)
{
    //initialization of ports and interrupts
    DDRB = 0xFF; //port b output
    DDRD = (1 << PD2); //PD2 as input
 
    //interrupts covered on p.66 of atmega8 datasheet
    MCUCR = (1 << ISC01) | (1 << ISC00); // rising edge trigger
    GICR = (1 << INT0);                         // enable INT0
 
    sei(); //enable interrupts
 
   while(1) //infinite looooooooooooooooooooooooooop
   {
        display();
          blank();
   }
    return 1;
}

Revision Two:

// ***********************************************************
// Project: A silly counter using interrupts
// Author: Charles Hart
// Module description: Count the number of rising edges on (INT0)/PD2
//                              Output this number on PORTB
// ***********************************************************
 
#include <avr/io.h>              
#include <avr/interrupt.h>    
#include <util/delay.h>
 
uint8_t globalCount = 13;
 
ISR(INT0_vect)
{
   globalCount++;
   PORTB = globalCount;
 
   // Disable INT0 to avoid button bounces
   GICR &= ~(1 << INT0);
}
 
// ***********************************************************
// Main program
//
int main(void)
{
    //initialization of ports and interrupts
    //p. 58 SFIOR details
    SFIOR &= ~(1 << PUD); //ensure pull-up-disable is disabled
    DDRB = 0xFF; //port b output
    DDRD = 0x00; //(0 << PD2); //PD2 as input
    PORTD = 0xFF; //set internal pull-ups on
 
    //interrupts covered on p.66 of atmega8 datasheet
    MCUCR |= (1 << ISC01) | (1 << ISC00); // rising edge trigger
    GICR |= (1 << INT0);                         // enable INT0
 
    sei(); //enable interrupts
 
   while(1) //infinite looooooooooooooooooooooooooop
   {
     // If INT0 is off, then turn it back on after waiting a bit
     if((GICR & (1 << INT0)) == 0) {
       // Wait a couple hundred ms for button to stop bouncing
       _delay_loop_2(60000);
 
       // Clear any waiting interrupts and enable
       GIFR |= (1 << INTF0); // Clear
       GICR |= (1 << INT0); // Enable
     }
   }
    return 1;
}
page_revision: 2, last_edited: 1203477029|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License