TIMER FOR FUNCTION MICROS()

Hello!

I'm working on trajectories control with the Arduino,
my program reads encoders each millisecond (checking micros() function) and calculates new control actions for the motors and send the new PWM's each 10 milliseconds (checking millis() function). I've read that if I use PWM and millis() function at the same time, duty cycles will be higher than expected... So I'd like to know which timer uses micros() function, in order to know if this duty cycle thing won't happen if I check everything (10 milliseconds actions and 1 millisecond reading) with the micros() function.

Thank you very much!

So I'd like to know which timer uses micros() function

Timer0

Thank you very much for your answer, so does it means that if I'm checking continuously Timer0 (for both functions millis() and micros()) Am I interrupting the PWM generation each time? Anyway, checking millis() and/or micros() means increase duty cycle a big amount of time? (I'm not using delays anywhere, just checking if millis() = 10 mseg and micros()= 1mseg each loop)

Once again thanks, really glad!

Thank you very much for your answer

You are welcome.

so does it means that if I'm checking continuously Timer0 (for both functions millis() and micros()) Am I interrupting the PWM generation each time?

No. The single timer is used for both PWM on two pins and the micros / millis functions. Basically, the output of the timer is fed to the I/O circuitry for PWM and the counter's value is used for micros / millis.

Anyway, checking millis() and/or micros() means increase duty cycle a big amount of time?

I don't think it changes the duty cycle at all.

(I'm not using delays anywhere, just checking if millis() = 10 mseg and micros()= 1mseg each loop)

delay should not affect PWM either.

Thanks Again,

just if you can please check the Notes and Known Issues of this link:

They explain how delay affects PWM, and Millis function.

A delay will stop the hole program and check the counter of Timer0, using Millis() function, and till the delay is not completed, the program doesn't runs again, so if you were generating duty in the delay moment it will affect... But they don't mention about Micros(), so I don't know if it's Timer0 or Timer1 for Micros().

Anyway, I'm not sure of how can Millis() affect to the PWM, if this function only checks the timer0 counter, the biggest time it could affect should be if any interruption is done in order to check the counter... really I'd like to know...

Looking at the analogWrite reference regarding PWM/millis/micros issues I'm pretty certain it is no longer valid. PWM on the AtMega168/328 is handled by dedicated hardware (timer0) and duty cycle accuracy is not affected by either millis or micros.

As long as the timer is running (which also is required for millis/micros to work), PWM duty cycle will be as accurate as the Arduino quartz crystal. Even if you disable interrupts alltogehter or generally "misbehave" in all sorts of manners - PWM will continue to run unaffected and dead accurate for all duty cycles.

There may be some historic reason for this note (I'm not sure how the Atmega8 handled PWM), but for the current duino's (Duemillanove, Mega, Pro etc.) it is not an issue.

Why use both micros() & millis()?
With a bit of care you can get multiple delays and timeouts from a single read. Something like:

#define MILLISEC  1000
#define TENMIL    10000
#define ROLLOVER  300000

unsigned long now;
unsigned long shortdel;
unsigned long longdel;
unsigned long debounce;

void setup(){
  now = micros();
  shortdel = now;
  longdel = now;
  debounce = now;
}

void loop(){
  now = micros();
  if ( now - shortdel >= MILLISEC ){
    shortdel = now;
   // 1mS delay code
  }
  if ( now - longdel >= TENMIL ){
    longdel = now;
   // 10mS delay code
  }
  if ( now - debounce >= ROLLOVER ){
    debounce = now;
   // I use this for button debounce and repeat timings
  }
  / rest of loop code
}

I've had quite a few apparently unrelated timeouts working this way. Comparing the subtraction with the delay time automagically deals with zero rollover.

P.S.
If you have relatively long loop code then there will be some jitter on the timings, but the average will remain correct.

Thank you all for your answers!
So I guess we can conclude saying that millis() and micros() doesn't affect the PWM, but not the delays, which obviously will.

So I guess we can conclude saying that millis() and micros() doesn't affect the PWM, but not the delays, which obviously will.

Delays (synced to time or otherwise) have no bearing on PWM duty cycle whatsoever. PWM duty cycle (using analogWrite) is generated by hardware and will continue to run uninterrupted regardless of your sketch delaying, processing interrupts, calculating or whatever as long as the CPU clock is ticking. Use of delays may interfere with the responsiveness of your sketch (e.g. if you want to change duty cycle in response to external events), but it does not influence current PWM output once it is started.