Pages: [1]   Go Down
Author Topic: how many clock cycles take this code to be executed?  (Read 454 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hallo everybody,
i am impeleneting a protocol on arduino due and timing is very important , i want to know
 how many clock cycles or the time in microsecond take this code to be executed?
  if (counts >=0 && counts <=80) b1 = 1;
  else if(counts >=330 && counts <=410) b1 = 2;
  else if(counts >=450 && counts <=530) b1 = 3;
  else if(counts >=820 && counts <=1024) b1 = 4;
and how many clock cycles take the for loop to check condition and make anew loop?

Thanks
Logged

The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 123
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you exactly want to know how long a piece of code takes, you're better off with asm. Because you never know what a compiler will produce.
Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what about:
Code:
int t1, t2;
t1 = micros();
if (counts >=0 && counts <=80) b1 = 1;
  else if(counts >=330 && counts <=410) b1 = 2;
  else if(counts >=450 && counts <=530) b1 = 3;
  else if(counts >=820 && counts <=1024) b1 = 4;
t2 = micros();
Serial.println(t2-t1, DEC);
Of course there should be a "Serial.begin(115200);" command in your setup routine. You should mind that your code execution time depends on the value of "counts" (for counts = 60 only the first line is executed, for counts = 1000 all 4 if's are evaluated). To get the worst case you should ensure proper values for "counts".

I guess your code will take about 4 usec (in worst case), so you have to take into accout that the "micros()" commands take about 1usec each, so you will measure something like 6 usec.

If you want to improve your accuracy you can recopy your code several times between the two micros() commands and divide the final number accordingly. If you want to measure properly below 1us you need to set digital channels using port manipulation before and after your code and measure with an oscilloscope...
Logged

Pages: [1]   Go Up
Jump to: