Split from: delay and interrupt

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

why can't use delay in ISR?

 


#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>

void Ext_INT0_init();

int main(void)
{

	DDRB=(1<<DDB0)|(1<<DDB1);
	Ext_INT0_init();
	sei();
    while (1);
}

ISR(INT0_vect)
{
	PORTB^=(1<<PB0);
	_delay_ms(400);
	PORTB^=(1<<PB1);
}

void Ext_INT0_init(){
	MCUCR=(1<<ISC01);
	GICR=(1<<INT0);
}

 

 

i know it's not a good idea to use delay in ISR but if want to do it anyway why sends error?

Last Edited: Mon. Oct 30, 2017 - 07:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because you forgot #include <avr/delay.h>.

Stefan Ernst

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

The error is because you have forgotten the #include <util/delay.h>

 

It's OK to delay in an ISR in a simple LED flashing program or something. It's when your program gets more complex and 400ms in the ISR means that the main() loop activity stops and worse you prevent all other interrupts for 400ms that the problems will start.

 

The real solution here is to do the PB0 toggle but don't delay just start a timer that is programmed for 400ms and in the ISR triggered at the end of that you do the PB1 toggle. The two events are still 400ms apart but you aren't holding everything else up. 

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

sorry i found my problem

i didnt include delay.h

 

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

reza_mslm wrote:

 

i know it's not a good idea to use delay in ISR but if want to do it anyway why sends error?

The Universe is trying to do you a favor.

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

Why do you need a delay?  Can you do something else--perhaps a state machine would make more sense.

When in the dark remember-the future looks brighter than ever.

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

reza_mslm wrote:
i know it's not a good idea to use delay in ISR but if want to do it anyway

If you know it's not a good idea, then why do you want to do it anyway?!

 

surprise

 

And why did you wake up an unrelated, 7-year-old thread?

(read the OP - it is not about having delays inside an ISR).

 

EDIT

 

This was the hijacked thread: http://www.avrfreaks.net/forum/delay-and-interrupt

Last Edited: Mon. Oct 30, 2017 - 11:58 PM