cycles per operation?

I am working on a small project on C-51 (yes, I know this forum is about C++ for the x86 type processor but sorry, you are the only source of knowledge I know) and am currently working on the delay subroutine.

right now I just have a simple loop of constant decrements

1
2
3
4
5
6
7
void delay(int wait)
{
	while(wait)
	{
		wait=wait-1;
	}
}


however, it has the flaw of having no accurate way to choose the delay due to the fact I dont know how many cycles a single loop takes, so I was wondering whether anyone happens to know how many cycles it takes to call a function, how many cycles it takes to return and how many cycles each loop takes. Seeing how these commands are the same as in C (x86) and C++ I assume its cycle amount is identical to what you use.

Also, it would be great if someone could refer to me to list containing just how many cycles are needed per operation in general (especially mathematical formulas so I could input the delay needed in milliseconds to start with)

I dont need something too accurate, even an inaccuracy of half a second will do.

Thanks in advance.
will this help? if you can use the same headers and such ,,,,
Otherwise I would have thought the cycles per second would be computer specific. eg. a cycle ona quad core 2.6 with 12gb ram is going to be faster than an old p1 200mhz, 56kb ram, so really it would need to be on a sliding scale as well ??? or am I not understanding this correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* clock example: This function can be added to your class, 
 * calling it and passing an integer will make it wait a certain number of seconds
 * as an example: wait(55);  
 * will make the program pause for 55 seconds.
 * wait(1); will cause the program to pause for 1 second
 */
#include <stdio.h> // probably don't need to include this
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}
Last edited on
as far as I know the time.h header isnt avaliable on x51. I do however know the clocks per second I have (24Mhz -> approximatly 24,000,000 CPU cycles per second). however seeing how some commands require multiple cycles to run I dont know how many cycles the loop takes.
The best way to determine how long it takes to perform a loop is to measure it. Write a simple program, which toggles any I/O pin (I assume C-51 has some kind of general purpose I/O pins), and measure the frequency using an oscilloscope. Not very appealing? Well, you don't have much choice unless C-51 provides an ability to measure time, which I'm sure it does.

So forget about counting cycles, get the manual, find any kind of time facility (timebase, decrementer, whatever), and simply use it.
x51 processors do have a timer ability, but unfortunately it overflows after 65,535 cycles (meaning ~0.002 seconds), plus I never quite understood how to use it. thats why I seek an alternative way.

but seeing how I/O loop will probably be worse off (due to the fact I cant be certain that I/O commands need the same cycle amount as a while loop, and due to the fact my I/O lines are already taken)

I have one last question on the matter, is there a way (like some software) to convert the C commands into assembler commands? if so how do I integrate the Assembler commands as part of the main C program (From C, call to a subroutine written in Assembler)
Last edited on
gammarays wrote:
I have one last question on the matter, is there a way (like some software) to convert the C commands into assembler commands?

AFAIK every compiler does that. Use the appropriate flags to make your compiler not deleting intermediate files.
gammarays wrote:
if so how do I integrate the Assembler commands as part of the main C program (From C, call to a subroutine written in Assembler)

You can use inline assembly although I'm not the one who will help you with this topic :|.
Last edited on
x51 processors do have a timer ability, but unfortunately it overflows after 65,535 cycles (meaning ~0.002 seconds), plus I never quite understood how to use it. thats why I seek an alternative way.
I think you should look into this feature. Maybe it's possible to configure the timer to generate an interrupt on overflow? You could then write an interrupt handler which simply increments some global variable. Delay would be implemented based on this global variable.

if so how do I integrate the Assembler commands as part of the main C program (From C, call to a subroutine written in Assembler)
You can use inline assembly, as R0mai said, assuming your compiler supports it. Or you can compile assembler source separately, and then link the produced object file with object file created from C source. In the latter case, to call an assembler routine from C, you need to simply import it with extern (the routine must be declared as global in the assembler source).
Last edited on
Topic archived. No new replies allowed.