Need help with AVR external interrupt0

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using external interrupt to make three blinking LEDs with different delay. The idea is when interrupt occurs, then all LED will start blinking at the same time but with different delay. I've tried it using 1 LED only and it work well. Now I need help to make it three. for example 1st LED blinking with 1s delay, 2nd LED 3s, 3rd LED 5s. I'm using codevisionavr and this is the part of my code.

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
    {
    // Place your code here
    delay_ms(1000);
    PORTB.0=0;
    delay_ms(1000);
    PORTB.0=1;
    }


#define ADC_VREF_TYPE 0x40

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
    {
    ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCW;
    }


// Declare your global variables here
void main(void)
    {
    // Declare your local variables here

    PORTB=0x07;
    DDRB=0xFF;

    // External Interrupt(s) initialization
    GICR|=0x40;
    MCUCR=0x02;
    MCUCSR=0x00;
    GIFR=0x40;
          
    // Global enable interrupts
    #asm("sei")

    while (1)
        {

        }

    }

Help me please coz I'm very new here.
Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you need to use an external interrupt? I wrote a tutorial on multitasking that should address your needs. Do a search for me (kartman) and multitasking in the tutorial section.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And just as a general poijnt don't ever call a delay() function in an ISR. ISRs should always be designed to complete in the fastest time possible. If it's more than about 50us you probably want to reconsider your design.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use the external interrupt to start a timer. The timer is set to trigger an interrupt every second (or whatever amount of time that is a common convenient interval). In that interrupt, use the number of interrupts that have happened to determine which LED should be on at the moment (or whether to stop the timer if the sequence is complete).

Regards,
Steve A.

The Board helps those that help themselves.