OCR1A , minimum value

Hello i am tryng to make a digital filter with arduino, and tryng to sample at 25khz that correspoeand to +- 640 value of OCR1A.

But less than 2500 value of OCR1A arduino seems to "not follow" the step.
I make analogWrite(ledPin, 200);
analogWrite(ledPin, 0);

at every interrupt and log it via oscilloscope to check if arduino follow frequency.

Till 2500 /3000 of OCR1A arduino seems follow and made an output on pwm port at regular and correct frequency, but less this value range begin to output some "strange" and not regular.

Any idea?

thanks

Interrupt latency is about 20 ticks (1us), and you may need some processing in there as well: longer with the wire library.

I would put the time to be somewhere between 5 - 10us (around 100 ticks).

If i understood well i had to decrease interrupt latency in native library of arduino?
If i understood well, where i can find it for mod?

thanks

If i understood well i had to decrease interrupt latency in native library of arduino?

There are ways to reduce the latency but none is good.

What you can do is:

  1. Use hardware control of the output pin;
  2. Write your own isr;
  3. Use direct pin manipulation for the output.

I ran a test on this. Without optimization, the shortest ocr1a I got is just over 6us -> 100 ticks.

That's without doing anything in the isr, other than flip a pin.

Somewhere between 40 - 50 ticks with full optimization.

Can yu suggest me some code to reduce interrupt latency? or operative strategy...because i not know where begin to make what yu suggested me

newsamo:
I make analogWrite(ledPin, 200);
analogWrite(ledPin, 0);

at every interrupt and log it via oscilloscope to check if arduino follow frequency.

Don't understand this. You're turning PWM output on and off at a high frequency? What does that achieve?

Can yu suggest me some code to reduce interrupt latency?

I think I outlined that earlier.

The easiest would be to ditch the arduino's isr management system: it does so via a set of function calls - costly. You can speed that by writing your own isr.

Under that approach, the most common and dangerous way to reduce latency is to use naked ISR (using the ISR_NAKED attribute). For that approach to work, you have to manually save the context.

Then there is the option to hand-code assembly isr.

I think there is not a whole lot to gain by going beyond writing your own isr (in C), in terms of reducing latency.

or operative strategy

This is where I think the attempt at reducing latency may be misguided. Unless you are doing something super time critical, there is a lot more to be gained by stepping back and ask yourself what you are trying to do and think of an approach that does it better.

Why you are using analogWrite to drive a pin to monitor on an oscilloscope? Surely you should use digitalWrite?

dc42:
Why you are using analogWrite to drive a pin to monitor on an oscilloscope? Surely you should use digitalWrite?

I am sending a pwm et every interrupt.
So with time scale division on oscilloscope i can see "how often" arduino sends pwm.

At low frequency all work, i set clock divider and arduino send exactly pwm wave at frequency.

If i decrease divifer (more frequancy) arduino make something "wrong" and pwm out is not outputed in time frequency well

I am sending a pwm et every interrupt.

You don't "send a pwm". You start PWM going.

I am sending a pwm et every interrupt.

Your pwm period is far bigger (by a few magnitude) than the latency you are trying to reduce. So essentially you are resetting the pwm module over and over.

That's probably not your desired effect.

newsamo:
I am sending a pwm et every interrupt.

No you aren't. You are reconfiguring the PWM output at every interrupt. I think what you're trying to achieve is to configure it once and leave it running. Usually with PWM you would configure the duty cycle and it would run at a fixed frequency. If you want to vary the frequency, be prepared to do a little digging into the way PWM uses hardware timers and how the hardware timers are configured.

I am tryng to make some pin not pwm but high/low...but same result

newsamo:
I am tryng to make some pin not pwm but high/low...but same result

Well that is certainly clear. :smiley:

Lefty