the problem is changing OCR2A and OCR2B values I've no variation in pulse width output on pin 13 (PORTB.5) .I'm sure timer's ISR is call and works because I have a pulse output on pin 13, but the width is costantly about 1.8uS not depending from the value OCR2B-OCR2A.I need some help ...

"The Output Compare unit can be used to generate interrupts at some given time. Using the OutputCompare to generate waveforms in Normal mode is not recommended, since this willoccupy too much of the CPU time."

If I understand well the question, value 32 on PORTB set pin 13. I think it doesn't matter which one is the bit I toggle, pin 13 or another. I know programming timer for fast interrupt, occupy lot of CPU time but if all the CPU job is made by ISR, that is the faster and more precise timing we can obtain and It's what I need. In my real program ISR should update a 16bit DAC, in this code I just set pin 13 for checking the timing on oscilloscope. I haven't tried CTC mode because I don't need really to toggle a bit on a port, I need to update some port and toggle few bits in the interrupt service rountine. But I can't understand why this program doesn't works. I can suppose Arduino compiler puts some extra code on mine but I can't find any confirm to this supposition.

If I understand well the question, value 32 on PORTB set pin 13. I think it doesn't matter which one is the bit I toggle, pin 13 or another. I know programming timer for fast interrupt, occupy lot of CPU time but if all the CPU job is made by ISR, that is the faster and more precise timing we can obtain and It's what I need. In my real program ISR should update a 16bit DAC, in this code I just set pin 13 for checking the timing on oscilloscope. I haven't tried CTC mode because I don't need really to toggle a bit on a port, I need to update some port and toggle few bits in the interrupt service rountine. But I can't understand why this program doesn't works. I can suppose Arduino compiler puts some extra code on mine but I can't find any confirm to this supposition.

What you did with the 32 was set and reset 4 bytes. Not the port.

If you want to set or reset bits in the Arduino faster, this is the way.

So his code is setting / clearing a particular pin, and is identical to your code.

Quote

But how does this relate to a DAC???

For debugging purposes. He is trying to try to the code so it flips a pin - to be observed by a scope that signals that the timing portion of the code is working. He can then add the DAC portion of the code.

For debugging purposes. He is trying to try to the code so it flips a pin - to be observed by a scope that signals that the timing portion of the code is working. He can then add the DAC portion of the code.

A fairly common way to build up and debug one's code.

So why the complex timing arrangement with two timed interrupts keeping the processor busy all the time?And, most importantly, if the timing part underestimates the load that driving the DAC will have on the program, then all this is useless. First, test the DAC, see how fast can the Arduino drive it without timings and then worry about triggering the times at a rate the arduino can handle. Since no PWM is really needed, go for the simplest timing arrangement, with an overflow or with an Clear to count and overflow to better adjust the desired frequency.

So why the complex timing arrangement with two timed interrupts keeping the processor busy all the time?

Not really sure about the "interrupts keeping the processor busy all the time" part. Interrupts are useful precisely because they do not keep the processor busy - they interrupt the processor only when certain conditions are met.

Quote

And, most importantly, if the timing part underestimates the load that driving the DAC will have on the program, ...

I do not know how the DAC part is to be implemented. The DAC could be really fast - a parallel DAC for example, or it could be preloaded by the main loop and strobed / activated by the isr. It could be implemented without causing excessive delays in the isr, rendering your question moot.

Quote

First, test the DAC, ...

Different ways of skinning the cat. Without knowing all the details, it is impossible to assert if one approach is better than another.

So why the complex timing arrangement with two timed interrupts keeping the processor busy all the time?

Not really sure about the "interrupts keeping the processor busy all the time" part. Interrupts are useful precisely because they do not keep the processor busy - they interrupt the processor only when certain conditions are met.

Interrupts _can_ keep the processor busy. If they fire too frequently and/or take too long, the CPU will have only little time to execute loop() code.

I'm really grateful to everybody for all the comments and the debate, meanwhile, unfortunately, the point of my question is fading away. My trouble is "why this code doesn't works?" I know you could write the code in a different way but this should works and generate a pulse on pin 13 at a frequency of 62.5KHz (16MHz clock/256 - and it do it exactly) but with a variable pulse width related to the value OCR2B-OCR2A, while the output pulse is about 1.8uS wide for any value of OCR2B and OCR2A I've tested. I can't understand why . I'm going to put the same code on Arduino board using GNU Ccompiler, AVRstudio and ISP to check if I've the same problem. I'll keep you updated.

Thanks Nick your link is useful for timers programming even if this is not my problem at the moment.As I say in previous post I've tested the code compiled with WInAVR and put on the same Arduino Uno with ISP .This is the adapted code

In this case it works fine and if( OCR2A-OCR2B) > 30 we obtain a pulse width equal to ((OCR2A-OCR2B) * 62.5nS) as it should happens in previous case. The limit of 30 for OCR2A-OCR2B is probably due to the time for ISR call, the storing and retrieving of registers, ect.What I would like to know is how Arduino waste all that time and, if is possible, where can I find the assembly code of my C source compiled in Arduino environment ? What Arduino is doing in "loop() "?