Connect with us

Microcontroller ClockCycle speed thing question

Discussion in 'Microcontrollers, Programming and IoT' started by ag273n, Feb 15, 2017.

Scroll to continue with content
  1. ag273n

    ag273n

    74
    4
    Nov 24, 2016
    i found this website that directly converts 1MHz is as good as 1 Cycles Per Micro Second
    http://www.kylesconverter.com/frequency/megahertz-to-cycles-per-microsecond

    so I became curious.... is this common accross all microcontrollers?... that 1 instruction cycle takes only 1 μS and that is 1MHz?....

    I'm hoping to know how these values would be like for Atmel's, their common ones are at 1MHz default, 8MHz, and 16MHz
    --- like... how many times would my code cycle in a microsecond?
     
  2. hevans1944

    hevans1944 Hop - AC8NS

    4,474
    2,085
    Jun 21, 2012
    It depends not only on the clock speed but also how many clock cycles are required to complete a given instruction. Refer to the datasheets of specific processors for this information.
     
  3. ag273n

    ag273n

    74
    4
    Nov 24, 2016
    an example one i looked up is the ATtiny85...

    upload_2017-2-15_15-52-46.png

    i'm thinking this section suggests its roughly 1 clock cycle per MHz.... are my thoughts correct?...
     
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,387
    2,772
    Jan 21, 2010
    If the frequency is f, and the number of clock cycles required is c, then you might imagine that the number of instructions or second is f/c.

    However, instructions frequently require several processing steps, let's say the are 4 steps each taking at least 1 clock cycle. Whilst each instruction must pass through these 4 stages, it may be possible for one instruction to be in each stage. This means that although each instruction takes 4 cycles from start to end (latency), one instruction completes each cycle (throughput).

    So you need to know if your processor has pipelines, how many stages, how often branch prediction fails (or other causes of pipeline stalls or flushes) before you can really answer this.

    Atmel AVRs have a 2 stage pipeline with most instructions taking 2 clock cycles. This means that well written code could average very close to one instruction or clock cycle.
     
    Last edited: Feb 16, 2017
    hevans1944 and ag273n like this.
  5. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,387
    2,772
    Jan 21, 2010
    No there are 1 million clock cycles per MHz.

    1 Hz is literally 1 cycle per second, so 1MHz is a million per second.

    Hopefully my previous post answers some of the other confusing details.
     
    ag273n likes this.
  6. ag273n

    ag273n

    74
    4
    Nov 24, 2016
    Thanks a lot Steve, yes you did answer my confusions about this. :) clicked like on your posts
     
  7. ag273n

    ag273n

    74
    4
    Nov 24, 2016
    for anyone's benefit who might be wondering about the same thing as i did, I'm posting my code...
    I uploaded this sketch to my Arduino Uno and figured it takes approx. 8-12 microseconds to complete one loop with a code of such length:

    Code:
    /*Author: ag273n
    Speed Test for a microcontroller how fast it can loop through several lines of code
    every code cycle is measured - four measurements
    
    Open Serial Monitor to see readings
    Baud Rate is 9600
    */
    unsigned long RunTime;
    unsigned long MicroS_A;
    unsigned long MicroS_B;
    unsigned long MicroS_C;
    unsigned long MicroS_D;
    int TIME_A;
    int TIME_B;
    int TIME_C;
    int TIME_D;
    
    void setup() {
    Serial.begin(9600);
    }
    
    void loop() {
    RunTime = micros();
        if(TIME_D==1 && TIME_C==2 && TIME_B==3 && TIME_A==4){
            Serial.print("1st Loop: ");
            Serial.println(MicroS_A);
            
            Serial.print("2nd Loop: ");
            Serial.println(MicroS_B);
            
            Serial.print("3rd Loop: ");
            Serial.println(MicroS_C);
            
            Serial.print("4th Loop: ");
            Serial.println(MicroS_D);
            Serial.println("------------------------------");
            delay(10000); //little delay (10 secs) so serial monitor wouldn't flood with data
    
            TIME_A = 0;
            TIME_B = 0;
            TIME_C = 0;
            TIME_D = 0;
    
        }
        else if(TIME_D==0 && TIME_C==1 && TIME_B==2 && TIME_A==3){
            ++TIME_A;
            ++TIME_B;
            ++TIME_C;
            ++TIME_D;
            MicroS_C = micros();
        }
        else if(TIME_C==0 && TIME_B==1 && TIME_A==2){
            ++TIME_A;
            ++TIME_B;
            ++TIME_C;
            MicroS_C = micros();
        }
        else if(TIME_B==0 && TIME_A==1){
            ++TIME_A;
            ++TIME_B;
            MicroS_B = micros();
        }
        else if(TIME_A==0){
            ++TIME_A;
            MicroS_A = micros();
        }
    }
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-