I want to produce quick square waveform for testing purposes. If I will use this code:
void setup()
{
pinMode(2, OUTPUT);
}
void loop()
{
bitSet(PORTD, 2);
bitClear(PORTD, 2);
}
I will get expected pulses but I also will get these 6.25 us dips in waveform roughly every 1 ms.
I use latest Arduino 1.6.9 environment.
Whole loop cycle take 1 us which mean 16 instructions (62.5 ns per instruction). So I will get 125 ns high and rest of the time low signal.
Any idea what I'm doing wrong?
Same results if I use asm sbi and sci instead of macros bitSet/bitClear.
Thank you!
/*
So after yours advices and little bit further searching I came up with solution to produce clean square waveform. It make 2.66 Mhz and it's probably fastest what you can get:
void setup()
{
pinMode(2, OUTPUT);
noInterrupts();
}
void loop()
{
while(true)
{
bitSet(PORTD, 2);
bitClear(PORTD, 2);
}
}
