1

I have programmed a tiny85 using the Arduino IDE. I am using the pulseIn() function. Wrapping it in noInterrupts()/interrupts(); appears to make the function more consistent. This would imply that interrupts are running.

Which interrupts are running? Can I disable them?

I am using the analogWrite() function to pins that have PWM support. Disabling the interrupts for a "long" time does not seam to effect these, this implies the PWMs are producing the analogue signal in hardware and so interrupts are not required.

(I know I can improve the code by not using pulseIn and using interrupts, however the question I have at the moment is which interrupts are running when I am not explicitly enabling any.)

  • It could depend on the arduino core you're using for the ATTiny. As there are multiple, and there is not one present in the default IDE (afaik). – Gerben Oct 31 '16 at 16:15
1

The only interrupt that is always running is the one which generates the count for millis() and micros(). Disabling interrupts disables your ability to keep track of time and use delay().

Timer interrupts are also used by the tone() function and for sending / receiving serial data if the chip has a UART.

Disabling interrupts would also affect the use of external interrupts and pin change interrupts should you be using them.

It sounds, from your brief description, that you would only be effecting the use of millis() and delay() since you aren't using the other systems that use interrupts at all.

  • After more reading it states that the pulseIn() requires interrupts, this is a bit strange. I would not expect to see an "improvement" doing what I am doing. Perhaps the tiny85 implementation is different than the standard one. If timing is available I would expect pulseIn to use it, as the description implies, and therefore require interrupts to be enabled. What you describe makes a lot of sense, I will recheck my findings. – BillyBag2 Oct 31 '16 at 11:33
  • pulseIn() function has got a very high accuracy, to the level of us (microseconds), which millis() function has obviously not. This is why pulseIn does not use it. Note that even micros() does not offer high precision, despite its name. – jfpoilpret Oct 31 '16 at 15:06
0

Which interrupts are running?

It depends on your code which you haven't shown. The Timer 0 overflow interrupt is usually active, as that lets the library count when a millisecond is up (roughly).

If you have done a Serial.begin then serial interrupts would be active. If you have used the I2C library (Wire) then I2C interrupts would be active.

(Disclaimer: That really refers to the Atmega328P. I'm not quite so sure on the ATtiny, it would depend on what the libraries do).


Can I disable them?

If you want to. Doing noInterrupts() is the simplest way. Note that if you disable interrupts for more than a millisecond (approximately) then the time returned by millis and micros will be wrong. Plus delay won't work. Plus you won't be able to serial print properly or at all.


I am using the analogWrite() function to pins that have PWM support. Disabling the interrupts for a "long" time does not seam to effect these, this implies the PWMs are producing the analogue signal in hardware and so interrupts are not required.

That is exactly what it does. Once activated the hardware timers can generate a PWM signal purely in hardware.


There are other ways of timing intervals (than pulseIn). One method is to use one of the other timers, configured with a small, or no prescaler. Then you can measure up to very high accuracy. I have examples of that on my page about timers.

(The ATtiny85 only has two timers, so you couldn't use the second timer for measuring and do PWM output with it, however you can use Timer 0 for the PWM output). That limits the PWM frequency to its default, however you could still alter the duty cycle)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.