pix-os
Well-known member
hi all, i have written a small example on how to get dynamic cpu speed detection instead of using the sille F_CPU macro, which is only used at compile time.
why i did this?
well, to determine the cpu cpeed at run-time, because the environment and such can have influence on the cpu clock.
this bit is also useful for when one wants to change cpu cpeed and adjust at run time things according to the speed.
the measurements are slightly off (like if setting cpu speed to 96mhz optimized oc, the value shown will be at around 100mhz)
code below:
i hope that this is usefull
why i did this?
well, to determine the cpu cpeed at run-time, because the environment and such can have influence on the cpu clock.
this bit is also useful for when one wants to change cpu cpeed and adjust at run time things according to the speed.
the measurements are slightly off (like if setting cpu speed to 96mhz optimized oc, the value shown will be at around 100mhz)
code below:
Code:
uint32_t UNLOADED_IDLE_COUNTS = 0;
uint8_t cpu_speed_ = 0;
void setup() {
detect_cpu_speed();
delay(2000);
Serial.begin(9600);
Serial.print(cpu_speed_);
Serial.println(" MHZ");
}
void loop() {
}
void detect_cpu_speed(){
//cpu detection magic!
//make sure only standard arduino ISR's are running, and NO user ISR's!!
uint32_t microspassed = micros();
while (1) {
UNLOADED_IDLE_COUNTS++;
if (micros() - microspassed > 250000) {
break; //250ms to detect calculations/second
}
}
cpu_speed_ = (UNLOADED_IDLE_COUNTS * 8)/50000; //convert to the actual CPU speed
}
i hope that this is usefull