Thread: cpu clock cycles

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43

    cpu clock cycles

    this may not be the best forum to post this in, but i have had great results posting comments here....thank you everyone for your help

    what i am trying to figure out is how many clock cycles are in a second. My power settings for the device (the PSoC micro) is 5v at 24 mega herts but the cpu_clock is sysclk/1 which is just a setting i have it set to over clock it slightly

    so in a for loop if i had

    Code:
    for(i=0; i<1000;i++)
    {....
    }
    how many seconds would be in that 1000 cpu clock cycles?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A megahertz is 1,000,000 clock cycles per second...

    Thus 24mhz is 24,000,000 clocks per second.

    But here's the bugaboo in your idea... a for loop does not increment on clock cycles, it can take many clock cycles just to do the i++ part since it has to fetch the variable from the stack, increment it, put it back... then there's the comparison i<1000 which is not done in a single clock cycle either... Then of course there's whatever's going on inside the loop... Each turn through the loop could take dozens or hundreds of clock cycles and there's no way of being certain how many...

    I suggest you check your uC documentation to see if there's a precision counter/interrupt you can use instead of relying upon software for timing...

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Obviously if you have a 24MHz CPU with 1:1 CPU clock, there are 24 million cycles in one second. That's the definition of hertz.

    It's impossible to tell how many cycles the loop will take. You can only do instruction level timing in assembly, and even then only on non-superscalar architectures.

    The compiler will compile your code into any sequence of instructions that will achieve the effect. It's all up to the compiler.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    NO ASSEMBLY FOR THIS GUY i took a class that using the HC112 micro in all asembly and about shot my self. i was just thinking there was going to be an easy way to tell how many cpu clock cycles it would be

    what i am really doing is controlling different leds with the rising and falling edge of the clock cycle and i need to know in my code how many are on per second so that i can calculate the power output of them

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why not just loop some huge number of times (say, 500 million iterations) and time it? With a stopwatch, if necessary. I've timed many a thing with a stopwatch.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brewbuck View Post
    Why not just loop some huge number of times (say, 500 million iterations) and time it? With a stopwatch, if necessary. I've timed many a thing with a stopwatch.
    And probably gotten well within 20% accuracy...

    (just kidding)

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    i hate to go on a tangent about this, but here is what i am thinking, both of your points make tremendous sense but here is some of my code and though process...please let me know if i am on the right track about thinking about this....i am studying electrical engineering, and the programming half of it just don't not make any sense...so like i said before i am controlling leds on the rising edge and falling edge of the clock(i need to figure out power of the leds, so i need to know how long they are on for, which is the point of the thread) so my for lop runs though the pattern and inside the for loop i have the leds on separate counters that interrupt once, twice, four and eight times a second

    so for this for loop
    for (int i; i<1000; i++)
    {
    if(G_Flag1)
    {
    led1_on;
    led2_off;
    }
    else
    {
    led1_off;
    led2_on;
    }

    void Counter16_ISR(void)
    {
    G_Flag1= !G_Flag1;
    num1=rand( );

    }
    this interrupt interrupts four times a second so would led1 and led 2 both be on for four times a second, depending if the clock was on a rising edge or falling edge right, or could they be on longer because the cpu clock is still less then 1000

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by begginer View Post
    so like i said before i am controlling leds on the rising edge and falling edge of the clock(i need to figure out power of the leds, so i need to know how long they are on for, which is the point of the thread)
    With the logic you have going you don't care how often they switch on or off, it makes no difference with LEDs (it might with lamp loads). You turn one on and the other off... thus you have a 50% duty cycle, no matter the switching frequency. One of them is always on while the other is off... so the actual power loading is that of 1 led on all the time.

    A bit of ohms law with the series resistor and you know the power drain down to microwatts...
    Last edited by CommonTater; 03-28-2011 at 10:06 PM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by begginer View Post
    i hate to go on a tangent about this, but here is what i am thinking, both of your points make tremendous sense but here is some of my code and though process...please let me know if i am on the right track about thinking about this....i am studying electrical engineering, and the programming half of it just don't not make any sense...so like i said before i am controlling leds on the rising edge and falling edge of the clock(i need to figure out power of the leds, so i need to know how long they are on for, which is the point of the thread) so my for lop runs though the pattern and inside the for loop i have the leds on separate counters that interrupt once, twice, four and eight times a second
    Provide more details:
    - what oscillator frequency is input to the microcontroller.
    - are the leds activated on both falling / rising edges.
    - width of hardware interrupt timers (8/16/32 bit).
    - and post the schematic if you have one.
    Quote Originally Posted by begginer View Post
    so for this for loop

    this interrupt interrupts four times a second so would led1 and led 2 both be on for four times a second, depending if the clock was on a rising edge or falling edge right, or could they be on longer because the cpu clock is still less then 1000
    Depends on what the interrupt is setup to do, does it activate LEDs whenever an interrupt is generated?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    Provide more details:
    - what oscillator frequency is input to the microcontroller.
    - are the leds activated on both falling / rising edges.
    - width of hardware interrupt timers (8/16/32 bit).
    - and post the schematic if you have one.

    Depends on what the interrupt is setup to do, does it activate LEDs whenever an interrupt is generated?
    Code:
    if(G_Flag1)
    { 
    led1_on;
    led2_off;
    }
    else 
    {
    led1_off;
    led2_on;
    }
    Look at his logic, there are 2 leds... The width of the pulses is irrelevent, the time between pulses doesn't matter, how fast his loop runs makes no difference... Even the schematic is irrelevent (so long as it actually works). One LED is on while the other is off... thus there is always 1 led on... determining the power usage is as simple as calculating the load of 1 led...

    Determining how long each is on for is equally simple... 50% of the time... because the flag he uses toggles on each interrupt.

    Matter of fact he doesn't even need the for() loop...
    Last edited by CommonTater; 03-29-2011 at 01:40 PM.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by CommonTater View Post
    Look at his logic, there are 2 leds... The width of the pulses is irrelevent, the time between pulses doesn't matter, how fast his loop runs makes no difference... Even the schematic is irrelevent (so long as it actually works). One LED is on while the other is off... thus there is always 1 led on... determining the power usage is as simple as calculating the load of 1 led...

    Determining how long each is on for is equally simple... 50% of the time... because the flag he uses toggles on each interrupt.

    Matter of fact he doesn't even need the for() loop...
    The frequency IS important. LEDs have a maximum frequency because the depletion region at the PN junction needs time to establish and collapse. IIRC, the frequency limit is actually quite low, unless the LED is properly biased (always kept around the switching voltage). Especially true if you are driving with a high impedance source (like microcontroller output).
    Last edited by cyberfish; 03-29-2011 at 02:06 PM.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by CommonTater View Post
    Look at his logic, there are 2 leds... The width of the pulses is irrelevent, the time between pulses doesn't matter
    It matters because the timer overflow is related to the frequency of the internal clock.
    Quote Originally Posted by CommonTater View Post
    how fast his loop runs makes no difference... Even the schematic is irrelevent (so long as it actually works).
    Correct.
    Quote Originally Posted by CommonTater View Post
    One LED is on while the other is off... thus there is always 1 led on... determining the power usage is as simple as calculating the load of 1 led...
    Plenty of assumptions that need to be clarifed with more data, imo.
    Quote Originally Posted by CommonTater View Post
    Determining how long each is on for is equally simple... 50% of the time... because the flag he uses toggles on each interrupt.
    We're on the same page regarding that.
    Quote Originally Posted by CommonTater View Post
    Matter of fact he doesn't even need the for() loop...
    Depends on how the whole thing's been coded.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cyberfish View Post
    The frequency IS important. LEDs have a maximum frequency because the depletion region at the PN junction needs time to establish and collapse. IIRC, the frequency limit is actually quite low, unless the LED is properly biased (always kept around the switching voltage). Especially true if you are driving with a high impedance source (like microcontroller output).
    From the op...
    separate counters that interrupt once, twice, four and eight times a second
    I don't think it's much of an issue... most LEDs are good to at least a kilohertz.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The question is how fast do you need to blink the LEDs? Slower than 18 Hz? Systems have a regular interrupt happening... on most machines something like 55ms. I read "The Win95/98 timer ticks every 55 ms and the Win NT's every 10 ms." On a micro controller it's configurable some place.
    So you need to tap into that interval and use it as a basis for timing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. How many CPU cycles (ARM) for add vs if..then
    By Pea in forum C Programming
    Replies: 11
    Last Post: 01-26-2005, 02:40 PM
  4. approx. # CPU cycles
    By MadCow257 in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2005, 04:39 PM
  5. C Programming CPU clock cycled Help
    By kishorepalle in forum C Programming
    Replies: 1
    Last Post: 05-26-2004, 12:57 PM