Pages: [1]   Go Down
Author Topic: time delay in interrupt  (Read 611 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have what I thought was a simple task, to insert a short time delay in a signal (in this case the output from a hall sensor (signal transition every 9ms)) via the use of an interrupt and a simple loop:

Code:
void HAInt()
{
   for (unsigned long i = 0;i<5;i++);
   digitalWrite(HAOut, digitalRead(HAInt3));
}

  attachInterrupt(3, HAInt, CHANGE);

Interestingly, no matter what number I put in the for loop (5, 50, 500, 5000, 50000, 500000) it has no effect on when the digitalWrite event occurs.  Could this be because the compiler sees that the for loop does nothing and optimizes it out?

Since I can't use delay() functions in interrupts what is the proper method of inserting a time delay?

Thank you,

DougM
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Interestingly, no matter what number I put in the for loop (5, 50, 500, 5000, 50000, 500000) it has no effect on when the digitalWrite event occurs.  Could this be because the compiler sees that the for loop does nothing and optimizes it out?

Yup.

Quote
Since I can't use delay() functions in interrupts what is the proper method of inserting a time delay?

The proper method of delaying in an ISR is to not do it.

Quote
a short time delay in a signal (in this case the output from a hall sensor (signal transition every 9ms))

Are you hoping to get a 9 millisecond delay in your ISR?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

re:

>
> Are you hoping to get a 9 millisecond delay in your ISR?
>
no, I really don't know but I'm guessing the range could be anywhere from 20uS to 2mS

I've got 3 signals I need to condition.  I realize that putting delays in ISR's isn't good, but this is a proof of concept, not a final implementation.  the final implementation will use a hardware delay - I am using Arduino to determine what that delay is.

Thanks,

DougM
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm guessing the range could be anywhere from 20uS to 2mS

Just to be clear...  20 us is probably OK.  2 milliseconds will absolutely cause problems.

Use the avr-libc delay functions...
http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice!

final (pertinent parts of)code:

Code:
#define F_CPU 16000000UL  // 16 MHz

#include <util/delay.h>

attachInterrupt(3, HAInt, CHANGE);

void HAInt()
{
   _delay_us (hallDelay);
   digitalWrite(HAOut, digitalRead(HAInt3));
}

Thanks so much!

DougM
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


There is no need to include this...
Code:
#define F_CPU 16000000UL  // 16 MHz
I suggest removing it.

You are welcome!
Logged

Pages: [1]   Go Up
Jump to: