Hello,
I have a code that I run on Mega using DDRx and PORTx manupulation commands. Works fine and does everything I have in there.
Recently, I got Due (84MHz internal clock) thinking that it might run faster than Mega at 16MHz. So, I went ahead and wrote simple code as below
On Mega
void setup() {
// put your setup code here, to run once:
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
DDRA = 0xFF;
PORTA = 0x00;
}
void loop() {
// put your main code here, to run repeatedly:
PORTA = 0x2;
PORTA = 0x4;
delayMicroseconds(10);
PORTA = 0;
delayMicroseconds(10);
}
and on Due
void setup() {
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
PIOA->PIO_OWER |= 0xC000;
PIOA->PIO_ODSR = 0;
}
void loop() {
PIOA->PIO_ODSR = 0x4000;
PIOA->PIO_ODSR = 0x8000;
delayMicroseconds(10);
PIOA->PIO_ODSR = 0;
delayMicroseconds(10);
}
The results are very different.
On Mega, the width of Pin23 positive pulse width is 62.5ns vs 59.5ns on Due which should give us an idea of single command execution time comparison. Not much faster on Due.
Then the positive pulse width of Pin24 output on Mega is something strange, for 10us, it gives 8.817us while the Due is always pretty much exact to whatever delay I use, in this case , 10.05us (if I use 1us, it is the same 1.05us).
The total loop time for both
Mega - 17.95us
Due - 22.36us (60ns5+10us2+loop restart time)
One thing I noticed is on Mega, using delayMicroseconds always end up having lower delay than the one in the command until I get to milli second intervals while Due is always exactly the same as I give in delayMicroseconds command.
Any thoughts or insights about what I am seeing here?
thanks