I have some questions about the Arduino's interrupt. I tried to implement, in Arduino uno and Arduino due, a little test to realize an interrupt that work with 8 Mhz. This interrupt will change the status of the pin 13 in on/off using the port manipulation method. I can switch at frequency maximum around 700kHz. I don't understand why I can't switch this pin with frequency more than 700kHz.
Do you have an idea to resolve this problem and understand this behavior?
Because instructions take time? Let's assume that your ISR takes 10 cycles. Your micro can do e.g. 8 million instructions per second. It takes 10 cycles for a toggle so effectively you can do 8 million / 10 = 800000 toggles per second.
arunta:
In this code I used the DueTimer library. Fixing a frequency at 2MHz I expect that the led to vibrate at a frequency of 1 MHz, but up to 500 kHz.
My question is: Why?
I do not have many experience in Arduino and i hope that you can help me to understand.
Thanks
I think that when you describe your problem, you need to be specific and accurate about the description. For example, what does 'fixing a frequency at 2 MHz' mean? Do you mean interrupt handling frequency?
And what do you mean by 'at a frequency of 1 MHz, but up to 500 kHz'? This sentence doesn't appear to be coherent.
Anyway, could try looking up info about 'maximum switching speed', eg...
If I recall correctly, the Arduino attachInterrupt implementation has a lot of overhead because it looks at all the bits of the interrupt mask. It is much better (if you are trying to maximize performance) to configure the interrupt manually and read the specific bit you are after in your handler.
You will be faster if you program the timer directly for a compare match at 2MHz with one of the timer output pins set to toggle. Do not use an interrupt, but rather use the hardware output capabilities of the timer.