is noInterrupt() interfering with micros()?

Dear reader,

Do you know if noInterrupt() interfering with micros()? Can't find anything in the documentation.

I have a Cornelian dilemma, In a time critical part of a sketch I need to disable interrupts using the noInterrupts(), as even if I don't use hardware or time interrupt I proved that my code is running faster thus it gives me the accuracy I need for my application. On the other hand, I need to do some accurate time measurement so I use micros() but value returned seems to be slightly wrong when used with noInterrupt.

To summarize: noInterrupt () = very fast code thus good accuracy for my application but bad time measurement interrupt() = slow code but good time measurement

any advice?

Use a higher clock speed.

Use a higher clock speed.

I don't get it, could you rephrase?

Faster board.

micros() uses an interrupt to make a time ‘tick’ every 16 clocks (1uS) (or 64 clocks (4uS)?)
Turn off interrupts, turn off that time tick.

Make sure you are using a board that uses separate timers (timerA, timerB...) for interrupts and the millis() function like the Uno. Each board has at least one timer. Each is names timerA, timerB, timerC, and so on. Usually one is used for mills(), one for interrupts(), one for servos, and one for other stuff (I think). Look at your board's documentation and/or your board's micro controller's documentation for what each timer does. The Arduino Uno uses separate timers for millis() and interrupts so it should work for your uses. Try an Arduino Uno if you have one available.

thanks 2007jingz

I'm using a NANO which is the same than UNO I belive. You gave me an idea, maybe cutting off all interrupts using noInterrupt() is not a good idea as in will affect millis() that uses Timer0. Instead I should try to disable all interrupts but Timer0.

QUESTION: How to enable only Timer0 and disabling all others interrupts?

See Section 7.7 of the 328P datasheet: When using the CLI instruction to disable interrupts, the interrupts will be immediately disabled. No interrupt will be executed after the CLI instruction, even if it occurs simultaneously with the CLI instruction.

Better check the code behind noInterrupt();

I'm sure there is a way to disable everything then re-enable only Timer0, the proof is that you do it when you need to do custom timer interrupts with timer1 , "just" need to find what are the default values of timer0 not to messup with millis (prescalers and everything)

or maybe I'm too optimistic? :)

I'm using a NANO which is the same than UNO I belive. You gave me an idea, maybe cutting off all interrupts using noInterrupt() is not a good idea as in will affect millis() that uses Timer0. Instead I should try to disable all interrupts but Timer0.

Your welcome. The nano is the same as the uno for timers I believe, and your I idea should work.