Given the Arduino's 16 MHz clock, I had hoped to be able to switch the state of the digital outputs at at least 1 MHz. I set up an interrupt to set the pins using PORTD, and used a prescaler of 1.
The problem is that the switching refuses to get faster once I hit 100 kHz. Pushing the counter roll-over lower than 150 or so just doesn't make a difference.
Have I hit a fundamental limit of this platform?
What speed does this code work at. It should toggle Pin 8 (not tested)
void loop() {
PINB = 0b00000001;
}
There may be some useful demo code in this Thread about oscilloscopes
...R
Yes and no. The platform can toggle a pin at 1MHz, no it can't do it using interrupts.
See this link for the overhead using interrupts
Even if you hard coded the interrupt you would only get about 500kHz maximum. If you did this the processor would spend all its time entering and leaving the interrupt.
Is it possible to use PWM for your application?
If not, to generate the signal you need to have all the update code in a loop. The problem is the exact rate of the loop will be unpredictable if you write it in C. Use assembly to guarantee the cycle count and fix the frequency. Note not all instructions are single cycle, and you must disable interrupts to generate a reliable wave! The arduino environment has some timer related interrupts which cause glitches in your waveform otherwise.
Roughly (I only have a phone at the minute)
SBI PORTD,0 Set portd bit
NOP. Nop *6
...
CBI PORTD, 0. Clear ports bit
NOP
...
loop test
Branch
You can inline assembly into the C without any environment changes
Many thanks for the helpful posts. This has given me several new routes to investigate. Thanks.