Collin80:
Also, the Due is a LOT faster than an 8MHz or 16MHz 8 bit AVR. It runs at 84Mhz and can execute a full instruction most every clock.
Collin, no, Due is only 33% faster then Uno that runs on 16 MHZ.
I was doing some testing few days ago, before testing I was thinking that my Uno is on 8 MHZ,
but testing clearly says that Uno is running on 16 MHZ.
Here is the speed test program on Uno:
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
}
void loop() {
for (float i=0; i<5000000; i++)
{
PORTD = B00000001;
PORTD = B00000010;
PORTD = B00000100;
}
PORTD = B00000000;
delay(5000);
}
The program would run 5 million loops with only one comand (PORTD = B00000001;) with LED high,
then it will set off the LED for 5 seconds, enough to prepare for the measurments of the next LED on period,
then again run 5 million loops, and so on...
The time needed for this 5 million loops to execute is 64,45 seconds, that is the average time done manually with a stopwatch with 7 samples taken.
Then, to measure the number of comands "PORTD = B00000001;" done in 1 second,
I simply added the comands in for loop:
PORTD = B00000001;
PORTD = B00000010;
PORTD = B00000100;
19 times, so total of 57 additional lines/ "PORTD=" comands, total of 60 "PORTD=" comands in for loop now.
The time measurments, in 7 samples with stopwatch, shows that total time for the for loop to execute
was 82,44 seconds, which is 17,99 seconds more then if there are only 3 "PORTD=" comands in loop.
So, 5.000.000 loops multiply by 57 additional lines = 285.000.000 comands, done in 17,99 seconds.
285 millions comands divided by 17,99 seconds gives us 15.842.134 executed "PORTD=" comands per second!
The close proximity of that number to 16 MHZ means that my Uno's CPU is running on 16 MHZ,
and also important conclusion is that comand "PORTD=" takes only one CPU's clock tick. Only one.
Now, lets compare the similar speed measurment on Due...
And we see that if we compare Due with Uno, Due is only 33% faster then Uno, when executing the similar operation.
Here is the code for time testing on Due:
void setup() {
pinMode(25, OUTPUT);
}
void loop() {
for (float i=0; i<5000000; i++)
{
// digital 1, on port D pins 0:
REG_PIOD_CODR = 0xFFFFFFFF;
REG_PIOD_SODR = 0x00000001;
}
REG_PIOD_CODR = 0XFFFFFFFF;
delay(5000);
}
The 5 million loops will now execute in 12,787 seconds, according to the 7 samples of time measurment with manual stopwatch.
Then, I added the comand, acctually the pair of comands:
REG_PIOD_CODR = 0xFFFFFFFF;
REG_PIOD_SODR = 0x00000001;
100 times into the for loop, simply by copy and paste. The final code is very long, so I'm not going to post it here.
After adding 100 comands, acctually 100 pairs of comands, the time needed to execute for loop with 5 millions loops, is 36,478 seconds.
The simple math to calculate number of comands executed per 1 second:
5 million loops with additional 100 comands takes additional 23,691 seconds.
So, 500 million comands divided by 23,691 seconds gives us 21.105.061 comands per second.
Just little over 21 million comands per second.
As 21 millions is 4 times smaller then 84 MHZ declared speed of Due,
the conclusion is:
Each comand, in fact 2 comands:
REG_PIOD_CODR = 0xFFFFFFFF;
REG_PIOD_SODR = 0x00000001;
takes 4 CPU clock ticks, and each comand separately takes 2 CPU clock ticks to execute!
Finally, comparing Due to Uno:
We are comparing 21.105.061 comands in Due with 15.842.134 comands executed per one second on Uno.
The Due is only 33% faster then Uno.
Taking into consideration that pins on ports of Uno are simplier to work with, they are in order, not jumping around like it is in Due, the Uno might be better choise to work on the project of simple function generator.