AVR external interrupt0

Status
Not open for further replies.

ArdyNT

Full Member level 2
Joined
Nov 6, 2012
Messages
126
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Visit site
Activity points
2,304
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.

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.
 

The simplest way you can use is with a counter inside main

Code:
unsigned char counter=0;

	while(1)
	{	if (counnt_enable==1)
		{
			delay_ms(1000);
			counter++;

			if (counter==1) --blink led1-- ;
			if (counter==2) --blink led2-- ;
			if (counter==3) 
			{
				--blink led3-- ;
				counter=0;  // clear counter
			}
		}
	}

also use a global enable variable
Code:
volatile counnt_enable=0;

and set it to 1 when external interrupt occurs so that the blinking starts

Note that using that way any additional code you may have in the while loop will be execute once per second

A better way is to set a timer to give interrupt in predefined intervals and increment the counter variable in there (use volatile in this case) , then check the counter value in main to blink the leds
 

where is the interrupt?

Besides that, I need to display ADC value into LCD and it should also executed at the same time.

Any suggestion or keyword for my problem?
 

where is the interrupt?

My intension wasn't to give you a full working code but to show you how to do what you want and the way to use the counter variable.

Since you seem to be interested in the second way I have suggested check the datasheet of your device and use the CTC mode of a timer to get interrupts in a predefined interval (usually a round number for example 10ms).
Increment the count variable there and check it in main so that you toggle the output when it reaches a specified value.

If you only intend to toggle a couple of pins then you could implement both the counter and the condition that toggles the pin inside the interrupt since this executes very fast and it not a problem.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…