How to double check internal clock frequency?

Hi,

Based on https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard AND http://www.homautomation.org/2014/11/27/running-atmega-328p-without-external-quartz/
I have connected the circuit and burned the bootloader. But now I want to double check that my code is really running at 8MHz, how can I do that?

I tried to use a for loop while blink LED like this in void loop():

digitalWrite(12,LOW)
for(long i = 0; i < 8000000; i++)
{
}
digitalWrite(12,HIGH)
for(long i = 0; i < 8000000; i++)
{
}

and I tried to estimate whether the blinking timing interval is 1s, but it turned out that the LED is ALWAYS ON.

Any suggestions?

Thanks

Use

delay(1000);

i.e. blink an LED. That will delay for 1 second only if the clock is running at the assumed speed.

The Blink example which comes with the IDE does exactly that. Run that and see if the LED blinks every second. Maybe change 1000 to 5000 to make it easier to time (two places).

Yes thanks for your reply.

But I am still confused with my code above, anything wrong with that for loop? Why does the LED always be ON?

It’s not really “code” is it, because it doesn’t compile.

digitalWrite(12,LOW)

No semicolon there.


Fixing that up, the compiler optimized away both of your loops because it could see they did nothing useful.

void setup ()
{
  digitalWrite(12,LOW);
  for(long i = 0; i < 8000000; i++)
    {
    }
  digitalWrite(12,HIGH);
  for(long i = 0; i < 8000000; i++)
    {
    }
}  // end of setup

void loop () { }

Generates:

00000102 <setup>:
 102: 8c e0       ldi r24, 0x0C ; 12
 104: 60 e0       ldi r22, 0x00 ; 0
 106: 0e 94 8a 00 call 0x114 ; 0x114 <digitalWrite>
 10a: 8c e0       ldi r24, 0x0C ; 12
 10c: 61 e0       ldi r22, 0x01 ; 1
 10e: 0e 94 8a 00 call 0x114 ; 0x114 <digitalWrite>
 112: 08 95       ret
 }
}

Your two loops have vanished!