Go Down

Topic: Missed interrupts. How do I turn timer interrupts off? (Read 1 time) previous topic - next topic

derryuk

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: [Select]

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: [Select]

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.

holmes4


PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

Magician

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.

derryuk

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.

Nick Gammon

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

Code: [Select]
(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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

derryuk

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.

derryuk

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

Derry.

Go Up