Pages: [1]   Go Down
Author Topic: Missed interrupts. How do I turn timer interrupts off?  (Read 691 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm building a system that includes a motor that runs from 1000-7000 rpm.
On the shaft of the motor is a 36-1 wheel, that is a 36 toothed wheel with one tooth missing.
I have a slotted opto-switch picking up the passing of the teeth and whose output is fed into a Diecimila interrupt. The output of the switch is a 5V square wave.

All I am trying to do at the moment is prove that the Diecimila can reliably track an opto-switch interrupt. I monitor the Diecimila's output pin 13 with an oscilloscope.

If I run the following code I notice that occasionally the output will change phase, indicating it has missed an edge change point. ie if the output is in-phase with  opto-switch input it will switch to out of phase. Then, after a few seconds, it will flip back to in-phase. The wheel change point and time of change seems to be random.


Code:
int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink,CHANGE);
}

void loop()
{
  digitalWrite(pin, state);
}

void blink()
{
  state = !state;
}


However, if I run the following code the rate of random phase changes drops dramatically but still occurs.


Code:
int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink,CHANGE);
}

void loop()
{

}

void blink()
{
  state = !state;
  digitalWrite(pin, state);
}


It occurs to me that the missed changes might be caused by timer interrupts. I don't need a timer for my intended application so how do I turn timer interrupts off?

Thanks,
Derry.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try using a byte not an int for state.

Mark
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think timer interrupts account for your problem. If two interrupts occur concurrently then the second interrupt is handled when the first one has completed. As long as you don't get *two* interrupts within the duration of an interrupt handler, there will be no problem.

Have you tested this across a range of speeds, and if so does the problem vary with speed? Maybe your optical sensor isn't switching fast enough to trigger the interrupt reliably.

You could reduce the impact of the problem by explicitly reading the state of the input in the interrupt, rather than simply inverting the state.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2486
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try:
        TIMSK0  = 0;  
        TIMSK1  = 0;
        TIMSK2  = 0;

I'm not sure about WatchDog, interrupt that may be causing you troubles, I know you can turn it off.
Logged

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

Thanks for the replies all. I'll give them a go tomorrow.

Peter, I did wonder about switching speed of the switch (!?) but I discounted that when I got a dramatic reduction of problems with the second piece of code.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just tried your first sketch on my Uno. According to my calculations your frequency would be around 4200 Hz being:

Code:
(7000 / 60) * 36

Feeding in 4200 Hz from the function generator into pin 2, and monitoring pin 13, I couldn't see (by eye) any phase shifts at all.

In fact, it seemed OK up to 10 KHz after which the time taken to process the interrupt and do the digital writes was about as long as the period of the input (100 uS).

I agree about making the flag a byte rather than an int. Also the digital writes are rather slow, you may want to do direct port manipulation to speed that up (or find the library digitalWriteFast).

However the phase shifts, by my reckoning, might be something else at your end. The watchdog should not be relevant in this case, as it isn't enabled.
Logged

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

Thank you for taking the time to try it out Nick. I agree with the 4200Hz.

I have just rebuilt my system and tried it out. Initially it worked perfectly though with a different version of code. I did some edits and tried them out and I was back to phase changing again.

So, just to check I reloaded the 'worked perfectly' code and I still had phase changes. It looks like something in the hardware then.

I am only using two components, the opto-switch and the Arduino. I have another Arduino to try and perhaps it's also time to add a Schmitt trigger as Peter suggested.

Derry.
Logged

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

Adding the Schmitt trigger cured my problems. Thank you all.

Derry.
Logged

Pages: [1]   Go Up
Jump to: